From dcab9228de307e1a361716d476f6d24c535dc29e Mon Sep 17 00:00:00 2001
From: Mysterious-Owl <66870959+Mysterious-Owl@users.noreply.github.com>
Date: Sun, 9 Oct 2022 23:58:15 +0530
Subject: [PATCH] Added Geocoode API program
---
scripts/GeoCode API/README.md | 16 ++++++++
scripts/GeoCode API/code.py | 61 ++++++++++++++++++++++++++++
scripts/GeoCode API/config.ini | 3 ++
scripts/GeoCode API/index.html | 48 ++++++++++++++++++++++
scripts/GeoCode API/requirements.txt | 2 +
scripts/GeoCode API/where.js | 24 +++++++++++
scripts/GeoCode API/where.txt | 22 ++++++++++
7 files changed, 176 insertions(+)
create mode 100644 scripts/GeoCode API/README.md
create mode 100644 scripts/GeoCode API/code.py
create mode 100644 scripts/GeoCode API/config.ini
create mode 100644 scripts/GeoCode API/index.html
create mode 100644 scripts/GeoCode API/requirements.txt
create mode 100644 scripts/GeoCode API/where.js
create mode 100644 scripts/GeoCode API/where.txt
diff --git a/scripts/GeoCode API/README.md b/scripts/GeoCode API/README.md
new file mode 100644
index 0000000..33435d7
--- /dev/null
+++ b/scripts/GeoCode API/README.md
@@ -0,0 +1,16 @@
+# Google Maps API
+
+This code will take the name of places from where.txt and then use *forward Geocode API* to get the coordinates of that location, then the coordinates are stored in javascript file and the webpage is opened automatically, to show the pin locations on map.
+
+### Prerequisites
+
+To install configparser ```pip install configparser``` or check [here](https://pypi.org/project/configparser/)
+If you have [Google Geocode API](https://developers.google.com/maps/documentation/geocoding/overview), so use that, otherwise you can use
+[Open Cage API](https://opencagedata.com/api) (its free, 2500 requests/day).
+
+### How to run the script
+
+Enter the API key and service URL in config file (without qoutes) and the places to be marked in where.txt file, then run the script.
+
+
+
diff --git a/scripts/GeoCode API/code.py b/scripts/GeoCode API/code.py
new file mode 100644
index 0000000..5ebd635
--- /dev/null
+++ b/scripts/GeoCode API/code.py
@@ -0,0 +1,61 @@
+import urllib.request
+import urllib.parse
+import urllib.error
+import json
+import os
+import webbrowser
+import ssl
+import configparser
+
+config = configparser.ConfigParser()
+config.read('config.ini')
+
+api_key = config['keys']['api_key']
+service_url = config['keys']['service_url']
+
+# Ignore SSL certificate errors
+ctx = ssl.create_default_context()
+ctx.check_hostname = False
+ctx.verify_mode = ssl.CERT_NONE
+
+with open("where.txt") as fh, open("where.js", "w", encoding="utf-8") as where:
+ adrs = []
+ parms = {}
+ for line in fh:
+
+ address = line.strip()
+ parms["address"] = address
+ parms['key'] = api_key
+ url = service_url + urllib.parse.urlencode(parms)
+
+ if url.lower().startswith('http'):
+ req = urllib.request.Request(url)
+ else:
+ raise ValueError from None
+
+ with urllib.request.urlopen(req, context=ctx) as resp:
+
+ data = resp.read().decode()
+
+ try:
+ js = json.loads(data)
+ except Exception as e:
+ print(f"{e}: {data}")
+ continue
+
+ try:
+ adrs.append([js['results'][0]['geometry']['lat'],
+ js['results'][0]['geometry']['lng'],
+ js['results'][0]['formatted']])
+ print('Retrieved ', url)
+ except Exception as e:
+ print(f"Not Found: {e}: {line.strip()}")
+
+ print("\nOpening Webpage")
+
+ where.write("myData = [\n")
+ for item in adrs:
+ where.write(f"[{str(item[0])}, {str(item[1])}, '{str(item[2])}' ], \n")
+ where.write("];\n")
+
+webbrowser.open('file://' + os.path.realpath("index.html"))
diff --git a/scripts/GeoCode API/config.ini b/scripts/GeoCode API/config.ini
new file mode 100644
index 0000000..953b7bb
--- /dev/null
+++ b/scripts/GeoCode API/config.ini
@@ -0,0 +1,3 @@
+[keys]
+service_url=https://api.opencagedata.com/geocode/v1/json?q=
+api_key=
\ No newline at end of file
diff --git a/scripts/GeoCode API/index.html b/scripts/GeoCode API/index.html
new file mode 100644
index 0000000..f4970b9
--- /dev/null
+++ b/scripts/GeoCode API/index.html
@@ -0,0 +1,48 @@
+
+
About this Map
+
+This is a cool script by
+Mysterious-Owl with the use of API.
+To see the details of a marker, hover over the marker.
+