Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZyX-I/5610420 to your computer and use it in GitHub Desktop.
Save ZyX-I/5610420 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
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
After downloading the html file, run this script on it to generate a KML.
"""
from __future__ import unicode_literals
from lxml.html import document_fromstring
from urllib2 import urlopen
import re
import time
import sqlite3
filename = r'GoogleBookmarks.html'
db = sqlite3.connect('labels.db')
id = 1
with open(filename) as bookmarks_file:
data = bookmarks_file.read()
# Hacky and doesn't work for all of the stars:
lat_re = re.compile('markers:[^\]]*latlng[^}]*lat:([^,]*)')
lon_re = re.compile('markers:[^\]]*latlng[^}]*lng:([^}]*)')
coords_in_url = re.compile('\?q=(-?\d{,3}\.\d*),\s*(-?\d{,3}\.\d*)')
doc = document_fromstring(data)
for element, attribute, url, pos in doc.body.iterlinks():
if 'maps.google' in url:
description = element.text or ''
print description.encode('UTF8')
print "URL: {0}".format(url)
if coords_in_url.search(url):
# Coordinates are in URL itself
latitude = coords_in_url.search(url).groups()[0]
longitude = coords_in_url.search(url).groups()[1]
else:
# Load map and find coordinates in source of page
try:
sock = urlopen(url.replace(' ','+'))
except Exception, e:
print 'Connection problem:'
print repr(e)
print 'Waiting 2 minutes and trying again'
time.sleep(120)
sock = urlopen(url.replace(' ','+'))
content = sock.read()
sock.close()
time.sleep(3) # Don't annoy server
try:
latitude = lat_re.findall(content)[0]
longitude = lon_re.findall(content)[0]
except IndexError:
print '[Coordinates not found]'
print
continue
id = id
label = '\'' + description + '\''
label_lower = label.lower()
# latitude
# longitude
# geocode
t = int(time.time())
# oid
db.execute('INSERT INTO mylabels VALUES ({id}, {label}, {label_lower}, {latitude}, {longitude}, NULL, {t}, NULL)'.format(**locals()))
db.commit()
id += 1
print latitude, longitude
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment