def _parse_smoothstreams_epg_json(cls, db_session, channel_name_map, do_use_provider_icons): epg_json_stream = cls._request_smoothstreams_epg_json() logger.debug( 'Processing SmoothStreams JSON EPG\nFile name => %s', SmoothStreamsConstants.EPG_FILE_NAME, ) number_of_objects_added_to_db_session = 0 data_id = None events_id = None generated_on = None channel_number = None channel_name = None channel_icon_source = None programs = [] program_title = None program_description = None program_start = None program_runtime = None try: ijson_parser = ijson.parse(epg_json_stream) for (prefix, event, value) in ijson_parser: if (prefix, event) == ('generated_on', 'string'): generated_on = datetime.fromtimestamp(int(value), pytz.utc) elif (prefix, event) == ('data', 'map_key'): data_id = value elif (prefix, event) == ('data.{0}.events'.format(data_id), 'map_key'): events_id = value elif (prefix, event) == ('data.{0}.number'.format(data_id), 'string'): channel_number = int(value.strip()) elif (prefix, event) == ('data.{0}.name'.format(data_id), 'string'): channel_name = html.unescape(value.strip()) elif (prefix, event) == ('data.{0}.img'.format(data_id), 'string'): channel_icon_source = value.strip() elif (prefix, event) == ( 'data.{0}.events.{1}'.format(data_id, events_id), 'end_map', ): program_stop = program_start + timedelta( minutes=program_runtime) programs.append( XMLTVProgram( provider='SmoothStreams', start=program_start, stop=program_stop, pdc_start=None, vps_start=None, show_view=None, video_plus=None, channel_xmltv_id=None, clump_index=None, titles=[ XMLTVTitle(language=None, text=program_title) ], sub_titles=[], descriptions=[ XMLTVDescription(language=None, text=program_description) ], credits_=None, date=None, categories=[], keywords=[], language=None, original_language=None, length=None, icons=[], urls=[], countries=[], episode_numbers=[], video=None, audio=None, previously_shown=None, premiere=None, last_chance=None, new=None, subtitles=[], ratings=[], star_ratings=[], reviews=[], )) program_title = None program_description = None program_start = None program_runtime = None elif (prefix, event) == ( 'data.{0}.events.{1}.description'.format( data_id, events_id), 'string', ): program_description = html.unescape(value) elif (prefix, event) == ( 'data.{0}.events.{1}.name'.format(data_id, events_id), 'string', ): program_title = html.unescape(value) elif (prefix, event) == ( 'data.{0}.events.{1}.runtime'.format( data_id, events_id), 'number', ): program_runtime = value elif (prefix, event) == ( 'data.{0}.events.{1}.runtime'.format( data_id, events_id), 'string', ): program_runtime = int(value) elif (prefix, event) == ( 'data.{0}.events.{1}.time'.format(data_id, events_id), 'string', ): program_start = datetime.fromtimestamp( int(value), pytz.utc) elif (prefix, event) == ('data.{0}'.format(data_id), 'end_map'): channel = XMLTVChannel( provider='SmoothStreams', m3u8_group='SmoothStreams', xmltv_id=None, number=channel_number, display_names=[ XMLTVDisplayName(language=None, text=channel_name) ], icons=[ XMLTVIcon(source=channel_icon_source, width=None, height=None) ], urls=[], ) cls._apply_channel_transformations( channel, channel_name_map, not do_use_provider_icons) db_session.add( SmoothStreamsChannel( id_=channel.xmltv_id, m3u8_group='SmoothStreams', number=channel.number, name=channel.display_names[0].text, pickle=pickle.dumps( channel, protocol=pickle.HIGHEST_PROTOCOL), complete_xmltv=channel.format(minimal_xmltv=False), minimal_xmltv=channel.format(), )) number_of_objects_added_to_db_session += 1 if (number_of_objects_added_to_db_session and number_of_objects_added_to_db_session % 1000 == 0): db_session.flush() for program in programs: program.channel_xmltv_id = channel.xmltv_id db_session.add( SmoothStreamsProgram( id_='{0}'.format(uuid.uuid4()), start=program.start, stop=program.stop, channel_xmltv_id=channel.xmltv_id, channel_number=channel.number, pickle=pickle.dumps( program, protocol=pickle.HIGHEST_PROTOCOL), complete_xmltv=program.format( minimal_xmltv=False), minimal_xmltv=program.format(), )) number_of_objects_added_to_db_session += 1 if (number_of_objects_added_to_db_session and number_of_objects_added_to_db_session % 1000 == 0): db_session.flush() channel_number = None channel_name = None channel_icon_source = None programs = [] db_session.flush() logger.debug( 'Processed SmoothStreams JSON EPG\n' 'File name => %s\n' 'Generated on => %s', SmoothStreamsConstants.EPG_FILE_NAME, generated_on, ) except Exception: logger.error( 'Failed to process SmoothStreams JSON EPG\nFile name => %s', SmoothStreamsConstants.EPG_FILE_NAME, ) raise
def _parse_fog_channels_json( cls, db_session, channel_name_map, do_use_provider_icons, parsed_channel_xmltv_id_to_channel, ): epg_json_stream = cls._request_fog_channels_json() logger.debug( 'Processing Fog JSON channels\nFile name => %s', SmoothStreamsConstants.FOG_CHANNELS_JSON_FILE_NAME, ) key = None channel_number = None channel_name = None channel_xmltv_id = None channel_icon_source = None try: ijson_parser = ijson.parse(epg_json_stream) for (prefix, event, value) in ijson_parser: if prefix.isdigit() and (event, value) == ('start_map', None): key = prefix channel_number = None channel_name = None channel_xmltv_id = None channel_icon_source = None elif (prefix, event) == ('{0}.channum'.format(key), 'string'): channel_number = int(value.strip()) elif (prefix, event) == ('{0}.channame'.format(key), 'string'): channel_name = html.unescape(value.strip()) elif (prefix, event) == ('{0}.xmltvid'.format(key), 'string'): channel_xmltv_id = html.unescape(value.strip()) elif (prefix, event) == ('{0}.icon'.format(key), 'string'): channel_icon_source = value.strip() elif (prefix, event) == (key, 'end_map'): channel = XMLTVChannel( provider='SmoothStreams', m3u8_group='SmoothStreams', xmltv_id=channel_xmltv_id, number=channel_number, display_names=[ XMLTVDisplayName(language=None, text=channel_name) ], icons=[ XMLTVIcon(source=channel_icon_source, width=None, height=None) ], urls=[], ) cls._apply_channel_transformations( channel, channel_name_map, not do_use_provider_icons) parsed_channel_xmltv_id_to_channel[ channel_xmltv_id] = channel db_session.add( SmoothStreamsChannel( id_=channel.xmltv_id, m3u8_group='SmoothStreams', number=channel.number, name=channel.display_names[0].text, pickle=pickle.dumps( channel, protocol=pickle.HIGHEST_PROTOCOL), complete_xmltv=channel.format(minimal_xmltv=False), minimal_xmltv=channel.format(), )) db_session.flush() logger.debug( 'Processed Fog JSON channels\nFile name => %s', SmoothStreamsConstants.FOG_CHANNELS_JSON_FILE_NAME, ) except Exception: logger.error( 'Failed to process Fog JSON channels\nFile name => %s', SmoothStreamsConstants.FOG_CHANNELS_JSON_FILE_NAME, ) raise
def _parse_channels_json(cls, db_session, categories_map, channel_name_map, do_use_provider_icons, parsed_channel_xmltv_id_to_channel, parsed_channel_number_to_channel): for category_id in categories_map: channels_json_stream = cls._request_epg_json(VaderStreamsConstants.CHANNELS_PATH, VaderStreamsConstants.CHANNELS_JSON_FILE_NAME, dict(category_id=category_id)) logger.debug('Processing VaderStreams JSON channels\n' 'File name => {0}\n' 'Category => {1}'.format(VaderStreamsConstants.CHANNELS_JSON_FILE_NAME, categories_map[category_id])) number_of_objects_added_to_db_session = 0 channel_number = None channel_icon_source = None channel_xmltv_id = None channel_name = None try: ijson_parser = ijson.parse(channels_json_stream) for (prefix, event, value) in ijson_parser: if (prefix, event) == ('item.id', 'number'): channel_number = value elif (prefix, event) == ('item.stream_icon', 'string'): channel_icon_source = html.unescape(value) elif (prefix, event) == ('item.channel_id', 'string'): channel_xmltv_id = html.unescape(value) elif (prefix, event) == ('item.stream_display_name', 'string'): channel_name = html.unescape(value) elif (prefix, event) == ('item', 'end_map'): try: if channel_number not in parsed_channel_number_to_channel: if 'sport' in channel_name.lower(): channel_m3u8_group = 'VaderStreams - Sports' elif 'movie' in channel_name.lower(): channel_m3u8_group = 'VaderStreams - Premium Movies' else: channel_m3u8_group = 'VaderStreams - {0}'.format(categories_map[category_id]) channel = XMLTVChannel(provider='VaderStreams', m3u8_group=channel_m3u8_group, xmltv_id=channel_xmltv_id, number=channel_number, display_names=[XMLTVDisplayName(language=None, text=channel_name)], icons=[XMLTVIcon(source=channel_icon_source, width=None, height=None)], urls=[]) cls._apply_channel_transformations(channel, channel_name_map, not do_use_provider_icons) if channel_xmltv_id in parsed_channel_xmltv_id_to_channel: parsed_channel_xmltv_id_to_channel[channel_xmltv_id].append(channel) else: parsed_channel_xmltv_id_to_channel[channel_xmltv_id] = [channel] parsed_channel_number_to_channel[channel_number] = channel db_session.add(VaderStreamsChannel( id_=channel.xmltv_id, m3u8_group=channel.m3u8_group, number=channel.number, name=channel.display_names[0].text, pickle=pickle.dumps(channel, protocol=pickle.HIGHEST_PROTOCOL), complete_xmltv=channel.format(minimal_xmltv=False), minimal_xmltv=channel.format())) number_of_objects_added_to_db_session += 1 except KeyError: pass finally: channel_number = None channel_icon_source = None channel_xmltv_id = None channel_name = None if number_of_objects_added_to_db_session and number_of_objects_added_to_db_session % 1000 == 0: db_session.flush() db_session.flush() logger.debug('Processed VaderStreams JSON channels\n' 'File name => {0}\n' 'Category => {1}'.format(VaderStreamsConstants.CHANNELS_JSON_FILE_NAME, categories_map[category_id])) except Exception: logger.debug('Failed to process VaderStreams JSON channels\n' 'File name => {0}\n' 'Category => {1}'.format(VaderStreamsConstants.CHANNELS_JSON_FILE_NAME, categories_map[category_id])) raise