Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. from collections import Counter
  2. from time import sleep
  3.  
  4. import pymongo
  5. from geopy.exc import GeocoderTimedOut, GeocoderServiceError
  6. from geopy.geocoders import Nominatim
  7. geolocator = Nominatim()
  8. client = pymongo.MongoClient('130.212.214.188',27017)
  9. db = client.hepc_hiv_addiction_users
  10. collection1 = db.friends_followers_of_new_york_collection
  11.  
  12. while(True):
  13. cursor1 = collection1.find({"classifier_type" : 4},no_cursor_timeout = True).sort("_id",-1)
  14. # cursor1 = collection1.find({"screen_name":"cleymonart"})
  15. for i in cursor1:
  16. if "geolocated_geopy_second" not in i:
  17. print "****************"
  18. print i["screen_name"]
  19. print "***************"
  20. try:
  21. loc = geolocator.geocode(i["location"],addressdetails=True, timeout= None)
  22. if loc != None:
  23. # print loc.raw['address']
  24. if "city" in loc.raw['address']:
  25. i["city"] = loc.raw['address']['city']
  26. if "state" in loc.raw['address']:
  27. i["state"] = loc.raw['address']['state']
  28. print i["state"]
  29. i["geolocated_geopy_second"]=1
  30. collection1.save(i)
  31. except GeocoderTimedOut as e:
  32. i["geolocated_geopy_second"]=1
  33. collection1.save(i)
  34. print("Error: geocode failed on input %s with message %s"%(i["location"], e.message))
  35. except GeocoderServiceError as e:
  36. print e.message
  37. print "sleeping"
  38. sleep(900)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement