-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path44sq-to-Kml.py
executable file
·118 lines (99 loc) · 3.76 KB
/
44sq-to-Kml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Copyright (C) 2013 Dominique DERRIER <[email protected]>
#
# This file is part of 44sq-to-Kml.
#
# 44sq-to-Kml is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 44sq-to-Kml is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 44sq-to-Kml. If not, see <http://www.gnu.org/licenses/>
#
from lxml import etree
import lxml.builder as lb
import pickle
import os
import datetime
try:
import foursquare
except ImportError:
print "install foursquare : pip install foursquare"
exit(1)
try:
import cred
except ImportError:
print "Please create a cred.py file in this package, based upon cred.example.py"
exit(1)
TOKEN_FILE='token_file.key'
CONSUMER_KEY = cred.CONSUMER_KEY
CONSUMER_SECRET = cred.CONSUMER_SECRET
URL_CALLBACK= cred.URL_CALLBACK
TOKEN=''
""" Load file """
if (os.path.exists(TOKEN_FILE)):
vfile = open(TOKEN_FILE,'r');
keys=pickle.load(vfile)
TOKEN=keys
if (TOKEN==''):
""" Get token with Oauth"""
Api = foursquare.Foursquare(client_id=CONSUMER_KEY,client_secret=CONSUMER_SECRET,redirect_uri=URL_CALLBACK)
auth_url = Api.oauth.auth_url()
print 'Please authorize: ' + auth_url
code = raw_input('Copy/Past The code XXXX : ').strip()
access_token = Api.oauth.get_token(code)
print 'Your access token is ' + access_token + ' Relaunch'
keys=(access_token)
vfile=open(TOKEN_FILE,'w+')
pickle.dump(keys,vfile)
vfile.close()
Api.set_access_token(access_token)
exit(0)
else:
Api= foursquare.Foursquare(client_id=CONSUMER_KEY,client_secret=CONSUMER_SECRET,
access_token=TOKEN)
root = lb.E.kml(xmlns="http://www.opengis.net/kml/2.2")
document=lb.E.Document(
lb.E.Style(lb.E.IconStyle(lb.E.Icon(lb.E.href('http://maps.google.com/mapfiles/kml/paddle/red-blank.png'))),id='red'),
lb.E.Style(lb.E.IconStyle(lb.E.Icon(lb.E.href('http://maps.google.com/mapfiles/kml/paddle/blue-blank.png'))),id='blue'))
root.append(document)
folder={}
for checkin in Api.users.all_checkins():
if ('venue' in checkin ):
categorie=" "
icon="https://playfoursquare.s3.amazonaws.com/press/logo/icon-36x36.png"
if len(checkin['venue']['categories']):
""" Avoid empty fields """
categorie = checkin['venue']['categories'][0]['name']
icon= checkin['venue']['categories'][0]['icon']['prefix'][:-1]+checkin['venue']['categories'][0]['icon']['suffix']
name=checkin['venue']['name']
url=checkin['venue']['canonicalUrl']
if ('country' in checkin['venue']['location']):
""" Avoid empty fields"""
country=checkin['venue']['location']['country']
if ('city' in checkin['venue']['location']):
city=checkin['venue']['location']['city']
if not (country in folder):
folder[country]=lb.E.Folder(lb.E.name(country),lb.E.description('4sq data in %s'%(country)))
date=datetime.date.fromtimestamp(checkin['createdAt'])
data={'icon':icon,
'categorie':categorie,
'city':city,
'country':country,
'url':url,
'date':date}
desc ="<img src=%(icon)s> %(categorie)s in %(city)s <br>%(country)s<br>Le %(date)s <a href='%(url)s'>Go</a>"%(data)
coordinates ="%s,%s,0"%(checkin['venue']['location']['lng'],checkin['venue']['location']['lat'])
place=lb.E.Placemark(lb.E.name(name),lb.E.styleUrl('#red'),lb.E.description(desc),
lb.E.Point(lb.E.coordinates(coordinates)))
folder[country].append(place)
for key in folder:
document.append(folder[key])
print etree.tostring(root,pretty_print=True)