freearhey / epg-grabber

Node.js CLI tool for grabbing EPG from different websites
47 stars 7 forks source link

Async functions #2

Closed mrcanelas closed 3 years ago

mrcanelas commented 3 years ago

Is it possible to do asynchronous functions? for example, make an each, search for a url, wait to make a request on that url and scratch data and return with the function, is it possible to do this?

freearhey commented 3 years ago

I don't know, but I'll try to look into it when I have some free time.

mrcanelas commented 3 years ago

Hey man all right, I had left this project a bit of a lake but now I'm back with it, from what I understand epg-grabber doesn't work with asyncronous functions, I want to increase the size of my tab to about three days, but my api only provides one day per request so i have to make three requests and merge their data, can you help me with that?


async function getMoreDays(channel) {
  const dates = [
    `https://contentapi-br.cdn.telefonica.com/25/default/pt-BR/schedules?ca_deviceTypes=null%7C401&fields=Title,Description,Start,End,EpgSerieId,SeriesPid,SeasonPid,images.videoFrame,images.banner&orderBy=START_TIME:a&filteravailability=false&starttime=${getTimestamp(yesterday)}&endtime=${getTimestamp(today)}&livechannelpids=${channel}`,
    `https://contentapi-br.cdn.telefonica.com/25/default/pt-BR/schedules?ca_deviceTypes=null%7C401&fields=Title,Description,Start,End,EpgSerieId,SeriesPid,SeasonPid,images.videoFrame,images.banner&orderBy=START_TIME:a&filteravailability=false&starttime=${getTimestamp(today)}&endtime=${getTimestamp(tomorrow)}&livechannelpids=${channel}`,
    `https://contentapi-br.cdn.telefonica.com/25/default/pt-BR/schedules?ca_deviceTypes=null%7C401&fields=Title,Description,Start,End,EpgSerieId,SeriesPid,SeasonPid,images.videoFrame,images.banner&orderBy=START_TIME:a&filteravailability=false&starttime=${getTimestamp(tomorrow)}&endtime=${getTimestamp(afterTomorrow)}&livechannelpids=${channel}`,
  ]
  const data = await Promise.all(dates.map(async (elem) => {
    const content = await axios.get(elem)
    return content.data.Content
  }))
  const items = [].concat(...data)
  return items
}

parser: function ({channel}) {
      const items = await getMoreDays(channel.site_id)
      const programs = items.map(item => {
        const title = (item.Title.split(':')[1] != undefined) ? item.Title.split(':')[0] : item.Title
        const category = (item.Title.split(':')[1] != undefined) ? item.Title.split(':')[1] : ''
        const start = (dayjs.unix(item.Start).get('h') < 3) ? dayjs.unix(item.Start).add(1, 'd') : dayjs.unix(item.Start)
        const stop = (dayjs.unix(item.End).get('h') < 3) ? dayjs.unix(item.End).add(1, 'd') : dayjs.unix(item.End)
        const icon = item.Images.VideoFrame[0].Url
        return {
          title,
          category,
          description: item.Description,
          start: start,
          stop: stop,
          icon
        }
      })
      return programs
  },
freearhey commented 3 years ago

You can do the same by passing the desired number of days as a parameter:

example.com.config.js

module.exports = {
  site: 'example.com',
  channels: 'example.com.channels.xml',
  days: 3,
  url: function({ channel, date }) {
    return `http://example.com/${date.format('YYYYMMDD')}/${channel.site_id}.json`
  },
  parser: ({ content }) {
    // parse
  }
}

In this case the parser will run three times with three different dates.

mrcanelas commented 3 years ago

Sorry I didn't know about this feature, it worked for me.