<s> from __future__ import unicode_literals , division , absolute_import <EOL> import logging <EOL> import re <EOL> from flexget import plugin <EOL> from flexget . event import event <EOL> from flexget . utils import json , requests <EOL> from flexget . utils . soup import get_soup <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> session = requests . Session ( ) <EOL> class PluginTheXEM ( object ) : <EOL> def validator ( self ) : <EOL> from flexget import validator <EOL> root = validator . factory ( '<STR_LIT>' ) <EOL> root . accept ( '<STR_LIT>' , key = '<STR_LIT:source>' , required = True ) . accept_choices ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> root . accept ( '<STR_LIT>' , key = '<STR_LIT>' , required = True ) . accept_choices ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> return root <EOL> @ plugin . priority ( <NUM_LIT> ) <EOL> def on_task_metainfo ( self , task , config ) : <EOL> if not config : <EOL> return <EOL> for entry in task . entries : <EOL> if entry . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> if '<STR_LIT>' in entry : <EOL> log . debug ( '<STR_LIT>' ) <EOL> else : <EOL> log . debug ( '<STR_LIT>' ) <EOL> try : <EOL> response = session . get ( '<STR_LIT>' % entry [ '<STR_LIT>' ] ) <EOL> except requests . RequestException as e : <EOL> log . debug ( '<STR_LIT>' % e ) <EOL> continue <EOL> shownames = json . loads ( response . content ) <EOL> for tvdb_id in shownames [ '<STR_LIT:data>' ] . keys ( ) : <EOL> if entry [ '<STR_LIT>' ] in shownames [ '<STR_LIT:data>' ] [ tvdb_id ] : <EOL> entry [ '<STR_LIT>' ] = tvdb_id <EOL> log . debug ( '<STR_LIT>' , entry [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] ) <EOL> break <EOL> if '<STR_LIT>' not in entry : <EOL> log . debug ( '<STR_LIT>' ) <EOL> try : <EOL> response = session . get ( '<STR_LIT>' % entry [ '<STR_LIT>' ] ) <EOL> except requests . RequestException as e : <EOL> log . debug ( '<STR_LIT>' % e ) <EOL> continue <EOL> if response . url . startswith ( '<STR_LIT>' ) : <EOL> soup = get_soup ( response . content ) <EOL> try : <EOL> entry [ '<STR_LIT>' ] = soup . findAll ( '<STR_LIT:a>' , { '<STR_LIT>' : re . compile ( '<STR_LIT>' ) } ) [ <NUM_LIT:0> ] . next <EOL> log . debug ( '<STR_LIT>' , entry [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] ) <EOL> except : <EOL> pass <EOL> if '<STR_LIT>' not in entry : <EOL> log . error ( '<STR_LIT>' , entry [ '<STR_LIT>' ] ) <EOL> try : <EOL> response = session . get ( '<STR_LIT>' % entry [ '<STR_LIT>' ] ) <EOL> except requests . RequestException as e : <EOL> log . debug ( '<STR_LIT>' % e ) <EOL> continue <EOL> episode_map = json . loads ( response . content ) <EOL> if episode_map [ '<STR_LIT:result>' ] == '<STR_LIT:success>' : <EOL> try : <EOL> for episode_entry in episode_map [ '<STR_LIT:data>' ] : <EOL> if config [ '<STR_LIT:source>' ] not in episode_entry : <EOL> continue <EOL> if config [ '<STR_LIT>' ] not in episode_entry : <EOL> continue <EOL> if episode_entry [ config [ '<STR_LIT:source>' ] ] [ '<STR_LIT>' ] == entry [ '<STR_LIT>' ] and episode_entry [ config [ '<STR_LIT:source>' ] ] [ '<STR_LIT>' ] == entry [ '<STR_LIT>' ] : <EOL> log . debug ( '<STR_LIT>' % ( entry [ '<STR_LIT>' ] , config [ '<STR_LIT:source>' ] , entry [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] , config [ '<STR_LIT>' ] , episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] , episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT:description>' in entry : <EOL> entry [ '<STR_LIT:description>' ] = entry [ '<STR_LIT:description>' ] . replace ( '<STR_LIT>' % ( entry [ '<STR_LIT>' ] , entry [ '<STR_LIT>' ] ) , '<STR_LIT>' % ( episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] , episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] ) ) <EOL> entry [ '<STR_LIT>' ] = episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] <EOL> entry [ '<STR_LIT>' ] = episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] <EOL> entry [ '<STR_LIT>' ] = '<STR_LIT>' % ( episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] , episode_entry [ config [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] ) <EOL> break <EOL> except : <EOL> log . error ( '<STR_LIT>' % <EOL> ( entry [ '<STR_LIT:title>' ] , entry [ '<STR_LIT>' ] ) ) <EOL> log . debug ( '<STR_LIT>' % response . content ) <EOL> @ event ( '<STR_LIT>' ) <EOL> def register_plugin ( ) : <EOL> plugin . register ( PluginTheXEM , '<STR_LIT>' , api_ver = <NUM_LIT:2> ) </s>
