Advertisement
Guest User

FCM69 - TVRage partie 1

a guest
Mar 27th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. """
  2. Filename        : tvragetest.py
  3. Author          : G.D. Walters
  4. Purpose         : Cherche sur TVRage.com des informations sur des emissions TV
  5. """
  6. #=============================================================
  7. #                        IMPORTS
  8. #=============================================================
  9. from xml.etree import ElementTree as ET
  10. import urllib
  11. import sys
  12.  
  13. #=============================================================
  14. #                   Classe principale
  15. #=============================================================
  16.  
  17. class TvRage:
  18.     def __init__(self):
  19.         self.CleAPI = "Itnl8IyY1hsR9n0IP6zI"
  20.         self.ChaineRechercheSerie = "http://services.tvrage.com/myfeeds/search.php?key="
  21.         self.ChaineInformationEmission = "http://services.tvrage.com/myfeeds/showinfo.php?key="
  22.         self.ChaineListeEpisodes = "http://services.tvrage.com/myfeeds/episode_list.php?key="
  23.         self.ListeEmissions = []
  24.         self.InfosEmissions = []
  25.         self.ListeEpisodes = []
  26.         self.ElementEpisode = []
  27.  
  28.  
  29.     def TrouverIdParNom(self,nomEmission,debug = 0):
  30.         chaine = self.ChaineRechercheSerie + self.CleAPI + "&show=" + nomEmission
  31.         urllib.socket.setdefaulttimeout(8)
  32.         usock = urllib.urlopen(chaine)
  33.         resultat = ET.parse(usock).getroot()
  34.         usock.close()
  35.         compteurTrouves = 0
  36.         self.listeEmissions = []
  37.         for noeud in resultat.findall('show'):
  38.             infosEmissions = []
  39.             chaineGenre = None
  40.             dict = {}
  41.             for n in noeud:
  42.                 if n.tag == 'showid':
  43.                     showid = n.text
  44.                     dict['ID'] = showid
  45.                 elif n.tag == 'name':
  46.                     nomEmission = n.text
  47.                     dict['Nom'] = nomEmission
  48.                 elif n.tag == 'link':
  49.                     showlink = n.text
  50.                     dict['Lien'] = showlink
  51.                 elif n.tag == 'country':
  52.                     showcountry = n.text
  53.                     dict['Pays'] = showcountry
  54.                 elif n.tag == 'started':
  55.                     showstarted = n.text
  56.                     dict['Debut'] = showstarted
  57.                 elif n.tag == 'ended':
  58.                     showended = n.text
  59.                     dict['Fin'] = showended
  60.                 elif n.tag == 'seasons':
  61.                     showseasons = n.text
  62.                     dict['Saisons'] = showseasons
  63.                 elif n.tag == 'status':
  64.                     showstatus = n.text
  65.                     dict['Etat'] = showstatus
  66.                 elif n.tag == 'classification':
  67.                     showclassification = n.text
  68.                     dict['Classification'] = showclassification
  69.                 elif n.tag == 'genres':
  70.                     for sousElement in n:
  71.                         if sousElement.tag == 'genre':
  72.                             if sousElement.text != None:
  73.                                 if chaineGenre == None:
  74.                                     chaineGenre = sousElement.text
  75.                                 else:
  76.                                     chaineGenre += " | " + sousElement.text
  77.                     dict['Genres'] = chaineGenre
  78.             compteurTrouves += 1
  79.             self.listeEmissions.append(dict)
  80.         return self.listeEmissions
  81. #========================================================
  82.  
  83.     def AfficheResultatsEmission(self, ListeEmissionsDict):
  84.         tailleListe = len(ListeEmissionsDict)
  85.         print "%d resultat(s)" % tailleListe
  86.         print "------------------------"
  87.         compteur = 1
  88.         for c in ListeEmissionsDict:
  89.             print "%d - %s - %s" % (compteur,c['Nom'],c['ID']) #,c['Fin'],c['Lien']
  90.             compteur += 1
  91.         sel = raw_input("Choisir un nombre ou 0 pour quitter -> ")
  92.         return sel
  93.  
  94. def main():
  95.     tr = TvRage()
  96.     #--------------------
  97.     # Chercher une serie par son nom
  98.     #--------------------
  99.     nom = raw_input("Entrer le nom de la serie -> ")
  100.     if nom != None:
  101.         liste = tr.TrouverIdParNom(nom)
  102.         choix = tr.AfficheResultatsEmission(liste)
  103.     print "choix %d" % int(choix)
  104.  
  105.         if int(choix) == 0:
  106.             sys.exit()
  107.         else:
  108.             option = int(choix)-1
  109.             id = liste[option]['ID']
  110.             print "Le ShowID choisi est %s" % id
  111.  
  112. #===========================================================
  113. #       Boucle principale
  114. #===========================================================
  115. if __name__ == "__main__":
  116.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement