issue178: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 | ||
issue178:python [2022/02/26 11:25] – d52fr | issue178:python [2022/03/01 10:51] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
The Movie Database (themoviedb.org) is a great place to find out information about your favorite movies and TV shows, as well as the people who star and help create them. The first thing to do is create an account on the main system and then you can sign up for an API key. Once you have a key, you can query the database through a simple Python program using the wrapper that I’ve created and am presenting here. The API wrapper library covers only a few of the various calls that can be made to the system, mainly the ones that were immediately useful to me and probably to you.** | The Movie Database (themoviedb.org) is a great place to find out information about your favorite movies and TV shows, as well as the people who star and help create them. The first thing to do is create an account on the main system and then you can sign up for an API key. Once you have a key, you can query the database through a simple Python program using the wrapper that I’ve created and am presenting here. The API wrapper library covers only a few of the various calls that can be made to the system, mainly the ones that were immediately useful to me and probably to you.** | ||
+ | |||
+ | Je voulais vous présenter un exemple de création de votre propre bibliothèque d' | ||
+ | |||
+ | The Movie Database (themoviedb.org) est un site formidable pour trouver des informations sur vos films et émissions de télévision préférés, | ||
+ | |||
**Once you have your key, you should look at the different API calls that can be made and what those calls will return to you. The documentation is at https:// | **Once you have your key, you should look at the different API calls that can be made and what those calls will return to you. The documentation is at https:// | ||
Ligne 8: | Ligne 13: | ||
https:// | https:// | ||
+ | |||
+ | Une fois que vous avez votre clé, vous devez examiner les différents appels d'API qui peuvent être effectués et ce que ces appels vous retourneront. La documentation se trouve à l' | ||
+ | |||
+ | Toute requête d' | ||
+ | |||
+ | https:// | ||
+ | |||
**To break it down, it would be… | **To break it down, it would be… | ||
Ligne 25: | Ligne 37: | ||
https:// | https:// | ||
+ | |||
+ | En la décomposant, | ||
+ | - Base UR : https:// | ||
+ | - Version de l'API : 3/ | ||
+ | - Commande : search/ | ||
+ | - Type : movie ? | ||
+ | - Clé API : < | ||
+ | - Langue : en-US& | ||
+ | - Requête : (Nom du film)& | ||
+ | - Page # : 1& | ||
+ | - Inclure les films pour adultes : false | ||
+ | |||
+ | Il existe également deux autres champs que vous pouvez utiliser pour affiner votre recherche : year et primary_release_year. | ||
+ | |||
+ | Disons que nous voulons rechercher le film Ant Man. En utilisant le format ci-dessus, l'URL ressemblerait à ceci (sans la clé API) : | ||
+ | |||
+ | https:// | ||
+ | |||
**The information that comes back will be in JSON format. | **The information that comes back will be in JSON format. | ||
Ligne 37: | Ligne 67: | ||
That gives a fair amount of data about the movie. If that isn’t enough, you can go for the Movie Detail: | That gives a fair amount of data about the movie. If that isn’t enough, you can go for the Movie Detail: | ||
https:// | https:// | ||
+ | |||
+ | Les informations retournées seront au format JSON. (Ronnie a eu un problème avec la réponse JSON, en essayant de la faire tenir correctement dans le magazine. | ||
+ | |||
+ | Dans la réponse JSON, vous trouverez un champ nommé id ainsi que des champs avec le titre original, l' | ||
+ | |||
+ | id : 102899, | ||
+ | original_language : " | ||
+ | original_title : " | ||
+ | overview: (aperçu) : (traduction) « Armé de l' | ||
+ | |||
+ | Cela donne une bonne quantité d' | ||
+ | https:// | ||
+ | |||
**Again, the data comes back in JSON format. | **Again, the data comes back in JSON format. | ||
Ligne 49: | Ligne 92: | ||
While you can simply use your API key and run the queries (all of them) via a web browser, I think that it’s a bit simpler to use Python to make the calls. Hence the API wrapper.** | While you can simply use your API key and run the queries (all of them) via a web browser, I think that it’s a bit simpler to use Python to make the calls. Hence the API wrapper.** | ||
+ | |||
+ | Là encore, les données sont retournées au format JSON. (Encore une fois, la réponse JSON réelle est présentée sur mon dépôt github). | ||
+ | |||
+ | id : 102899, | ||
+ | imdb_id : " | ||
+ | original_language : " | ||
+ | original_title : " | ||
+ | aperçu : « Armé de l' | ||
+ | |||
+ | Les recherches de séries télévisées sont similaires, mais en plus de rechercher une série télévisée spécifique et ses détails, vous pouvez également obtenir les détails de la saison et des épisodes. En allant encore plus loin, pour les films, vous pouvez obtenir les noms des acteurs et de l' | ||
+ | |||
+ | Bien que vous puissiez simplement utiliser votre clé API et exécuter les requêtes (toutes) via un navigateur Web, je pense que c'est un peu plus simple d' | ||
+ | |||
**So the wrapper is named (imagine how many hours it took me to come up with this name…) wrapper.py. As always, it starts out with a series of imports: | **So the wrapper is named (imagine how many hours it took me to come up with this name…) wrapper.py. As always, it starts out with a series of imports: | ||
Ligne 67: | Ligne 123: | ||
loc = locale.getlocale()[0]** | loc = locale.getlocale()[0]** | ||
+ | |||
+ | Le wrapper s' | ||
+ | |||
+ | # ====================== | ||
+ | # Imports | ||
+ | # ====================== | ||
+ | import requests | ||
+ | import json | ||
+ | import pprint | ||
+ | import locale | ||
+ | |||
+ | J'ai inclus pprint pour me permettre d' | ||
+ | |||
+ | Juste après la section d' | ||
+ | |||
+ | mykey3 = <Votre clé API ici> | ||
+ | |||
+ | loc = locale.getlocale()[0] | ||
+ | |||
**The first major function will search the movie database by name, and return the requests’ status code, the number of results, the number of pages, and a list of dictionaries that has been snipped out of the JSON data stream. | **The first major function will search the movie database by name, and return the requests’ status code, the number of results, the number of pages, and a list of dictionaries that has been snipped out of the JSON data stream. | ||
Ligne 83: | Ligne 158: | ||
movie_id = endresults[0][" | movie_id = endresults[0][" | ||
+ | |||
+ | La première fonction principale recherchera dans la base de données par nom de film et renverra le code d' | ||
+ | |||
+ | À ce stade, vous vous demandez probablement ce qu'il en est des pages. C'est très important, car s'il y a plus de 20 correspondances trouvées, l'API divisera l' | ||
+ | |||
+ | Ainsi, la fonction de recherche d'un film à l'aide du wrapper serait celle présentée en haut à droite. | ||
+ | |||
+ | Je dois dire que si vous avez l' | ||
+ | |||
+ | Toutes les données fournies dans le fichier JSON sont incluses, elles sont juste formatées dans un format plus facile à gérer, dans la variable endresults (encore une fois, c'est une liste d' | ||
+ | |||
+ | En utilisant la fonction « pretty print », les retours de la fonction ressembleraient à ce qui est montré sur la page suivante, en haut à droite. | ||
+ | |||
+ | L' | ||
+ | |||
+ | movie_id = endresults[0][" | ||
+ | |||
**Then the call to get the movie details would be: | **Then the call to get the movie details would be: | ||
Ligne 104: | Ligne 196: | ||
Get_movie_watch_providers, | Get_movie_watch_providers, | ||
+ | |||
+ | L' | ||
+ | |||
+ | status_code, | ||
+ | |||
+ | Les informations renvoyées sont affichées en bas à droite. | ||
+ | |||
+ | La version actuelle prend en charge les fonctions suivantes de la version 3 de l'API. | ||
+ | |||
+ | Currently supported functions: | ||
+ | |||
+ | *** Search functions will return a number of results depending on query *** | ||
+ | Search_movie, | ||
+ | |||
+ | *** Get detail functions (REQUIRE ID FROM SEARCH FUNCTIONS) *** | ||
+ | |||
+ | Get_movie_by_id, | ||
+ | |||
+ | Get_tv_season_detail, | ||
+ | |||
+ | Get_movie_watch_providers, | ||
+ | |||
**I’ve decided to go ahead and release this early version (0.4) that you can use as a learning tool. | **I’ve decided to go ahead and release this early version (0.4) that you can use as a learning tool. | ||
Ligne 115: | Ligne 229: | ||
As you can see, creating a wrapper for many APIs can be fairly simple. That’s not to say that every API would be this easy, but this should give a good starting point for you to create your own API library wrappers for fun and potentially profit. | As you can see, creating a wrapper for many APIs can be fairly simple. That’s not to say that every API would be this easy, but this should give a good starting point for you to create your own API library wrappers for fun and potentially profit. | ||
- | I’ve placed the wrapper.py file on my repository at https:// | + | I’ve placed the wrapper.py file on my repository at https:// |
+ | |||
+ | J'ai décidé d' | ||
+ | |||
+ | A la fin du fichier se trouve un programme de test simple que vous pouvez utiliser en appelant simplement : | ||
+ | |||
+ | python wrapper.py | ||
+ | |||
+ | Si vous souhaitez l' | ||
+ | |||
+ | Comme vous pouvez le constater, la création d'un wrapper pour de nombreuses API peut être assez simple. Cela ne veut pas dire que toutes les API sont aussi simples, mais cela devrait vous donner un bon point de départ pour créer vos propres wrappers de bibliothèque API pour votre plaisir et potentiellement pour votre profit. | ||
- | Until next time, as always; stay safe, healthy, positive and creative!** | + | J'ai placé le fichier wrapper.py sur mon dépôt à l' |
issue178/python.1645871130.txt.gz · Dernière modification : 2022/02/26 11:25 de d52fr