Esempio n. 1
0
def save_epg(live):
    if live[0] != '':
        w = xmltv.Writer(encoding='UTF-8',
                         date=str(time.time()),
                         source_info_url="",
                         source_info_name="",
                         generator_info_name="",
                         generator_info_url="")

        for i, channel in enumerate(live):
            w.addChannel({
                'display-name': [(channel['title'], u'bg')],
                'id': channel['epg_name'],
                'url': ['https://test.iptv.bulsat.com']
            })

            if channel.has_key('program'):
                for p in channel['program']:
                    if len(p['title']) > 0:
                        w.addProgramme({
                            'start': p['start'],
                            'stop': p['stop'],
                            'title': [(p['title'], u'')],
                            'desc': [(p['desc'], u'')],
                            'category': [(channel['genre'], u'')],
                            'channel': channel['epg_name']
                        })

        out = StringIO.StringIO()
        w.write(out, pretty_print=True)
        f_lmx = gzip.open(os.path.join(_files_path, '', 'bulsat.xml.gz'), 'w+',
                          9)
        f_lmx.write(out.getvalue())
        f_lmx.close()
        out.close()
Esempio n. 2
0
  def gen_all(self):
    self.__goforit()
    self.__log_dat('Len: %d' % len(self.__tv_list))

    map_e = {}
    dat = {}
    jdump = {}
    xml = None

    if self.__gen_epg:
      w = xmltv.Writer(encoding=self.__char_set.upper(),
                        date=str(time.time()),
                        source_info_url="",
                        source_info_name="",
                        generator_info_name="",
                        generator_info_url="")

    for i, ch in enumerate(self.__tv_list):
      if self.__cb:
        self.__cb(
                    {
                      'pr': int((i * 100) / len(self.__tv_list)),
                      'str': 'Sync: %s' % ch['epg_name'].encode('utf-8'),
                      'idx': i,
                      'max': len(dat)
                    }
                  )
      dat[ch['epg_name']] = {
          'group': ch['genre'],
          'title': ch['title'],
          'url': ch['sources'],
          'id': ch['epg_name'],
          'ch_idx': ch['channel'],
        }

      if self.__gen_jd:
        jdump[ch['epg_name']]=ch['epg_name']

      if self.__gen_epg:
        w.addChannel(
                    {'display-name': [(ch['title'], u'bg')],
                    'id': ch['epg_name'],
                    'url': ['https://test.iptv.bulsat.com']}
                    )
        if ch.has_key('program'):
          for p in ch['program']:
            w.addProgramme(
                          {'start': p['start'],
                          'stop': p['stop'],
                          'title': [(p['title'], u'')],
                          'desc': [(p['desc'], u'')],
                          'category': [(ch['genre'], u'')],
                          'channel': ch['epg_name']}
                        )

    return dat, self.__UA['User-Agent']
def write(file):
    w = xmltv.Writer()
    for i in get_channels_titles_dict():
        w.addChannel(i)
    for i in make_dict():
        w.addProgramme(i)
    els = w.root.getchildren()
    new_els = sorted(
        els,
        key=lambda el:
        (el.get("channel", ""), el.get("id", ""), el.get("start", "")))
    w.root[:] = new_els
    w.write(file, pretty_print=True)
    def main(self):
        config_file = self.__get_config_path(args.config_file)
        if not os.path.exists(config_file):
            error('Configuration file "%s" not fount' % (config_file))
            sys.exit(1)

        self.conf = self.__read_config(args.config_file)
        self.__init_session()

        self.dates = []
        self.str_director = u'Режиссеры'
        self.str_actors = u'В ролях'
        self.str_guest = u'Участники'

        if self.__web_login():
            log('Login success', self.conf['force_quiet'])
        else:
            error('Login failure')
            sys.exit(1)

        self.data = {}

        writer = xmltv.Writer()
        for region_id in self.conf['regions']:
            log('Read region_id = %s' % region_id, self.conf['force_quiet'])
            self.__web_read_region_cookies(region_id)
            self.__load_program(region_id)

        for key in self.data.keys():
            channel_info = self.data[key]
            writer.addChannel(channel_info['data'])
        for key in self.data.keys():
            # self.__init_session()
            channel_info = self.data[key]
            region_id = channel_info['data']['region_id']
            for event in channel_info['events']:
                event_id = event.get('event_id')
                if event_id:
                    sleep(self.conf['event_delay'])
                    self.add_event_description(event, event_id, region_id)

                writer.addProgramme(event)

        writer.write(args.output, pretty_print=True)
Esempio n. 5
0
  def gen_all(self, force_refresh = False):
    ret = False
    self.__data_fetch(force_refresh)

    if self.__tv_list:
      ret = True
      map = None
      if self.__gen_epg:
        w = xmltv.Writer(encoding=self.__char_set.upper(),
                          date=str(time.time()),
                          source_info_url="",
                          source_info_name="",
                          generator_info_name="",
                          generator_info_url="")
      else:
        try:
          with open('map.json') as f:
            map = json.load(f)
            self.__log_dat(map)
        except:
          pass

      pl = u'#EXTM3U\n'
      if self.__gen_jd:
        jdump = {}
      dat = [x for x in self.__tv_list if (x['pg'] == 'free' or self.__x)]
      for i, ch in enumerate(dat):
        if self.__cb:
          self.__cb(
                      {
                        'pr': int((i * 100) / len(dat)),
                        'str': 'Sync: %s' % ch['epg_name'].encode('utf-8'),
                        'idx': i,
                        'max': len(dat)
                      }
                    )

        if  self.__en_group_ch:
          ch_group_name = self.__en_group_ch
        else:
          ch_group_name = ch['genre']

        if self.__gen_m3u:
          if not map:
            pl = pl + '#EXTINF:-1 radio="%s" group-title="%s" tvg-logo="%s" tvg-id="%s",%s\n%s|User-Agent=%s\n' % (ch['radio'], ch_group_name, ch['epg_name'], ch['epg_name'], ch['title'], ch['sources'], urllib.quote_plus(self.__UA['User-Agent']))
          else:
            e_map = map.get(ch['epg_name'], {ch['epg_name']:{'id': ch['epg_name'], 'offset': '0', 'ch_logo': ch['epg_name']}})
            gid = e_map.get('id', ch['epg_name'])
            offset = e_map.get('offset', '0')
            logo = e_map.get('ch_logo', ch['epg_name'])
            pl = pl + '#EXTINF:-1 radio="%s" tvg-shift=%s group-title="%s" tvg-logo="%s" tvg-id="%s",%s\n%s|User-Agent=%s\n' % (ch['radio'], offset, ch_group_name, logo, gid, ch['title'], ch['sources'], urllib.quote_plus(self.__UA['User-Agent']))

        if self.__gen_jd:
          jdump[ch['epg_name']]=ch['epg_name']

        if self.__gen_epg:
          w.addChannel(
                      {'display-name': [(ch['title'], u'bg')],
                      'icon': [{'src': ch['logo_selected']}],
                      'id': ch['epg_name'],
                      'url': ['https://test.iptv.bulsat.com']}
                      )
          if ch.has_key('program'):
            for p in ch['program']:
              w.addProgramme(
                            {'start': p['start'],
                            'stop': p['stop'],
                            'title': [(p['title'], u'')],
                            'desc': [(p['desc'], u'')],
                            'category': [(ch['genre'], u'')],
                            'channel': ch['epg_name'],
                            'video': {'quality': ch['quality']},
                            'rating': [{'value': ch['pg']}]}
                          )

      if self.__gen_m3u:
        f_m3u =  open(os.path.join(self.__path, '', 'bulsat.m3u'), 'wb+')
        f_m3u.write(pl.encode(self.__char_set, 'replace'))
        f_m3u.close()
        if self.__gen_jd:
          with open(os.path.join(self.__path, '', 'map.json'), 'wb+') as f:
            f.write(unicode(json.dumps(jdump,
                            sort_keys = True,
                            indent = 1,
                            ensure_ascii=False)))

      if self.__gen_epg:
        if self.__compress:
          out = StringIO.StringIO()
          w.write(out, pretty_print=True)
          f_lmx = gzip.open(os.path.join(self.__path, '', 'bulsat.xml.gz'), 'w+', 9)
          f_lmx.write(out.getvalue())
          f_lmx.close()
          out.close()
        else:
          out = open(os.path.join(self.__path, '', 'bulsat.xml'), 'wb+')
          w.write(out, pretty_print=True)
          out.close()

    return ret
import sys, re, urllib.request, xmltv, xml.etree.ElementTree as ET
from datetime import datetime, date, time, timedelta

BASE_URL = "https://appletv.redbull.tv/products/tv"
request = urllib.request.Request(BASE_URL, headers={"Accept" : "application/xml"})
response = urllib.request.urlopen(request)
xml = ET.parse(response)
items = xml.findall('.//twoLineMenuItem')

w = xmltv.Writer()

w.addChannel({'display-name': [(u'Red Bull TV', u'en')],'id': u'hls.redbulltv'})

for i, element in enumerate(items):
    programme = {"channel":'hls.redbulltv', "title":[], "sub-title":[], "desc":[], "start":'', "stop":''}

    label = u''
    if element.find('.//label') is not None:
        label = element.find('.//label').text
    label2 = ''
    if element.find('.//label2') is not None:
        label2 = element.find('.//label2').text
    start = ''
    if element.find('.//rightLabel') is not None:
        start = element.find('.//rightLabel').text
        if start is not None:
            start = datetime.utcfromtimestamp(float(start))
        else:
            start = datetime.utcnow()#.strftime('%Y%m%d%H%M%S%z')

    # Set end has start to calculate duration from
def generate_new_xmltv_files(all_data, all_channels, all_programmes,
                             all_programmes_local):
    """In root directory, generate all new XMLTV files."""
    print('\n# Generate new XMLTV files in root directory', flush=True)
    for country_code, country_infos in COUNTRIES_CHANNELS.items():
        print('\n\t* Processing of {} country'.format(country_code),
              flush=True)

        # Write full xmltv file
        for fp_prefix in ['', '_local']:
            dst_fp = ROOT_DIRECTORY + country_infos['dst'].format(fp_prefix)
            print('\t\t- Write full{} xmltv file in {}'.format(
                fp_prefix, os.path.basename(dst_fp)),
                  flush=True)
            w = xmltv.Writer()

            # Add channels
            for channel_id in country_infos['channels']:
                if channel_id in all_channels:
                    w.addChannel(all_channels[channel_id])

            # Add programmes
            if fp_prefix == '_local':
                programmes = all_programmes_local
            else:
                programmes = all_programmes

            cnt = 0
            for channel_id in country_infos['channels']:
                if channel_id in programmes:
                    for programme in programmes[channel_id]:
                        w.addProgramme(programme)
                        cnt += 1

            if cnt == 0:
                print(
                    '\t\t* This file does not contain any TV shows :-/, do not write it',
                    flush=True)
                continue

            # Write XMLTV file
            with open(dst_fp, 'wb') as f:
                w.write(f, pretty_print=True)
            print('\t\t\t- Final file contains {} TV shows'.format(cnt),
                  flush=True)

        # Write one day xmltv files
        for offset in range(-2, 8):
            date = TODAY + timedelta(days=offset)
            date_s = date.strftime("%Y%m%d")

            for fp_prefix in ['', '_local']:
                dst_fp = ROOT_DIRECTORY + country_infos['dst'].format(
                    fp_prefix + '_' + date_s)
                print('\t\t- Write day {} in {}'.format(
                    date_s, os.path.basename(dst_fp)),
                      flush=True)
                w = xmltv.Writer()

                # Add channels
                for channel_id in country_infos['channels']:
                    if channel_id in all_channels:
                        w.addChannel(all_channels[channel_id])

                # Add programmes
                if fp_prefix == '_local':
                    programmes = all_programmes_local
                else:
                    programmes = all_programmes

                cnt = 0
                for channel_id in country_infos['channels']:
                    if channel_id in programmes:
                        for p in programmes[channel_id]:
                            add_it = False
                            if 'start' in p and 'stop' in p:
                                start_s = p['start'][0:8]
                                stop_s = p['stop'][0:8]
                                if start_s == date_s or stop_s == date_s:
                                    add_it = True
                            else:
                                add_it = True
                            if add_it:
                                w.addProgramme(p)
                                cnt = cnt + 1

                with open(dst_fp, 'wb') as f:
                    w.write(f, pretty_print=True)
                print('\t\t\t- Final file contains {} TV shows'.format(cnt),
                      flush=True)

    print('\n\t* Merge all country tv guides in tv_guide_all.xml', flush=True)

    w = xmltv.Writer()

    # Add all channels
    for channel_id, channel in all_channels.items():
        w.addChannel(channel)

    # Add all programmes
    cnt = 0
    for channel_id, programmes in all_programmes.items():
        for p in programmes:
            w.addProgramme(p)
            cnt = cnt + 1

    with open(ROOT_DIRECTORY + 'tv_guide_all.xml', 'wb') as f:
        w.write(f, pretty_print=True)
    print('\t\t- Final file contains {} TV shows'.format(cnt), flush=True)

    print('\n\t* Merge all country tv guides in tv_guide_all_local.xml',
          flush=True)

    w = xmltv.Writer()

    # Add all channels
    for channel_id, channel in all_channels.items():
        w.addChannel(channel)

    # Add all programmes
    cnt = 0
    for channel_id, programmes in all_programmes_local.items():
        for p in programmes:
            w.addProgramme(p)
            cnt = cnt + 1

    with open(ROOT_DIRECTORY + 'tv_guide_all_local.xml', 'wb') as f:
        w.write(f, pretty_print=True)
    print('\t\t- Final file contains {} TV shows'.format(cnt), flush=True)
Esempio n. 8
0
    def write_xmltv_file(self):
        " Retrieve EPG data from Locast instance and write xmltv file "
        writer = xmltv.Writer()
        channels = []
        progs = []
        epg = self.lcst.get_epg()

        # Need an EPG to continue
        if not epg:
            return

        # Process the electronic program guide data
        for epg_chan in epg:
            # Create xmltv channel for each channel
            channel = {}
            channel['display-name'] = [(epg_chan['callSign'], u'en'),
                                       (epg_chan['name'], u'en')]
            channel['id'] = str(epg_chan['id']) + '.locast.org'
            channel['icon'] = [{'src': epg_chan['logoUrl']}]
            channels.append(channel)

            # Create xmltv programme for each listing
            for listing in epg_chan['listings']:
                prog = {
                    'channel':
                    str(listing.get('stationId')) + '.locast.org',
                    'new':
                    listing.get('isNew', False),
                    'length': {
                        'units': u'seconds',
                        'length': str(listing.get('duration', -1))
                    },
                    'rating': [{
                        'value': listing.get('rating', 'Unknown')
                    }],
                    'start':
                    time.strftime(
                        '%Y%m%d%H%M%S %Z',
                        time.localtime(listing.get('startTime', 0) / 1000)),
                    'title': [(listing.get('title', ''), u'')]
                }
                if 'genres' in listing:
                    prog['category'] = [(g, u'')
                                        for g in listing['genres'].split(',')]
                if 'topCast' in listing or 'directors' in listing:
                    prog['credits'] = {
                        'director': listing.get('directors', '').split(','),
                        'actor': listing.get('topCast', '').split(',')
                    }
                if 'releaseYear' in listing:
                    prog['date'] = str(listing['releaseYear'])
                if 'description' in listing:
                    prog['desc'] = [(listing['description'], u'')]
                if 'seasonNumber' in listing and 'episodeNumber' in listing:
                    episode = f"S{listing['seasonNumber']}E{listing['episodeNumber']}"
                    prog['episode-num'] = [(episode, u'common')]
                if 'episodeTitle' in listing:
                    prog['sub-title'] = [(listing.get('episodeTitle',
                                                      ''), u'')]
                progs.append(prog)

        # Add channels and programs to xmltv writer
        for channel in channels:
            writer.addChannel(channel)
        for prog in progs:
            writer.addProgramme(prog)

        # Write the xmltv file
        writer.write(self.outfile, pretty_print=True)
        self.logger.info(
            f"Grabbed {len(channels)} channels and {len(progs)} programs for EPG"
        )

        return
Esempio n. 9
0
            # Convert to UTC timezone
            utc_tz = pytz.UTC
            d = d.astimezone(utc_tz)

            # Finally replace the datetime with the UTC one
            s = d.strftime(date_format_notz)
            # print('Replace {} by {}'.format(programme[elt], s))
            programme[elt] = s

    # Write corrected full xmltv files
    for fp_prefix in ['', '_local']:
        dst_fp = country_infos['dst_fp'].format(fp_prefix)
        print('\t- Write corrected full xmltv file in {}'.format(
            os.path.basename(dst_fp)))
        w = xmltv.Writer(
            source_info_url=country_infos['data_l']['source-info-url'])

        # Add channels
        for c in country_infos['channels_l']:
            w.addChannel(c)

        # Add programmes
        if fp_prefix == '_local':
            for p in country_infos['programmes_local_datetime_l']:
                w.addProgramme(p)
        else:
            for p in country_infos['programmes_l']:
                w.addProgramme(p)

        # Write XMLTV file
        with open(dst_fp, 'w') as f:
Esempio n. 10
0
import json
import xmltv
import sys
import os, os.path
import subprocess
from dateutil.parser import parse

channelNumber = 1
writerClass = xmltv.Writer()

if os.path.exists('dump.json'):
    os.unlink('dump.json')

#Let's start

FNULL = open(os.devnull, 'w')

while True:
    if subprocess.call(['xvfb-run', 'phantomjs', 'dump.js', str(channelNumber)], stdout = FNULL, stderr=subprocess.STDOUT) == 0:
        if os.path.exists('dump.json'):
            with open('dump.json') as dumpfp:
                #try:
                    jsonDump = json.load(dumpfp)
                    if 'content' in jsonDump:
                        if len(jsonDump['content']) > 0 and jsonDump['content'][len(jsonDump['content']) - 1]['id'] != channelNumber:
                            for channelData in jsonDump['content']:
                                writerClass.addChannel({
                                    'display-name': [(unicode(channelData['title']), u'pt')],
                                    'id': unicode(channelData['id']),
                                    'icon': [{'src': unicode(channelData['imageUrl'])}]
                                })
Esempio n. 11
0
import sys
import re
import xmltv
import datetime
import traceback
from requests_html import HTMLSession

CHANNELS = [('TV Sergipe', '1245'), ('SBT Aracaju', '177'),
            ('TV Aperipê', '1244'), ('Band', '89'), ('TV Atalaia', '129')]

session = HTMLSession()
writer_class = xmltv.Writer()

for channel in CHANNELS:
    print("Scrapping {}".format(channel[0]))

    r = session.get("http://lineup.tv.br/gdc.php?idCanal={}".format(
        channel[1]))
    if r.status_code == 200:
        url_grade = None
        channel_icon = None

        for img in r.html.find('img'):
            if 'src' in img.attrs:
                if img.attrs['src'].startswith(
                        'http://www.lineup.tv.br/canais'):
                    channel_icon = img.attrs['src']
                    break

        for link in r.html.find("a"):
            if 'href' in link.attrs:
Esempio n. 12
0
def generate_xmltv(channels):
    date_from = datetime.datetime.now() - datetime.timedelta(days=0)
    date_to = datetime.datetime.now() + datetime.timedelta(
        days=int(os.environ.get('MAGIO_GUIDE_DAYS', 7)))
    channel_ids = list(map(lambda c: c.id, channels))
    epg = magio.epg(channel_ids, date_from, date_to)
    with open("public/magioGuide.xmltv", "wb") as guide_file:
        writer = xmltv.Writer(
            date=datetime.datetime.now().strftime("%Y%m%d%H%M%S"),
            generator_info_name="MagioGoIPTVServer",
            generator_info_url="",
            source_info_name="Magio GO Guide",
            source_info_url="https://skgo.magio.tv/v2/television/epg")
        # Write channels
        for channel in channels:
            channel_dict = {
                'display-name': [(channel.name, u'sk')],
                'icon': [{
                    'src': channel.logo
                }],
                'id': channel.id
            }
            writer.addChannel(channel_dict)
        # Write programmes
        for (channel_id, programmes) in epg.items():
            for programme in programmes:
                programme_dict = {
                    'category': [(genre, u'en') for genre in programme.genres],
                    'channel':
                    channel_id,
                    'credits': {
                        'producer':
                        [producer for producer in programme.producers],
                        'actor': [actor for actor in programme.actors],
                        'writer': [writer for writer in programme.writers],
                        'director':
                        [director for director in programme.directors]
                    },
                    'date':
                    str(programme.year),
                    'desc': [(programme.description, u'')],
                    'icon': [{
                        'src': programme.poster
                    }, {
                        'src': programme.thumbnail
                    }],
                    'length': {
                        'units': u'seconds',
                        'length': str(programme.duration)
                    },
                    'start':
                    programme.start_time.strftime("%Y%m%d%H%M%S"),
                    'stop':
                    programme.end_time.strftime("%Y%m%d%H%M%S"),
                    'title': [(programme.title, u'')]
                }

                # Define episode info only if provided
                if programme.episodeNo is not None:
                    # Since seasonNo seems to be always null, try parsing the season from the title (e.g. Kosti X. = 10)
                    if programme.seasonNo is None:
                        (show_title_sans_season,
                         programme.seasonNo) = parse_season_number(
                             programme.title)
                        programme_dict['title'] = [(show_title_sans_season,
                                                    u'')]

                    programme_dict['episode-num'] = [(
                        f'{(programme.seasonNo or 1) - 1} . {(programme.episodeNo or 1) - 1} . 0',
                        u'xmltv_ns')]

                writer.addProgramme(programme_dict)

        writer.write(guide_file, True)
    # Gzip the guide file
    gzip_file("public/magioGuide.xmltv")