diff --git a/.gitignore b/.gitignore index 736006e..65fa99a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .vscode **/*mt.ipynb **/*mt +.env diff --git a/challenge-2.ipynb b/challenge-2.ipynb index df5e7f0..54cf33f 100644 --- a/challenge-2.ipynb +++ b/challenge-2.ipynb @@ -10,15 +10,66 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "9b373771", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import requests\n", "import json\n", "import pandas as pd\n", - "from getpass import getpass" + "from getpass import getpass\n", + "import os\n", + "import time\n", + "from pymongo import MongoClient\n", + "from dotenv import load_dotenv\n", + "load_dotenv()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "a067bb78", + "metadata": {}, + "outputs": [], + "source": [ + "token = os.getenv(\"token\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "8ae99123", + "metadata": {}, + "outputs": [], + "source": [ + "def requests_for_foursquare (query, lat, lon, radius=None, limit=1, sort='DISTANCE'):\n", + "\n", + " url = f\"https://api.foursquare.com/v3/places/search?query={query}&ll={lat}%2C{lon}&sort={sort}&limit={limit}\"\n", + "\n", + " if radius:\n", + " url += f'&radius={radius}'\n", + "\n", + " headers = {\n", + " \"accept\": \"application/json\",\n", + " \"Authorization\": token\n", + " }\n", + " \n", + " try:\n", + " return requests.get(url, headers=headers).json()\n", + " except:\n", + " print(\"no :(\")" ] }, { @@ -31,12 +82,102 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 16, "id": "1d72f571", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'fsq_id': '556d6217498ef50c5d323048',\n", + " 'categories': [{'id': 13003,\n", + " 'name': 'Bar',\n", + " 'short_name': 'Bar',\n", + " 'plural_name': 'Bars',\n", + " 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/nightlife/pub_',\n", + " 'suffix': '.png'}},\n", + " {'id': 13035,\n", + " 'name': 'Coffee Shop',\n", + " 'short_name': 'Coffee Shop',\n", + " 'plural_name': 'Coffee Shops',\n", + " 'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/food/coffeeshop_',\n", + " 'suffix': '.png'}}],\n", + " 'chains': [],\n", + " 'closed_bucket': 'VeryLikelyOpen',\n", + " 'distance': 450,\n", + " 'geocodes': {'main': {'latitude': 41.397977, 'longitude': 2.195589},\n", + " 'roof': {'latitude': 41.397977, 'longitude': 2.195589}},\n", + " 'link': '/v3/places/556d6217498ef50c5d323048',\n", + " 'location': {'address': 'Calle de Pujades, 95',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': 'C. de Badajoz',\n", + " 'formatted_address': 'Calle de Pujades, 95 (C. de Badajoz), 08005 Barcelona',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08005'},\n", + " 'name': \"Nømad Roaster's Home\",\n", + " 'related_places': {},\n", + " 'timezone': 'Europe/Madrid'}" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "where = \"Pamplona, 96, 08018 Barcelona\"\n", + "url_geocode = f\"https://geocode.xyz/{where}?json=1\"\n", + "response = requests.get(url_geocode)\n", + "response = response.json()\n", + "\n", + "res = requests_for_foursquare(\"coffee\", response['latt'], response['longt'], limit=10)\n", + "res = res['results']\n", + "res[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "0881b7a0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'name': \"Nømad Roaster's Home\",\n", + " 'location': {'address': 'Calle de Pujades, 95',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': 'C. de Badajoz',\n", + " 'formatted_address': 'Calle de Pujades, 95 (C. de Badajoz), 08005 Barcelona',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08005'},\n", + " 'latitude': 41.397977,\n", + " 'longitude': 2.195589},\n", + " {'name': 'Three Marks Coffee',\n", + " 'location': {'address': 'Ausiàs Marc, 151',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'formatted_address': 'Ausiàs Marc, 151, 08013 Barcelona Catalunya',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08013',\n", + " 'region': 'Catalunya'},\n", + " 'latitude': 41.397168,\n", + " 'longitude': 2.183123}]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "def get_name_location(rest):\n", + " return {'name': rest['name'], 'location': rest['location'], 'latitude': rest['geocodes']['main']['latitude'], 'longitude': rest['geocodes']['main']['longitude']}\n", + "\n", + "coffes = [get_name_location(i) for i in res]\n", + "coffes[0:2]" ] }, { @@ -49,12 +190,78 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 17, "id": "fda8ddff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[{'name': \"Nømad Roaster's Home\",\n", + " 'location': {'address': 'Calle de Pujades, 95',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': 'C. de Badajoz',\n", + " 'formatted_address': 'Calle de Pujades, 95 (C. de Badajoz), 08005 Barcelona',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08005'},\n", + " 'latitude': 41.397977,\n", + " 'longitude': 2.195589},\n", + " {'name': 'Three Marks Coffee',\n", + " 'location': {'address': 'Ausiàs Marc, 151',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'formatted_address': 'Ausiàs Marc, 151, 08013 Barcelona Catalunya',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08013',\n", + " 'region': 'Catalunya'},\n", + " 'latitude': 41.397168,\n", + " 'longitude': 2.183123},\n", + " {'name': 'Brew Coffee',\n", + " 'location': {'address': 'Calle de Roger de Flor, 102',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': '',\n", + " 'formatted_address': 'Calle de Roger de Flor, 102, 08013 Barcelona Catalunya',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08013',\n", + " 'region': 'Catalunya'},\n", + " 'latitude': 41.394562,\n", + " 'longitude': 2.178425},\n", + " {'name': 'El Petit Príncep',\n", + " 'location': {'address': 'Gran Vía de Les Corts Catalanes, 677',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': 'btwn c/ Nàpols & c/ Roger de Flor',\n", + " 'formatted_address': 'Gran Vía de Les Corts Catalanes, 677 (btwn c/ Nàpols & c/ Roger de Flor), 08013 Barcelona Catalunya',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08013',\n", + " 'region': 'Catalunya'},\n", + " 'latitude': 41.396371,\n", + " 'longitude': 2.177069},\n", + " {'name': 'Federal',\n", + " 'location': {'address': 'Carrer del Taulat, 109',\n", + " 'admin_region': 'Cataluña',\n", + " 'country': 'ES',\n", + " 'cross_street': '',\n", + " 'formatted_address': 'Carrer del Taulat, 109, 08005 Barcelona Catalunya',\n", + " 'locality': 'Barcelona',\n", + " 'postcode': '08005',\n", + " 'region': 'Catalunya'},\n", + " 'latitude': 41.399656,\n", + " 'longitude': 2.205785}]" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "res = requests_for_foursquare(\"coffee\", response['latt'], response['longt'], limit=5)\n", + "res = res['results']\n", + "coffes = [get_name_location(i) for i in res]\n", + "coffes" ] }, { @@ -67,13 +274,64 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 27, "id": "1fad5153", "metadata": {}, "outputs": [], "source": [ - "def foursquare_places (venue, coordinates):\n", - " pass" + "def foursquare_places(venue, coordinates):\n", + " lat, lon = str(coordinates).split(',')\n", + " res = requests_for_foursquare (venue, lat, lon, radius = 500, sort = \"DISTANCE\", limit = 10)\n", + " \n", + " try:\n", + " data = res['results']\n", + " results = []\n", + " \n", + " for i in data:\n", + " name = i['name']\n", + " location = i['geocodes']['main']\n", + " lat = location.get('latitude')\n", + " lng = location.get('longitude')\n", + " results.append({\n", + " 'name': name,\n", + " 'lat': lat,\n", + " 'lng': lng\n", + " })\n", + " return results\n", + " except requests.exceptions.RequestException as e:\n", + " print(f\"Error: {e}\")\n", + " return []" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "018cfa34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'name': 'Espacio 88', 'lat': 41.396993, 'lng': 2.191171},\n", + " {'name': 'SKYE Coffee Co', 'lat': 41.396937, 'lng': 2.191519},\n", + " {'name': 'Syra Coffee Poblenou', 'lat': 41.396518, 'lng': 2.19417},\n", + " {'name': 'Espai Joliu', 'lat': 41.398735, 'lng': 2.195069},\n", + " {'name': 'Granja Mabel', 'lat': 41.394816, 'lng': 2.187354},\n", + " {'name': 'Eva-2', 'lat': 41.401636, 'lng': 2.189006},\n", + " {'name': 'Cafeteria Industrial', 'lat': 41.399175, 'lng': 2.195368},\n", + " {'name': 'Market Cuina Fresca', 'lat': 41.398365, 'lng': 2.195559},\n", + " {'name': \"Nømad Roaster's Home\", 'lat': 41.397977, 'lng': 2.195589},\n", + " {'name': 'Mare Meva', 'lat': 41.402167, 'lng': 2.189879}]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res = foursquare_places(\"coffee\", response['latt'] + ',' + response['longt'])\n", + "res" ] }, { @@ -86,12 +344,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "7ac5f2a8", "metadata": {}, "outputs": [], "source": [ - "# your code here" + "pd.DataFrame(res).to_json('data/coffe_shops.json')" ] }, { @@ -104,12 +362,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "85b94955", "metadata": {}, "outputs": [], "source": [ - "# your code here" + "client = MongoClient(\"localhost:27017\")\n", + "db = client[\"ironhack\"]\n", + "collection = db['ironcoffee']\n", + "for coffe in coffes:\n", + " collection.insert_one(coffe)" ] } ], @@ -117,7 +379,7 @@ "kernelspec": { "display_name": "ironhack", "language": "python", - "name": "ironhack" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -129,7 +391,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1, diff --git a/challenge-3.ipynb b/challenge-3.ipynb index 8d8d1d6..3d038c5 100644 --- a/challenge-3.ipynb +++ b/challenge-3.ipynb @@ -1,5 +1,19 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "f1bb0679", + "metadata": {}, + "outputs": [], + "source": [ + "import folium\n", + "from folium import Choropleth, Circle, Marker, Icon, Map\n", + "from folium.plugins import HeatMap, MarkerCluster\n", + "import pandas as pd\n", + "import json" + ] + }, { "cell_type": "markdown", "id": "0809e785", @@ -10,11 +24,124 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "9b373771", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
namelatlng
0Espacio 8841.3969932.191171
1SKYE Coffee Co41.3969372.191519
2Syra Coffee Poblenou41.3965182.194170
3Espai Joliu41.3987352.195069
4Granja Mabel41.3948162.187354
5Eva-241.4016362.189006
6Cafeteria Industrial41.3991752.195368
7Market Cuina Fresca41.3983652.195559
8Nømad Roaster's Home41.3979772.195589
9Mare Meva41.4021672.189879
\n", + "
" + ], + "text/plain": [ + " name lat lng\n", + "0 Espacio 88 41.396993 2.191171\n", + "1 SKYE Coffee Co 41.396937 2.191519\n", + "2 Syra Coffee Poblenou 41.396518 2.194170\n", + "3 Espai Joliu 41.398735 2.195069\n", + "4 Granja Mabel 41.394816 2.187354\n", + "5 Eva-2 41.401636 2.189006\n", + "6 Cafeteria Industrial 41.399175 2.195368\n", + "7 Market Cuina Fresca 41.398365 2.195559\n", + "8 Nømad Roaster's Home 41.397977 2.195589\n", + "9 Mare Meva 41.402167 2.189879" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = pd.read_json('data/coffe_shops.json')\n", + "data" + ] }, { "cell_type": "markdown", @@ -26,22 +153,254 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "644bbf42", "metadata": {}, "outputs": [], "source": [ - "import geopandas as gdp\n", - "from cartoframes.viz import Map, Layer, popup_element" + "# import geopandas as gdp\n", + "# from cartoframes.viz import Map, Layer, popup_element" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "0938676d", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
Make this Notebook Trusted to load map: File -> Trust Notebook
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iron_lat = '41.3977461'\n", + "iron_long = '2.1876744'\n", + "map_ = Map(location = [iron_lat, iron_long], zoom_start=16)\n", + "\n", + "for idx, row in data.iterrows():\n", + " marker = folium.Marker(location = [row['lat'], row['lng']], tooltip=row['name'])\n", + " marker.add_to(map_)\n", + "\n", + "map_" + ] }, { "cell_type": "markdown", @@ -56,7 +415,7 @@ "kernelspec": { "display_name": "ironhack", "language": "python", - "name": "ironhack" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -68,7 +427,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1, diff --git a/data/coffe_shops.json b/data/coffe_shops.json new file mode 100644 index 0000000..4ecf401 --- /dev/null +++ b/data/coffe_shops.json @@ -0,0 +1 @@ +{"name":{"0":"Espacio 88","1":"SKYE Coffee Co","2":"Syra Coffee Poblenou","3":"Espai Joliu","4":"Granja Mabel","5":"Eva-2","6":"Cafeteria Industrial","7":"Market Cuina Fresca","8":"N\u00f8mad Roaster's Home","9":"Mare Meva"},"lat":{"0":41.396993,"1":41.396937,"2":41.396518,"3":41.398735,"4":41.394816,"5":41.401636,"6":41.399175,"7":41.398365,"8":41.397977,"9":41.402167},"lng":{"0":2.191171,"1":2.191519,"2":2.19417,"3":2.195069,"4":2.187354,"5":2.189006,"6":2.195368,"7":2.195559,"8":2.195589,"9":2.189879}} \ No newline at end of file