diff --git a/.gitignore b/.gitignore index d435c64..0529cd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ .idea *.html -maps/* \ No newline at end of file +maps/* + +venv/ +.venv/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9820450 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Export Google Maps Saved Places as KML + +This is a neat little tool to export your saved places from Google Maps as KML. I myself (@heyarne) did almost nothing. All the credit goes to @endolith and @ngzhian. I just added a requirements.txt and used the most up to date version. + +## Manual + +1. Go to Google Bookmarks: https://www.google.com/bookmarks/ +2. On the bottom left, click "Export bookmarks": https://www.google.com/bookmarks/bookmarks.html?hl=en +3. Install script dependencies `pip install -r requirements.txt` +4. After downloading the html file, run this script on it to generate a KML file per bookmark label: `python bookmarkstokml.py GoogleBookmarks.html` + +## Disclaimer + +It's hacky and doesn't work on all of them, but it kinda works. +Tested with bookmarks exported on January 29, 2017 and python version 3.5.2. diff --git a/bookmarkstokml.py b/bookmarkstokml.py index a20066b..6f85352 100644 --- a/bookmarkstokml.py +++ b/bookmarkstokml.py @@ -8,13 +8,16 @@ import simplekml from urllib.request import FancyURLopener +from urllib.parse import quote_plus, urlparse, parse_qsl import os import re import sys import time +import random -coords_in_content = re.compile('\/@(-?\d+\.\d+),(-?\d+\.\d+),') +coords_in_content = re.compile(r'\/@(-?\d+\.\d+),(-?\d+\.\d+),') +coords_in_description = re.compile(r'^(-?\d+\.\d+),(-?\d+\.\d+)$') user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36' filename = r'GoogleBookmarks.html' @@ -27,6 +30,7 @@ doc = document_fromstring(data) + class Browser(FancyURLopener): version = user_agent @@ -36,36 +40,46 @@ class Browser(FancyURLopener): kml = simplekml.Kml() kml.document.name = labelName - for element, attribute, url, pos in label.getparent().getnext().iterlinks(): + for element, _, url, _ in label.getparent().getnext().iterlinks(): if 'maps.google' in url: - print description = element.text or '' - print('GET {0} {1}'.format(url, description.encode('UTF8'))) - browser = Browser() - - # Load map and find coordinates in source of page - sock = False - while not sock: - try: - sock = browser.open(url.replace(' ','+')) - except Exception as e: - print('Connection problem:' + repr(e)) - print('Retrying randomly between 15 and 60 seconds.') - time.sleep(random.randint(15, 60)) - - content = sock.read().decode("utf-8") - sock.close() - + latitude = longitude = None try: - coords = coords_in_content.search(content).groups() - latitude = coords[0] - longitude = coords[1] - + # check if the link itself contains the coordinate + latitude, longitude = coords_in_description.search( + description).groups() + print("Found point {0},{1} in description".format( + latitude, longitude)) except (AttributeError, IndexError): - print('[Coordinates not found: ' + str(coords) + '. Try to update "user_agent"]') - continue + safe_query = ['{0}={1}'.format(k, quote_plus(v)) + for (k, v) in parse_qsl(urlparse(url).query)] + url = '{0}/?{1}'.format(url.split('/?') + [0], '&'.join(safe_query)) + + print('GET {0} {1}'.format(url, description.encode('UTF8'))) + browser = Browser() + + # Load map and find coordinates in source of page + sock = False + while not sock: + try: + sock = browser.open(url) + except Exception as e: + print('Connection problem:' + repr(e)) + print('Retrying randomly between 15 and 60 seconds.') + time.sleep(random.randint(15, 60)) + + content = sock.read().decode("utf-8") + sock.close() + + try: + latitude, longitude = coords_in_content.search( + content).groups() + except (AttributeError, IndexError): + print('[Coordinates not found: ({0},{1}).' + 'Try to update "user_agent"]'.format(latitude, longitude)) + continue - print(latitude, longitude) try: kml.newpoint(name=description, coords=[(float(longitude), float(latitude))]) diff --git a/readme.md b/readme.md deleted file mode 100644 index 58b01df..0000000 --- a/readme.md +++ /dev/null @@ -1,13 +0,0 @@ -V2016.04.17 - -Go to Google Bookmarks: https://www.google.com/bookmarks/ - -On the bottom left, click "Export bookmarks": https://www.google.com/bookmarks/bookmarks.html?hl=en - -Install script dependencies: -pip3 install simplekml lxml - -After downloading the html file, run this script on it to generate a KML file per bookmark label: -python3 bookmarkstokml.py GoogleBookmarks.html - -It's hacky and doesn't work on all of them, but it kinda works. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9cf2ea3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +lxml==3.7.2 +simplekml==1.3.0