issue69:programmer_en_python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue69:programmer_en_python [2013/03/27 21:19] – [6] fredphil91 | issue69:programmer_en_python [2013/04/03 19:52] (Version actuelle) – [12] fredphil91 | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
3 - Get episode specific information based on ShowID** | 3 - Get episode specific information based on ShowID** | ||
- | La dernière fois, nous avons eu une longue discussion à propos de l'API web TVRAGE. Cette fois-ci, nous allons commencer à écrire du code et à s'en servir. | + | La dernière fois, nous avons eu une longue discussion à propos de l'API web TVRAGE. Cette fois-ci, nous allons commencer à écrire du code et à nous en servir. |
- | Le but de cette partie est de commencer le processus de création de code qui sera un module réutilisable pouvant être importé dans un autre programme python et donnera accès à l'API facilement. | + | Le but de cette partie est de commencer le processus de création de code qui sera un module réutilisable pouvant être importé dans un autre programme python et qui donnera accès à l'API facilement. |
Bien que l'API TVRAGE nous fournisse un certain nombre de possibilités, | Bien que l'API TVRAGE nous fournisse un certain nombre de possibilités, | ||
- | 1 - Rechercher une émission par son nom, et obtenir le ShowID | + | 1. Rechercher une émission par son nom et obtenir le ShowID. |
- | 2 - Obtenir de l' | + | 2. Obtenir de l' |
- | 3 - Obtenir | + | 3. Obtenir |
====== 2 ====== | ====== 2 ====== | ||
Ligne 24: | Ligne 24: | ||
We will create three main routines to make the calls and return the information, | We will create three main routines to make the calls and return the information, | ||
- | La dernière fois, je vous ai montré les appels de l'API « non enregistrée » qui sont accessibles par tout le monde. Cette fois, nous allons utiliser les appels enregistrés - basés sur une clé d' | + | La dernière fois, je vous ai montré les appels de l'API « non enregistrée » qui sont accessibles par tout le monde. Cette fois, nous allons utiliser les appels enregistrés - basés sur une clé d' |
- | Nous allons créer trois programmes principaux pour faire les appels et retourner l' | + | Nous allons créer trois programmes principaux pour faire les appels et retourner l' |
====== 3 ====== | ====== 3 ====== | ||
Ligne 47: | Ligne 47: | ||
- | Voici la liste des routines que nous allons créer (enfin pas toutes pour cette fois. Je veux laisser la place à d' | + | Voici la liste des routines que nous allons créer (enfin pas toutes pour cette fois-ci. Je veux laisser la place à d' |
def TrouverIdParNom(self, | def TrouverIdParNom(self, | ||
Ligne 74: | Ligne 74: | ||
- | La routine TrouverIdParNom prend une chaîne (nomEmission), | + | La routine TrouverIdParNom prend une chaîne (nomEmission), |
- | Nous utiliserons une série de chaînes pour contenir la clé et l'URL de base, puis leur ajouter ce dont nous avons besoin. Par exemple, considérons le code suivant (nous compléterons | + | Nous utiliserons une série de chaînes pour contenir la clé et l'URL de base, puis leur ajouter ce dont nous avons besoin. Par exemple, considérons le code suivant (nous le compléterons plus tard). |
self.CleApi = " | self.CleApi = " | ||
Ligne 98: | Ligne 98: | ||
http:// | http:// | ||
- | Nous combinons la chaîne comme ceci... | + | Nous combinons la chaîne comme ceci : |
chaine = self.ChaineRechercheSerie + self.CleAPI + "& | chaine = self.ChaineRechercheSerie + self.CleAPI + "& | ||
- | Pour les besoins des tests, je vais utiliser une série intitulée « Continuum » qui, si vous ne l'avez jamais vue, est une série géniale de science fiction sur la chaîne canadienne Showcase. J' | + | Pour les besoins des tests, je vais utiliser une série intitulée « Continuum » qui, si vous ne l'avez jamais vue, est une série géniale de science-fiction sur la chaîne canadienne Showcase. J' |
====== 6 ====== | ====== 6 ====== | ||
Ligne 117: | Ligne 117: | ||
http:// | http:// | ||
- | Vous devriez avoir une idée de ce que vous rechercherez dans vos routines d' | + | Vous devriez avoir une idée de ce que vous rechercherez dans vos routines d' |
- | Rechercher en utilisant un nom d' | + | Rechercher en utilisant un nom d' |
http:// | http:// | ||
- | Récupérer des informations sur la série avec le ShowID (sid) | + | Récupérer des informations sur la série avec le ShowID (sid) : |
http:// | http:// | ||
- | Récupérer la liste des épisodes et leurs informations avec le ShowID (sid) | + | Récupérer la liste des épisodes et leurs informations avec le ShowID (sid) : |
http:// | http:// | ||
====== 7 ====== | ====== 7 ====== | ||
- | Now that we have all that out of the way, let’s get started with our code. | + | **Now that we have all that out of the way, let’s get started with our code. |
You’ll create a file with the name of “tvrage.py”. We’ll be using this for the next issue or two. | You’ll create a file with the name of “tvrage.py”. We’ll be using this for the next issue or two. | ||
Ligne 136: | Ligne 136: | ||
We’ll start with our imports shown above right. | We’ll start with our imports shown above right. | ||
- | You can see that we will be using ElementTree to do the XML parsing, and urllib for the internet communication. The sys library is used for sys.exit. | + | You can see that we will be using ElementTree to do the XML parsing, and urllib for the internet communication. The sys library is used for sys.exit.** |
+ | |||
+ | Maintenant que nous avons vu tout cela, nous allons commencer à écrire le code. | ||
+ | |||
+ | Vous allez créer un fichier nommé « tvrage.py ». Nous allons nous en servir pendant un ou deux articles. | ||
+ | |||
+ | Nous allons commencer avec nos importations indiquées en haut à droite. | ||
+ | |||
+ | Vous pouvez voir que nous allons utiliser ElementTree pour faire l' | ||
====== 8 ====== | ====== 8 ====== | ||
- | We’ll set up the main loop now so we can test things as we go (bottom right). | + | **We’ll set up the main loop now so we can test things as we go (bottom right). |
As I said earlier, the first four lines are our partial strings to build the URL for the function that we want to use. (GetEpisodeListString should all be on one line.) The last four lines are the initialization of the lists we will be using later. | As I said earlier, the first four lines are our partial strings to build the URL for the function that we want to use. (GetEpisodeListString should all be on one line.) The last four lines are the initialization of the lists we will be using later. | ||
- | First (middle right), we set up the string that will be used as the URL. Next, we set up the socket with an 8 second default timeout. Then we call urllib.urlopen with our generated URL and (hopefully) receive our xml file in the usock object. We call ElementTree setup so we can parse the xml information. (If you are lost here, please re-read my articles on XML (parts 10, 11 and 12 appearing in FCM #36, 37 and 38)). Next, we close the socket, and initialize the counter for the number of matches found, and reset the list ‘showlist’ to an empty list. | + | First (middle right), we set up the string that will be used as the URL. Next, we set up the socket with an 8 second default timeout. Then we call urllib.urlopen with our generated URL and (hopefully) receive our xml file in the usock object. We call ElementTree setup so we can parse the xml information. (If you are lost here, please re-read my articles on XML (parts 10, 11 and 12 appearing in FCM #36, 37 and 38)). Next, we close the socket, and initialize the counter for the number of matches found, and reset the list ‘showlist’ to an empty list.** |
+ | |||
+ | Nous allons mettre en place la boucle principale maintenant afin de pouvoir tester les choses au fur et à mesure (en bas à droite). Rappelez-vous que ceci doit être tout à la fin de notre fichier source. | ||
+ | |||
+ | Comme je l'ai dit plus tôt, les quatre premières lignes sont nos chaînes partielles pour construire l'URL de la fonction que nous voulons utiliser. (ChaineListeEpisodes doit être sur une seule ligne.) Les quatre dernières lignes sont l' | ||
+ | |||
+ | Tout d' | ||
====== 9 ====== | ====== 9 ====== | ||
- | Now we will step through the xml information using the tag ‘show’ as the parent for what we want. Remember the returned information looks something like that shown top right. | + | **Now we will step through the xml information using the tag ‘show’ as the parent for what we want. Remember the returned information looks something like that shown top right. |
We will be going through each group of information for the parent ‘show’ and parsing out the information. In practice, all we really need is the show name (< | We will be going through each group of information for the parent ‘show’ and parsing out the information. In practice, all we really need is the show name (< | ||
- | I’ll discuss the first one and you’ll understand the rest. As we go through the information, | + | I’ll discuss the first one and you’ll understand the rest. As we go through the information, |
+ | |||
+ | Maintenant, nous allons passer en revue les informations XML en utilisant la balise « show » comme parent de ce que nous voulons. Rappelez-vous que les informations retournées ressemblent à ce qui est en haut à droite. | ||
+ | |||
+ | Nous allons parcourir chaque groupe d' | ||
+ | |||
+ | Je vais expliquer le premier et vous comprendrez le reste. Lorsque nous parcourons les informations, | ||
====== 10 ====== | ====== 10 ====== | ||
- | The next portion (next page, top right) deals with the genre(s) of the show. As you can see from the above XML snippet, this show has four different genres that it fits into. Action, Crime, Drama, and Sci-Fi. We need to handle each. | + | **The next portion (next page, top right) deals with the genre(s) of the show. As you can see from the above XML snippet, this show has four different genres that it fits into. Action, Crime, Drama, and Sci-Fi. We need to handle each. |
- | Finally, we increment the foundcounter variable, and append this dictionary into the list ‘showlist’. Then we start the entire thing over until there is no more XML data. Once everything is done, we return the list of dictionaries (bottom right). | + | Finally, we increment the foundcounter variable, and append this dictionary into the list ‘showlist’. Then we start the entire thing over until there is no more XML data. Once everything is done, we return the list of dictionaries (bottom right).** |
+ | |||
+ | La partie suivante (page suivante, en haut à droite) porte sur le(s) genre(s) de la série. Comme vous pouvez le voir dans l' | ||
+ | |||
+ | Enfin, on incrémente la variable compteurTrouves et on ajoute ce dictionnaire dans la liste « listeEmissions ». Ensuite, nous recommençons le processus jusqu' | ||
====== 11 ====== | ====== 11 ====== | ||
- | Most of the code is pretty self explanatory. We’ll concentrate on the for loop we use to print out the information. We loop through each item in the list of dictionaries and print a counter variable, the show name (c[‘Name’]), | + | **Most of the code is pretty self explanatory. We’ll concentrate on the for loop we use to print out the information. We loop through each item in the list of dictionaries and print a counter variable, the show name (c[‘Name’]), |
Enter Series Name -> continuum | Enter Series Name -> continuum | ||
Ligne 169: | Ligne 193: | ||
1 - Continuum - 30789 | 1 - Continuum - 30789 | ||
2 - Continuum (Web series) - 32083 | 2 - Continuum (Web series) - 32083 | ||
- | Enter Selection or 0 to exit -> | + | Enter Selection or 0 to exit ->** |
+ | |||
+ | La plupart du code est assez explicite. Nous allons nous concentrer sur la boucle « for » que nous utilisons pour afficher les informations. Nous bouclons sur chaque élément de la liste de dictionnaires et affichons une variable compteur, le nom de l' | ||
+ | |||
+ | Entrer le nom de la série -> continuum | ||
+ | 2 resultat(s) | ||
+ | ------------------------ | ||
+ | 1 - Continuum - 30789 | ||
+ | 2 - Continuum (Web series) - 32083 | ||
+ | Choisir un nombre ou 0 pour quitter | ||
====== 12 ====== | ====== 12 ====== | ||
- | Please remember that the list of items is zero based, so when the user enters ‘1’, they are really asking for dictionary number 0. We do this, because “regular” people think that counting should start with ‘1’ not 0. And we can then use 0 to escape the routine and not make them use ‘Q’ or ‘q’ or ‘-1’. | + | **Please remember that the list of items is zero based, so when the user enters ‘1’, they are really asking for dictionary number 0. We do this, because “regular” people think that counting should start with ‘1’ not 0. And we can then use 0 to escape the routine and not make them use ‘Q’ or ‘q’ or ‘-1’. |
Now, the “main” routine that pulls it all together for us. | Now, the “main” routine that pulls it all together for us. | ||
Ligne 181: | Ligne 214: | ||
Next time, we’ll add the other routines. For now, the code can be found at http:// | Next time, we’ll add the other routines. For now, the code can be found at http:// | ||
- | See you soon. | + | See you soon.** |
+ | Souvenez-vous que la liste des articles commence à zéro, donc lorsque l' | ||
+ | Maintenant, la routine « main » va tout rassembler pour nous. | ||
+ | Pour aujourd' | ||
- | code en français : http:// | + | La prochaine fois, nous ajouterons les autres routines. Pour l' |
+ | À bientôt. |
issue69/programmer_en_python.1364415560.txt.gz · Dernière modification : 2013/03/27 21:19 de fredphil91