Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Filename : tvragetest.py
- Author : G.D. Walters
- Purpose : Cherche sur TVRage.com des informations sur des emissions TV
- """
- #=============================================================
- # IMPORTS
- #=============================================================
- from xml.etree import ElementTree as ET
- import urllib
- import sys
- #=============================================================
- # Classe principale
- #=============================================================
- class TvRage:
- def __init__(self):
- self.CleAPI = "Itnl8IyY1hsR9n0IP6zI"
- self.ChaineRechercheSerie = "http://services.tvrage.com/myfeeds/search.php?key="
- self.ChaineInformationEmission = "http://services.tvrage.com/myfeeds/showinfo.php?key="
- self.ChaineListeEpisodes = "http://services.tvrage.com/myfeeds/episode_list.php?key="
- self.ListeEmissions = []
- self.InfosEmissions = []
- self.ListeEpisodes = []
- self.ElementEpisode = []
- def TrouverIdParNom(self,nomEmission,debug = 0):
- chaine = self.ChaineRechercheSerie + self.CleAPI + "&show=" + nomEmission
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(chaine)
- resultat = ET.parse(usock).getroot()
- usock.close()
- compteurTrouves = 0
- self.listeEmissions = []
- for noeud in resultat.findall('show'):
- infosEmissions = []
- chaineGenre = None
- dict = {}
- for n in noeud:
- if n.tag == 'showid':
- idemission = n.text
- dict['ID'] = idemission
- elif n.tag == 'name':
- nomEmission = n.text
- dict['Nom'] = nomEmission
- elif n.tag == 'link':
- showlink = n.text
- dict['Lien'] = showlink
- elif n.tag == 'country':
- showcountry = n.text
- dict['Pays'] = showcountry
- elif n.tag == 'started':
- showstarted = n.text
- dict['Debut'] = showstarted
- elif n.tag == 'ended':
- showended = n.text
- dict['Fin'] = showended
- elif n.tag == 'seasons':
- showseasons = n.text
- dict['Saisons'] = showseasons
- elif n.tag == 'status':
- showstatus = n.text
- dict['Etat'] = showstatus
- elif n.tag == 'classification':
- showclassification = n.text
- dict['Classification'] = showclassification
- elif n.tag == 'genres':
- for sousElement in n:
- if sousElement.tag == 'genre':
- if sousElement.text != None:
- if chaineGenre == None:
- chaineGenre = sousElement.text
- else:
- chaineGenre += " | " + sousElement.text
- dict['Genres'] = chaineGenre
- compteurTrouves += 1
- self.listeEmissions.append(dict)
- return self.listeEmissions
- #========================================================
- def AfficheResultatsEmission(self, ListeEmissionsDict):
- tailleListe = len(ListeEmissionsDict)
- print "%d resultat(s)" % tailleListe
- print "------------------------"
- compteur = 1
- for c in ListeEmissionsDict:
- print "%d - %s - %s" % (compteur,c['Nom'],c['ID']) #,c['Fin'],c['Lien']
- compteur += 1
- sel = raw_input("Choisir un nombre ou 0 pour quitter -> ")
- return sel
- #========================================================
- """
- TrouveInfoEmission(idemission,debug)
- Balises autorisees :
- ID,Name,Link,Country,Seasons,Image,Started,StartDate,Ended,
- Status,Classification,Summary,Genres,Runtime,NetworkCountry,
- Network,Airtime,Airday,Timezone
- """
- def TrouveInfoEmission(self,idemission,debug=0):
- idemissionchaine = str(idemission)
- chaine = self.ChaineInformationEmission + self.CleAPI + "&sid=" + idemissionchaine
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(chaine)
- arbre = ET.parse(usock).getroot()
- usock.close()
- dict = {}
- for enfant in arbre:
- if enfant.tag == 'showid':
- dict['ID'] = enfant.text
- elif enfant.tag == 'showname':
- dict['Nom'] = enfant.text
- elif enfant.tag == 'showlink':
- dict['Lien'] = enfant.text
- elif enfant.tag == 'origin_country':
- dict['Pays'] = enfant.text
- elif enfant.tag == 'seasons':
- dict['Saisons'] = enfant.text
- elif enfant.tag == 'image':
- dict['Image'] = enfant.text
- elif enfant.tag == 'started':
- dict['Debut'] = enfant.text
- elif enfant.tag == 'startdate':
- dict['DateDebut'] = enfant.text
- elif enfant.tag == 'ended':
- dict['Fin'] = enfant.text
- elif enfant.tag == 'status':
- dict['Etat'] = enfant.text
- elif enfant.tag == 'classification':
- dict['Classification'] = enfant.text
- elif enfant.tag == 'summary':
- dict['Resume'] = enfant.text
- elif enfant.tag == 'genres':
- chainegenre = None
- for souselement in enfant:
- if souselement.tag == 'genre':
- if souselement.text != None:
- if chainegenre == None:
- chainegenre = souselement.text
- else:
- chainegenre += " | " + souselement.text
- dict['Genres'] = chainegenre
- elif enfant.tag == 'runtime':
- dict['Diffusion'] = enfant.text
- elif enfant.tag == 'network': # a un attribut
- #print enfant.attrib['country'],enfant.text
- dict['PaysDiffusion'] = enfant.attrib['country']
- dict['Chaine'] = enfant.text
- elif enfant.tag == 'airtime':
- dict['HeureDiffusion'] = enfant.text
- elif enfant.tag == 'airday':
- dict['JourDiffusion'] = enfant.text
- elif enfant.tag == 'timezone':
- dict['FuseauHoraire'] = enfant.text
- return dict
- def AfficheInfoEmission(self,dict):
- print "Emission : %s" % dict['Nom']
- print "ID : %s Debut : %s Fin : %s Date debut : %s Saisons : %s" % (dict['ID'],dict['Debut'],dict['Fin'],dict['DateDebut'],dict['Saisons'])
- print "Lien : %s" % dict['Lien']
- print "Image : %s" % dict['Image']
- print "Pays : %s Etat : %s Classification : %s" % (dict['Pays'],dict['Etat'],dict['Classification'])
- print "Diffusion : %s Chaine : %s Jour diffusion : %s Heure diffusion : %s" % (dict['Diffusion'],dict['Chaine'],dict['JourDiffusion'],dict['HeureDiffusion'])
- print "Genres : %s" % dict['Genres']
- print "Resume : \n%s" % dict['Resume']
- def TrouveListeEpisodes(self,idemission,debug=0):
- idemissionchaine = str(idemission)
- chaine = self.ChaineListeEpisodes + self.CleAPI + "&sid=" + idemissionchaine
- urllib.socket.setdefaulttimeout(8)
- usock = urllib.urlopen(chaine)
- arbre = ET.parse(usock).getroot()
- usock.close()
- NomEmission = ""
- SaisonsTotal = ""
- for enfant in arbre:
- if enfant.tag == 'name':
- NomEmission = enfant.text
- elif enfant.tag == 'totalseasons':
- SaisonsTotal = enfant.text
- elif enfant.tag == 'Episodelist':
- for c in enfant:
- if c.tag == 'Season':
- dict = {}
- numsaison = c.attrib['no']
- for el in c:
- if el.tag == 'episode':
- dict={}
- dict['Saison'] = numsaison
- for ep in el:
- if ep.tag == 'epnum':
- dict['NumeroEpisode'] = ep.text
- elif ep.tag == 'seasonnum':
- dict['NumeroEpisodeSaison'] = ep.text
- elif ep.tag == 'prodnum':
- dict['ProductionNumber'] = ep.text
- elif ep.tag == 'airdate':
- dict['DateDiffusion'] = ep.text
- elif ep.tag == 'link':
- dict['Lien'] = ep.text
- elif ep.tag == 'title':
- dict['Titre'] = ep.text
- elif ep.tag == 'summary':
- dict['Resume'] = ep.text
- elif ep.tag == 'rating':
- dict['Notation'] = ep.text
- elif ep.tag == 'screencap':
- dict['CaptureEcran'] = ep.text
- self.ElementEpisode.append(dict)
- return NomEmission,SaisonsTotal,self.ElementEpisode
- #========================================================
- #========================================================
- def AfficheListeEpisodes(self,NomSerie,NombreSaisons,ListeEpisodes):
- print "--------------------------------------"
- print "Nom de la serie : %s" % NomSerie
- print "Nombre total de saisons : %s" % NombreSaisons
- print "Nombre total episodes : %d" % len(ListeEpisodes)
- print "--------------------------------------"
- for e in ListeEpisodes:
- print "Saison : %s" % e['Saison']
- print " Numero Episode Saison : %s - Numero Episode Serie : %s" % (e['NumeroEpisodeSaison'],e['NumeroEpisode'])
- print " Titre: %s" % e['Titre']
- if e.has_key('Notation'):
- print " Date Diffusion : %s Notation : %s" % (e['DateDiffusion'],e['Notation'])
- else:
- print " Date Diffusion : %s Notation : NONE" % e['DateDiffusion']
- if e.has_key('Resume'):
- print " Resume : \n%s" % e['Resume']
- else:
- print " Resume : NA"
- print "==========================="
- print "------------ Fin de liste episodes ------------"
- def main():
- tr = TvRage()
- #--------------------
- # Chercher une serie par son nom
- #--------------------
- nom = raw_input("Entrer le nom de la serie -> ")
- if nom != None:
- liste = tr.TrouverIdParNom(nom)
- choix = tr.AfficheResultatsEmission(liste)
- if int(choix) == 0:
- sys.exit()
- else:
- option = int(choix)-1
- id = liste[option]['ID']
- print "Le ShowID choisi est %s" % id
- #------------------------------
- # Recupere les informations
- #------------------------------
- infoemission = tr.TrouveInfoEmission(id)
- #------------------------------
- # Affiche les informations
- #------------------------------
- tr.AfficheInfoEmission(infoemission)
- #-----------------------------
- # recupere la liste des episodes
- #-----------------------------
- NomSerie,SaisonsTotal,listeepisodes = tr.TrouveListeEpisodes(id)
- #-----------------------------
- # Affiche la liste des episodes
- #-----------------------------
- tr.AfficheListeEpisodes(NomSerie,SaisonsTotal,listeepisodes)
- #-----------------------------
- #===========================================================
- # Boucle principale
- #===========================================================
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement