-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate_feeds.py
executable file
·358 lines (274 loc) · 11.2 KB
/
generate_feeds.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
import argparse
import configparser
from datetime import datetime
import logging
import os
import pytz
from xml.sax.saxutils import escape as xml_escape
import foursquare
from ics import Calendar, Event
import simplekml
logging.basicConfig(level=logging.INFO, format="%(message)s")
logger = logging.getLogger(__name__)
current_dir = os.path.realpath(os.path.dirname(__file__))
CONFIG_FILE = os.path.join(current_dir, "config.ini")
# The kinds of file we can generate:
VALID_KINDS = ["ics", "kml"]
class FeedGenerator:
fetch = "recent"
def __init__(self, fetch="recent"):
"Loads config, sets up Foursquare API client."
self.fetch = fetch
self._load_config(CONFIG_FILE)
self.client = foursquare.Foursquare(access_token=self.api_access_token)
def _load_config(self, config_file):
"Set object variables based on supplied config file."
config = configparser.ConfigParser()
try:
config.read_file(open(config_file))
except IOError:
logger.critical("Can't read config file: " + config_file)
exit()
self.api_access_token = config.get("Foursquare", "AccessToken")
self.ics_filepath = config.get("Local", "IcsFilepath")
self.kml_filepath = config.get("Local", "KmlFilepath")
def generate(self, kind="ics"):
"Call this to fetch the data from the API and generate the file."
if kind not in VALID_KINDS:
raise ValueError("kind should be one of {}.".format(", ".join(VALID_KINDS)))
if self.fetch == "all":
checkins = self._get_all_checkins()
else:
checkins = self._get_recent_checkins()
plural = "" if len(checkins) == 1 else "s"
logger.info("Fetched {} checkin{} from the API".format(len(checkins), plural))
if kind == "ics":
filepath = self._generate_ics_file(checkins)
elif kind == "kml":
filepath = self._generate_kml_file(checkins)
logger.info("Generated file {}".format(filepath))
exit(0)
def _get_recent_checkins(self):
"Make one request to the API for the most recent checkins."
results = self._get_checkins_from_api()
return results["checkins"]["items"]
def _get_all_checkins(self):
"Make multiple requests to the API to get ALL checkins."
offset = 0
checkins = []
# Temporary total:
total_checkins = 9999999999
while offset < total_checkins:
results = self._get_checkins_from_api(offset)
if offset == 0:
# First time, set the correct total:
total_checkins = results["checkins"]["count"]
plural = "" if total_checkins == 1 else "s"
logger.debug("{} checkin{} to fetch".format(total_checkins, plural))
logger.debug("Fetched {}-{}".format(offset + 1, offset + 250))
checkins += results["checkins"]["items"]
offset += 250
return checkins
def _get_checkins_from_api(self, offset=0):
"""Returns a list of recent checkins for the authenticated user.
Keyword arguments:
offset -- Integer, the offset number to send to the API.
The number of results to skip.
"""
try:
return self.client.users.checkins(
params={"limit": 250, "offset": offset, "sort": "newestfirst"}
)
except foursquare.FoursquareException as err:
logger.error(
"Error getting checkins, with offset of {}: {}".format(offset, err)
)
exit(1)
def _get_user(self):
"Returns details about the authenticated user."
try:
user = self.client.users()
except foursquare.FoursquareException as err:
logger.error("Error getting user: {}".format(err))
exit(1)
return user["user"]
def _generate_ics_file(self, checkins):
"""Supplied with a list of checkin data from the API, generates
and saves a .ics file.
Returns the filepath of the saved file.
Keyword arguments:
checkins -- A list of dicts, each one data about a single checkin.
"""
calendar = self._generate_calendar(checkins)
with open(self.ics_filepath, "w") as f:
f.writelines(calendar)
return self.ics_filepath
def _generate_calendar(self, checkins):
"""Supplied with a list of checkin data from the API, generates
an ics Calendar object and returns it.
Keyword arguments:
checkins -- A list of dicts, each one data about a single checkin.
"""
user = self._get_user()
c = Calendar()
for checkin in checkins:
if "venue" not in checkin:
# I had some checkins with no data other than
# id, createdAt and source.
continue
venue_name = checkin["venue"]["name"]
tz_offset = self._get_checkin_timezone(checkin)
e = Event()
e.name = "@ {}".format(venue_name)
e.location = venue_name
e.url = "{}/checkin/{}".format(user["canonicalUrl"], checkin["id"])
e.uid = "{}@foursquare.com".format(checkin["id"])
e.begin = checkin["createdAt"]
# Use the 'shout', if any, and the timezone offset in the
# description.
description = []
if "shout" in checkin and len(checkin["shout"]) > 0:
description = [checkin["shout"]]
description.append("Timezone offset: {}".format(tz_offset))
e.description = "\n".join(description)
# Use the venue_name and the address, if any, for the location.
location = venue_name
if "location" in checkin["venue"]:
loc = checkin["venue"]["location"]
if "formattedAddress" in loc and len(loc["formattedAddress"]) > 0:
address = ", ".join(loc["formattedAddress"])
location = "{}, {}".format(location, address)
e.location = location
c.events.add(e)
return c
def _generate_kml_file(self, checkins):
"""Supplied with a list of checkin data from the API, generates
and saves a kml file.
Returns the filepath of the saved file.
Keyword arguments:
checkins -- A list of dicts, each one data about a single checkin.
"""
user = self._get_user()
kml = simplekml.Kml()
# The original Foursquare files had a Folder with name and
# description like this, so:
names = [user.get("firstName", ""), user.get("lastName", "")]
user_name = " ".join(names).strip()
name = "foursquare checkin history for {}".format(user_name)
fol = kml.newfolder(name=name, description=name)
for checkin in checkins:
if "venue" not in checkin:
# I had some checkins with no data other than
# id, createdAt and source.
continue
venue_name = checkin["venue"]["name"]
tz_offset = self._get_checkin_timezone(checkin)
url = "https://foursquare.com/v/{}".format(checkin["venue"]["id"])
description = ['@<a href="{}">{}</a>'.format(url, venue_name)]
if "shout" in checkin and len(checkin["shout"]) > 0:
description.append('"{}"'.format(checkin["shout"]))
description.append("Timezone offset: {}".format(tz_offset))
coords = [
(
checkin["venue"]["location"]["lng"],
checkin["venue"]["location"]["lat"],
)
]
visibility = 0 if "private" in checkin else 1
pnt = fol.newpoint(
name=venue_name,
description="<![CDATA[{}]]>".format("\n".join(description)),
coords=coords,
visibility=visibility,
# Both of these were set like this in Foursquare's original KML:
altitudemode=simplekml.AltitudeMode.relativetoground,
extrude=1,
)
# Foursquare's KML feeds had 'updated' and 'published' elements
# in the Placemark, but I don't *think* those are standard, so:
pnt.timestamp.when = (
datetime.utcfromtimestamp(checkin["createdAt"])
.replace(tzinfo=pytz.utc)
.isoformat()
)
# Use the address, if any:
if "location" in checkin["venue"]:
loc = checkin["venue"]["location"]
if "formattedAddress" in loc and len(loc["formattedAddress"]) > 0:
address = ", ".join(loc["formattedAddress"])
# While simplexml escapes other strings, it threw a wobbly
# over '&' in addresses, so escape them:
pnt.address = xml_escape(address)
kml.save(self.kml_filepath)
return self.kml_filepath
def _get_checkin_timezone(self, checkin):
"""Given a checkin from the API, returns a string representing the
timezone offset of that checkin.
In the API they're given as a number of minutes, positive or negative.
e.g. if offset is 60, this returns '+01:00'
if offset is 0, this returns '+00:00'
if offset is -480, this returns '-08:00'
Keyword arguments
checkin -- A dict of data about a single checkin
"""
# In minutes, e.g. 60 or -480
minutes = checkin["timeZoneOffset"]
# e.g. 1 or -8
hours = minutes / 60
# e.g. '01.00' or '-08.00'
if hours >= 0:
offset = "{:05.2f}".format(hours)
symbol = "+"
else:
offset = "{:06.2f}".format(hours)
symbol = ""
# e.g. '+01:00' or '-08.00'
return "{}{}".format(symbol, offset).replace(".", ":")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Makes a .ics file from your Foursquare/Swarm checkins"
)
parser.add_argument(
"--all",
help="Fetch all checkins, not only the most recent",
required=False,
action="store_true",
default=False,
)
parser.add_argument(
"-k",
"--kind",
action="store",
help="Either ics (default) or kml",
required=False,
type=str,
)
parser.add_argument(
"-v",
"--verbose",
action="count",
help="-v or --verbose for brief output; -vv for more.",
required=False,
)
args = parser.parse_args()
if args.verbose == 1:
logger.setLevel(logging.INFO)
elif args.verbose == 2:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
if args.all:
to_fetch = "all"
else:
to_fetch = "recent"
if args.kind:
if args.kind in VALID_KINDS:
kind = args.kind
else:
raise ValueError("kind should be one of {}.".format(", ".join(VALID_KINDS)))
else:
kind = "ics"
generator = FeedGenerator(fetch=to_fetch)
generator.generate(kind=kind)
exit(0)