mirror of
https://github.com/heyarne/bookmarks-to-kml.git
synced 2026-05-06 18:43:40 +02:00
made compatible with python 3, changed regex to match coords with negative signs, create ./maps/ if it doesn't exist,
This commit is contained in:
parent
78583d384b
commit
56786ac11e
1 changed files with 21 additions and 16 deletions
|
|
@ -7,20 +7,21 @@ See readme.md
|
||||||
from lxml.html import document_fromstring
|
from lxml.html import document_fromstring
|
||||||
import simplekml
|
import simplekml
|
||||||
|
|
||||||
from urllib import FancyURLopener
|
from urllib.request import FancyURLopener
|
||||||
|
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
coords_in_content = re.compile('\/@(\d+\.\d+),(\d+\.\d+),')
|
coords_in_content = re.compile('\/@(-?\d+\.\d+),(-?\d+\.\d+),')
|
||||||
mobile_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36'
|
mobile_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'
|
filename = r'GoogleBookmarks.html'
|
||||||
if(len(sys.argv) > 1):
|
if(len(sys.argv) > 1):
|
||||||
filename = sys.argv[1]
|
filename = sys.argv[1]
|
||||||
|
|
||||||
print 'opening ' + filename
|
print('opening ' + filename)
|
||||||
with open(filename) as bookmarks_file:
|
with open(filename) as bookmarks_file:
|
||||||
data = bookmarks_file.read()
|
data = bookmarks_file.read()
|
||||||
|
|
||||||
|
|
@ -39,7 +40,7 @@ for label in doc.body.iterfind('dl/dt/h3'):
|
||||||
if 'maps.google' in url:
|
if 'maps.google' in url:
|
||||||
print
|
print
|
||||||
description = element.text or ''
|
description = element.text or ''
|
||||||
print 'GET {0} {1}'.format(url, description.encode('UTF8'))
|
print('GET {0} {1}'.format(url, description.encode('UTF8')))
|
||||||
browser = Browser()
|
browser = Browser()
|
||||||
|
|
||||||
# Load map and find coordinates in source of page
|
# Load map and find coordinates in source of page
|
||||||
|
|
@ -47,30 +48,34 @@ for label in doc.body.iterfind('dl/dt/h3'):
|
||||||
while not sock:
|
while not sock:
|
||||||
try:
|
try:
|
||||||
sock = browser.open(url.replace(' ','+'))
|
sock = browser.open(url.replace(' ','+'))
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
print 'Connection problem:' + repr(e)
|
print('Connection problem:' + repr(e))
|
||||||
print 'Retrying randomly between 15 and 60 seconds.'
|
print('Retrying randomly between 15 and 60 seconds.')
|
||||||
time.sleep(random.randint(15, 60))
|
time.sleep(random.randint(15, 60))
|
||||||
|
|
||||||
content = sock.read()
|
content = sock.read().decode("utf-8")
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
coords = coords_in_content.search(content)
|
coords = coords_in_content.search(content).groups()
|
||||||
latitude = coords.groups()[0]
|
latitude = coords[0]
|
||||||
longitude = coords.groups()[1]
|
longitude = coords[1]
|
||||||
|
|
||||||
except (AttributeError, IndexError):
|
except (AttributeError, IndexError):
|
||||||
print '[Coordinates not found] ' + str(coords) + ' Try to update "mobile_agent"'
|
print('[Coordinates not found] ' + str(coords) + ' Try to update "mobile_agent"')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print latitude, longitude
|
print(latitude, longitude)
|
||||||
try:
|
try:
|
||||||
kml.newpoint(name=description,
|
kml.newpoint(name=description,
|
||||||
coords=[(float(longitude), float(latitude))])
|
coords=[(float(longitude), float(latitude))])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print '[Invalid coordinates]'
|
print('[Invalid coordinates]')
|
||||||
|
|
||||||
output = './maps/' + labelName + '.kml'
|
output = './maps/' + labelName + '.kml'
|
||||||
print 'saving results to ' + output
|
print('saving results to ' + output)
|
||||||
kml.save(output)
|
|
||||||
|
if not os.path.exists('./maps/'):
|
||||||
|
os.makedirs('./maps/')
|
||||||
|
|
||||||
|
kml.save(output)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue