<s> import urllib2 <EOL> import zipfile <EOL> import StringIO <EOL> import random <EOL> from datetime import datetime <EOL> from xml . etree . ElementTree import parse , Element , SubElement <EOL> import metadata <EOL> MIRROR_URL = '<STR_LIT>' <EOL> SERVER_TIME_URL = '<STR_LIT>' <EOL> SERIES_LOOKUP_URL = '<STR_LIT>' <EOL> SERIES_URL = '<STR_LIT>' <EOL> EPISODE_URL = '<STR_LIT>' <EOL> EPISODE_HISTORY_URL = '<STR_LIT>' <EOL> EPISODE_LOOKUP_BY_DATE_URL = '<STR_LIT>' <EOL> API_KEY = "<STR_LIT>" <EOL> class TheTvDb : <EOL> def __init__ ( self , config , debug ) : <EOL> self . debug = debug <EOL> self . active_mirror = None <EOL> def find_mirror ( self ) : <EOL> if self . active_mirror is None : <EOL> full_url = MIRROR_URL % ( API_KEY , ) <EOL> mirrors_xml = parse ( urllib2 . urlopen ( full_url ) ) <EOL> mirrors = mirrors_xml . findall ( '<STR_LIT>' ) <EOL> random . seed ( ) <EOL> mirror_to_get = random . randrange ( <NUM_LIT:0> , len ( mirrors ) , <NUM_LIT:1> ) <EOL> mirror = mirrors [ mirror_to_get ] <EOL> self . active_mirror = mirror . findtext ( '<STR_LIT>' ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % ( self . active_mirror , ) <EOL> def get_server_time ( self ) : <EOL> self . find_mirror ( ) <EOL> full_url = self . active_mirror + SERVER_TIME_URL % ( API_KEY , ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> series_xml = parse ( urllib2 . urlopen ( full_url ) ) <EOL> data_element = series_xml . getroot ( ) <EOL> to_return = int ( data_element . attrib [ '<STR_LIT:time>' ] ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % to_return <EOL> return to_return <EOL> def get_series_info ( self , series_id ) : <EOL> self . find_mirror ( ) <EOL> full_url = self . active_mirror + SERIES_URL % ( API_KEY , series_id ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> full_series = [ ] <EOL> try : <EOL> full_series_xml = parse ( urllib2 . urlopen ( full_url ) ) <EOL> for full_s in full_series_xml . findall ( '<STR_LIT>' ) : <EOL> full_series . append ( full_s ) <EOL> except urllib2 . HTTPError : <EOL> raise Exception ( '<STR_LIT>' % ( series_id , ) ) <EOL> if len ( full_series ) > <NUM_LIT:0> : <EOL> if self . debug : <EOL> print "<STR_LIT>" % len ( full_series ) <EOL> s_obj = self . parse_series_xml ( full_series [ <NUM_LIT:0> ] ) <EOL> return s_obj <EOL> return None <EOL> def lookup_series_info ( self , name ) : <EOL> self . find_mirror ( ) <EOL> full_url = self . active_mirror + SERIES_LOOKUP_URL % ( name . strip ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) , ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> series_xml = parse ( urllib2 . urlopen ( full_url ) ) <EOL> series = [ Series for Series in series_xml . findall ( '<STR_LIT>' ) ] <EOL> full_series = [ ] <EOL> for s in series : <EOL> id = self . get_int ( s , '<STR_LIT:id>' , <NUM_LIT:0> ) <EOL> full_url = self . active_mirror + SERIES_URL % ( API_KEY , id ) <EOL> full_series_xml = parse ( urllib2 . urlopen ( full_url ) ) <EOL> for full_s in full_series_xml . findall ( '<STR_LIT>' ) : <EOL> full_series . append ( full_s ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % len ( full_series ) <EOL> to_return = [ ] <EOL> for s in full_series : <EOL> s_obj = self . parse_series_xml ( s ) <EOL> to_return . append ( s_obj ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % s_obj . title <EOL> return to_return <EOL> def get_full_episode_list ( self , series ) : <EOL> self . find_mirror ( ) <EOL> full_url = self . active_mirror + EPISODE_HISTORY_URL % ( API_KEY , series . id ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> zip_stream = urllib2 . urlopen ( full_url ) <EOL> zip_file_string = self . create_string_from_stream ( zip_stream ) <EOL> local_stream = StringIO . StringIO ( zip_file_string ) <EOL> zip_file = zipfile . ZipFile ( local_stream ) <EOL> episode_xml = zip_file . read ( '<STR_LIT>' ) <EOL> to_return = self . parse_episode_xml ( episode_xml , series ) <EOL> return to_return <EOL> def get_specific_episode ( self , series , season_number , episode_number ) : <EOL> self . find_mirror ( ) <EOL> full_url = self . active_mirror + EPISODE_URL % ( API_KEY , series . id , season_number , episode_number ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> try : <EOL> xml_stream = urllib2 . urlopen ( full_url ) <EOL> xml_file_string = self . create_string_from_stream ( xml_stream ) <EOL> to_return = self . parse_episode_xml ( xml_file_string , series ) <EOL> return to_return [ <NUM_LIT:0> ] <EOL> except urllib2 . HTTPError : <EOL> return None <EOL> def get_specific_episode_by_date ( self , series , year , month , day ) : <EOL> self . find_mirror ( ) <EOL> formatted_date = '<STR_LIT>' % ( year , month , day ) <EOL> full_url = self . active_mirror + EPISODE_LOOKUP_BY_DATE_URL % ( API_KEY , series . id , formatted_date ) <EOL> if self . debug : <EOL> print "<STR_LIT>" % full_url <EOL> try : <EOL> xml_stream = urllib2 . urlopen ( full_url ) <EOL> xml_file_string = self . create_string_from_stream ( xml_stream ) <EOL> to_return = self . parse_episode_xml ( xml_file_string , series ) <EOL> return to_return [ <NUM_LIT:0> ] <EOL> except urllib2 . HTTPError : <EOL> return None <EOL> def create_string_from_stream ( self , stream ) : <EOL> to_return = '<STR_LIT>' <EOL> try : <EOL> while True : <EOL> to_return = to_return + stream . next ( ) <EOL> except : <EOL> to_return = to_return <EOL> return to_return <EOL> def parse_series_xml ( self , series_xml ) : <EOL> to_return = metadata . Series ( ) <EOL> to_return . id = self . get_int ( series_xml , '<STR_LIT:id>' , - <NUM_LIT:1> ) <EOL> to_return . zap2it_id = self . get_string ( series_xml , '<STR_LIT>' , '<STR_LIT>' ) <EOL> to_return . imdb_id = self . get_string ( series_xml , '<STR_LIT>' , '<STR_LIT>' ) <EOL> to_return . title = self . get_string ( series_xml , '<STR_LIT>' , '<STR_LIT>' ) <EOL> to_return . description = self . get_string ( series_xml , '<STR_LIT>' , '<STR_LIT>' ) <EOL> to_return . actors = self . get_string_list ( series_xml , '<STR_LIT>' , '<STR_LIT:|>' , [ ] ) <EOL> to_return . genres = self . get_string_list ( series_xml , '<STR_LIT>' , '<STR_LIT:|>' , [ ] ) <EOL> to_return . content_rating = self . get_string ( series_xml , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return to_return <EOL> def parse_episode_xml ( self , xml_string , series ) : <EOL> xml_stream = StringIO . StringIO ( xml_string ) <EOL> xml_object = parse ( xml_stream ) <EOL> episodes = [ Episode for Episode in xml_object . findall ( '<STR_LIT>' ) ] <EOL> to_return = [ ] <EOL> for episode in episodes : <EOL> episode_metadata = metadata . Episode ( ) <EOL> to_return . append ( episode_metadata ) <EOL> episode_metadata . series = series ; <EOL> episode_metadata . title = self . get_string ( episode , '<STR_LIT>' , '<STR_LIT>' ) <EOL> episode_metadata . description = self . get_string ( episode , '<STR_LIT>' , '<STR_LIT>' ) <EOL> episode_metadata . rating = self . get_float ( episode , '<STR_LIT>' , <NUM_LIT:0.0> ) <EOL> episode_metadata . season_number = self . get_int ( episode , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> episode_metadata . episode_number = self . get_int ( episode , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> episode_metadata . director = self . get_string ( episode , '<STR_LIT>' , '<STR_LIT>' ) <EOL> episode_metadata . host = self . get_string ( episode , '<STR_LIT>' , '<STR_LIT>' ) <EOL> episode_metadata . writers = self . get_string_list ( episode , '<STR_LIT>' , '<STR_LIT:U+002C>' , [ ] ) <EOL> episode_metadata . guest_stars = self . get_string_list ( episode , '<STR_LIT>' , '<STR_LIT:|>' , [ ] ) <EOL> episode_metadata . original_air_date = self . get_datetime ( episode , '<STR_LIT>' , <EOL> None ) <EOL> episode_metadata . time = datetime . now ( ) <EOL> return to_return <EOL> def get_string ( self , xml_element , sub_element_name , default ) : <EOL> try : <EOL> return xml_element . findtext ( sub_element_name ) <EOL> except : <EOL> return default <EOL> def get_string_list ( self , xml_element , sub_element_name , separator , default ) : <EOL> try : <EOL> text = xml_element . findtext ( sub_element_name ) <EOL> return [ t for t in text . split ( separator ) ] <EOL> except : <EOL> return [ t for t in default ] <EOL> def get_int ( self , xml_element , sub_element_name , default ) : <EOL> try : <EOL> return int ( xml_element . findtext ( sub_element_name ) ) <EOL> except : <EOL> return default <EOL> def get_float ( self , xml_element , sub_element_name , default ) : <EOL> try : <EOL> return float ( xml_element . findtext ( sub_element_name ) ) <EOL> except : <EOL> return default <EOL> def get_datetime ( self , xml_element , sub_element_name , default ) : <EOL> try : <EOL> date_text = xml_element . findtext ( sub_element_name ) <EOL> return datetime . strptime ( date_text , "<STR_LIT>" ) <EOL> except ValueError : <EOL> if self . debug : <EOL> print "<STR_LIT>" % date_text , <EOL> return default <EOL> except : <EOL> return default </s>
