issue178:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue178:python [2022/02/26 10:04] – créée auntiee | issue178:python [2022/03/01 10:51] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | I had wanted to present you with an example of creating your own API wrapper library, but the site that I was going to use has moved to a paid model. However, I have found a different site that does basically the same thing at no cost to you, so I’ll start. | + | **I had wanted to present you with an example of creating your own API wrapper library, but the site that I was going to use has moved to a paid model. However, I have found a different site that does basically the same thing at no cost to you, so I’ll start. |
- | 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.** |
- | 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:// | + | 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:// | ||
Any query for information on a movie or TV show starts with obtaining the ID number of the show you want to investigate. However, in order to obtain the ID, you must first do a search. We must format a URL which includes our API key and the name of the movie. Here is a dummy URL that you can use as an example. | Any query for information on a movie or TV show starts with obtaining the ID number of the show you want to investigate. However, in order to obtain the ID, you must first do a search. We must format a URL which includes our API key and the name of the movie. Here is a dummy URL that you can use as an example. | ||
+ | |||
+ | 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:// | https:// | ||
- | To break it down, it would be… | + | |
+ | **To break it down, it would be… | ||
• Base UR: https:// | • Base UR: https:// | ||
• API Version: 3/ | • API Version: 3/ | ||
Ligne 24: | Ligne 36: | ||
Let’s say we want to search for the Ant Man movie. Using the above format, the URL would look like this (without the API key): | Let’s say we want to search for the Ant Man movie. Using the above format, the URL would look like this (without the API key): | ||
- | https:// | + | https:// |
- | The information that comes back will be in JSON format. | + | 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. | ||
Inside the JSON response you will find a field named id as well as fields for original title, overview and so on that will help you decide which of the results contain the ID that you are looking for. | Inside the JSON response you will find a field named id as well as fields for original title, overview and so on that will help you decide which of the results contain the ID that you are looking for. | ||
Ligne 36: | Ligne 66: | ||
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:// |
- | Again, the data comes back in JSON format. | + | 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. | ||
id: 102899, | id: 102899, | ||
Ligne 48: | Ligne 91: | ||
Television series queries would be similar, but in addition to looking for a specific TV series and its details, you can get season and episode details as well. Going even further, for movies you can get the names of the cast and crew, and the same thing for TV shows plus guest stars for each episode (if available). | Television series queries would be similar, but in addition to looking for a specific TV series and its details, you can get season and episode details as well. Going even further, for movies you can get the names of the cast and crew, and the same thing for TV shows plus guest stars for each episode (if available). | ||
- | 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.** |
- | 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: | + | 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: | ||
# ====================== | # ====================== | ||
Ligne 65: | Ligne 121: | ||
mykey3 = <Your API Key Here> | mykey3 = <Your API Key Here> | ||
+ | |||
+ | 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] | 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. | ||
At this point, you are probably wondering what is the deal with pages? This is very important, since if there are more than about 20 matches found, the API will break the result set into a set of pages. You will need to make multiple requests using the Search Movie query – asking for each page in order. To keep you (and myself) from having to do this, I handle all the requests for the search within the function, and merge all of the data and only a single list of dictionaries. | At this point, you are probably wondering what is the deal with pages? This is very important, since if there are more than about 20 matches found, the API will break the result set into a set of pages. You will need to make multiple requests using the Search Movie query – asking for each page in order. To keep you (and myself) from having to do this, I handle all the requests for the search within the function, and merge all of the data and only a single list of dictionaries. | ||
Ligne 81: | Ligne 156: | ||
So the information we REALLY want would be the id, so we can simply extract this by using… | So the information we REALLY want would be the id, so we can simply extract this by using… | ||
+ | |||
+ | 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][" | movie_id = endresults[0][" | ||
- | Then the call to get the movie details would be: | + | |
+ | **Then the call to get the movie details would be: | ||
status_code, | status_code, | ||
Ligne 91: | Ligne 183: | ||
The current version supports the following functions from the API version 3. | The current version supports the following functions from the API version 3. | ||
+ | |||
+ | 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, | ||
+ | |||
+ | 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: | Currently supported functions: | ||
Ligne 105: | Ligne 218: | ||
Get_movie_watch_providers, | 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. | ||
At the end of the file is a simple test program that you can use by simply calling: | At the end of the file is a simple test program that you can use by simply calling: | ||
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.1645866289.txt.gz · Dernière modification : 2022/02/26 10:04 de auntiee