{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import requests" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import utils\n", "\n", "utils.load_env()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `GooglePlacesTool` was deprecated in LangChain 0.0.33 and will be removed in 0.3.0. An updated version of the class exists in the langchain-google-community package and should be used instead. To use it run `pip install -U langchain-google-community` and import as `from langchain_google_community import GooglePlacesTool`.\n", " warn_deprecated(\n", "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `GooglePlacesAPIWrapper` was deprecated in LangChain 0.0.33 and will be removed in 0.3.0. An updated version of the class exists in the langchain-google-community package and should be used instead. To use it run `pip install -U langchain-google-community` and import as `from langchain_google_community import GooglePlacesAPIWrapper`.\n", " warn_deprecated(\n" ] }, { "data": { "text/plain": [ "'1. Fashion Island\\nAddress: 587-589/7-9, Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJd57HEQRjHTERG_xSTcdQoYs\\nPhone: 02 947 5000\\nWebsite: http://www.fashionisland.co.th/\\n\\n\\n2. Food Island-Fashion Island\\nAddress: 5/5-6 หมู่ 5 Fashion Island, Ram Inthra Rd, Khwaeng Khan Na Yao, บางเขน Krung Thep Maha Nakhon 10220, Thailand\\nGoogle place ID: ChIJxVlvKA9jHTERAaDXThEk0sU\\nPhone: Unknown\\nWebsite: https://www.fashionisland.co.th/\\n\\n\\n3. Grand Station @ Fashion Island\\nAddress: Fashion Island Shopping Mall คันนายาว Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJgYJ0xNxjHTERqh7EMAXGflk\\nPhone: 02 947 5000\\nWebsite: https://www.fashionisland.co.th/about-us/grandstation/\\n\\n\\n4. Central Department Store Fashion Island\\nAddress: 591 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJU51oO09jHTERkAkthlfrvMQ\\nPhone: 02 947 5320\\nWebsite: http://www.central.co.th/\\n\\n\\n5. Fashion Island\\nAddress: 401 Newport Center Dr, Newport Beach, CA 92660, USA\\nGoogle place ID: ChIJUetYrofg3IARvxarZNginsY\\nPhone: (949) 721-2000\\nWebsite: https://www.fashionisland.com/?utm_source=soci&utm_medium=organic&utm_campaign=gmb_soci_listing\\n\\n\\n6. Pomelo Fashion Island\\nAddress: Unit GSSH101, 1st Floor, Fashion Island Shopping Mall, 587-589, Ramindra Rd, แขวงคันนายาว เขตคันนายาว กรุงเทพมหานคร 10230, Thailand\\nGoogle place ID: ChIJaYz1YRFjHTERHcxGtqrypsk\\nPhone: 02 000 6811\\nWebsite: https://www.pomelofashion.com/\\n\\n\\n7. Haborland Fashion Island\\nAddress: ศูนย์การค้า แฟชั่นไอส์แลนด์ ชั้น3, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJ7wF25A5jHTERyBbgZ24hjGU\\nPhone: 065 848 1000\\nWebsite: https://harborlandgroup.com/\\n\\n\\n8. IF Store art & chara สาขา Fasion Island\\nAddress: KI B006-B007 โซนแกรนด์สเตชั่น ซอย 7 ศูนย์การค้าแฟชั่นไอส์แลนด์ ชั้น 2 587,589, 589 7 9 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJx5zaF3RjHTER8ROSKii-tVQ\\nPhone: 063 232 6356\\nWebsite: https://shopee.co.th/ifstore_art\\n\\n\\n9. Panya Ramintra\\nAddress: Khan Na Yao, Bangkok 10230, Thailand\\nGoogle place ID: ChIJ9e97xwhjHTERR-MWDuKtUYY\\nPhone: Unknown\\nWebsite: Unknown\\n\\n\\n10. ร้านชุดชั้นใน GIRLY SECRET @Fasion Island\\nAddress: GIRLY SECRET แฟชั่นไอส์แลนด์ ชั้น2 โซนแกรนด์เสตชั่น สตรีท 11 Lat Phrao Road Khwaeng Khan Na Yao, คันนายาว Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJMRAHkpKd4jARZBQhL3tzF28\\nPhone: 092 697 9698\\nWebsite: Unknown\\n\\n\\n11. OkurimonoJapan! (Fashion Island)\\nAddress: Level 2, Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJUTgHRlBjHTERw3I3e-uH7hI\\nPhone: 080 594 4644\\nWebsite: Unknown\\n\\n\\n12. HomePro Fashion Island\\nAddress: 589, 10 Ram Inthra Rd, Khwaeng Khan Na Yao, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJEyIpYLBjHTERibQeHSiSERE\\nPhone: 02 029 7700\\nWebsite: https://lin.ee/nfKDS60\\n\\n\\n13. Bake a Wish (Fashion Island)\\nAddress: ชั้น B, Fashion Island, 587 Ram Inthra Rd, รามอินทรา, Khet Khan Na Yao, Krung Thep Maha Nakhon 10230, Thailand\\nGoogle place ID: ChIJfyU40Q5jHTERTKLwRhRWk-o\\nPhone: 064 121 7146\\nWebsite: Unknown\\n\\n\\n14. Opposite Fashion Island\\nAddress: Khan Na Yao, Bangkok 10230, Thailand\\nGoogle place ID: ChIJV8YMrwhjHTERTd8eEupPwZY\\nPhone: Unknown\\nWebsite: Unknown\\n\\n'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain_community.tools import GooglePlacesTool\n", "\n", "places = GooglePlacesTool()\n", "places.run(\"fasion islands\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "#find places id\n", "#near by search" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'candidates': [{'formatted_address': '444 ถ. พญาไท แขวงวังใหม่ เขตปทุมวัน กรุงเทพมหานคร 10330 ไทย', 'geometry': {'location': {'lat': 13.744677, 'lng': 100.5295593}, 'viewport': {'northeast': {'lat': 13.74598387989272, 'lng': 100.5309299798927}, 'southwest': {'lat': 13.74328422010728, 'lng': 100.5282303201073}}}, 'name': 'เอ็ม บี เค เซ็นเตอร์', 'opening_hours': {'open_now': True}, 'rating': 4.3}], 'status': 'OK'}\n" ] } ], "source": [ "def find_place_from_text(input_text, location=None, radius=2000):\n", " # Retrieve the API key from environment variables\n", " api_key = os.getenv('GPLACES_API_KEY')\n", "\n", " if not api_key:\n", " raise ValueError(\"API key not found. Please set the GOOGLE_MAPS_API_KEY environment variable.\")\n", "\n", " # Define the endpoint URL\n", " url = \"https://maps.googleapis.com/maps/api/place/findplacefromtext/json\"\n", "\n", " # Define the parameters for the request\n", " params = {\n", " 'fields': 'formatted_address,name,rating,opening_hours,geometry',\n", " 'input': input_text,\n", " 'inputtype': 'textquery',\n", " 'key': api_key\n", " }\n", " \n", " params['locationbias'] = f'circle:{radius}@{location}' if location is not None and radius is not None else None\n", "\n", " # Make the request to the Google Maps API\n", " response = requests.get(url, params=params)\n", "\n", " # Check if the request was successful\n", " if response.status_code == 200:\n", " return response.json() # Return the JSON response\n", " else:\n", " response.raise_for_status() # Raise an exception for HTTP errors\n", "\n", "# Example usage:\n", "location = \"15.87,100.9925\" # Latitude and Longitude\n", "input_text = \"มาบุญครอง\"\n", "result = find_place_from_text(input_text, location)\n", "\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Coordinates: 13.744677,100.5295593\n" ] } ], "source": [ "def find_location(input_text:str, location:str=None, radius=10000):\n", " # Call the find_place_from_text function to get the location data\n", " data = find_place_from_text(input_text, location, radius)\n", "\n", " # Extract the latitude and longitude from the response\n", " candidates = data.get('candidates', [])\n", " if len(candidates)==0:\n", " raise ValueError(\"No location found.\")\n", "\n", " # Assuming we're taking the first candidate\n", " geometry = candidates[0].get('geometry', {})\n", " location = geometry.get('location', {})\n", "\n", " latitude = location.get('lat')\n", " longitude = location.get('lng')\n", "\n", " if latitude is None or longitude is None:\n", " raise ValueError(\"Latitude or Longitude not found in the response.\")\n", "\n", " # Return the latitude and longitude as a formatted string\n", " return f\"{latitude},{longitude}\"\n", "\n", "\n", "location = \"15.87,100.9925\" # Latitude and Longitude\n", "input_text = \"มาบุญครอง\"\n", "coordinates = find_location(input_text, location)\n", "print(f\"Coordinates: {coordinates}\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def nearby_search(keyword:str, location:str, radius=2000, place_type=None):\n", " # Retrieve the API key from environment variables\n", " api_key = os.getenv('GPLACES_API_KEY')\n", "\n", " if not api_key:\n", " raise ValueError(\"API key not found. Please set the GOOGLE_MAPS_API_KEY environment variable.\")\n", "\n", " # Define the endpoint URL\n", " url = \"https://maps.googleapis.com/maps/api/place/nearbysearch/json\"\n", "\n", " # Set up the parameters for the request\n", " params = {\n", " 'keyword': keyword,\n", " 'location': location,\n", " 'radius': radius,\n", " 'type': place_type,\n", " 'key': api_key,\n", " \"rankPreference\": \"DISTANCE\"\n", " }\n", "\n", " # Send the GET request to the Google Maps API\n", " response = requests.get(url, params=params)\n", "\n", " # Check if the request was successful\n", " if response.status_code != 200:\n", " raise Exception(f\"Error with request: {response.status_code}, {response.text}\")\n", "\n", " # Parse the JSON response\n", " data = response.json()\n", " results = data['results']\n", "\n", " # search into next page\n", " while data.get('next_page_token', False):\n", " params = {'next_page_token': data['next_page_token']}\n", " response = requests.get(url, params=params)\n", " \n", " if response.status_code != 200:\n", " raise Exception(f\"Error with request: {response.status_code}, {response.text}\")\n", " \n", " data = response.json()\n", " \n", " results.append(data['results'])\n", " \n", "\n", " # Return the response data\n", " return results\n", "\n", "\n", "# Example usage\n", "location = \"13.744677,100.5295593\" # Latitude and Longitude\n", "keyword = \"ร้านอาหาร\"\n", "search_results = nearby_search(keyword, location)\n", "len(search_results)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }