def refresh_session(cls, force_refresh=False): with cls._session_lock.writer_lock: do_start_timer = False if force_refresh or cls._do_refresh_session(): do_start_timer = True cls._clear_nimble_session_id_map() session = cls._refresh_session() if session: cls._session = session with SmoothStreamsDatabase.get_write_lock( ), SmoothStreamsDatabase.get_access_lock().shared_lock: db_session = SmoothStreamsDatabase.create_session() try: db_session.merge( SmoothStreamsSetting( 'session', jsonpickle.encode(cls._session))) db_session.commit() except Exception: (type_, value_, traceback_) = sys.exc_info() logger.error('\n'.join( traceback.format_exception( type_, value_, traceback_))) db_session.rollback() finally: db_session.close() if cls._refresh_session_timer: cls._refresh_session_timer.cancel() elif not cls._refresh_session_timer: do_start_timer = True if do_start_timer: interval = (cls._get_session_parameter('expires_on') - datetime.now(pytz.utc)).total_seconds() - 1800 cls._refresh_session_timer = Timer(interval, cls._timed_refresh_session) cls._refresh_session_timer.daemon = True cls._refresh_session_timer.start() logger.debug( 'Started SmoothStreams session refresh timer\n' 'Interval => %s seconds', interval, )
def _initialize(cls, **kwargs): do_refresh_session = False if 'do_refresh_session' in kwargs: do_refresh_session = kwargs['do_refresh_session'] else: with SmoothStreamsDatabase.get_access_lock().shared_lock: db_session = SmoothStreamsDatabase.create_session() try: setting_row = SmoothStreamsDatabaseAccess.query_setting( db_session, 'session') if setting_row is not None: cls._session = jsonpickle.decode(setting_row.value) current_date_time_in_utc = datetime.now(pytz.utc) if current_date_time_in_utc < cls._session[ 'expires_on']: logger.debug( 'Loaded SmoothStreams session\n' 'Authorization token => %s\n' 'Expires on => %s', cls._session['authorization_token'], cls._session['expires_on'].astimezone( tzlocal.get_localzone()).strftime( '%Y-%m-%d %H:%M:%S%z'), ) else: do_refresh_session = True else: do_refresh_session = True finally: db_session.close() if do_refresh_session: cls.refresh_session(force_refresh=True)
def _update_epg(cls, **kwargs): with cls._lock: super()._update_epg() channel_name_map = kwargs['channel_name_map'] do_use_provider_icons = kwargs['do_use_provider_icons'] was_exception_raised = False SmoothStreamsDatabase.initialize_temporary() db_session = SmoothStreamsDatabase.create_temporary_session() try: if (Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_EPG_SOURCE') == SmoothStreamsEPGSource.FOG.value): parsed_channel_xmltv_id_to_channel = {} cls._parse_fog_channels_json( db_session, channel_name_map, do_use_provider_icons, parsed_channel_xmltv_id_to_channel, ) cls._parse_fog_epg_xml(db_session, parsed_channel_xmltv_id_to_channel) elif (Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_EPG_SOURCE') == SmoothStreamsEPGSource.OTHER.value): cls._parse_external_epg_xml( db_session, channel_name_map=channel_name_map, do_use_provider_icons=do_use_provider_icons, ) elif (Configuration.get_configuration_parameter( 'SMOOTHSTREAMS_EPG_SOURCE') == SmoothStreamsEPGSource.PROVIDER.value): cls._parse_smoothstreams_epg_json(db_session, channel_name_map, do_use_provider_icons) db_session.add( SmoothStreamsSetting( 'epg_settings_md5', cls._calculate_epg_settings_md5(**kwargs))) db_session.add( SmoothStreamsSetting( 'last_epg_refresh_date_time_in_utc', datetime.strftime(datetime.now(pytz.utc), '%Y-%m-%d %H:%M:%S%z'), )) db_session.commit() except Exception: was_exception_raised = True db_session.rollback() raise finally: cls._initialize_refresh_epg_timer( do_set_timer_for_retry=was_exception_raised) db_session.close() if not was_exception_raised: try: SmoothStreamsDatabase.migrate() except Exception: cls._initialize_refresh_epg_timer( do_set_timer_for_retry=True) raise