Skip to content

Commit f93f3d4

Browse files
committed
Works with python 3
1 parent 642374d commit f93f3d4

File tree

9 files changed

+92
-91
lines changed

9 files changed

+92
-91
lines changed

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ v1.1.0, 12 Feb 2013 -- Replaced TwitterAPI with puttytat for Twitter requests.
1212

1313
v1.1.1, 19 Feb 2013 -- Geocoder uses viewport instead of bounds.
1414

15-
v2.0.0, 14 Jun 2013 -- Switch to TwitterAPI and renamed to TwitterGeoPics
15+
v2.0.1, 14 Jun 2013 -- Switch to TwitterAPI and renamed to TwitterGeoPics

MANIFEST

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# file GENERATED by distutils, do NOT edit
22
CHANGES.txt
33
setup.py
4-
twittergeo/Geocoder.py
5-
twittergeo/SearchGeo.py
6-
twittergeo/SearchPics.py
7-
twittergeo/StreamGeo.py
8-
twittergeo/StreamPics.py
9-
twittergeo/__init__.py
4+
TwitterGeoPics/Geocoder.py
5+
TwitterGeoPics/GetNewGeo.py
6+
TwitterGeoPics/GetNewPics.py
7+
TwitterGeoPics/GetOldGeo.py
8+
TwitterGeoPics/GetOldPics.py
9+
TwitterGeoPics/__init__.py

TwitterGeoPics/Geocoder.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import math
88
import pygeocoder
99
import socket
10+
import sys
1011
import time
1112

1213
SOCKET_TIMEOUT = 3 # seconds -- need to set a timeout or connection can hang indefinitely
@@ -106,7 +107,7 @@ def geocode(self, place):
106107
data = pygeocoder.Geocoder.geocode(place)
107108
self.count_request_ok += 1
108109
return data
109-
except pygeocoder.GeocoderError, e:
110+
except pygeocoder.GeocoderError as e:
110111
if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry():
111112
return self.geocode(place)
112113
else:
@@ -120,7 +121,7 @@ def latlng_to_address(self, lat, lng):
120121
place = pygeocoder.Geocoder.latlng_to_address(lat, lng)
121122
self.count_request_ok += 1
122123
return place
123-
except pygeocoder.GeocoderError, e:
124+
except pygeocoder.GeocoderError as e:
124125
if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry():
125126
return self.latlng_to_address(lan, lng)
126127
else:
@@ -134,7 +135,7 @@ def address_to_latlng(self, place):
134135
lat, lng = pygeocoder.Geocoder.address_to_latlng(place)
135136
self.count_request_ok += 1
136137
return lat, lng
137-
except pygeocoder.GeocoderError, e:
138+
except pygeocoder.GeocoderError as e:
138139
if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry():
139140
return self.address_to_latlng(place)
140141
else:
@@ -175,7 +176,7 @@ def geocode_tweet(self, status):
175176
lat, lng = lat.strip(), lng.strip()
176177
place = self.latlng_to_address(float(lat), float(lng))
177178
self.count_has_location += 1
178-
except ValueError, TypeError:
179+
except ValueError or TypeError:
179180
pass
180181
elif place is not None and place != '':
181182
# there is a location in the user profile, so see if it is usable
@@ -197,8 +198,8 @@ def geocode_tweet(self, status):
197198
else:
198199
lat, lng = None, None
199200
self.count_nowhere += 1
200-
return place, lat, lng
201-
201+
return place, lat, lng
202+
202203
def get_region_box(self, place):
203204
"""Get the coordinates of a place and its bounding box.
204205
The size of bounding box that Google returns depends on whether the place is
@@ -255,14 +256,19 @@ def distance(cls, lat1, lng1, lat2, lng2):
255256
return earth_radius*c
256257

257258
def print_stats(self):
258-
print '\n--STATS--'
259-
print 'geo requests: ', self.count_request
260-
print 'geo requets ok: ', self.count_request_ok
261-
print 'geo quota exceeded:', self.quota_exceeded_at
262-
print 'geo throttle: ', self.throttle
263-
print 'has none: ', self.count_nowhere
264-
print 'has geocode: ', self.count_has_geocode
265-
print 'has location: ', self.count_has_location
259+
stats = \
260+
('\n--STATS--\n'
261+
'geo requests: %s\n'
262+
'geo requets ok: %s\n'
263+
'geo quota exceeded: %s\n'
264+
'geo throttle: %s\n'
265+
'has none: %s\n'
266+
'has geocode: %s\n'
267+
'has location: %s\n') % \
268+
(self.count_request, self.count_request_ok, self.quota_exceeded_at, self.throttle,
269+
self.count_nowhere, self.count_has_geocode, self.count_has_location)
270+
271+
sys.stdout.write(stats)
266272

267273
if self.cache:
268274
counts = [ 0, 0, 0 ]
@@ -277,7 +283,9 @@ def print_stats(self):
277283
counts[2] += 1
278284
if count > max_place[1]:
279285
max_place = ( item, count )
280-
print '\n--CACHE--'
281-
print 'size: ', len(self.cache)
282-
print 'counts: ', counts
283-
print 'max place: ', max_place
286+
cache = \
287+
('\n--CACHE--\n'
288+
'size: %s\n'
289+
'counts: %s\n'
290+
'max place: %s\n') % (len(self.cache), counts, max_place)
291+
sys.stdout.write(cache)

TwitterGeoPics/GetNewGeo.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
__date__ = "December 20, 2012"
33
__license__ = "MIT"
44

5-
# unicode printing for Windows
6-
import sys, codecs
7-
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
8-
95
import argparse
10-
import Geocoder
6+
from Geocoder import Geocoder
7+
import sys
118
from TwitterAPI import TwitterAPI, TwitterOAuth
129

1310

14-
GEO = Geocoder.Geocoder()
11+
GEO = Geocoder()
1512

1613

1714
def parse_tweet(status, region):
1815
"""Print tweet, location and geocode."""
1916
try:
2017
geocode = GEO.geocode_tweet(status)
21-
print '\n%s: %s' % (status['user']['screen_name'], status['text'])
22-
print 'LOCATION:', status['user']['location']
23-
print 'GEOCODE:', geocode
24-
except Exception, e:
18+
sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text']))
19+
sys.stdout.write('LOCATION: %s\n' % status['user']['location'])
20+
sys.stdout.write('GEOCODE: %s\n' % geocode)
21+
except Exception as e:
2522
if GEO.quota_exceeded:
26-
print>>sys.stderr, '*** GEOCODER QUOTA EXCEEDED:', GEO.count_request
23+
sys.stderr.write('*** GEOCODER QUOTA EXCEEDED: %s\n' % GEO.count_request)
2724
raise
2825

2926

@@ -35,7 +32,7 @@ def stream_tweets(api, list, region):
3532
params['track'] = words
3633
if region is not None:
3734
params['locations'] = '%f,%f,%f,%f' % region
38-
print 'REGION', region
35+
sys.stdout.write('REGION %s\n' % region)
3936
while True:
4037
try:
4138
api.request('statuses/filter', params)
@@ -46,9 +43,9 @@ def stream_tweets(api, list, region):
4643
parse_tweet(item, region)
4744
elif 'disconnect' in item:
4845
raise Exception('Disconnect: %s' % item['disconnect'].get('reason'))
49-
except Exception, e:
46+
except Exception as e:
5047
# reconnect on 401 errors and socket timeouts
51-
print>>sys.stderr, '*** MUST RECONNECT', e
48+
sys.stderr.write('*** MUST RECONNECT %s\n' % e)
5249

5350

5451
if __name__ == '__main__':
@@ -70,13 +67,13 @@ def stream_tweets(api, list, region):
7067
else:
7168
latC, lngC, latSW, lngSW, latNE, lngNE = GEO.get_region_box(args.location)
7269
region = (lngSW, latSW, lngNE, latNE)
73-
print 'Google found region at %f,%f and %f,%f' % region
70+
sys.stdout.write('Google found region at %f,%f and %f,%f\n' % region)
7471
else:
7572
region = None
7673

7774
try:
7875
stream_tweets(api, args.words, region)
7976
except KeyboardInterrupt:
80-
print>>sys.stderr, '\nTerminated by user'
77+
sys.stderr.write('\nTerminated by user\n')
8178

8279
GEO.print_stats()

TwitterGeoPics/GetNewPics.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22
__date__ = "December 20, 2012"
33
__license__ = "MIT"
44

5-
# unicode printing for Windows
6-
import sys, codecs
7-
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
8-
95
import argparse
10-
import Geocoder
6+
from Geocoder import Geocoder
117
import os
8+
import sys
129
from TwitterAPI import TwitterAPI, TwitterOAuth
1310
import urllib
1411

1512

16-
GEO = Geocoder.Geocoder()
13+
GEO = Geocoder()
1714

1815

1916
def parse_tweet(status, photo_dir, stalk):
@@ -24,21 +21,21 @@ def parse_tweet(status, photo_dir, stalk):
2421
if media['type'] == 'photo':
2522
photo_count += 1
2623
if photo_count == 1:
27-
print '\n%s: %s' % (status['user']['screen_name'], status['text'])
24+
sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text']))
2825
if stalk and not GEO.quota_exceeded:
2926
try:
3027
geocode = GEO.geocode_tweet(status)
31-
print 'LOCATION:', status['user']['location']
32-
print 'GEOCODE:', geocode
33-
except Exception, e:
28+
sys.stdout.write('LOCATION: %s\n' % status['user']['location'])
29+
sys.stdout.write('GEOCODE: %s\n' % geocode)
30+
except Exception as e:
3431
if GEO.quota_exceeded:
35-
print>>sys.stderr, '*** GEOCODER QUOTA EXCEEDED:', GEO.count_request
32+
sys.stderr.write('*** GEOCODER QUOTA EXCEEDED: %s\n' % GEO.count_request)
3633
if photo_dir:
3734
photo_url = media['media_url_https']
3835
screen_name = status['user']['screen_name']
39-
print screen_name
4036
file_name = os.path.join(photo_dir, screen_name) + '.' + photo_url.split('.')[-1]
4137
urllib.urlretrieve(photo_url, file_name)
38+
sys.stdout.write(screen_name + '\n')
4239

4340

4441
def stream_tweets(api, list, photo_dir, region, stalk, no_retweets):
@@ -49,7 +46,7 @@ def stream_tweets(api, list, photo_dir, region, stalk, no_retweets):
4946
params['track'] = words
5047
if region is not None:
5148
params['locations'] = '%f,%f,%f,%f' % region
52-
print 'REGION', region
49+
sys.stdout.write('REGION %s\n' % region)
5350
while True:
5451
try:
5552
api.request('statuses/filter', params)
@@ -61,9 +58,9 @@ def stream_tweets(api, list, photo_dir, region, stalk, no_retweets):
6158
parse_tweet(item, photo_dir, stalk)
6259
elif 'disconnect' in item:
6360
raise Exception('Disconnect: %s' % item['disconnect'].get('reason'))
64-
except Exception, e:
61+
except Exception as e:
6562
# reconnect on 401 errors and socket timeouts
66-
print>>sys.stderr, '*** MUST RECONNECT', e
63+
sys.stderr.write('*** MUST RECONNECT %s\n' % e)
6764

6865

6966
if __name__ == '__main__':
@@ -88,13 +85,13 @@ def stream_tweets(api, list, photo_dir, region, stalk, no_retweets):
8885
else:
8986
latC, lngC, latSW, lngSW, latNE, lngNE = GEO.get_region_box(args.location)
9087
region = (lngSW, latSW, lngNE, latNE)
91-
print 'Google found region at %f,%f and %f,%f' % region
88+
sys.stdout.write('Google found region at %f,%f and %f,%f\n' % region)
9289
else:
9390
region = None
9491

9592
try:
9693
stream_tweets(api, args.words, args.photo_dir, region, args.stalk, args.no_retweets)
9794
except KeyboardInterrupt:
98-
print>>sys.stderr, '\nTerminated by user'
95+
sys.stderr.write('\nTerminated by user\n')
9996

10097
GEO.print_stats()

TwitterGeoPics/GetOldGeo.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22
__date__ = "December 20, 2012"
33
__license__ = "MIT"
44

5-
# unicode printing for Windows
6-
import sys, codecs
7-
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
8-
95
import argparse
10-
import Geocoder
6+
from Geocoder import Geocoder
7+
import sys
118
from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager
129

1310

14-
GEO = Geocoder.Geocoder()
11+
GEO = Geocoder()
1512

1613

1714
def parse_tweet(status):
1815
"""Print tweet, location and geocode."""
1916
try:
2017
geocode = GEO.geocode_tweet(status)
21-
print '\n%s: %s' % (status['user']['screen_name'], status['text'])
22-
print 'LOCATION:', status['user']['location']
23-
print 'GEOCODE:', geocode
24-
except Exception, e:
18+
sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text']))
19+
sys.stdout.write('LOCATION: %s\n' % status['user']['location'])
20+
sys.stdout.write('GEOCODE: %s\n' % geocode)
21+
except Exception as e:
2522
if GEO.quota_exceeded:
2623
raise
2724

@@ -41,7 +38,7 @@ def search_tweets(api, list, region):
4138
if item['code'] == 131:
4239
continue # ignore internal server error
4340
elif item['code'] == 88:
44-
print>>sys.stderr, 'Suspend search until %s' % search.get_quota()['reset']
41+
sys.stderr.write('Suspend search until %s\n' % search.get_quota()['reset'])
4542
raise Exception('Message from twiter: %s' % item['message'])
4643

4744

@@ -59,16 +56,16 @@ def search_tweets(api, list, region):
5956
try:
6057
if args.location:
6158
lat, lng, radius = GEO.get_region_circle(args.location)
62-
print 'Google found region at %f,%f with a radius of %s km' % (lat, lng, radius)
59+
sys.stdout.write('Google found region at %f,%f with a radius of %s km\n' % (lat, lng, radius))
6360
if args.radius:
6461
radius = args.radius
6562
region = (lat, lng, radius)
6663
else:
6764
region = None
6865
search_tweets(api, args.words, region)
6966
except KeyboardInterrupt:
70-
print>>sys.stderr, '\nTerminated by user'
71-
except Exception, e:
72-
print>>sys.stderr, '*** STOPPED', e
67+
sys.stdout.write('\nTerminated by user\n')
68+
except Exception as e:
69+
sys.stdout.write('*** STOPPED %s\n' % e)
7370

7471
GEO.print_stats()

0 commit comments

Comments
 (0)