From f93f3d4498f12226c55267bd332f92bafc7a3a72 Mon Sep 17 00:00:00 2001 From: Jonas Geduldig Date: Tue, 18 Jun 2013 21:52:30 -0400 Subject: [PATCH] Works with python 3 --- CHANGES.txt | 2 +- MANIFEST | 12 +++++----- TwitterGeoPics/Geocoder.py | 44 +++++++++++++++++++++--------------- TwitterGeoPics/GetNewGeo.py | 29 +++++++++++------------- TwitterGeoPics/GetNewPics.py | 31 ++++++++++++------------- TwitterGeoPics/GetOldGeo.py | 27 ++++++++++------------ TwitterGeoPics/GetOldPics.py | 31 ++++++++++++------------- TwitterGeoPics/__init__.py | 5 ++++ setup.py | 2 +- 9 files changed, 92 insertions(+), 91 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index a267d57..7c303bc 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -12,4 +12,4 @@ v1.1.0, 12 Feb 2013 -- Replaced TwitterAPI with puttytat for Twitter requests. v1.1.1, 19 Feb 2013 -- Geocoder uses viewport instead of bounds. -v2.0.0, 14 Jun 2013 -- Switch to TwitterAPI and renamed to TwitterGeoPics \ No newline at end of file +v2.0.1, 14 Jun 2013 -- Switch to TwitterAPI and renamed to TwitterGeoPics \ No newline at end of file diff --git a/MANIFEST b/MANIFEST index b73bf1a..ad4a897 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,9 +1,9 @@ # file GENERATED by distutils, do NOT edit CHANGES.txt setup.py -twittergeo/Geocoder.py -twittergeo/SearchGeo.py -twittergeo/SearchPics.py -twittergeo/StreamGeo.py -twittergeo/StreamPics.py -twittergeo/__init__.py +TwitterGeoPics/Geocoder.py +TwitterGeoPics/GetNewGeo.py +TwitterGeoPics/GetNewPics.py +TwitterGeoPics/GetOldGeo.py +TwitterGeoPics/GetOldPics.py +TwitterGeoPics/__init__.py diff --git a/TwitterGeoPics/Geocoder.py b/TwitterGeoPics/Geocoder.py index 2097c23..6b8c857 100755 --- a/TwitterGeoPics/Geocoder.py +++ b/TwitterGeoPics/Geocoder.py @@ -7,6 +7,7 @@ import math import pygeocoder import socket +import sys import time SOCKET_TIMEOUT = 3 # seconds -- need to set a timeout or connection can hang indefinitely @@ -106,7 +107,7 @@ def geocode(self, place): data = pygeocoder.Geocoder.geocode(place) self.count_request_ok += 1 return data - except pygeocoder.GeocoderError, e: + except pygeocoder.GeocoderError as e: if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry(): return self.geocode(place) else: @@ -120,7 +121,7 @@ def latlng_to_address(self, lat, lng): place = pygeocoder.Geocoder.latlng_to_address(lat, lng) self.count_request_ok += 1 return place - except pygeocoder.GeocoderError, e: + except pygeocoder.GeocoderError as e: if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry(): return self.latlng_to_address(lan, lng) else: @@ -134,7 +135,7 @@ def address_to_latlng(self, place): lat, lng = pygeocoder.Geocoder.address_to_latlng(place) self.count_request_ok += 1 return lat, lng - except pygeocoder.GeocoderError, e: + except pygeocoder.GeocoderError as e: if e.status == pygeocoder.GeocoderError.G_GEO_OVER_QUERY_LIMIT and self._should_retry(): return self.address_to_latlng(place) else: @@ -175,7 +176,7 @@ def geocode_tweet(self, status): lat, lng = lat.strip(), lng.strip() place = self.latlng_to_address(float(lat), float(lng)) self.count_has_location += 1 - except ValueError, TypeError: + except ValueError or TypeError: pass elif place is not None and place != '': # there is a location in the user profile, so see if it is usable @@ -197,8 +198,8 @@ def geocode_tweet(self, status): else: lat, lng = None, None self.count_nowhere += 1 - return place, lat, lng - + return place, lat, lng + def get_region_box(self, place): """Get the coordinates of a place and its bounding box. 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): return earth_radius*c def print_stats(self): - print '\n--STATS--' - print 'geo requests: ', self.count_request - print 'geo requets ok: ', self.count_request_ok - print 'geo quota exceeded:', self.quota_exceeded_at - print 'geo throttle: ', self.throttle - print 'has none: ', self.count_nowhere - print 'has geocode: ', self.count_has_geocode - print 'has location: ', self.count_has_location + stats = \ + ('\n--STATS--\n' + 'geo requests: %s\n' + 'geo requets ok: %s\n' + 'geo quota exceeded: %s\n' + 'geo throttle: %s\n' + 'has none: %s\n' + 'has geocode: %s\n' + 'has location: %s\n') % \ + (self.count_request, self.count_request_ok, self.quota_exceeded_at, self.throttle, + self.count_nowhere, self.count_has_geocode, self.count_has_location) + + sys.stdout.write(stats) if self.cache: counts = [ 0, 0, 0 ] @@ -277,7 +283,9 @@ def print_stats(self): counts[2] += 1 if count > max_place[1]: max_place = ( item, count ) - print '\n--CACHE--' - print 'size: ', len(self.cache) - print 'counts: ', counts - print 'max place: ', max_place \ No newline at end of file + cache = \ + ('\n--CACHE--\n' + 'size: %s\n' + 'counts: %s\n' + 'max place: %s\n') % (len(self.cache), counts, max_place) + sys.stdout.write(cache) \ No newline at end of file diff --git a/TwitterGeoPics/GetNewGeo.py b/TwitterGeoPics/GetNewGeo.py index 9f2d79f..79a7914 100644 --- a/TwitterGeoPics/GetNewGeo.py +++ b/TwitterGeoPics/GetNewGeo.py @@ -2,28 +2,25 @@ __date__ = "December 20, 2012" __license__ = "MIT" -# unicode printing for Windows -import sys, codecs -sys.stdout = codecs.getwriter('utf8')(sys.stdout) - import argparse -import Geocoder +from Geocoder import Geocoder +import sys from TwitterAPI import TwitterAPI, TwitterOAuth -GEO = Geocoder.Geocoder() +GEO = Geocoder() def parse_tweet(status, region): """Print tweet, location and geocode.""" try: geocode = GEO.geocode_tweet(status) - print '\n%s: %s' % (status['user']['screen_name'], status['text']) - print 'LOCATION:', status['user']['location'] - print 'GEOCODE:', geocode - except Exception, e: + sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text'])) + sys.stdout.write('LOCATION: %s\n' % status['user']['location']) + sys.stdout.write('GEOCODE: %s\n' % geocode) + except Exception as e: if GEO.quota_exceeded: - print>>sys.stderr, '*** GEOCODER QUOTA EXCEEDED:', GEO.count_request + sys.stderr.write('*** GEOCODER QUOTA EXCEEDED: %s\n' % GEO.count_request) raise @@ -35,7 +32,7 @@ def stream_tweets(api, list, region): params['track'] = words if region is not None: params['locations'] = '%f,%f,%f,%f' % region - print 'REGION', region + sys.stdout.write('REGION %s\n' % region) while True: try: api.request('statuses/filter', params) @@ -46,9 +43,9 @@ def stream_tweets(api, list, region): parse_tweet(item, region) elif 'disconnect' in item: raise Exception('Disconnect: %s' % item['disconnect'].get('reason')) - except Exception, e: + except Exception as e: # reconnect on 401 errors and socket timeouts - print>>sys.stderr, '*** MUST RECONNECT', e + sys.stderr.write('*** MUST RECONNECT %s\n' % e) if __name__ == '__main__': @@ -70,13 +67,13 @@ def stream_tweets(api, list, region): else: latC, lngC, latSW, lngSW, latNE, lngNE = GEO.get_region_box(args.location) region = (lngSW, latSW, lngNE, latNE) - print 'Google found region at %f,%f and %f,%f' % region + sys.stdout.write('Google found region at %f,%f and %f,%f\n' % region) else: region = None try: stream_tweets(api, args.words, region) except KeyboardInterrupt: - print>>sys.stderr, '\nTerminated by user' + sys.stderr.write('\nTerminated by user\n') GEO.print_stats() \ No newline at end of file diff --git a/TwitterGeoPics/GetNewPics.py b/TwitterGeoPics/GetNewPics.py index 232fb60..a5641b3 100644 --- a/TwitterGeoPics/GetNewPics.py +++ b/TwitterGeoPics/GetNewPics.py @@ -2,18 +2,15 @@ __date__ = "December 20, 2012" __license__ = "MIT" -# unicode printing for Windows -import sys, codecs -sys.stdout = codecs.getwriter('utf8')(sys.stdout) - import argparse -import Geocoder +from Geocoder import Geocoder import os +import sys from TwitterAPI import TwitterAPI, TwitterOAuth import urllib -GEO = Geocoder.Geocoder() +GEO = Geocoder() def parse_tweet(status, photo_dir, stalk): @@ -24,21 +21,21 @@ def parse_tweet(status, photo_dir, stalk): if media['type'] == 'photo': photo_count += 1 if photo_count == 1: - print '\n%s: %s' % (status['user']['screen_name'], status['text']) + sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text'])) if stalk and not GEO.quota_exceeded: try: geocode = GEO.geocode_tweet(status) - print 'LOCATION:', status['user']['location'] - print 'GEOCODE:', geocode - except Exception, e: + sys.stdout.write('LOCATION: %s\n' % status['user']['location']) + sys.stdout.write('GEOCODE: %s\n' % geocode) + except Exception as e: if GEO.quota_exceeded: - print>>sys.stderr, '*** GEOCODER QUOTA EXCEEDED:', GEO.count_request + sys.stderr.write('*** GEOCODER QUOTA EXCEEDED: %s\n' % GEO.count_request) if photo_dir: photo_url = media['media_url_https'] screen_name = status['user']['screen_name'] - print screen_name file_name = os.path.join(photo_dir, screen_name) + '.' + photo_url.split('.')[-1] urllib.urlretrieve(photo_url, file_name) + sys.stdout.write(screen_name + '\n') 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): params['track'] = words if region is not None: params['locations'] = '%f,%f,%f,%f' % region - print 'REGION', region + sys.stdout.write('REGION %s\n' % region) while True: try: api.request('statuses/filter', params) @@ -61,9 +58,9 @@ def stream_tweets(api, list, photo_dir, region, stalk, no_retweets): parse_tweet(item, photo_dir, stalk) elif 'disconnect' in item: raise Exception('Disconnect: %s' % item['disconnect'].get('reason')) - except Exception, e: + except Exception as e: # reconnect on 401 errors and socket timeouts - print>>sys.stderr, '*** MUST RECONNECT', e + sys.stderr.write('*** MUST RECONNECT %s\n' % e) if __name__ == '__main__': @@ -88,13 +85,13 @@ def stream_tweets(api, list, photo_dir, region, stalk, no_retweets): else: latC, lngC, latSW, lngSW, latNE, lngNE = GEO.get_region_box(args.location) region = (lngSW, latSW, lngNE, latNE) - print 'Google found region at %f,%f and %f,%f' % region + sys.stdout.write('Google found region at %f,%f and %f,%f\n' % region) else: region = None try: stream_tweets(api, args.words, args.photo_dir, region, args.stalk, args.no_retweets) except KeyboardInterrupt: - print>>sys.stderr, '\nTerminated by user' + sys.stderr.write('\nTerminated by user\n') GEO.print_stats() \ No newline at end of file diff --git a/TwitterGeoPics/GetOldGeo.py b/TwitterGeoPics/GetOldGeo.py index 14d5caf..c66bd45 100644 --- a/TwitterGeoPics/GetOldGeo.py +++ b/TwitterGeoPics/GetOldGeo.py @@ -2,26 +2,23 @@ __date__ = "December 20, 2012" __license__ = "MIT" -# unicode printing for Windows -import sys, codecs -sys.stdout = codecs.getwriter('utf8')(sys.stdout) - import argparse -import Geocoder +from Geocoder import Geocoder +import sys from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager -GEO = Geocoder.Geocoder() +GEO = Geocoder() def parse_tweet(status): """Print tweet, location and geocode.""" try: geocode = GEO.geocode_tweet(status) - print '\n%s: %s' % (status['user']['screen_name'], status['text']) - print 'LOCATION:', status['user']['location'] - print 'GEOCODE:', geocode - except Exception, e: + sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text'])) + sys.stdout.write('LOCATION: %s\n' % status['user']['location']) + sys.stdout.write('GEOCODE: %s\n' % geocode) + except Exception as e: if GEO.quota_exceeded: raise @@ -41,7 +38,7 @@ def search_tweets(api, list, region): if item['code'] == 131: continue # ignore internal server error elif item['code'] == 88: - print>>sys.stderr, 'Suspend search until %s' % search.get_quota()['reset'] + sys.stderr.write('Suspend search until %s\n' % search.get_quota()['reset']) raise Exception('Message from twiter: %s' % item['message']) @@ -59,7 +56,7 @@ def search_tweets(api, list, region): try: if args.location: lat, lng, radius = GEO.get_region_circle(args.location) - print 'Google found region at %f,%f with a radius of %s km' % (lat, lng, radius) + sys.stdout.write('Google found region at %f,%f with a radius of %s km\n' % (lat, lng, radius)) if args.radius: radius = args.radius region = (lat, lng, radius) @@ -67,8 +64,8 @@ def search_tweets(api, list, region): region = None search_tweets(api, args.words, region) except KeyboardInterrupt: - print>>sys.stderr, '\nTerminated by user' - except Exception, e: - print>>sys.stderr, '*** STOPPED', e + sys.stdout.write('\nTerminated by user\n') + except Exception as e: + sys.stdout.write('*** STOPPED %s\n' % e) GEO.print_stats() \ No newline at end of file diff --git a/TwitterGeoPics/GetOldPics.py b/TwitterGeoPics/GetOldPics.py index b78365c..7ee05ca 100644 --- a/TwitterGeoPics/GetOldPics.py +++ b/TwitterGeoPics/GetOldPics.py @@ -2,18 +2,15 @@ __date__ = "December 20, 2012" __license__ = "MIT" -# unicode printing for Windows -import sys, codecs -sys.stdout = codecs.getwriter('utf8')(sys.stdout) - import argparse -import Geocoder +from Geocoder import Geocoder import os +import sys from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager import urllib -GEO = Geocoder.Geocoder() +GEO = Geocoder() def parse_tweet(status, photo_dir, stalk): @@ -24,21 +21,21 @@ def parse_tweet(status, photo_dir, stalk): if media['type'] == 'photo': photo_count += 1 if photo_count == 1: - print '\n%s: %s' % (status['user']['screen_name'], status['text']) + sys.stdout.write('\n%s: %s\n' % (status['user']['screen_name'], status['text'])) if stalk and not GEO.quota_exceeded: try: geocode = GEO.geocode_tweet(status) - print 'LOCATION:', status['user']['location'] - print 'GEOCODE:', geocode - except Exception, e: + sys.stdout.write('LOCATION: %s\n' % status['user']['location']) + sys.stdout.write('GEOCODE: %s\n' % geocode) + except Exception as e: if GEO.quota_exceeded: - print>>sys.stderr, 'GEOCODER QUOTA EXCEEDED:', GEO.count_request + sys.stderr.write('GEOCODER QUOTA EXCEEDED: %s\n' % GEO.count_request) if photo_dir: photo_url = media['media_url_https'] screen_name = status['user']['screen_name'] - print screen_name file_name = os.path.join(photo_dir, screen_name) + '.' + photo_url.split('.')[-1] urllib.urlretrieve(photo_url, file_name) + sys.stdout.write(screen_name + '\n') def search_tweets(api, list, photo_dir, region, stalk, no_retweets): @@ -57,7 +54,7 @@ def search_tweets(api, list, photo_dir, region, stalk, no_retweets): if item['code'] == 131: continue # ignore internal server error elif item['code'] == 88: - print>>sys.stderr, 'Suspend search until %s' % search.get_quota()['reset'] + sys.stderr.write('Suspend search until %s\n' % search.get_quota()['reset']) raise Exception('Message from twiter: %s' % item['message']) @@ -78,7 +75,7 @@ def search_tweets(api, list, photo_dir, region, stalk, no_retweets): try: if args.location: lat, lng, radius = GEO.get_region_circle(args.location) - print 'Google found region at %f,%f with a radius of %s km' % (lat, lng, radius) + sys.stdout.write('Google found region at %f,%f with a radius of %s km\n' % (lat, lng, radius)) if args.radius: radius = args.radius region = (lat, lng, radius) @@ -86,8 +83,8 @@ def search_tweets(api, list, photo_dir, region, stalk, no_retweets): region = None search_tweets(api, args.words, args.photo_dir, region, args.stalk, args.no_retweets) except KeyboardInterrupt: - print>>sys.stderr, '\nTerminated by user' - except Exception, e: - print>>sys.stderr, '*** STOPPED', e + sys.stderr.write('\nTerminated by user\n') + except Exception as e: + sys.stderr.write('*** STOPPED %s\n' % e) GEO.print_stats() \ No newline at end of file diff --git a/TwitterGeoPics/__init__.py b/TwitterGeoPics/__init__.py index e69de29..0628d0e 100644 --- a/TwitterGeoPics/__init__.py +++ b/TwitterGeoPics/__init__.py @@ -0,0 +1,5 @@ +__title__ = 'TwitterGeoPics' +__version__ = '2.0.1' +__author__ = 'Jonas Geduldig' +__license__ = 'MIT' +__copyright__ = 'Copyright 2013 Jonas Geduldig' diff --git a/setup.py b/setup.py index ea3149d..d82800a 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='TwitterGeoPics', - version='2.0.0', + version='2.0.1', author='Jonas Geduldig', author_email='boxnumber03@gmail.com', packages=['TwitterGeoPics'],