SHARE
TWEET

geolocation_friends

a guest Mar 20th, 2017 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.new_york
  11. location_city_dic={}
  12. location_state_dic = {}
  13. c=0
  14. while(True):
  15.     # cursor1 = collection1.find(no_cursor_timeout = True).sort("_id",-1)
  16.     cursor1 = collection1.find({"screen_name":"cleymonart"})
  17.     for i in cursor1:
  18.         print i["screen_name"]
  19.         friends = i["friends"]
  20.         if friends !=3000:
  21.             for fr in friends:
  22.                 print fr["screen_name"]
  23.                 print fr["location"]
  24.                 if "geolocated_geopy_second" in fr:
  25.                     continue
  26.                 try:
  27.                     loc = geolocator.geocode(fr["location"],addressdetails=True, timeout= None)
  28.                     if loc != None:
  29.                         # print loc.raw['address']
  30.                         if "city" in loc.raw['address']:
  31.                             fr["city"] = loc.raw['address']['city']
  32.                         if "state" in loc.raw['address']:
  33.                             fr["state"] = loc.raw['address']['state']
  34.                     fr["geolocated_geopy_second"]=1
  35.                     collection1.save(i)
  36.                 except GeocoderTimedOut as e:
  37.                     fr["geolocated_geopy_second"]=1
  38.                     collection1.save(i)
  39.                     print("Error: geocode failed on input %s with message %s"%(i["location"], e.message))
  40.                 except GeocoderServiceError as e:
  41.                     print e.message
  42.                     print "sleeping"
  43.                     sleep(900)
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top