{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# 2A.eco - API, API REST\n", "\n", "Petite revue d'[API REST](https://fr.wikipedia.org/wiki/Representational_state_transfer)."]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/html": ["
run previous cell, wait for 2 seconds
\n", ""], "text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import add_notebook_menu\n", "add_notebook_menu()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## D\u00e9finition : \n", "\n", "API, \u00e0 part que ce mot qui vaut 5 au scrabble, c'est quoi au juste ?\n", "\n", "\n", "API signifie Application Programming Interface. Le mot le plus important est \u201cinterface\u201d, et c\u2019est le mot le plus simple, car nous utilisons tous des interfaces.\n", "\n", "Bon, et une interface ?\n", "\n", "D\u00e9finition Larrouse : \"Une interface est un dispositif qui permet des \u00e9changes et interactions entre diff\u00e9rents acteurs\"\n", "\n", "Pour faire simple, une API est un moyen efficace de faire communiquer entre elles deux applications : concr\u00e8tement, un fournisseur de service met \u00e0 disposition des d\u00e9veloppeurs une interface codifi\u00e9e, qui leur permet d'obtenir des informations \u00e0 partir de requ\u00eates.\n", "\n", "Sans rentrer dans le d\u00e9tail technique, le dialogue ressemble \u00e0 : \"envoie moi ton adresse sous la forme X = rue, Y = Ville, Z = Pays\" et moi, en retour, je t'enverrai le code \u00e0 afficher sur ton site pour avoir la carte interactive.\n", "\n", "\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Les API qui existent\n", "\n", "\n", "De plus en plus de sites mettent \u00e0 disposition des d\u00e9veloppeurs et autres curieux des API. \n", "\n", "Pour en citer quelques-uns : \n", "\n", "- Twitter : https://dev.twitter.com/rest/public\n", "- Facebook : https://developers.facebook.com/\n", "- Instagram : https://www.instagram.com/developer/\n", "- Spotify : https://developer.spotify.com/web-api/\n", "\n", "\n", "Ou encore : \n", "\n", "- Pole Emploi : https://www.emploi-store-dev.fr/portail-developpeur-cms/home.html\n", "- SNCF : https://data.sncf.com/api\n", "- AirFrance KLM : https://developer.airfranceklm.com/Our_Apis\n", "- Banque Mondiale : https://datahelpdesk.worldbank.org/knowledgebase/topics/125589\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Comment parler \u00e0 une API ?\n", "\n", "La plupart des API donnent des exemples par communiquer avec les donn\u00e9es pr\u00e9sentes sur le site.\n", "\n", "Simplement, il faut trouver l'url qui renvoit les donn\u00e9es que vous souhaitez avoir\n", "\n", "Par exemple, avec l'API de la Banque mondiale, voici comme s'\u00e9crit une requ\u00eate pour les donn\u00e9es de la Banque Mondiale : \n", "\n", "http://api.worldbank.org/countries?incomeLevel=LMC\n", "\n", "Avec cette url, on demande la liste des pays dont le niveau de revenus est LMC, c'est \u00e0 dire \"Lower middle income\". \n", "\n", "En cliquant sur le lien, le site renvoit des donn\u00e9es en XML, qui ressemblent pas mal \u00e0 ce qu'on a vu plus t\u00f4t avec le scraping : une structure avec des balises qui s'ouvrent et qui se ferment."]}, {"cell_type": "raw", "metadata": {}, "source": ["\n", "\n", " AM\n", " Armenia\n", " Europe & Central Asia\n", " Europe & Central Asia (excluding high income)\n", " Lower middle income\n", " IBRD\n", " Yerevan\n", " 44.509\n", " 40.1596\n", "\n", "\n", "\n", " BD\n", " Bangladesh\n", " South Asia\n", " South Asia\n", " Lower middle income\n", " IDA\n", " Dhaka\n", " 90.4113\n", " 23.7055\n", "\n", "\n", "....."]}, {"cell_type": "markdown", "metadata": {}, "source": [" Quand on regare de plus pr\u00e8s, on voit que les informations suivantes apparaissent\n", " \n", "Code du pays | Nom du pays | R\u00e9gion | Classification en termes de revenus | Les types de pr\u00eat pour ces pays | La capitale | Longitude | Latitude"]}, {"cell_type": "markdown", "metadata": {}, "source": ["----------------\n", "\n", " AM\n", " Armenia\n", " Europe & Central Asia\n", " Europe & Central Asia (excluding high income)\n", " Lower middle income\n", " IBRD\n", " Yerevan\n", " 44.509\n", " 40.1596\n", "\n", "\n", "\n", " BD\n", " Bangladesh\n", " South Asia\n", " South Asia\n", " Lower middle income\n", " IDA\n", " Dhaka\n", " 90.4113\n", " 23.7055\n", ""]}, {"cell_type": "markdown", "metadata": {}, "source": ["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."]}, {"cell_type": "markdown", "metadata": {}, "source": ["Rien de plus simple donc pour demander quelque chose \u00e0 une API, il suffit d'avoir la bonne url."]}, {"cell_type": "markdown", "metadata": {}, "source": ["## Et Python : comment il s'adresse aux API ?\n", "\n", "C'est l\u00e0 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\u00e9ussit \u00e0 obtenir un json.\n", "\n", "On va utiliser le module requests et sa m\u00e9thode get : on lui donne l'url de l'API qui nous int\u00e9resse, on lui demande d'en faire un json et le tour est jou\u00e9 !"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Faire appel \u00e0 l'API de la Banque Mondiale"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"data": {"text/plain": ["[{'page': 1, 'pages': 1, 'per_page': '50', 'total': 47},\n", " [{'id': 'AGO',\n", " 'iso2Code': 'AO',\n", " 'name': 'Angola',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Luanda',\n", " 'longitude': '13.242',\n", " 'latitude': '-8.81155'},\n", " {'id': 'BGD',\n", " 'iso2Code': 'BD',\n", " 'name': 'Bangladesh',\n", " 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Dhaka',\n", " 'longitude': '90.4113',\n", " 'latitude': '23.7055'},\n", " {'id': 'BOL',\n", " 'iso2Code': 'BO',\n", " 'name': 'Bolivia',\n", " 'region': {'id': 'LCN',\n", " 'iso2code': 'ZJ',\n", " 'value': 'Latin America & Caribbean '},\n", " 'adminregion': {'id': 'LAC',\n", " 'iso2code': 'XJ',\n", " 'value': 'Latin America & Caribbean (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'La Paz',\n", " 'longitude': '-66.1936',\n", " 'latitude': '-13.9908'},\n", " {'id': 'BTN',\n", " 'iso2Code': 'BT',\n", " 'name': 'Bhutan',\n", " 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Thimphu',\n", " 'longitude': '89.6177',\n", " 'latitude': '27.5768'},\n", " {'id': 'CIV',\n", " 'iso2Code': 'CI',\n", " 'name': \"Cote d'Ivoire\",\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Yamoussoukro',\n", " 'longitude': '-4.0305',\n", " 'latitude': '5.332'},\n", " {'id': 'CMR',\n", " 'iso2Code': 'CM',\n", " 'name': 'Cameroon',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Yaounde',\n", " 'longitude': '11.5174',\n", " 'latitude': '3.8721'},\n", " {'id': 'COG',\n", " 'iso2Code': 'CG',\n", " 'name': 'Congo, Rep.',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Brazzaville',\n", " 'longitude': '15.2662',\n", " 'latitude': '-4.2767'},\n", " {'id': 'COM',\n", " 'iso2Code': 'KM',\n", " 'name': 'Comoros',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Moroni',\n", " 'longitude': '43.2418',\n", " 'latitude': '-11.6986'},\n", " {'id': 'CPV',\n", " 'iso2Code': 'CV',\n", " 'name': 'Cabo Verde',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Praia',\n", " 'longitude': '-23.5087',\n", " 'latitude': '14.9218'},\n", " {'id': 'DJI',\n", " 'iso2Code': 'DJ',\n", " 'name': 'Djibouti',\n", " 'region': {'id': 'MEA',\n", " 'iso2code': 'ZQ',\n", " 'value': 'Middle East & North Africa'},\n", " 'adminregion': {'id': 'MNA',\n", " 'iso2code': 'XQ',\n", " 'value': 'Middle East & North Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Djibouti',\n", " 'longitude': '43.1425',\n", " 'latitude': '11.5806'},\n", " {'id': 'EGY',\n", " 'iso2Code': 'EG',\n", " 'name': 'Egypt, Arab Rep.',\n", " 'region': {'id': 'MEA',\n", " 'iso2code': 'ZQ',\n", " 'value': 'Middle East & North Africa'},\n", " 'adminregion': {'id': 'MNA',\n", " 'iso2code': 'XQ',\n", " 'value': 'Middle East & North Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Cairo',\n", " 'longitude': '31.2461',\n", " 'latitude': '30.0982'},\n", " {'id': 'FSM',\n", " 'iso2Code': 'FM',\n", " 'name': 'Micronesia, Fed. Sts.',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Palikir',\n", " 'longitude': '158.185',\n", " 'latitude': '6.91771'},\n", " {'id': 'GHA',\n", " 'iso2Code': 'GH',\n", " 'name': 'Ghana',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Accra',\n", " 'longitude': '-0.20795',\n", " 'latitude': '5.57045'},\n", " {'id': 'HND',\n", " 'iso2Code': 'HN',\n", " 'name': 'Honduras',\n", " 'region': {'id': 'LCN',\n", " 'iso2code': 'ZJ',\n", " 'value': 'Latin America & Caribbean '},\n", " 'adminregion': {'id': 'LAC',\n", " 'iso2code': 'XJ',\n", " 'value': 'Latin America & Caribbean (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Tegucigalpa',\n", " 'longitude': '-87.4667',\n", " 'latitude': '15.1333'},\n", " {'id': 'IDN',\n", " 'iso2Code': 'ID',\n", " 'name': 'Indonesia',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Jakarta',\n", " 'longitude': '106.83',\n", " 'latitude': '-6.19752'},\n", " {'id': 'IND',\n", " 'iso2Code': 'IN',\n", " 'name': 'India',\n", " 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'New Delhi',\n", " 'longitude': '77.225',\n", " 'latitude': '28.6353'},\n", " {'id': 'KEN',\n", " 'iso2Code': 'KE',\n", " 'name': 'Kenya',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Nairobi',\n", " 'longitude': '36.8126',\n", " 'latitude': '-1.27975'},\n", " {'id': 'KGZ',\n", " 'iso2Code': 'KG',\n", " 'name': 'Kyrgyz Republic',\n", " 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'},\n", " 'adminregion': {'id': 'ECA',\n", " 'iso2code': '7E',\n", " 'value': 'Europe & Central Asia (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Bishkek',\n", " 'longitude': '74.6057',\n", " 'latitude': '42.8851'},\n", " {'id': 'KHM',\n", " 'iso2Code': 'KH',\n", " 'name': 'Cambodia',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Phnom Penh',\n", " 'longitude': '104.874',\n", " 'latitude': '11.5556'},\n", " {'id': 'KIR',\n", " 'iso2Code': 'KI',\n", " 'name': 'Kiribati',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Tarawa',\n", " 'longitude': '172.979',\n", " 'latitude': '1.32905'},\n", " {'id': 'LAO',\n", " 'iso2Code': 'LA',\n", " 'name': 'Lao PDR',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Vientiane',\n", " 'longitude': '102.177',\n", " 'latitude': '18.5826'},\n", " {'id': 'LSO',\n", " 'iso2Code': 'LS',\n", " 'name': 'Lesotho',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Maseru',\n", " 'longitude': '27.7167',\n", " 'latitude': '-29.5208'},\n", " {'id': 'MAR',\n", " 'iso2Code': 'MA',\n", " 'name': 'Morocco',\n", " 'region': {'id': 'MEA',\n", " 'iso2code': 'ZQ',\n", " 'value': 'Middle East & North Africa'},\n", " 'adminregion': {'id': 'MNA',\n", " 'iso2code': 'XQ',\n", " 'value': 'Middle East & North Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Rabat',\n", " 'longitude': '-6.8704',\n", " 'latitude': '33.9905'},\n", " {'id': 'MDA',\n", " 'iso2Code': 'MD',\n", " 'name': 'Moldova',\n", " 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'},\n", " 'adminregion': {'id': 'ECA',\n", " 'iso2code': '7E',\n", " 'value': 'Europe & Central Asia (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Chisinau',\n", " 'longitude': '28.8497',\n", " 'latitude': '47.0167'},\n", " {'id': 'MMR',\n", " 'iso2Code': 'MM',\n", " 'name': 'Myanmar',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Naypyidaw',\n", " 'longitude': '95.9562',\n", " 'latitude': '21.914'},\n", " {'id': 'MNG',\n", " 'iso2Code': 'MN',\n", " 'name': 'Mongolia',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Ulaanbaatar',\n", " 'longitude': '106.937',\n", " 'latitude': '47.9129'},\n", " {'id': 'MRT',\n", " 'iso2Code': 'MR',\n", " 'name': 'Mauritania',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Nouakchott',\n", " 'longitude': '-15.9824',\n", " 'latitude': '18.2367'},\n", " {'id': 'NGA',\n", " 'iso2Code': 'NG',\n", " 'name': 'Nigeria',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Abuja',\n", " 'longitude': '7.48906',\n", " 'latitude': '9.05804'},\n", " {'id': 'NIC',\n", " 'iso2Code': 'NI',\n", " 'name': 'Nicaragua',\n", " 'region': {'id': 'LCN',\n", " 'iso2code': 'ZJ',\n", " 'value': 'Latin America & Caribbean '},\n", " 'adminregion': {'id': 'LAC',\n", " 'iso2code': 'XJ',\n", " 'value': 'Latin America & Caribbean (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Managua',\n", " 'longitude': '-86.2734',\n", " 'latitude': '12.1475'},\n", " {'id': 'PAK',\n", " 'iso2Code': 'PK',\n", " 'name': 'Pakistan',\n", " 'region': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'adminregion': {'id': 'SAS', 'iso2code': '8S', 'value': 'South Asia'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Islamabad',\n", " 'longitude': '72.8',\n", " 'latitude': '30.5167'},\n", " {'id': 'PHL',\n", " 'iso2Code': 'PH',\n", " 'name': 'Philippines',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Manila',\n", " 'longitude': '121.035',\n", " 'latitude': '14.5515'},\n", " {'id': 'PNG',\n", " 'iso2Code': 'PG',\n", " 'name': 'Papua New Guinea',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Port Moresby',\n", " 'longitude': '147.194',\n", " 'latitude': '-9.47357'},\n", " {'id': 'PSE',\n", " 'iso2Code': 'PS',\n", " 'name': 'West Bank and Gaza',\n", " 'region': {'id': 'MEA',\n", " 'iso2code': 'ZQ',\n", " 'value': 'Middle East & North Africa'},\n", " 'adminregion': {'id': 'MNA',\n", " 'iso2code': 'XQ',\n", " 'value': 'Middle East & North Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'LNX', 'iso2code': 'XX', 'value': 'Not classified'},\n", " 'capitalCity': '',\n", " 'longitude': '',\n", " 'latitude': ''},\n", " {'id': 'SDN',\n", " 'iso2Code': 'SD',\n", " 'name': 'Sudan',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Khartoum',\n", " 'longitude': '32.5363',\n", " 'latitude': '15.5932'},\n", " {'id': 'SEN',\n", " 'iso2Code': 'SN',\n", " 'name': 'Senegal',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Dakar',\n", " 'longitude': '-17.4734',\n", " 'latitude': '14.7247'},\n", " {'id': 'SLB',\n", " 'iso2Code': 'SB',\n", " 'name': 'Solomon Islands',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Honiara',\n", " 'longitude': '159.949',\n", " 'latitude': '-9.42676'},\n", " {'id': 'SLV',\n", " 'iso2Code': 'SV',\n", " 'name': 'El Salvador',\n", " 'region': {'id': 'LCN',\n", " 'iso2code': 'ZJ',\n", " 'value': 'Latin America & Caribbean '},\n", " 'adminregion': {'id': 'LAC',\n", " 'iso2code': 'XJ',\n", " 'value': 'Latin America & Caribbean (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'San Salvador',\n", " 'longitude': '-89.2073',\n", " 'latitude': '13.7034'},\n", " {'id': 'STP',\n", " 'iso2Code': 'ST',\n", " 'name': 'Sao Tome and Principe',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Sao Tome',\n", " 'longitude': '6.6071',\n", " 'latitude': '0.20618'},\n", " {'id': 'SWZ',\n", " 'iso2Code': 'SZ',\n", " 'name': 'Eswatini',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Mbabane',\n", " 'longitude': '31.4659',\n", " 'latitude': '-26.5225'},\n", " {'id': 'TLS',\n", " 'iso2Code': 'TL',\n", " 'name': 'Timor-Leste',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Dili',\n", " 'longitude': '125.567',\n", " 'latitude': '-8.56667'},\n", " {'id': 'TUN',\n", " 'iso2Code': 'TN',\n", " 'name': 'Tunisia',\n", " 'region': {'id': 'MEA',\n", " 'iso2code': 'ZQ',\n", " 'value': 'Middle East & North Africa'},\n", " 'adminregion': {'id': 'MNA',\n", " 'iso2code': 'XQ',\n", " 'value': 'Middle East & North Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Tunis',\n", " 'longitude': '10.21',\n", " 'latitude': '36.7899'},\n", " {'id': 'UKR',\n", " 'iso2Code': 'UA',\n", " 'name': 'Ukraine',\n", " 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'},\n", " 'adminregion': {'id': 'ECA',\n", " 'iso2code': '7E',\n", " 'value': 'Europe & Central Asia (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Kiev',\n", " 'longitude': '30.5038',\n", " 'latitude': '50.4536'},\n", " {'id': 'UZB',\n", " 'iso2Code': 'UZ',\n", " 'name': 'Uzbekistan',\n", " 'region': {'id': 'ECS', 'iso2code': 'Z7', 'value': 'Europe & Central Asia'},\n", " 'adminregion': {'id': 'ECA',\n", " 'iso2code': '7E',\n", " 'value': 'Europe & Central Asia (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Tashkent',\n", " 'longitude': '69.269',\n", " 'latitude': '41.3052'},\n", " {'id': 'VNM',\n", " 'iso2Code': 'VN',\n", " 'name': 'Vietnam',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Hanoi',\n", " 'longitude': '105.825',\n", " 'latitude': '21.0069'},\n", " {'id': 'VUT',\n", " 'iso2Code': 'VU',\n", " 'name': 'Vanuatu',\n", " 'region': {'id': 'EAS', 'iso2code': 'Z4', 'value': 'East Asia & Pacific'},\n", " 'adminregion': {'id': 'EAP',\n", " 'iso2code': '4E',\n", " 'value': 'East Asia & Pacific (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Port-Vila',\n", " 'longitude': '168.321',\n", " 'latitude': '-17.7404'},\n", " {'id': 'ZMB',\n", " 'iso2Code': 'ZM',\n", " 'name': 'Zambia',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDX', 'iso2code': 'XI', 'value': 'IDA'},\n", " 'capitalCity': 'Lusaka',\n", " 'longitude': '28.2937',\n", " 'latitude': '-15.3982'},\n", " {'id': 'ZWE',\n", " 'iso2Code': 'ZW',\n", " 'name': 'Zimbabwe',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IDB', 'iso2code': 'XH', 'value': 'Blend'},\n", " 'capitalCity': 'Harare',\n", " 'longitude': '31.0672',\n", " 'latitude': '-17.8312'}]]"]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["import requests\n", "data_json = requests.get(\"http://api.worldbank.org/v2/countries?incomeLevel=LMC&format=json\").json()\n", "data_json"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"data": {"text/plain": ["[{'page': 2, 'pages': 1, 'per_page': '50', 'total': 47}, []]"]}, "execution_count": 4, "metadata": {}, "output_type": "execute_result"}], "source": ["data_json[0]\n", "# On voit qu'il y a nous manque des informations : \n", "# il y a un total de 52 \u00e9l\u00e9ments\n", "data_json_page_2 = requests.get(\"http://api.worldbank.org/v2/countries?incomeLevel=LMC&format=json&page=2\").json()\n", "data_json_page_2"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'id': 'AGO',\n", " 'iso2Code': 'AO',\n", " 'name': 'Angola',\n", " 'region': {'id': 'SSF', 'iso2code': 'ZG', 'value': 'Sub-Saharan Africa '},\n", " 'adminregion': {'id': 'SSA',\n", " 'iso2code': 'ZF',\n", " 'value': 'Sub-Saharan Africa (excluding high income)'},\n", " 'incomeLevel': {'id': 'LMC',\n", " 'iso2code': 'XN',\n", " 'value': 'Lower middle income'},\n", " 'lendingType': {'id': 'IBD', 'iso2code': 'XF', 'value': 'IBRD'},\n", " 'capitalCity': 'Luanda',\n", " 'longitude': '13.242',\n", " 'latitude': '-8.81155'}"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["# pour obtenir une observation \n", "# on voit dans l'objet que l'\u00e9l\u00e9ment 0 correspond \u00e0 des informations sur les pages \n", "data_json[1][0]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Faire appel \u00e0 l'API de Tastekid\n", "\n", "La Banque Mondiale c'\u00e9tait 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.\n", "\n", "Pour cela, il faut commencer par cr\u00e9er un compte : "]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": ["import os\n", "from pyquickhelper.loghelper import get_password\n", "key = get_password(\"tastekid\", \"ensae_teaching_cs,key\")\n", "if key is None:\n", " raise ValueError(\"password cannot be None.\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Pour demander \u00e0 l'API quels sont les oeuvres similaires \u00e0 Pulp Fiction, nous utilisons la requ\u00eate suivante"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": ["url = \"https://tastedive.com/api/similar?q=pulp+fiction&info=1&k={}\".format(key)\n", "recommandations_res = requests.get(url)\n", "if \"401 Unauthorized\" in recommandations_res.text:\n", " print(\"Le site tastekid n'accepte pas les requ\u00eates non authentifi\u00e9e.\")\n", " print(recommandations_res.text)\n", " recommandations_res = None\n", "if recommandations_res is not None:\n", " try:\n", " recommandations = recommandations_res.json()\n", " except Exception as e:\n", " print(e)\n", " # Parfois le format json est mal form\u00e9. On regarde pourquoi.\n", " print()\n", " raise Exception(recommandations_res.text) from e"]}, {"cell_type": "code", "execution_count": 7, "metadata": {"scrolled": false}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'Pulp 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 title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino 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.', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Fight Club', 'Type': 'movie', 'wTeaser': 'Fight Club is a 1999 American film directed by David Fincher and starring Brad Pitt, Edward Norton, and Helena Bonham Carter. It is based on the 1996 novel of the same name by Chuck Palahniuk. Norton plays the unnamed narrator, who is discontented with his white-collar job. He forms a \"fight club\" with soap salesman Tyler Durden (Pitt), and becomes embroiled in a relationship with him and a destitute woman, Marla Singer (Bonham Carter).Palahniuk\\'s novel was optioned by Fox 2000 Pictures producer Laura Ziskin, who hired Jim Uhls to write the film adapta\n"]}], "source": ["if recommandations_res is not None:\n", " print(str(recommandations)[:2000])"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": ["[{'Name': 'Pulp Fiction',\n", " 'Type': 'movie',\n", " 'wTeaser': 'Pulp 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 title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino 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)',\n", " 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY',\n", " 'yID': 's7EdQ4FqbhY'}]"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["# on nous rappelle les informations sur l'\u00e9lement que l'on recherche : Pulp Fiction\n", "recommandations['Similar']['Info']"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Fight Club movie\n", "Seven movie\n", "The Silence Of The Lambs movie\n", "The Matrix movie\n", "Inglourious Basterds movie\n", "American History X movie\n", "Django Unchained movie\n", "Reservoir Dogs movie\n", "The Shawshank Redemption movie\n", "Memento movie\n", "One Flew Over The Cuckoo's Nest movie\n", "The Godfather movie\n", "The Dark Knight movie\n", "Forrest Gump movie\n", "Goodfellas movie\n", "L\u00e9on: The Professional movie\n", "The Usual Suspects movie\n", "The Green Mile movie\n", "The Shining movie\n", "The Departed movie\n"]}], "source": ["# on nous donnes des livres / filmes proches selon le gout des gens\n", "for element in recommandations['Similar']['Results'] : \n", " print(element['Name'],element['Type'])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["On peut aussi ne demander que des films, on ajoute juste l'option type = movies dans l'url"]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["{'Similar': {'Info': [{'Name': 'Pulp Fiction', 'Type': 'movie', 'wTeaser': 'Pulp 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 title refers to the pulp magazines and hardboiled crime novels popular during the mid-20th century, known for their graphic violence and punchy dialogue.Tarantino 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.', 'wUrl': 'http://en.wikipedia.org/wiki/Pulp_Fiction_(movie)', 'yUrl': 'https://www.youtube-nocookie.com/embed/s7EdQ4FqbhY', 'yID': 's7EdQ4FqbhY'}], 'Results': [{'Name': 'Fight Club', 'Type': 'movie', 'wTeaser': 'Fight Club is a 1999 American film directed by David Fincher and starring Brad Pitt, Edward Norton, and Helena Bonham Carter. It is based on the 1996 novel of the same name by Chuck Palahniuk. Norton plays the unnamed narrator, who is discontented with his white-collar job. He forms a \"fight club\" with soap salesman Tyler Durden (Pitt), and becomes embroiled in a relationship with him and a destitute woman, Marla Singer (Bonham Carter).Palahniuk\\'s novel was optioned by Fox 2000 Pictures producer Laura Ziskin, who hired Jim Uhls to write the film adapta\n"]}], "source": ["recommandations_films = requests.get(\"https://tastedive.com/api/similar?q=pulp+fiction&type=movie&info=1&k={}\"\n", " .format(key)).json()\n", "print(str(recommandations_films)[:2000])"]}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["Fight Club movie\n", "Seven movie\n", "The Silence Of The Lambs movie\n", "The Matrix movie\n", "Inglourious Basterds movie\n", "American History X movie\n", "Django Unchained movie\n", "Reservoir Dogs movie\n", "The Shawshank Redemption movie\n", "Memento movie\n", "One Flew Over The Cuckoo's Nest movie\n", "The Godfather movie\n", "The Dark Knight movie\n", "Forrest Gump movie\n", "Goodfellas movie\n", "L\u00e9on: The Professional movie\n", "The Usual Suspects movie\n", "The Green Mile movie\n", "The Shining movie\n", "The Departed movie\n"]}], "source": ["# on nous donnes des livres / filmes proches selon le gout des gens\n", "for element in recommandations_films['Similar']['Results'] : \n", " print(element['Name'],element['Type'])"]}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["American History X movie\n", "Pulp Fiction movie\n", "The Usual Suspects movie\n", "No Country For Old Men movie\n", "Full Metal Jacket movie\n", "Snatch movie\n", "Inglourious Basterds movie\n", "Memento movie\n", "The Big Lebowski movie\n", "Fight Club movie\n", "Seven movie\n", "Goodfellas movie\n", "Lock, Stock And Two Smoking Barrels movie\n", "American Beauty movie\n", "Fargo movie\n", "Kill Bill movie\n", "One Flew Over The Cuckoo's Nest movie\n", "A Clockwork Orange movie\n", "Scarface movie\n", "The Departed movie\n"]}], "source": ["film_suivant = \"Reservoir Dogs\"\n", "recommandations_suivantes_films = requests.get(\n", " \"https://tastedive.com/api/similar?q={}&type=movie&info=1&k={}\"\n", " .format(film_suivant, key)).json()\n", "# on nous donnes des livres / filmes proches selon le gout des gens\n", "for element in recommandations_suivantes_films['Similar']['Results'] : \n", " print(element['Name'],element['Type'])"]}, {"cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [{"data": {"text/plain": ["({'American History X',\n", " 'Fight Club',\n", " 'Goodfellas',\n", " 'Inglourious Basterds',\n", " 'Memento',\n", " \"One Flew Over The Cuckoo's Nest\",\n", " 'Seven',\n", " 'The Departed',\n", " 'The Usual Suspects'},\n", " 9)"]}, "execution_count": 14, "metadata": {}, "output_type": "execute_result"}], "source": ["## On peut ensuite comparer les films communs aux deux recherches \n", "liste1 = [element['Name'] for element in recommandations_films['Similar']['Results'] ]\n", "liste2 = [element['Name'] for element in recommandations_suivantes_films['Similar']['Results'] ]\n", "films_commun = set(liste1).intersection(liste2)\n", "films_commun, len(films_commun)"]}, {"cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [{"data": {"text/plain": ["['The Silence Of The Lambs',\n", " 'The Matrix',\n", " 'Django Unchained',\n", " 'Reservoir Dogs',\n", " 'The Shawshank Redemption',\n", " 'The Godfather',\n", " 'The Dark Knight',\n", " 'Forrest Gump',\n", " 'L\u00e9on: The Professional',\n", " 'The Green Mile',\n", " 'The Shining',\n", " 'Pulp Fiction',\n", " 'No Country For Old Men',\n", " 'Full Metal Jacket',\n", " 'Snatch',\n", " 'The Big Lebowski',\n", " 'Lock, Stock And Two Smoking Barrels',\n", " 'American Beauty',\n", " 'Fargo',\n", " 'Kill Bill',\n", " 'A Clockwork Orange',\n", " 'Scarface']"]}, "execution_count": 15, "metadata": {}, "output_type": "execute_result"}], "source": ["films_non_partages = [f for f in liste1 if f not in liste2] + [f for f in liste2 if f not in liste1]\n", "films_non_partages"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A partir de ces requ\u00eates, on peut facilement construire le r\u00e9seau des films que les gens aiment en utilisant le package [networkx](https://networkx.github.io/)."]}, {"cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": []}], "metadata": {"anaconda-cloud": {}, "kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}}, "nbformat": 4, "nbformat_minor": 2}