def Update_Listings(self): #urllib.request.urlretrieve("http://www.xmltv.co.uk/feed/6721", "tvlistings.xml") self.channelsInputDict = xmltv.read_channels( open('tvlistings.xml', 'r')) self.programmeInputDict = xmltv.read_programmes( open('tvlistings.xml', 'r')) self.lastUpdatedListings = datetime.now()
def create_playlist(): credentials = get_service_creds(site) download_file('http://smoothstreams.tv/schedule/feed.xml') if os.path.exists('feed.xml'): channels = xmltv.read_channels('feed.xml') else: raise IOError('feed.xml does not exist!') global_header = '#EXTM3U\n' if time_shift != 0: global_header = '#EXTM3U tvg-shift=%s\n' % time_shift if playlist_file_name is None: output_name = 'playlist.m3u' else: output_name = playlist_file_name with open(output_name, 'w') as f: f.write(global_header) for c in channels: channel_id = c['id'] channel_icon = c['icon'][0]['src'].split('/')[-1].split('.')[0] channel_display_name = c['display-name'][0][0] channel_header = '#EXTINF:-1 tvg-id="%s" tvg-name="%s" tvg-logo="%s" group-title="Group 1",%s\n' \ % (channel_id, channel_display_name, channel_icon, channel_display_name) f.write(channel_header) f.write( get_stream_url(site, protocol, c['id'], credentials) + '\n') f.close()
def create_playlist(site, username, password, protocol, server, quality, time_shift, filename): playlist = '' credentials = get_service_creds(site, username, password) download_file('http://smoothstreams.tv/schedule/feed.xml') if os.path.exists('feed.xml'): channels = xmltv.read_channels('feed.xml') else: raise IOError('feed.xml does not exist!') global_header = '#EXTM3U\n' if time_shift != '0': global_header = '#EXTM3U tvg-shift=%s\n' % time_shift playlist += global_header for c in channels: channel_id = c['id'] channel_icon = c['icon'][0]['src'].split('/')[-1] channel_display_name = c['display-name'][0][0] group_title = 'SmoothStreams' channel_header = '#EXTINF:-1 tvg-id="%s" tvg-name="%s" tvg-logo="%s" group-title="%s",%s\n' \ % (channel_id, channel_display_name, channel_icon, group_title, channel_display_name) playlist += channel_header playlist += get_stream_url(site, protocol, server, c['id'], quality, credentials) + '\n' if filename is not None: output_name = filename with open(output_name, 'w') as f: f.write(playlist) f.close() else: return playlist
def create_playlist(): credentials = get_service_creds(site) download_file('http://smoothstreams.tv/schedule/feed.xml') if os.path.exists('feed.xml'): channels = xmltv.read_channels('feed.xml') else: raise IOError('feed.xml does not exist!') global_header = '#EXTM3U\n' if time_shift != 0: global_header = '#EXTM3U tvg-shift=%s\n' % time_shift if playlist_file_name is None: output_name = 'playlist.m3u' else: output_name = playlist_file_name with open(output_name, 'w') as f: f.write(global_header) for c in channels: channel_id = c['id'] channel_icon = c['icon'][0]['src'].split('/')[-1].split('.')[0] channel_display_name = c['display-name'][0][0] channel_header = '#EXTINF:-1 tvg-id="%s" tvg-name="%s" tvg-logo="%s" group-title="Group 1",%s\n' \ % (channel_id, channel_display_name, channel_icon, channel_display_name) f.write(channel_header) f.write(get_stream_url(site, protocol, c['id'], credentials) + '\n') f.close()
def parse_channels(): channels = {} for key in xmltv.read_channels(open(filename, 'r')): channels[key['id']] = { 'id': key['id'], 'icon': key['icon'][0]['src'], 'name': map(itemgetter(0), key['display-name'])[0] } return channels
def __load_channels(self): for channel in xmltv.read_channels(open(self.path)): c = Channel(channel, self) self.__channel_names[c.id] = c.name self.listings.add_channel(c) gtk.gdk.threads_enter() self.emit("loaded-channel", (c)) gtk.gdk.threads_leave() if self.config.debug: print('Added channel "%s" to Listings.' % c.name)
def channels(): channels = {} for key in xmltv.read_channels(open(filename, 'r')): name = map(itemgetter(0), key['display-name']) id = key['id'] src = key['icon'][0]['src'] name = name[0] rec = dict(zip(Channel._fields, [id, name, src])) channel = Channel(**rec) channels[channel.id] = channel return channels
def run(self, *args, **kw): input_file = self.opt.get('in') if not input_file: self.logger.error('--in parameter is required') return if self.opt.get('update_channels'): country = self.opt.get('country') if not country: self.logger.error("--country parameter is required") return try: country = Country.objects.get(code=country.upper()) except Country.DoesNotExist, e: self.logger.error("invalid country code") for c in xmltv.read_channels(input_file): name = c['display-name'][0][0] id = c['id'] icon = c.get('icon') icon_url = icon and icon[0].get('src') self.logger.info(icon_url) def defaults(): if icon_url: try: icon = ContentFile(urllib2.urlopen(icon_url).read()) icon.name = icon_url.split('/')[-1] yield 'icon', icon except urllib2.HTTPError, e: pass yield 'name', name yield 'country', country channel, created = Channel.objects.get_or_create( type=Channel.TYPE_TV_CHANNEL, fetcher__cid=id, fetcher__name='xmltv', defaults=defaults(), ) if created: Fetcher(name='xmltv', channel=channel, cid=id).save() if not created and self.opt.get('force'): if channel.icon: channel.icon.delete() for (n, v) in defaults(): setattr(channel, n, v) channel.save()
def run(self, *args, **kw): input_file = self.opt.get('in') if not input_file: self.logger.error('--in parameter is required') return if self.opt.get('update_channels'): country = self.opt.get('country') if not country: self.logger.error("--country parameter is required") return try: country = Country.objects.get(code=country.upper()) except Country.DoesNotExist, e: self.logger.error("invalid country code") for c in xmltv.read_channels(input_file): name = c['display-name'][0][0] id = c['id'] icon = c.get('icon') icon_url = icon and icon[0].get('src') self.logger.info(icon_url) def defaults(): if icon_url: try: icon = ContentFile( urllib2.urlopen(icon_url).read()) icon.name = icon_url.split('/')[-1] yield 'icon', icon except urllib2.HTTPError, e: pass yield 'name', name yield 'country', country channel, created = Channel.objects.get_or_create( type=Channel.TYPE_TV_CHANNEL, fetcher__cid=id, fetcher__name='xmltv', defaults=defaults(), ) if created: Fetcher(name='xmltv', channel=channel, cid=id).save() if not created and self.opt.get('force'): if channel.icon: channel.icon.delete() for (n, v) in defaults(): setattr(channel, n, v) channel.save()
def chan(self, irc, msg, args): """takes no arguments Returns the list of TV channels from XMLTV feed. """ global filenames chan_list = '' for filename in filenames: try: chans = xmltv.read_channels(open(filename, 'r')) for chan in chans: try: chan_list = chan_list + chan['display-name'][0][0] + ' ' except: pass except: pass irc.reply(chan_list)
def tv(self, irc, msg, args): """takes no arguments Returns the list of current TV shows. """ global filenames now = datetime.datetime.today() show_list = "" show_count = 0 for filename in filenames: print filename try: chans = xmltv.read_channels(open(filename, 'r')) for chan in chans: try: chan_name = chan['display-name'][0][0] print chan_name shows = xmltv.read_programmes(open(filename, 'r')) for show in shows: try: start_time = parse(show['start'],ignoretz=True) stop_time = parse(show['stop'],ignoretz=True) if (start_time <= now and stop_time >= now): show_count = show_count + 1 show_list = show_list + "\x0F" + "\x02" + chan_name + "\x0F" + " " + str(start_time.strftime("%H:%M")) + " " + (show['title'][0][0]).encode("UTF-8") + " " # if not (show_count % 3): # irc.reply(show_list) # show_list = "" except: pass except: pass except: pass irc.reply(show_list) print show_list
def get_xmltv(): """ Download XMLTV url and store channels and programs in the database. :return: None :return: """ # http://wiki.xmltv.org/index.php/Main_Page/xmltvfileformat.html import urllib2 import gzip import StringIO import xmltv url = cfg.TVGURL # Download XMLTV source out_file_path = url.split("/")[-1][:-3] print('Downloading TV program from: {}'.format(url)) response = urllib2.urlopen(url) compressed_file = StringIO.StringIO(response.read()) decompressed_file = gzip.GzipFile(fileobj=compressed_file) # Extract XMLTV with open(out_file_path, 'w') as outfile: outfile.write(decompressed_file.read()) # Print XMLTV header xmltv_data = xmltv.read_data(open(out_file_path, 'r')) ic(xmltv_data) # Read xml channels xmlchannels = xmltv.read_channels(open(out_file_path, 'r')) print("Got {} channels from XMLTV source".format(len(xmlchannels))) # Drop content of XMLChannel XMLChannel.query.delete() db.session.commit() # Populate XMLChannel with channels from XMLTV source for xc in xmlchannels: xmlchannel = XMLChannel(id=int(xc['id']), label=xc['display-name'][0][0].strip()) db.session.add(xmlchannel) db.session.commit() programs = xmltv.read_programmes(open(out_file_path, 'r')) chunk = 1024 index = 0 for pr in programs: desc = "" try: desc = pr['desc'][0][0] except KeyError: pass a_category = Category.query.filter( Category.name == pr['category'][0][0]).first() if a_category: p = Program(channel=int(pr['channel']), title=pr['title'][0][0], start=duparse(pr['start']), stop=duparse(pr['stop']), desc=desc, category_id=a_category.id) db.session.add(p) else: py = Category(name=pr['category'][0][0]) Program(channel=int(pr['channel']), title=pr['title'][0][0], start=duparse(pr['start']), stop=duparse(pr['stop']), desc=desc, category=py) db.session.add(py) index += 1 if index % chunk == 0: db.session.commit() db.session.commit() categories = [x.name for x in Category.query.all()] ic(u', '.join(categories))
import xmltv from pprint import pprint # If you need to change the locale: xmltv.locale = 'UTF-8' # If you need to change the date format used in the XMLTV file: # xmltv.date_format = '%Y%m%d%H%M%S %Z' filename = '/home/martin/src/supybot-xmltv/11_channeldata.xml' # Print info for XMLTV file (source, etc.) pprint(xmltv.read_data(open(filename, 'r'))) # Print channels pprint(xmltv.read_channels(open(filename, 'r'))) # Print programmes #pprint(xmltv.read_programmes(open(filename, 'r'))) #print chr(2) + tere + chr(2) + "\n";
def parse_raw_xmltv_files(): """Parse all xmltv files to deserialize info in dict.""" print('\n# Parse all raw XMLTV files', flush=True) all_channels = {} # Key: channel id in xmltv file, Value: channel dict all_data = {} # Key: grabber, Value: data dict all_programmes = { } # Key: channel id, Value: List of dict programme with UTC time all_programmes_local = { } # Key: channel id, Value: List of dict programme with local time for grabber, grabber_infos in GRABBERS.items(): print('\n\t* Processing of {} grabber\'s raw files:'.format(grabber), flush=True) for offset in range(-10, 20): date = TODAY + timedelta(days=offset) xmltv_fp = RAW_DIRECTORY + grabber_infos['raw'].format( '_' + date.strftime("%Y%m%d")) if not os.path.exists(xmltv_fp): continue print('\t\t* Parse {} file'.format(xmltv_fp), flush=True) # Deserialize xmltv file try: data = xmltv.read_data(xmltv_fp) channels = xmltv.read_channels(xmltv_fp) programmes = xmltv.read_programmes(xmltv_fp) except Exception as e: print( '\t\t\t- This file seems to be corrupt :-/, delete it ({})' .format(e), flush=True) os.remove(xmltv_fp) continue # Data if grabber not in all_data: all_data[grabber] = data # Channels print('\t\t\t- This file contains {} channels'.format( len(channels)), flush=True) for channel in channels: channel_id = channel['id'] if channel_id not in all_channels: all_channels[channel_id] = channel if channel_id not in all_programmes: all_programmes[channel_id] = [] if channel_id not in all_programmes_local: all_programmes_local[channel_id] = [] # Programmes # If there is no programme, delete this raw xmltv file if not programmes: print( '\t\t\t- This file does not contain any TV shows :-/, delete it', flush=True) os.remove(xmltv_fp) else: print('\t\t\t- This file contains {} TV shows'.format( len(programmes)), flush=True) for programme in programmes: channel_id = programme['channel'] all_programmes_local[channel_id].append(dict(programme)) # Replace local datetime by UTC datetime programme_utc = dict(programme) for elt in ['start', 'stop']: if elt not in programme_utc: continue s = programme_utc[elt] # Remove timezone part to get %Y%m%d%H%M%S format s = s.split(' ')[0] # Get the naive datetime object d = datetime.strptime(s, DATE_FORMAT_NOTZ) # Add correct timezone tz = pytz.timezone(grabber_infos['tz']) d = tz.localize(d) # 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_utc[elt], s)) programme_utc[elt] = s all_programmes[channel_id].append(dict(programme_utc)) return (all_data, all_channels, all_programmes, all_programmes_local)
# This probably won't work for you, and I've removed basically all the error checking. # # You've been warned, be sure to back up your data before running this garbage. import os import sys import time import re import shutil import xmltv # Define variables lamedb_path="/xmltv/" lamedb=lamedb_path + "lamedb" xmltv_file="/xmltv/tvguidesat.xml" map_file="/xmltv/channels.xml" xml_channels = xmltv.read_channels(open(xmltv_file, 'r')) combined_dict = {} def decode_charset(s): u = None charset_list = ('utf-8','iso-8859-1','iso-8859-2','iso-8859-15') for charset in charset_list: try: u = unicode(s,charset,"strict") except: pass else: break if u == None:
def write_cats(root, cont, is_channel_cats): global cat_id for item in cont.keys(): if not item[0]: continue elem = SubElement(root, 'Category') if item[1]: elem.set('lang', item[1]) elem.set('id', str(cat_id)) cat_id += 1 if is_channel_cats: for ch in cont[item]: ch_elem = SubElement(elem, 'Channel') ch_elem.set('id', ch) elem.set('channel', 'true') elem.set('name', item[0]) if len(sys.argv) != 2: print 'Usage: %s epg.xml' % sys.argv[0] sys.exit(-1) channel_cats = find_cats(xmltv.read_channels(open(sys.argv[1]))) programmes_cats = find_cats(xmltv.read_programmes(open(sys.argv[1]))) root = Element('Categories') et = ElementTree(root) write_cats(root, channel_cats, True) write_cats(root, programmes_cats, False) et.write(sys.stdout, 'UTF-8')
'it': { 'raw_fp': os.path.join(WD, '../raw/tv_guide_it.xml'), 'dst_fp': os.path.join(WD, '../tv_guide_it{}.xml'), 'tz': 'Europe/Rome' } } for country_code, country_infos in countries.items(): print('* Processing of {} country:'.format(country_code)) # Parse channels, data and programmes in the raw xmltv file try: # country_infos['channels_list'] = xmltv.read_channels(open(country_infos['raw_fp'], 'r')) # country_infos['programmes_list_local_datetime'] = xmltv.read_programmes(open(country_infos['raw_fp'], 'r')) # country_infos['data_list'] = xmltv.read_data(open(country_infos['raw_fp'], 'r')) channels_l = xmltv.read_channels(open(country_infos['raw_fp'], 'r')) programmes_local_datetime_l = xmltv.read_programmes( open(country_infos['raw_fp'], 'r')) data_l = xmltv.read_data(open(country_infos['raw_fp'], 'r')) except Exception: continue # XMLTV data stays untouched country_infos['data_l'] = data_l # If any filter on channels exists, remove unwanted channels if 'channels_to_add' in country_infos: country_infos['channels_l'] = [] for channel in channels_l: if 'id' in channel and channel['id'] in country_infos[ 'channels_to_add']:
def Update_Listings(self): # Download the file from `url` and save it locally under `file_name`: # urllib.request.urlretrieve("http://www.xmltv.co.uk/feed/6721", "tvlistings.xml") self.channelsInputDict = xmltv.read_channels(open('tvlistings.xml', 'r')) self.programmeInputDict = xmltv.read_programmes(open('tvlistings.xml', 'r')) self.lastUpdatedListings = datetime.now()
def __init__(self, xmltvfile): self.stations = xmltv.read_channels(xmltvfile) self.programm = xmltv.read_programmes(xmltvfile) self.stations = self.get_stations(self.stations)
#!/usr/bin/python import xmltv import MySQLdb filename = '/PATH/tvguide.xml' db = MySQLdb.connect(host="localhost",user="******",passwd="PASSWORD",db="mythconverg") cur = db.cursor() channels = xmltv.read_channels(open(filename, 'r')) for channel in channels: # This will only work if the 2nd value of display-name in the XML file is a numerical SID cur.execute("UPDATE channel SET xmltvid='" + channel['id'] + "' WHERE channum=" + channel['display-name'][1][0])
def __init__(self,xmltvfile): self.stations = xmltv.read_channels(xmltvfile) self.programm = xmltv.read_programmes(xmltvfile) self.stations = self.get_stations(self.stations)
run with -d to download e.xml ''' import sys import os import requests import xmltv EPGURL = "http://epg.51zmt.top:8000/e.xml" EPGXML = "e.xml" ALLCHN = "all.chn" currentdir = os.path.dirname(os.path.realpath(__file__)) ename = os.path.join(currentdir, EPGXML) aname = os.path.join(currentdir, ALLCHN) if len(sys.argv) > 1 and sys.argv[1] == '-d': try: r = requests.get(EPGURL) with open(ename, 'wb') as f: f.write(r.content) except: print("file download failed.") sys.exit() channels = xmltv.read_channels(open(ename, 'r')) with open(aname, 'w') as f: for channel in channels: f.write((channel["display-name"][0][0]) + '\n')
sys.path.append(cristel_lib_dir) from scheduledb import ScheduleDB from eitdb import EITDatabase log=logging.getLogger("cristel") log.setLevel(logging.DEBUG) handler=logging.handlers.SysLogHandler(address = '/dev/log', facility=logging.handlers.SysLogHandler.LOG_DAEMON) log.addHandler(handler) appdir=os.path.expanduser("~/.epgdb") eitdb=os.path.join(appdir,"database.db") scheddb=os.path.join(appdir,"cristel.db") xmltvfile=os.path.join(appdir,"database.xmltv") eit=EITDatabase(eitdb,log) sch=ScheduleDB(scheddb,log) sql="" for ch in xmltv.read_channels(open(xmltvfile,'r')): name=ch["display-name"][0][0] xid=ch["id"] if len(sql): sql=sql + ",('" + name + "','" + xid + "',100,1,0,0,0)" else: sql="('" + name + "','" + xid + "',100,1,0,0,0)" sql="insert into channels (name,source,priority,visible,favourite,logicalid,muxid) values " + sql print sql