Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noemí - lab-coffee-shops #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.vscode
**/*mt.ipynb
**/*mt
.env
298 changes: 280 additions & 18 deletions challenge-2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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 :(\")"
]
},
{
Expand All @@ -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]"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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"
]
},
{
Expand All @@ -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')"
]
},
{
Expand All @@ -104,20 +362,24 @@
},
{
"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)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ironhack",
"language": "python",
"name": "ironhack"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -129,7 +391,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
"version": "3.11.5"
},
"toc": {
"base_numbering": 1,
Expand Down
Loading