TEst Ora in Onda con Json

This commit is contained in:
Alhaziel01
2020-06-25 20:19:06 +02:00
parent f0ec7321f3
commit 277b5495f1
+105 -67
View File
@@ -5,13 +5,18 @@
import datetime import datetime
import glob import glob
import time import time
import xml.etree.ElementTree as ET # import xml.etree.ElementTree as ET
from core import filetools, downloadtools from core import filetools, downloadtools, jsontools, scrapertools
from core.item import Item from core.item import Item
from platformcode import logger, config from platformcode import logger, config
import gzip import gzip
host = "http://www.epgitalia.tv/xml/guide2.gzip" host = "http://www.epgitalia.tv/xml/guide.gzip"
now = datetime.datetime.now()
fileName = config.get_temp_file('guidatv-') + now.strftime('%Y %m %d')
archiveName = fileName + '.gz'
# xmlName = fileName + '.xml'
jsonName = fileName + '.json'
def mainlist(item): def mainlist(item):
@@ -20,17 +25,13 @@ def mainlist(item):
itemlist.append(Item(title='Film in onda oggi', channel=item.channel, action='peliculas', contentType='movie')) itemlist.append(Item(title='Film in onda oggi', channel=item.channel, action='peliculas', contentType='movie'))
itemlist.append(Item(title='Serie Tv in onda oggi', channel=item.channel, action='peliculas', contentType='tvshow')) itemlist.append(Item(title='Serie Tv in onda oggi', channel=item.channel, action='peliculas', contentType='tvshow'))
itemlist.append(Item(title='Guida tv per canale', channel=item.channel, action='listaCanali')) # itemlist.append(Item(title='Guida tv per canale', channel=item.channel, action='listaCanali'))
itemlist.append(Item(title='Canali live (Rai Play)', channel=item.channel, action='live')) itemlist.append(Item(title='Canali live (Rai Play)', channel=item.channel, action='live'))
return itemlist return itemlist
def getEpg(): def getEpg():
now = datetime.datetime.now()
fileName = config.get_temp_file('guidatv-') + now.strftime('%Y %m %d')
archiveName = fileName + '.gz'
xmlName = fileName + '.xml'
if not filetools.exists(archiveName): if not filetools.exists(archiveName):
logger.info('downloading epg') logger.info('downloading epg')
# cancello quelli vecchi # cancello quelli vecchi
@@ -38,71 +39,40 @@ def getEpg():
filetools.remove(f, silent=True) filetools.remove(f, silent=True)
# inmemory = io.BytesIO(httptools.downloadpage(host).data) # inmemory = io.BytesIO(httptools.downloadpage(host).data)
downloadtools.downloadfile(host, archiveName) downloadtools.downloadfile(host, archiveName)
logger.info('opening gzip and writing xml') if filetools.exists(archiveName) and not filetools.exists(jsonName):
logger.info('opening gzip and writing Json')
fStream = gzip.GzipFile(archiveName, mode='rb') fStream = gzip.GzipFile(archiveName, mode='rb')
guide = fStream.read() guide = fStream.read()
with open(xmlName, 'w') as f: write_json(guide)
f.write(guide)
guide = open(xmlName).read() with open(jsonName) as f:
return ET.fromstring(guide) json = jsontools.load(f.read())
return json
def peliculas(item): def peliculas(item):
itemlist = [] itemlist = []
xml = getEpg() start = time.time()
json_file = getEpg()
for prog in xml.findall('programme[credits]'): # se hanno il cast probabilmente si tratta di un contenuto cinematografico logger.info('PARSE TIME: ' + str(time.time() - start))
title = prog.find('title') start = time.time()
if title is not None: if item.contentType == 'movie':
title = title.text.replace(' - 1^TV', '') json = json_file['movies']
# non mostro 2 volte lo stesso film else:
for it in itemlist: json = json_file['tvshows']
if it.contentTitle == title: for title, value in json.items():
break if 'infoLabels' in value:
else: infoLabels = value['infoLabels']
episode = prog.find('episode-num') itemlist.append(Item(
if episode is not None: channel=item.channel,
episode = ' (' + episode.text + ')' search_text=title,
else: title=title + (' - ' + value['episode'] if 'episode' in value else ''),
episode = '' plot=value['plot'],
if (episode and item.contentType == 'tvshow') or (not episode and item.contentType == 'movie'): thumbnail=value['thumbnail'],
desc = prog.find('desc') contentType=item.contentType,
if desc is not None: infoLabels=infoLabels
desc = desc.text ))
actors = prog.find('credits') logger.info('BUILD ITEMLIST: ' + str(time.time() - start))
if actors is not None:
actors = [(actor.text, actor.attrib['role'] if 'role' in actor.attrib else '') for actor in actors]
date = prog.find('date')
if date is not None:
date = date.text
genres = prog.find('category')
if genres is not None:
genres = ','.join([cat.text for cat in genres])
thumbnail = prog.find('icon')
if thumbnail is not None:
thumbnail = thumbnail.attrib['src']
country = prog.find('country')
if country is not None:
country = country.text
itemlist.append(Item(
channel=item.channel,
action='new_search',
title=title,
search_text=title,
mode=item.contentType,
year=date if date else '-',
thumbnail=thumbnail if thumbnail else '',
contentType=item.contentType,
infoLabels={
'title': title,
'plot': desc if desc else '',
'casta': actors if actors else '',
'genre': genres if genres else '',
'country': country if country else ''
}
))
# tmdb.set_infoLabels_itemlist(itemlist, seekTmdb=True) # tmdb.set_infoLabels_itemlist(itemlist, seekTmdb=True)
return itemlist return itemlist
@@ -184,7 +154,7 @@ def guidatv(item):
infoLabels={ infoLabels={
'title': title, 'title': title,
'plot': desc if desc else '', 'plot': desc if desc else '',
'casta': actors if actors else '', 'castandrole': tuple(actors) if actors else '',
'genre': genres if genres else '', 'genre': genres if genres else '',
'country': country if country else '' 'country': country if country else ''
} }
@@ -201,3 +171,71 @@ def new_search(item):
def live(item): def live(item):
from channels import raiplay from channels import raiplay
return raiplay.dirette(raiplay.mainlist(Item())[0]) return raiplay.dirette(raiplay.mainlist(Item())[0])
def write_json(f):
titles = []
channel_dict = {}
channel_dict['movies'] = {}
channel_dict['tvshows'] = {}
channel = ''
title = ''
episode = ''
plot = ''
thumbnail = ''
actors = []
director = ''
year = ''
genres = []
country = ''
start = ''
stop = ''
for line in f.splitlines():
if '<programme' in line: start, stop, channel = scrapertools.find_single_match(line, r'start="(\d+)[^"]*" stop="(\d+)[^"]*" channel="([^"]+)"')
elif '<title' in line: title = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<desc' in line: plot = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<actor' in line:
match = scrapertools.find_single_match(line, r'(?:role="([^"]*)")?>([^<]+)<')
actors.append([match[1], match[0]])
elif '<director' in line: director = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<date' in line: year = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<category' in line: genres.append(scrapertools.find_single_match(line, r'>([^<]+)<'))
elif '<country' in line: country = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<episode-num' in line: episode = scrapertools.find_single_match(line, r'>([^<]+)<')
elif '<icon' in line: thumbnail = scrapertools.find_single_match(line, r'src="([^"]+)"')
elif '</programme' in line and title and title not in titles and actors:
titles.append(title)
if episode:
t = channel_dict['tvshows']
else:
t = channel_dict['movies']
t[title] = {}
t[title]['infoLabels'] = {'castandrole':actors, 'director':director, 'genres':genres, 'country':country, 'year':year}
t['start'] = start
t['stop'] = stop
t['episode'] = episode
t[title]['title'] = title
t[title]['channel_name'] = channel
t[title]['thumbnail'] = thumbnail
t[title]['plot'] = plot
channel = ''
title = ''
episode = ''
plot = ''
thumbnail = ''
actors = []
director = ''
year = ''
genres = []
country = ''
start = ''
stop = ''
# f = json.dumps(channel_dict)
with open(jsonName, 'w') as json_file:
json_file.write(jsontools.dump(channel_dict, sort_keys=False))
json_file.close()