2A.eco - API, API REST

Links: notebook, html, PDF, python, slides, GitHub

Petite revue d’API REST.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

Définition :

API, à part que ce mot qui vaut 5 au scrabble, c’est quoi au juste ?

API signifie Application Programming Interface. Le mot le plus important est “interface”, et c’est le mot le plus simple, car nous utilisons tous des interfaces.

Bon, et une interface ?

Définition Larrouse : “Une interface est un dispositif qui permet des échanges et interactions entre différents acteurs”

Pour faire simple, une API est un moyen efficace de faire communiquer entre elles deux applications : concrètement, un fournisseur de service met à disposition des développeurs une interface codifiée, qui leur permet d’obtenir des informations à partir de requêtes.

Sans rentrer dans le détail technique, le dialogue ressemble à : “envoie moi ton adresse sous la forme X = rue, Y = Ville, Z = Pays” et moi, en retour, je t’enverrai le code à afficher sur ton site pour avoir la carte interactive.

Les API qui existent

De plus en plus de sites mettent à disposition des développeurs et autres curieux des API.

Pour en citer quelques-uns :

Ou encore :

Comment parler à une API ?

La plupart des API donnent des exemples par communiquer avec les données présentes sur le site.

Simplement, il faut trouver l’url qui renvoit les données que vous souhaitez avoir

Par exemple, avec l’API de la Banque mondiale, voici comme s’écrit une requête pour les données de la Banque Mondiale :

http://api.worldbank.org/countries?incomeLevel=LMC

Avec cette url, on demande la liste des pays dont le niveau de revenus est LMC, c’est à dire “Lower middle income”.

En cliquant sur le lien, le site renvoit des données en XML, qui ressemblent pas mal à ce qu’on a vu plus tôt avec le scraping : une structure avec des balises qui s’ouvrent et qui se ferment.

<wb:countries xmlns:wb= »http://www.worldbank.org » page= »1 » pages= »2 » per_page= »50 » total= »52 »> <wb:country id= »ARM »>

<wb:iso2Code>AM</wb:iso2Code> <wb:name>Armenia</wb:name> <wb:region id= »ECS »>Europe & Central Asia</wb:region> <wb:adminregion id= »ECA »>Europe & Central Asia (excluding high income)</wb:adminregion> <wb:incomeLevel id= »LMC »>Lower middle income</wb:incomeLevel> <wb:lendingType id= »IBD »>IBRD</wb:lendingType> <wb:capitalCity>Yerevan</wb:capitalCity> <wb:longitude>44.509</wb:longitude> <wb:latitude>40.1596</wb:latitude>

</wb:country>

<wb:country id= »BGD »>

<wb:iso2Code>BD</wb:iso2Code> <wb:name>Bangladesh</wb:name> <wb:region id= »SAS »>South Asia</wb:region> <wb:adminregion id= »SAS »>South Asia</wb:adminregion> <wb:incomeLevel id= »LMC »>Lower middle income</wb:incomeLevel> <wb:lendingType id= »IDX »>IDA</wb:lendingType> <wb:capitalCity>Dhaka</wb:capitalCity> <wb:longitude>90.4113</wb:longitude> <wb:latitude>23.7055</wb:latitude>

</wb:country>


Quand on regare de plus près, on voit que les informations suivantes apparaissent

Code du pays | Nom du pays | Région | Classification en termes de revenus | Les types de prêt pour ces pays | La capitale | Longitude | Latitude


AM Armenia Europe & Central Asia Europe & Central Asia (excluding high income) Lower middle income IBRD Yerevan 44.509 40.1596

BD Bangladesh South Asia South Asia Lower middle income IDA Dhaka 90.4113 23.7055

En utilisant cette url ci : http://api.worldbank.org/countries?incomeLevel=LMC&format=json, on obtient directement un json, qui est finalement presque comme un dictionnaire en python.

Rien de plus simple donc pour demander quelque chose à une API, il suffit d’avoir la bonne url.

Et Python : comment il s’adresse aux API ?

C’est là qu’on revient aux fondamentaux : on va avoir besoin du module requests de Python et suivant les API, un parser comme BeautifulSoup ou rien si on réussit à obtenir un json.

On va utiliser le module requests et sa méthode get : on lui donne l’url de l’API qui nous intéresse, on lui demande d’en faire un json et le tour est joué !

Faire appel à l’API de la Banque Mondiale

import requests
data_json = requests.get("http://api.worldbank.org/countries?incomeLevel=LMC&format=json").json()
data_json
[{'page': 1, 'pages': 1, 'per_page': '50', 'total': 47},
 [{'id': 'AGO',
   'iso2Code': 'AO',
   'name': 'Angola',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Luanda',
   'longitude': '13.242',
   'latitude': '-8.81155'},
  {'id': 'BGD',
   'iso2Code': 'BD',
   'name': 'Bangladesh',
   'region': {'id': 'SAS', 'value': 'South Asia'},
   'adminregion': {'id': 'SAS', 'value': 'South Asia'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Dhaka',
   'longitude': '90.4113',
   'latitude': '23.7055'},
  {'id': 'BOL',
   'iso2Code': 'BO',
   'name': 'Bolivia',
   'region': {'id': 'LCN', 'value': 'Latin America & Caribbean '},
   'adminregion': {'id': 'LAC',
    'value': 'Latin America & Caribbean (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'La Paz',
   'longitude': '-66.1936',
   'latitude': '-13.9908'},
  {'id': 'BTN',
   'iso2Code': 'BT',
   'name': 'Bhutan',
   'region': {'id': 'SAS', 'value': 'South Asia'},
   'adminregion': {'id': 'SAS', 'value': 'South Asia'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Thimphu',
   'longitude': '89.6177',
   'latitude': '27.5768'},
  {'id': 'CIV',
   'iso2Code': 'CI',
   'name': "Cote d'Ivoire",
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Yamoussoukro',
   'longitude': '-4.0305',
   'latitude': '5.332'},
  {'id': 'CMR',
   'iso2Code': 'CM',
   'name': 'Cameroon',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Yaounde',
   'longitude': '11.5174',
   'latitude': '3.8721'},
  {'id': 'COG',
   'iso2Code': 'CG',
   'name': 'Congo, Rep.',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Brazzaville',
   'longitude': '15.2662',
   'latitude': '-4.2767'},
  {'id': 'COM',
   'iso2Code': 'KM',
   'name': 'Comoros',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Moroni',
   'longitude': '43.2418',
   'latitude': '-11.6986'},
  {'id': 'CPV',
   'iso2Code': 'CV',
   'name': 'Cabo Verde',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Praia',
   'longitude': '-23.5087',
   'latitude': '14.9218'},
  {'id': 'DJI',
   'iso2Code': 'DJ',
   'name': 'Djibouti',
   'region': {'id': 'MEA', 'value': 'Middle East & North Africa'},
   'adminregion': {'id': 'MNA',
    'value': 'Middle East & North Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Djibouti',
   'longitude': '43.1425',
   'latitude': '11.5806'},
  {'id': 'EGY',
   'iso2Code': 'EG',
   'name': 'Egypt, Arab Rep.',
   'region': {'id': 'MEA', 'value': 'Middle East & North Africa'},
   'adminregion': {'id': 'MNA',
    'value': 'Middle East & North Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Cairo',
   'longitude': '31.2461',
   'latitude': '30.0982'},
  {'id': 'FSM',
   'iso2Code': 'FM',
   'name': 'Micronesia, Fed. Sts.',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Palikir',
   'longitude': '158.185',
   'latitude': '6.91771'},
  {'id': 'GHA',
   'iso2Code': 'GH',
   'name': 'Ghana',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Accra',
   'longitude': '-0.20795',
   'latitude': '5.57045'},
  {'id': 'HND',
   'iso2Code': 'HN',
   'name': 'Honduras',
   'region': {'id': 'LCN', 'value': 'Latin America & Caribbean '},
   'adminregion': {'id': 'LAC',
    'value': 'Latin America & Caribbean (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Tegucigalpa',
   'longitude': '-87.4667',
   'latitude': '15.1333'},
  {'id': 'IDN',
   'iso2Code': 'ID',
   'name': 'Indonesia',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Jakarta',
   'longitude': '106.83',
   'latitude': '-6.19752'},
  {'id': 'IND',
   'iso2Code': 'IN',
   'name': 'India',
   'region': {'id': 'SAS', 'value': 'South Asia'},
   'adminregion': {'id': 'SAS', 'value': 'South Asia'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'New Delhi',
   'longitude': '77.225',
   'latitude': '28.6353'},
  {'id': 'KEN',
   'iso2Code': 'KE',
   'name': 'Kenya',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Nairobi',
   'longitude': '36.8126',
   'latitude': '-1.27975'},
  {'id': 'KGZ',
   'iso2Code': 'KG',
   'name': 'Kyrgyz Republic',
   'region': {'id': 'ECS', 'value': 'Europe & Central Asia'},
   'adminregion': {'id': 'ECA',
    'value': 'Europe & Central Asia (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Bishkek',
   'longitude': '74.6057',
   'latitude': '42.8851'},
  {'id': 'KHM',
   'iso2Code': 'KH',
   'name': 'Cambodia',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Phnom Penh',
   'longitude': '104.874',
   'latitude': '11.5556'},
  {'id': 'KIR',
   'iso2Code': 'KI',
   'name': 'Kiribati',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Tarawa',
   'longitude': '172.979',
   'latitude': '1.32905'},
  {'id': 'LAO',
   'iso2Code': 'LA',
   'name': 'Lao PDR',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Vientiane',
   'longitude': '102.177',
   'latitude': '18.5826'},
  {'id': 'LSO',
   'iso2Code': 'LS',
   'name': 'Lesotho',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Maseru',
   'longitude': '27.7167',
   'latitude': '-29.5208'},
  {'id': 'MAR',
   'iso2Code': 'MA',
   'name': 'Morocco',
   'region': {'id': 'MEA', 'value': 'Middle East & North Africa'},
   'adminregion': {'id': 'MNA',
    'value': 'Middle East & North Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Rabat',
   'longitude': '-6.8704',
   'latitude': '33.9905'},
  {'id': 'MDA',
   'iso2Code': 'MD',
   'name': 'Moldova',
   'region': {'id': 'ECS', 'value': 'Europe & Central Asia'},
   'adminregion': {'id': 'ECA',
    'value': 'Europe & Central Asia (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Chisinau',
   'longitude': '28.8497',
   'latitude': '47.0167'},
  {'id': 'MMR',
   'iso2Code': 'MM',
   'name': 'Myanmar',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Naypyidaw',
   'longitude': '95.9562',
   'latitude': '21.914'},
  {'id': 'MNG',
   'iso2Code': 'MN',
   'name': 'Mongolia',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Ulaanbaatar',
   'longitude': '106.937',
   'latitude': '47.9129'},
  {'id': 'MRT',
   'iso2Code': 'MR',
   'name': 'Mauritania',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Nouakchott',
   'longitude': '-15.9824',
   'latitude': '18.2367'},
  {'id': 'NGA',
   'iso2Code': 'NG',
   'name': 'Nigeria',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Abuja',
   'longitude': '7.48906',
   'latitude': '9.05804'},
  {'id': 'NIC',
   'iso2Code': 'NI',
   'name': 'Nicaragua',
   'region': {'id': 'LCN', 'value': 'Latin America & Caribbean '},
   'adminregion': {'id': 'LAC',
    'value': 'Latin America & Caribbean (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Managua',
   'longitude': '-86.2734',
   'latitude': '12.1475'},
  {'id': 'PAK',
   'iso2Code': 'PK',
   'name': 'Pakistan',
   'region': {'id': 'SAS', 'value': 'South Asia'},
   'adminregion': {'id': 'SAS', 'value': 'South Asia'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Islamabad',
   'longitude': '72.8',
   'latitude': '30.5167'},
  {'id': 'PHL',
   'iso2Code': 'PH',
   'name': 'Philippines',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Manila',
   'longitude': '121.035',
   'latitude': '14.5515'},
  {'id': 'PNG',
   'iso2Code': 'PG',
   'name': 'Papua New Guinea',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Port Moresby',
   'longitude': '147.194',
   'latitude': '-9.47357'},
  {'id': 'PSE',
   'iso2Code': 'PS',
   'name': 'West Bank and Gaza',
   'region': {'id': 'MEA', 'value': 'Middle East & North Africa'},
   'adminregion': {'id': 'MNA',
    'value': 'Middle East & North Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'LNX', 'value': 'Not classified'},
   'capitalCity': '',
   'longitude': '',
   'latitude': ''},
  {'id': 'SDN',
   'iso2Code': 'SD',
   'name': 'Sudan',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Khartoum',
   'longitude': '32.5363',
   'latitude': '15.5932'},
  {'id': 'SEN',
   'iso2Code': 'SN',
   'name': 'Senegal',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Dakar',
   'longitude': '-17.4734',
   'latitude': '14.7247'},
  {'id': 'SLB',
   'iso2Code': 'SB',
   'name': 'Solomon Islands',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Honiara',
   'longitude': '159.949',
   'latitude': '-9.42676'},
  {'id': 'SLV',
   'iso2Code': 'SV',
   'name': 'El Salvador',
   'region': {'id': 'LCN', 'value': 'Latin America & Caribbean '},
   'adminregion': {'id': 'LAC',
    'value': 'Latin America & Caribbean (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'San Salvador',
   'longitude': '-89.2073',
   'latitude': '13.7034'},
  {'id': 'STP',
   'iso2Code': 'ST',
   'name': 'Sao Tome and Principe',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Sao Tome',
   'longitude': '6.6071',
   'latitude': '0.20618'},
  {'id': 'SWZ',
   'iso2Code': 'SZ',
   'name': 'Eswatini',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Mbabane',
   'longitude': '31.4659',
   'latitude': '-26.5225'},
  {'id': 'TLS',
   'iso2Code': 'TL',
   'name': 'Timor-Leste',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Dili',
   'longitude': '125.567',
   'latitude': '-8.56667'},
  {'id': 'TUN',
   'iso2Code': 'TN',
   'name': 'Tunisia',
   'region': {'id': 'MEA', 'value': 'Middle East & North Africa'},
   'adminregion': {'id': 'MNA',
    'value': 'Middle East & North Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Tunis',
   'longitude': '10.21',
   'latitude': '36.7899'},
  {'id': 'UKR',
   'iso2Code': 'UA',
   'name': 'Ukraine',
   'region': {'id': 'ECS', 'value': 'Europe & Central Asia'},
   'adminregion': {'id': 'ECA',
    'value': 'Europe & Central Asia (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Kiev',
   'longitude': '30.5038',
   'latitude': '50.4536'},
  {'id': 'UZB',
   'iso2Code': 'UZ',
   'name': 'Uzbekistan',
   'region': {'id': 'ECS', 'value': 'Europe & Central Asia'},
   'adminregion': {'id': 'ECA',
    'value': 'Europe & Central Asia (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Tashkent',
   'longitude': '69.269',
   'latitude': '41.3052'},
  {'id': 'VNM',
   'iso2Code': 'VN',
   'name': 'Vietnam',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IBD', 'value': 'IBRD'},
   'capitalCity': 'Hanoi',
   'longitude': '105.825',
   'latitude': '21.0069'},
  {'id': 'VUT',
   'iso2Code': 'VU',
   'name': 'Vanuatu',
   'region': {'id': 'EAS', 'value': 'East Asia & Pacific'},
   'adminregion': {'id': 'EAP',
    'value': 'East Asia & Pacific (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Port-Vila',
   'longitude': '168.321',
   'latitude': '-17.7404'},
  {'id': 'ZMB',
   'iso2Code': 'ZM',
   'name': 'Zambia',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDX', 'value': 'IDA'},
   'capitalCity': 'Lusaka',
   'longitude': '28.2937',
   'latitude': '-15.3982'},
  {'id': 'ZWE',
   'iso2Code': 'ZW',
   'name': 'Zimbabwe',
   'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
   'adminregion': {'id': 'SSA',
    'value': 'Sub-Saharan Africa (excluding high income)'},
   'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
   'lendingType': {'id': 'IDB', 'value': 'Blend'},
   'capitalCity': 'Harare',
   'longitude': '31.0672',
   'latitude': '-17.8312'}]]
data_json[0]
# On voit qu'il y a nous manque des informations :
# il y a un total de 52 éléments
data_json_page_2 = requests.get("http://api.worldbank.org/countries?incomeLevel=LMC&format=json&page=2").json()
data_json_page_2
[{'page': 2, 'pages': 1, 'per_page': '50', 'total': 47}, []]
# pour obtenir une observation
# on voit dans l'objet que l'élément 0 correspond à des informations sur les pages
data_json[1][0]
{'id': 'AGO',
 'iso2Code': 'AO',
 'name': 'Angola',
 'region': {'id': 'SSF', 'value': 'Sub-Saharan Africa '},
 'adminregion': {'id': 'SSA',
  'value': 'Sub-Saharan Africa (excluding high income)'},
 'incomeLevel': {'id': 'LMC', 'value': 'Lower middle income'},
 'lendingType': {'id': 'IBD', 'value': 'IBRD'},
 'capitalCity': 'Luanda',
 'longitude': '13.242',
 'latitude': '-8.81155'}

Faire appel à l’API de Tastekid

La Banque Mondiale c’était assez soft : on va passer sur du un peu plus costaud. On va utiliser l’API de Tastekid, site de recommandations de films, livres etc.

Pour cela, il faut commencer par créer un compte :

import keyring, os
key = keyring.get_password("tastekid", "ensae_teaching_cs,key")

Pour demander à l’API quels sont les oeuvres similaires à Pulp Fiction, nous utilisons la requête suivante

recommandations_res = requests.get("https://www.tastekid.com/api/similar?q=pulp+fiction&info=1&k={}".format(key))
try:
    recommandations = recommandations_res.json()
except Exception as e:
    # Parfois le formart json est mal formé. On regarde pourquoi.
    raise Exception(recommandations_res.text) from e
print(str(recommandations)[:2000])
{'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'nnPulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The film's title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.nTarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character's perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.n', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Reservoir Dogs', 'Type': 'movie', 'wTeaser': 'nReservoir Dogs is a 1992 American heist film written and directed by Quentin Tarantino in his feature-length debut. It stars Harvey Keitel, Tim Roth, Chris Penn, Steve Buscemi, Lawrence Tierney, Michael Madsen, Tarantino, and Edward Bunker, as diamond thieves whose planned heist of a jewelry store goes terribly wrong. The film depicts the events before and after the heist. Kirk Baltz, Randy Brooks and Steven Wright also play supporting roles. It incorporates many motifs that have become Tarantino's hallmarks: violent crime, pop cu
# on nous rappelle les informations sur l'élement que l'on recherche : Pulp Fiction
recommandations['Similar']['Info']
[{'Name': 'Pulp Fiction',
  'Type': 'movie',
  'wTeaser': 'nnPulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The film's title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.nTarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character's perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.n',
  'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)',
  'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY',
  'yID': 's7EdQ4FqbhY'}]
# on nous donnes des livres / filmes proches selon le gout des gens
for element in recommandations['Similar']['Results'] :
    print(element['Name'],element['Type'])
Reservoir Dogs movie
Inglourious Basterds movie
Django Unchained movie
The Big Lebowski movie
Goodfellas movie
Jackie Brown movie
Death Proof movie
Quentin Tarantino author
Trainspotting movie
Snatch movie
Taxi Driver movie
Full Metal Jacket movie
The Inglorious Bastards movie
A Clockwork Orange movie
From Dusk Till Dawn movie
Scarface movie
Lock, Stock And Two Smoking Barrels movie
No Country For Old Men movie
Sin City movie
The Godfather movie

On peut aussi ne demander que des films, on ajoute juste l’option type = movies dans l’url

recommandations_films = requests.get("https://www.tastekid.com/api/similar?q=pulp+fiction&type=movie&info=1&k={}"
                               .format(key)).json()
print(str(recommandations_films)[:2000])
{'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'nnPulp Fiction is a 1994 American crime film written and directed by Quentin Tarantino, who conceived it with Roger Avary. Starring John Travolta, Samuel L. Jackson, Bruce Willis, Tim Roth, Ving Rhames, and Uma Thurman, it tells several stories of criminal Los Angeles. The film's title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.nTarantino wrote Pulp Fiction in 1992 and 1993, incorporating scenes that Avary originally wrote for True Romance (1993). Its plot occurs out of chronological order. The film is also self-referential from its opening moments, beginning with a title card that gives two dictionary definitions of "pulp". Considerable screen time is devoted to monologues and casual conversations with eclectic dialogue revealing each character's perspectives on several subjects, and the film features an ironic combination of humor and strong violence. TriStar Pictures reportedly turned down the script as "too demented". Miramax co-chairman Harvey Weinstein was enthralled, however, and the film became the first that Miramax fully financed.n', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Reservoir Dogs', 'Type': 'movie', 'wTeaser': 'nReservoir Dogs is a 1992 American heist film written and directed by Quentin Tarantino in his feature-length debut. It stars Harvey Keitel, Tim Roth, Chris Penn, Steve Buscemi, Lawrence Tierney, Michael Madsen, Tarantino, and Edward Bunker, as diamond thieves whose planned heist of a jewelry store goes terribly wrong. The film depicts the events before and after the heist. Kirk Baltz, Randy Brooks and Steven Wright also play supporting roles. It incorporates many motifs that have become Tarantino's hallmarks: violent crime, pop cu
# on nous donnes des livres / filmes proches selon le gout des gens
for element in recommandations_films['Similar']['Results'] :
    print(element['Name'],element['Type'])
Reservoir Dogs movie
Inglourious Basterds movie
Django Unchained movie
The Big Lebowski movie
Goodfellas movie
Jackie Brown movie
Death Proof movie
Quentin Tarantino author
Trainspotting movie
Snatch movie
Taxi Driver movie
Full Metal Jacket movie
The Inglorious Bastards movie
A Clockwork Orange movie
From Dusk Till Dawn movie
Scarface movie
Lock, Stock And Two Smoking Barrels movie
No Country For Old Men movie
Sin City movie
The Godfather movie
film_suivant = "Reservoir Dogs"
recommandations_suivantes_films = requests.get("https://www.tastekid.com/api/similar?q={}&type=movie&info=1&k={}"
                               .format(film_suivant, key)).json()
# on nous donnes des livres / filmes proches selon le gout des gens
for element in recommandations_suivantes_films['Similar']['Results'] :
    print(element['Name'],element['Type'])
Jackie Brown movie
Death Proof movie
From Dusk Till Dawn movie
Lock, Stock And Two Smoking Barrels movie
Snatch movie
Taxi Driver movie
Goodfellas movie
Inglourious Basterds movie
Rocknrolla movie
The Usual Suspects movie
No Country For Old Men movie
Natural Born Killers movie
Full Metal Jacket movie
True Romance movie
The Big Lebowski movie
Pulp Fiction movie
Fargo movie
Sin City movie
Scarface movie
Planet Terror movie
## On peut ensuite comparer les films communs aux deux recherches
liste1 = [element['Name'] for element in recommandations_films['Similar']['Results'] ]
liste2 = [element['Name'] for element in recommandations_suivantes_films['Similar']['Results'] ]
films_commun = set(liste1).intersection(liste2)
films_commun, len(films_commun)
({'Death Proof',
  'From Dusk Till Dawn',
  'Full Metal Jacket',
  'Goodfellas',
  'Inglourious Basterds',
  'Jackie Brown',
  'Lock, Stock And Two Smoking Barrels',
  'No Country For Old Men',
  'Scarface',
  'Sin City',
  'Snatch',
  'Taxi Driver',
  'The Big Lebowski'},
 13)
films_non_partages = [f for f in liste1 if f not in liste2] + [f for f in liste2 if f not in liste1]
films_non_partages
['Reservoir Dogs',
 'Django Unchained',
 'Quentin Tarantino',
 'Trainspotting',
 'The Inglorious Bastards',
 'A Clockwork Orange',
 'The Godfather',
 'Rocknrolla',
 'The Usual Suspects',
 'Natural Born Killers',
 'True Romance',
 'Pulp Fiction',
 'Fargo',
 'Planet Terror']

A partir de ces requêtes, on peut facilement construire le réseau des films que les gens aiment en utilisant le package networkx.