Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import Counter
- from time import sleep
- import pymongo
- from geopy.exc import GeocoderTimedOut, GeocoderServiceError
- from geopy.geocoders import Nominatim
- geolocator = Nominatim()
- client = pymongo.MongoClient('130.212.214.188',27017)
- db = client.hepc_hiv_addiction_users
- collection1 = db.new_york
- location_city_dic={}
- location_state_dic = {}
- c=0
- while(True):
- # cursor1 = collection1.find(no_cursor_timeout = True).sort("_id",-1)
- cursor1 = collection1.find({"screen_name":"cleymonart"})
- for i in cursor1:
- print i["screen_name"]
- friends = i["friends"]
- if friends !=3000:
- for fr in friends:
- print fr["screen_name"]
- print fr["location"]
- if "geolocated_geopy_second" in fr:
- continue
- try:
- loc = geolocator.geocode(fr["location"],addressdetails=True, timeout= None)
- if loc != None:
- # print loc.raw['address']
- if "city" in loc.raw['address']:
- fr["city"] = loc.raw['address']['city']
- if "state" in loc.raw['address']:
- fr["state"] = loc.raw['address']['state']
- fr["geolocated_geopy_second"]=1
- collection1.save(i)
- except GeocoderTimedOut as e:
- fr["geolocated_geopy_second"]=1
- collection1.save(i)
- print("Error: geocode failed on input %s with message %s"%(i["location"], e.message))
- except GeocoderServiceError as e:
- print e.message
- print "sleeping"
- sleep(900)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement