-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_notes_rss_feed.py
64 lines (48 loc) · 1.74 KB
/
test_notes_rss_feed.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
import os
import time
import datetime
import calendar
import urllib2
import json
import xml.etree.cElementTree as ElementTree
url_templ = 'http://api.openstreetmap.org/api/0.6/notes/%d.json'
def pubdateToTimestamp(pubdate):
# Wed, 01 May 2013 18:51:34 +0000
t = datetime.datetime.strptime(pubdate, "%a, %d %B %Y %H:%M:%S +0000")
return calendar.timegm(t.utctimetuple())
def readState(filename):
# Read the state.txt
sf = open(filename, 'r')
state = {}
for line in sf:
if line[0] == '#':
continue
(k, v) = line.split('=')
state[k] = v.strip().replace("\\:", ":")
sf.close()
return state
last_note_id = None
if not os.path.exists('notes_state.txt'):
print "No notes_state file found to poll note feed."
while True:
notes_state = readState('notes_state.txt')
last_note_id = int(notes_state.get('last_note_id', None))
while True:
last_note_id += 1
url = url_templ % last_note_id
print "Requesting %s" % url
try:
result = urllib2.urlopen(url)
note = json.load(result)
attrs = note.get('properties')
geo = note.get('geometry').get('coordinates')
author = attrs['author'] if 'author' in attrs else 'Anonymous'
print "%s created a new note near %s, %s: %s: %s" % (author, geo[1], geo[0], attrs['url'].replace('api.openstreetmap', 'osm'), attrs['comments'][0]['text'][:50])
except urllib2.URLError, e:
if e.code == 404:
print "%s doesn't exist. Stopping." % last_note_id
last_note_id -= 1
break
with open('notes_state.txt', 'w') as f:
f.write('last_note_id=%s\n' % last_note_id)
time.sleep(60)