Skip to content
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
*.html
maps/*
maps/*

venv/
.venv/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
66 changes: 40 additions & 26 deletions bookmarkstokml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -27,6 +30,7 @@

doc = document_fromstring(data)


class Browser(FancyURLopener):
version = user_agent

Expand All @@ -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))])
Expand Down
13 changes: 0 additions & 13 deletions readme.md

This file was deleted.

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lxml==3.7.2
simplekml==1.3.0