const axios = require('@/utils/axios'); const dayjs = require('dayjs'); module.exports = async (ctx) => { const locations = ctx.params.locations; let nearby = ctx.params.nearby || '0'; nearby = nearby === '1' ? 'true' : 'false'; let host = 'https://alltheflightdeals.com/deals'; let url = 'https://alltheflightdeals.com/deals/locations?origins=['; locations.split(',').forEach(function(pair) { const country = pair.split('+')[0]; const city = titleCase(pair.split('+')[1]); url += `{"country":"${country}","city":"${city}"},`; host += `/${country}/${city.replace(' ', '_')}`; }); url = url.slice(0, -1) + `]&includeNearbyOrigins=${nearby}`; const response = await axios({ method: 'get', url, headers: { Referer: host, }, }); const data = response.data.data.slice(0, 10); ctx.state.data = { title: 'All the Flight Deals', link: host, description: 'All the Flight Deals', item: data.map((item) => ({ title: `[${item.dateRanges[0] ? dayjs(item.dateRanges[0].start).format('YYYY MMM DD') + ' to ' + dayjs(item.dateRanges[0].end).format('YYYY MMM DD') : ''}] ${item.title}`, description: `${item.cityPairs[0].destination.city},${
                item.cityPairs[0].destination.countryName
            }
FromToPrice
${ item.cityPairs[0].origin.iata }, ${item.cityPairs[0].origin.city}, ${item.cityPairs[0].origin.countryName}${item.cityPairs[0].destination.iata}, ${item.cityPairs[0].destination.city}, ${ item.cityPairs[0].destination.countryName }${item.currency} ${item.price}
`, pubDate: new Date(item.createdAt), guid: item.id, link: item.url, })), }; }; const titleCase = (str) => str .toLowerCase() .split(' ') .map(function(word) { return word.replace(word[0], word[0].toUpperCase()); }) .join(' ');