def return_latlng(enquiry): address = enquiry google_maps = GoogleMaps(api_key='AIzaSyCw6c6wq-VY06rOn401rwtv-Q7MU3-C89M') location = google_maps.search(location=address) # sends search to Google Maps. # print(location.all()) # returns all locations. my_location = location.first() # returns only first location. # route_name = my_location.route # st = my_location.street_number # place = my_location.formatted_address # print(route_name) # print(my_location.lng) # print(my_location.street_number) # print(my_location.formatted_address) # lat = 37.751502 # lng = -122.386707 # my_location = google_maps.search(lat=lat, lng=lng).first() # print (my_location.route) return [my_location.lat, my_location.lng]
def geolocation(self): self.maplist = [] self.buttonHeightCounter = .05 API_KEY = 'AIzaSyBPGAbevdKkeXaZT0ZsR0qbO30Bpqqm0Mc' google_places = GooglePlaces(API_KEY) self.query_result = google_places.nearby_search( location=self.entry.get(), radius=700, types=[types.TYPE_RESTAURANT]) self.current_places = self.query_result if self.query_result.has_attributions: print(self.query_result.html_attributions) for place in self.query_result.places: place.get_details() markers = "&markers=size:big|label:S|color:red|" + str( place.details['geometry']['location']['lat']) + "," + str( place.details['geometry']['location']['lng']) + "|" self.maplist.append(markers) print(place.name) self.button_list.append( Button(self, text=place.name, command=lambda pname=place.name: self.on_click(pname), width=25)) self.button_list[-1].place(relx=.70, rely=self.buttonHeightCounter, anchor="c") self.buttonHeightCounter += .035 print(place.formatted_address + "\n") google_maps = GoogleMaps( api_key='AIzaSyDlJqxwlOWWAPwf54ivrpAZw4R1Yb5j6Yk') location = google_maps.search( location=self.entry.get()) # sends search to Google Maps. my_location = location.first() # returns only first location. #MARKER = '&markers=color:blue' + '%' + str(7) + 'Clabel:S%' + str(7) + 'C' + str(my_location.lat) + ',' + str(my_location.lng) #MARKER = "&markers=size:big|label:S|color:blue|" + str(my_location.lat) + "," + str(my_location.lng) + "|" + \ MARKER = self.maplist[1] + self.maplist[2] + self.maplist[3] self.zoomlevel = ZOOM maptype_index = 0 self.radiovar.set(maptype_index) self.goompy = GooMPy(WIDTH, HEIGHT, my_location.lat, my_location.lng, ZOOM, MAPTYPE, MARKER) self.restart() print(self.query_result) print(str(my_location.lat)) print(str(my_location.lng))
def get_latlng(address): google_maps = GoogleMaps(api_key='AIzaSyCcuF3AbW0iS9QChjdMUsstXN7C5zOJ5gg') location = google_maps.search(location=address) my_location = location.first() return (str(my_location.lat), str(my_location.lng))
class Geo: def __init__(self, API_KEY): self.google_maps = GoogleMaps(api_key=API_KEY) def processLocation(self, location): """Converts the given Address object into JSON""" temp = location.__dict__ aa = temp['_administrative_area'] del temp['_administrative_area'] admin_areas = [] for admin_area in aa: this_admin = { 'type': admin_area.area_type, 'name': admin_area.name, 'short_name': admin_area.short_name } admin_areas.append(this_admin) temp["administrative_areas"] = admin_areas return temp def geo_from_address(self, address): """Gets full address details for the given address string. Returns JSON data""" locations = self.google_maps.search( location=address) # sends search to Google Maps. result = [] for location in locations.all(): this_result = self.processLocation(location) result.append(this_result) return result def address_from_geo(self, lat, lng): """Gets full address details for the given long/lat location. Returns JSON data""" locations = self.google_maps.search( lat=lat, lng=lng).all() # sends search to Google Maps. result = [] for location in locations: this_result = self.processLocation(location) result.append(this_result) return result
class GeoLocation(object): def __init__(self, city): self.mycity = city self.gmap = GoogleMaps(api_key=KEY) self.location = self.gmap.search(location=city).first() print('Your lat,long: {},{}'.format(self.location.lat, self.location.lng)) def get_distance_to(self, dest): return self.gmap.distance(self.mycity, dest).first().distance.kilometers
def get_geolocation(address): google_maps = GoogleMaps(api_key='your_geocoding_api_key') location = google_maps.search(location=address) my_location = location.first() if my_location is not None: coordinates = (my_location.lat, my_location.lng) return coordinates else: return 'Not Found'
def display_details(self): address = str(self.ui.lineEditLocation.text()) google_maps = GoogleMaps( api_key='AIzaSyCqZcxCYBoZ4ui8ihg02MmR0CLEWzGhZp0') location = google_maps.search(location=address) my_location = location.first() self.ui.labelCity.setText("City: " + str(my_location.city)) self.ui.labelPostalCode.setText("Postal Code: " + str(my_location.postal_code)) self.ui.labelLongitude.setText("Longitude: " + str(my_location.lng)) self.ui.labelLatitude.setText("Latitude: " + str(my_location.lat))
def displayDetails(self): address = str(self.ui.lineEditLocation.text()) google_maps = GoogleMaps(api_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxx') location = google_maps.search(location=address) my_location = location.first() self.ui.labelCity.setText("City:" + str(my_location.city)) self.ui.labelPostalCode.setText("Postal Code: " + str(my_location.postal_code)) self.ui.labelLongitude.setText("Longitude:" + str(my_location.lng)) self.ui.labelLatitude.setText("Latitude:" + str(my_location.lat))
def send_data(): #addr = Entry1.get() #print(addr) addr = 'MNNIT Allhabad, Uttar Pradesh' #quant = Scale1.get() #print(quant) ''' import urllib2 proxy = urllib2.ProxyHandler({'http':'edcguest:[email protected]:3128','https':'edcguest:[email protected]:3128'}) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) ''' address = addr print(address) google_maps = GoogleMaps( api_key=' AIzaSyD4QiVK9WflR5GCkzuks27x55V2_v5CX2k ') print(address) location = google_maps.search( location=address) # sends search to Google Maps. print(location.all()) # returns all locations. my_location = location.first() # returns only first location. print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("{}: {} ({})".format(administrative_area.area_type, administrative_area.name, administrative_area.short_name)) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat) print(my_location.lng) '''
def on_data(self, data): #Decode json data tweet = json.loads(data) username = tweet['user']['screen_name'] location_address = tweet['user']['location'] tweet_text = tweet['text'].encode('ascii', 'ignore') processed_tweet = preprocess(tweet_text) if processed_tweet != 1: try: if location_address and location_address is not None: location_address.replace(" ", "+") google_maps = GoogleMaps( api_key='AIzaSyCoMZR7wii10qjJarohY7Ru__9cxC8Ftgw') geo_location = google_maps.search( location=location_address) location = geo_location.first() if location and location is not None: latitude = location.lat longitude = location.lng print("Original token list:", tweet_text) print("Location: ", location_address) print("New token list:", processed_tweet) print "Latitude: {}, Longitude= {} ".format( latitude, longitude) #print("Sentiment: ", sentiment) tweet = " ".join(tweet_text.split()) processed_tweet = (processed_tweet + "\t" + str(latitude) + "\t" + str(longitude) + "\t" + tweet + "\n") conn.send(processed_tweet.encode('utf-8')) else: print("No location returned from the API!") else: print("No location data associated with the tweet!") except: print("Couldn't find location of the tweet!")
from geolocation.main import GoogleMaps address = 'São Luís Maranhão' google_maps = GoogleMaps(api_key='AIzaSyBCksh0B58c_C6k_Epm2k1ZQb-YF6kA6SE') location = google_maps.search(location=address) my_location = location.first() if my_location.city: print(my_location.city.decode('utf-8')) if my_location.route: print(my_location.route.decode('utf-8')) if my_location.street_number: print(my_location.street_number) if my_location.postal_code: print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("%s: %s" % (administrative_area.area_type, administrative_area.name.decode('utf-8'))) if my_location.country: print(my_location.country.decode('utf-8')) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat) print(my_location.lng)
class EventEyeSpider(scrapy.Spider): google_maps = '' month = [] name = "eventeye" allowed_domains = ["eventseye.com"] # start_urls = ['http://www.eventseye.com/fairs/Upcoming_Trade_Shows.html'] start_urls = [ 'http://www.eventseye.com/fairs/d1_trade-shows_january_2016.html' ] pagination_url = 'http://www.eventseye.com/fairs/d1_trade-shows_january_2016_%s.html' pag_max_count = 13 # start_urls = [] def __init__(self, **kwargs): super(EventEyeSpider, self).__init__(**kwargs) self.init_Month() self.url_per_month() self.google_maps = GoogleMaps( api_key='AIzaSyCaxLzZ2r7AbCJiIy5RtJ4jOQXcOlDbeV0') def parse(self, response): urls = response.css( 'table table tr[bgcolor] td.mt a::attr(href)').extract() dates = response.css( 'table table tr[bgcolor] td:first-child::text').extract() for num in range(len(urls)): url = urls[num] raw_date = dates[num] item = EventItem() item['url'] = 'http://www.eventseye.com/fairs/' + url.strip() item['date'] = raw_date.strip() self.get_event_date(item) request = scrapy.Request(item['url'], callback=self.parse_event, meta={'item': item}) yield request def url_per_month(self): for page_num in range(1, self.pag_max_count): url = self.pagination_url % page_num self.start_urls = self.start_urls + [url] def print_start_urls(self, response): urls = response.css('table td.mtb a::attr(href)').extract() for url in urls: item = EventItem() item['url'] = url yield item def parse_event(self, response): sel = response.css('html') item = response.meta['item'] item['source'] = response.request.url item['event_website'] = sel.css( 'table td.mt a[rel="nofollow"]::attr(href)').extract()[0].strip() item['title'] = sel.css('table table h1 b::text').extract()[0] item['description'] = '' item['addressRegion'] = '' item['postalCode'] = '' item['price_range'] = '' item['addressLocality'] = sel.css( 'table a font.etb::text').extract()[0] item['location'] = sel.css('table table h1 b::text').extract()[0] state_code = sel.css( 'table td.mt span a::attr(href)').extract()[0].strip() state_code = (state_code[state_code.rindex('-') + 1:state_code.index('.')]).upper() item['state_code'] = state_code item['category'] = 'Cant Figure this out' item['industry'] = 'Cant Figure this out' loc = self.google_maps.search(location=item['addressLocality']) my_location = loc.first() try: item['country_code'] = my_location.country_shortcut item['addressCountry'] = my_location.country_shortcut item['city'] = my_location.city item['longitude'] = my_location.lng item['latitude'] = my_location.lat item['streetAddress'] = my_location.formatted_address except Exception as e: item['country_code'] = '' item['city'] = '' item['longitude'] = '' item['latitude'] = '' item['streetAddress'] = '' pass yield item def get_dict(self, path): with open(path, 'r') as f: data = json.load(f) return data def get_event_date(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' raw_date = item['date'] try: raw_date = raw_date.split(' ') from_day = ((raw_date[0]).split('.'))[0] to_day = ((raw_date[2]).split('.'))[0] from_month = ((raw_date[0]).split('.'))[1] to_month = ((raw_date[2]).split('.'))[1] year = raw_date[3] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime( from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime( to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass def init_Month(self): self.month = { 'Jan': '01', 'Feb': '02', 'March': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jly': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' } def get_datetime(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' date = item['date'] try: check = date.split('.') if len(check) == 2: dot_index = date.index(".") comma_index = date.index(",") days = date[dot_index + 1:comma_index] days = days.split('-') from_day = (days[0]).strip() to_day = (days[1]).strip() year = (date[comma_index + 1:]).strip() month_str = (date[0:dot_index]).strip() month = self.month[month_str] from_date = year + '-' + month + '-' + from_day + 'T10:00:00' to_date = year + '-' + month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime( from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime( to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end else: comma_index = date.index(",") year = (date[comma_index + 1:]).strip() days = date.split('-') from_month_str = (days[0].strip())[0:3] to_month_str = (days[1].strip())[0:3] for_days = date.split(',') for_days = for_days[0] for_days = for_days[for_days.index('.') + 1:] for_days = for_days.split(to_month_str) from_day = (for_days[0]).strip() from_day = from_day[0:2] to_day = (for_days[1]).strip() to_day = to_day.split(' ') to_day = to_day[1] from_month = self.month[from_month_str] to_month = self.month[to_month_str] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime( from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime( to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass
from geolocation.main import GoogleMaps google_maps = GoogleMaps(api_key='AIzaSyBCksh0B58c_C6k_Epm2k1ZQb-YF6kA6SE') lat = -2.500044 lng = -44.288093 location = google_maps.search(lat=lat, lng=lng) my_location = location.first() if my_location.city: print(my_location.city.decode('utf-8')) if my_location.route: print(my_location.route.decode('utf-8')) if my_location.street_number: print(my_location.street_number) if my_location.postal_code: print(my_location.postal_code) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address)
def send_data(): global m1,m2 addr = Entry1.get() print(addr) quant = Scale1.get() print(quant) #top.withdraw() #top1.deiconify() ''' import urllib2 proxy = urllib2.ProxyHandler({'http':'edcguest:[email protected]:3128','https':'edcguest:[email protected]:3128'}) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) ''' address = addr print(address) google_maps = GoogleMaps(api_key=' AIzaSyD4QiVK9WflR5GCkzuks27x55V2_v5CX2k ') print(address) location = google_maps.search(location=address) # sends search to Google Maps. print(location.all()) # returns all locations. my_location = location.first() # returns only first location. print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("{}: {} ({})".format(administrative_area.area_type, administrative_area.name, administrative_area.short_name)) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat) print(my_location.lng) #host1 = socket.gethostname() host1 = '172.20.53.93' port = 2004 BUFFER_SIZE = 2000 print(socket.gethostbyname(host1)) #host1 = '192.168.225.26' m1 = str(my_location.lat) + ' ' m2 = str(my_location.lng) MESSAGE = m1 + m2 + ' ' + str(quant) print(MESSAGE) tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpClientA.connect((host1, port)) tcpClientA.send(MESSAGE) tcpClientA.close() top.withdraw() top1.deiconify()
class Twitthear(): def __init__(self): # Setup OSC self.maxServer = OSC.OSCServer(('127.0.0.1', 7000)) self.maxServerThread = threading.Thread(target=self.maxServer.serve_forever) self.maxServerThread.daemon = False self.maxServerThread.start() self.maxClient = OSC.OSCClient() self.maxClient.connect(('127.0.0.1', 57121)) self.maxServer.addMsgHandler("/saveTweetPhrase", self.saveTweetPhraseResponder) self.maxServer.addMsgHandler("/nextTweetPhrase", self.nextTweetPhraseResponder) self.nodeClient = OSC.OSCClient() self.nodeClient.connect(('127.0.0.1', 6000)) # Current tweets to be sonified self.tweets = [] self.tweetIndex = 0 # Instantiate a Google Maps client for geocoding self.maps = GoogleMaps(api_key=mapscredentials.api_key) # Instantiate a Twitter client self.twitterClient = twitter.Api(consumer_key=twittercredentials.consumer_key, consumer_secret=twittercredentials.consumer_secret, access_token_key=twittercredentials.access_token_key, access_token_secret=twittercredentials.access_token_secret, input_encoding=None, tweet_mode="extended") # Instantiate a Google NLP client cred = service_account.Credentials.from_service_account_file('TwittHear-a204ccf1b234.json') cred = cred.with_scopes( ['https://www.googleapis.com/auth/cloud-platform']) self.NLPClient = language.LanguageServiceClient(credentials=cred) # Instantiate CMU Pronunciation Dictionary try: self.dict = cmudict.dict() except: print "Downloading cmudict..." nltk.download('cmudict') print "Downloaded cmudict." self.dict = cmudict.dict() # Attempt to create databases self.setUpDatabase() ''' setUpDatabase() Initializes database tables ''' def setUpDatabase(self): try: db.create_tables([TweetPhrase]) except: pass ''' sendOSCMessage() Sends a message to the OSC client (Max/MSP) inputs: addr - OSC address to send to *msgArgs - message contents ''' def sendOSCMessage(self, addr, client=0, *msgArgs): msg = OSC.OSCMessage() msg.setAddress(addr) msg.append(*msgArgs) if client == 0: self.maxClient.send(msg) else: self.nodeClient.send(msg) ############################################################## # OSC Responder Functions ############################################################## ''' saveTweetPhraseResponder() Saves a tweet phrase to database inputs: stuff[0] - filename of the tweet to save, formatted as {id}.txt ''' def saveTweetPhraseResponder(self, addr, tags, stuff, source): filename = str(stuff[0]) id = str(filename[:-4]) tweet_phrase = TweetPhrase(tweet_id=id, filename=filename) try: tweet_phrase.save() print "Saved tweet phrase: " + str(filename) + " for Tweet #" + str(id) except: uq = TweetPhrase.update(filename=filename).where(TweetPhrase.tweet_id == id) uq.execute() print "Updated tweet phrase: " + str(filename) + " for Tweet #" + str(id) ''' nextTweetPhraseResponder() Queues up another tweet to be played inputs: None ''' def nextTweetPhraseResponder(self, addr, tags, stuff, source): # Get next tweet, called from Max if self.tweetIndex == len(self.tweets) - 1: self.sendOSCMessage("/finished", 0, ["finished"]) else: self.tweetIndex += 1 self.playTweet() ############################################################## # Twitter Function ############################################################## ''' getTweets() Gathers tweets to sonify based on user input inputs: feed - expects 'timeline', 'geocode', or 'search' term - search term or location ''' def getTweets(self, feed, term=''): if feed == "timeline": raw_tweets = self.twitterClient.GetHomeTimeline(count=100) elif feed == "geocode": location = self.maps.search(location=term).first() if location: raw_tweets = self.twitterClient.GetSearch(geocode=[location.lat, location.lng, "2mi"], count=100) else: print "Location not found." elif feed == "search": raw_tweets = self.twitterClient.GetSearch(term=term, count=100) if len(raw_tweets) > 0: self.tweets = [{ 'id': str(t.id), 'username': t.user.screen_name, 'text': t.full_text, 'favorites': t.favorite_count, 'retweets': t.retweet_count, 'mentioned_users': [u.screen_name for u in t.user_mentions] } for t in raw_tweets] # Stop previous playback, start playing self.pause() self.tweetIndex = 0 self.playTweet() else: print "No tweets found." return False ############################################################## # Transport Functions ############################################################## ''' pause() Pauses playback in Max ''' def pause(self): self.sendOSCMessage("/pause", 0, ["pause"]) ''' resume() Resumes playback in Max ''' def resume(self): self.sendOSCMessage("/resume", 0, ["resume"]) ''' back() Queues the previously played tweet phrase ''' def back(self): self.sendOSCMessage("/pause", 0, ["pause"]) print "Back, tweetIndex going from " + str(self.tweetIndex) + "to " + str(max(0, self.tweetIndex - 1)) self.tweetIndex = max(0, self.tweetIndex - 1) self.playTweet() ''' forward() Queues the next tweet phrase ''' def forward(self): self.sendOSCMessage("/pause", 0, ["pause"]) self.tweetIndex = min(len(self.tweets) - 1, self.tweetIndex + 1) self.playTweet() ############################################################## # Phrase Generation and Functions ############################################################## ''' playTweet() Queues and plays a tweet phrase ''' def playTweet(self): # Get tweet to play tweet = self.tweets[self.tweetIndex] # Check if its phrase exists and load/play it if it does exists = self.loadTweetPhrase(tweet['id']) if(exists): # Tweet phrase exists, and is now playing print "Playing " + str(tweet['id']) return else: # Tweet doesn't exist, create/play new tweet phrase self.createTweetPhrase(tweet) ''' createTweetPhrase() Generate a new tweet phrase in Max inputs: tweet - Twitthear representation of tweet information ''' def createTweetPhrase(self, tweet): # Create document for Google sentiment analysis document = types.Document( content=tweet['text'], type=enums.Document.Type.PLAIN_TEXT) # Detect the sentiment of the text try: sentiment = self.NLPClient.analyze_sentiment(document=document).document_sentiment tweet['sentiment_score'] = sentiment.score tweet['sentiment_magnitude'] = sentiment.magnitude except: print "Language not understood for sentiment analysis." tweet['sentiment_score'] = 0 tweet['sentiment_magnitude'] = 0 tweet['syllables'] = [] split = tweet['text'].encode('ascii', 'ignore').split(' ') for w in split: try: # Strip of non-alphanumeric characters word = re.sub('[\W_]', '', w) num_syl = self.nsyl(word) tweet['syllables'].append(num_syl) except: try: pass # print "Could not get the number of syllables in " + str(w) except: pass # print "Could not get the number of syllables in word." # Serialize the tweet serialized_tweet = json.dumps(tweet) # Send tweet to Max self.sendOSCMessage("/createTweetPhrase", 0, [serialized_tweet]) def nsyl(self, word): return [len(list(y for y in x if y[-1].isdigit())) for x in self.dict[word.lower()]][0] ''' loadTweetPhrase() Attempts to load a tweet phrase in Max given a tweet id inputs: id - tweet id to be loaded outputs: True if phrase was successfully loaded, False otherwise ''' def loadTweetPhrase(self, id): try: # Check if tweet exists in database filename = TweetPhrase.get(TweetPhrase.tweet_id == id).filename except: print "Tweet with id #" + str(id) + " not found." return False # Send the file to load to Max self.sendOSCMessage("/loadTweetPhrase", 0, [filename]) return True ''' saveTweet() Saves a tweet by sending it to the visual interface ''' def saveTweet(self): # Get currently playing tweet id try: id = self.tweets[self.tweetIndex]['id'] except: print "No tweet currently playing." return # Get tweet embed html html = self.twitterClient.GetStatusOembed(status_id=id, omit_script=True)['html'] if(html): # Send html to node self.sendOSCMessage("/showTweet", 1, [html.encode('ascii', 'ignore')]) else: print "Unable to get HTML for this tweet." return
def testRequests(decrypt): address = decrypt google_maps = GoogleMaps(api_key="AIzaSyAEk_XVCNBK9RavS6KiXXfQW-fRvMzKt5o") location = google_maps.search(location=address) print(location.all())
from pyexcel_xlsx import get_data from pyexcel_xlsx import save_data from collections import OrderedDict import json data = get_data("bikeinfo.xlsx") # print data['Blad1'] n = 0 sheet = {"Sheet 1": [["x", "y", "postcode", "city", "formatted_address"]]} google_maps = GoogleMaps(api_key='AIzaSyAMQRmEbUVvs7jZ5tW8hujZzUqqFy4syXc') for each in data['Blad1']: n = n + 1 if n > 1: print each[1] print each[0] try: location = google_maps.search( lat=each[1], lng=each[0]) # sends search to Google Maps. my_location = location.first() # returns only first location. print(my_location.city) print(my_location.postal_code) print(my_location.formatted_address) sheet["Sheet 1"].append([ each[1], each[0], my_location.postal_code, my_location.city, my_location.formatted_address ]) except: sheet["Sheet 1"].append( [each[1], each[0], "error", "error", "error"]) save_sheet = OrderedDict() save_sheet.update(sheet) save_data("out_put1.xlsx", save_sheet)
from operator import itemgetter from geolocation.main import GoogleMaps ifile = open('crash.csv', 'r') reader = csv.DictReader(ifile) default = 0 google_maps = GoogleMaps(api_key='AIzaSyARs71g1TIgn9VYqK4xmdLN64AbwZxBXn0') total = 0 listLat = [] listLng = [] for row in reader: try: location = google_maps.search(location=row['Location']) my_location = location.first() listLat.insert(total, my_location.lat) listLng.insert(total, my_location.lng) total += 1 except Exception as e: pass print('completed') with open('locationall.csv', 'w') as csvfile: fieldnames = ['latitude', 'longitude'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader()
def forecast(self, irc, msg, args, location): """<location> Fetch forecast information from <location>. """ channel = msg.args[0] darksky_api = self.registryValue('darksky_api') if not darksky_api: irc.error("No darksky.net API key.", Raise=True) geocode_api = self.registryValue('geocode_api') if not geocode_api: irc.error("No Google Geocode API key.", Raise=True) lang = self.registryValue('lang', channel=channel) units = self.registryValue('units', channel=channel) # Getting location information for i in range(0, 3): try: google_maps = GoogleMaps(api_key=geocode_api) loc = google_maps.search(location=location) my_loc = loc.first() except: continue # Getting forecast information try: fio = ForecastIO.ForecastIO(darksky_api, units=units, lang=lang, latitude=my_loc.lat, longitude=my_loc.lng) except: irc.error('Weather API error', Raise=True) print(format('URL: %s', fio.get_url())) units = fio.flags['units'] # Unit formatting if units != 'us': _tempU = '°C' else: _tempU = '°F' if units == 'ca': _speedU = 'kph' elif units == 'si': _speedU = 'm/s' else: _speedU = 'mph' # Emoji! _icons = { 'clear-day': '☀️', 'clear-night': '🌕', 'rain': '🌧️', 'snow': '❄️', 'sleet': '🌧️❄️', 'wind': '💨', 'fog': '🌁', 'cloudy': '☁️', 'partly-cloudy-day': '🌤️', 'partly-cloudy-night': '☁️', 'thunderstorm': '⛈️', 'tornado': '🌪' } now_summary = '' if fio.has_minutely() is True: minutely = FIOMinutely.FIOMinutely(fio) now_summary = minutely.summary elif fio.has_hourly() is True: hourly = FIOHourly.FIOHourly(fio) now_summary = hourly.summary # Current Conditions if fio.has_currently() is True: currently = FIOCurrently.FIOCurrently(fio) if not now_summary: now_summary = currently.summary now = format( '%s: %s%s (≈%s%s). %s %s Hum: %s%%, Wind: %s%s %s', ircutils.bold(my_loc.formatted_address), int(currently.temperature), _tempU, int(currently.apparentTemperature), _tempU, _icons[currently.icon], now_summary, int(currently.humidity * 100), int(currently.windSpeed), _speedU, self._degrees_to_cardinal(currently.windBearing), ) if fio.has_daily() is True: daily = FIODaily.FIODaily(fio) overall = format('%s%s', _icons[daily.icon], daily.summary) day_2 = daily.get_day(2) tomorow_name = calendar.day_name[ datetime.datetime.utcfromtimestamp(day_2['time']).weekday()] tomorow = format( '%s: %s%s Min: %s%s/%s%s Max: %s%s/%s%s', ircutils.underline(tomorow_name), _icons[day_2['icon']], day_2['summary'], int(day_2['temperatureLow']), _tempU, int(day_2['apparentTemperatureLow']), _tempU, int(day_2['temperatureHigh']), _tempU, int(day_2['apparentTemperatureHigh']), _tempU, ) day_3 = daily.get_day(3) dat_name = calendar.day_name[datetime.datetime.utcfromtimestamp( day_3['time']).weekday()] dat = format( '%s: %s%s Min: %s%s/%s%s Max: %s%s/%s%s', ircutils.underline(dat_name), _icons[day_3['icon']], day_3['summary'], int(day_3['temperatureLow']), _tempU, int(day_3['apparentTemperatureLow']), _tempU, int(day_3['temperatureHigh']), _tempU, int(day_3['apparentTemperatureHigh']), _tempU, ) irc.reply(now + ' ' + overall + ' ' + tomorow + '. ' + dat)
# -*- coding: utf-8 -*- from geolocation.main import GoogleMaps if __name__ == "__main__": address = "New York City" google_maps = GoogleMaps(api_key="your_google_maps_key") lat = 40.7060008 lng = -74.0088189 location = google_maps.search(lat=lat, lng=lng) print(location.all()) my_location = location.first() print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("%s: %s" % (administrative_area.area_type, administrative_area.name)) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat)
class EventEyeSpider(scrapy.Spider): google_maps = '' month = [] name = "eventeye" allowed_domains = ["eventseye.com"] # start_urls = ['http://www.eventseye.com/fairs/Upcoming_Trade_Shows.html'] start_urls = ['http://www.eventseye.com/fairs/d1_trade-shows_january_2016.html'] pagination_url = 'http://www.eventseye.com/fairs/d1_trade-shows_january_2016_%s.html' pag_max_count = 13 # start_urls = [] def __init__(self, **kwargs): super(EventEyeSpider, self).__init__(**kwargs) self.init_Month() self.url_per_month() self.google_maps = GoogleMaps(api_key='AIzaSyCaxLzZ2r7AbCJiIy5RtJ4jOQXcOlDbeV0') def parse(self, response): urls = response.css('table table tr[bgcolor] td.mt a::attr(href)').extract() dates = response.css('table table tr[bgcolor] td:first-child::text').extract() for num in range(len(urls)): url = urls[num] raw_date = dates[num] item = EventItem() item['url'] = 'http://www.eventseye.com/fairs/' + url.strip() item['date'] = raw_date.strip() self.get_event_date(item) request = scrapy.Request(item['url'], callback=self.parse_event, meta={'item': item}) yield request def url_per_month(self): for page_num in range(1, self.pag_max_count): url = self.pagination_url % page_num self.start_urls = self.start_urls + [url] def print_start_urls(self, response): urls = response.css('table td.mtb a::attr(href)').extract() for url in urls: item = EventItem() item['url'] = url yield item def parse_event(self, response): sel = response.css('html') item = response.meta['item'] item['source'] = response.request.url item['event_website'] = sel.css('table td.mt a[rel="nofollow"]::attr(href)').extract()[0].strip() item['title'] = sel.css('table table h1 b::text').extract()[0] item['description'] = '' item['addressRegion'] = '' item['postalCode'] = '' item['price_range'] = '' item['addressLocality'] = sel.css('table a font.etb::text').extract()[0] item['location'] = sel.css('table table h1 b::text').extract()[0] state_code = sel.css('table td.mt span a::attr(href)').extract()[0].strip() state_code = (state_code[state_code.rindex('-') + 1:state_code.index('.')]).upper() item['state_code'] = state_code item['category'] = 'Cant Figure this out' item['industry'] = 'Cant Figure this out' loc = self.google_maps.search(location=item['addressLocality']) my_location = loc.first() try: item['country_code'] = my_location.country_shortcut item['addressCountry'] = my_location.country_shortcut item['city'] = my_location.city item['longitude'] = my_location.lng item['latitude'] = my_location.lat item['streetAddress'] = my_location.formatted_address except Exception as e: item['country_code'] = '' item['city'] = '' item['longitude'] = '' item['latitude'] = '' item['streetAddress'] = '' pass yield item def get_dict(self, path): with open(path, 'r') as f: data = json.load(f) return data def get_event_date(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' raw_date = item['date'] try: raw_date = raw_date.split(' ') from_day = ((raw_date[0]).split('.'))[0] to_day = ((raw_date[2]).split('.'))[0] from_month = ((raw_date[0]).split('.'))[1] to_month = ((raw_date[2]).split('.'))[1] year = raw_date[3] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime(to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass def init_Month(self): self.month = {'Jan': '01', 'Feb': '02', 'March': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jly': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} def get_datetime(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' date = item['date'] try: check = date.split('.') if len(check) == 2: dot_index = date.index(".") comma_index = date.index(",") days = date[dot_index + 1:comma_index] days = days.split('-') from_day = (days[0]).strip() to_day = (days[1]).strip() year = (date[comma_index + 1:]).strip() month_str = (date[0:dot_index]).strip() month = self.month[month_str] from_date = year + '-' + month + '-' + from_day + 'T10:00:00' to_date = year + '-' + month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime(to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end else: comma_index = date.index(",") year = (date[comma_index + 1:]).strip() days = date.split('-') from_month_str = (days[0].strip())[0:3] to_month_str = (days[1].strip())[0:3] for_days = date.split(',') for_days = for_days[0] for_days = for_days[for_days.index('.') + 1:] for_days = for_days.split(to_month_str) from_day = (for_days[0]).strip() from_day = from_day[0:2] to_day = (for_days[1]).strip() to_day = to_day.split(' ') to_day = to_day[1] from_month = self.month[from_month_str] to_month = self.month[to_month_str] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime(to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass
from app import app import urllib.request, json from models import Location from flask import config from geolocation.main import GoogleMaps from geolocation.distance_matrix.client import DistanceMatrixApiClient address = "Nairobi" google_maps = GoogleMaps(api_key = 'AIzaSyDU63uIF4cTPGfAm8TZ4bQPK1nP_qm2ePE' location = google_maps.search(location=address) my_location = location.first() for administrative_area in my_location.administrative_area: print(“{}: {} ({})”.format(administrative_area.area_type, administrative_area.name, administrative_area.short_name)) lat = 40.7060008 lng = -74.0088189 my_location = google_maps.search(lat=lat, lng=lng).first() # api_key = config["AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY"] # base_url = config["https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings"]
class ConferenceAlertsSpider(scrapy.Spider): urls = '' google_maps = '' month = [] name = "conf" allowed_domains = ["conferencealerts.com"] start_urls = [] def __init__(self, **kwargs): super(ConferenceAlertsSpider, self).__init__(**kwargs) self.init_month() self.urls = self.get_dict('assets\conf_start_urls.json') for url in self.urls: self.start_urls = self.start_urls + [url['url']] self.google_maps = GoogleMaps(api_key='AIzaSyCaxLzZ2r7AbCJiIy5RtJ4jOQXcOlDbeV0') def parse(self, response): sel = response.css('table#searchResultTable tr td') title = sel.css('span#searchName a::text').extract() source = sel.css('span#searchName a::attr(href)').extract() location = sel.css('span#searchPlace::text').extract() country = sel.css('span#searchPlace span::text').extract() for num in range(0, len(title)): item = EventItem() item['url'] = response.request.url item['source'] = 'http://conferencealerts.com/' + source[num] item['title'] = title[num] item['category'] = 'Conference' item['industry'] = item['category'] item['location'] = location[num] item['addressCountry'] = country[num] item['addressLocality'] = location[num] + '' + country[num] item['addressRegion'] = '' item['price_range'] = '' request = scrapy.Request(item['source'], callback=self.parse_event, meta={'item': item}) yield request def parse_event(self, response): item = response.meta['item'] sel = response.css('div#eventInfoContainer table') description = sel.css('span#eventDescription::text').extract() event_website = sel.css('span#eventWebsite a::attr(href)').extract() date = sel.css('span#eventDate::text').extract() item['description'] = description[0] item['event_website'] = event_website[0] item['date'] = date[0] item['postalCode'] = '' self.get_datetime(item) loc = self.google_maps.search(location=item['addressLocality']) my_location = loc.first() try: item['country_code'] = my_location.country_shortcut item['longitude'] = my_location.lng item['latitude'] = my_location.lat item['streetAddress'] = my_location.formatted_address except Exception as e: item['country_code'] = '' item['longitude'] = '' item['latitude'] = '' item['streetAddress'] = '' pass yield item def print_start_urls(self, response): for href in response.css('div.topicTableColumn1Container table td, div.topicTableColumn2Container table td'): url = href.css('a::attr(href)').extract() if len(url) == 0 or url[0] == '#top': continue url = 'http://conferencealerts.com/' + url[0] item = EventItem() item['url'] = url yield item def get_dict(self, path): with open(path, 'r') as f: data = json.load(f) return data def init_month(self): self.month = {'January': '01', 'February': '02', 'March': '03', 'April': '04', 'May': '05', 'June': '06', 'July': '07', 'August': '08', 'September': '09', 'October': '10', 'November': '11', 'December': '12'} def get_datetime(self, item): date = item['date'] try: tokens = date.split(' ') from_day = (tokens[0]).strip() from_day = from_day[0:1] to_day = (tokens[4]).strip() to_day = to_day[0:1] year = tokens[6] month = tokens[5] month = month.strip() month = self.month[month] print from_day + ' ' + to_day + ' ' + month + ' ' + year from_date = year + '-' + month + '-' + from_day + 'T10:00:00' to_date = year + '-' + month + '-' + to_day + 'T10:00:00' event_start = datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime(to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass
class EventEyeSpider(scrapy.Spider): google_maps = '' month = [] name = "aiche" allowed_domains = ["aiche.org"] start_urls = ['http://www.aiche.org/resources/conferences'] def __init__(self, **kwargs): super(EventEyeSpider, self).__init__(**kwargs) self.init_Month() self.google_maps = GoogleMaps(api_key='AIzaSyCaxLzZ2r7AbCJiIy5RtJ4jOQXcOlDbeV0') def parse(self, response): titles = response.css('div.view-content div.views-row article h3 a::text').extract() urls = response.css('div.view-content div.views-row article h3 a::attr(href)').extract() for num in range(len(urls)): url = urls[num] item = EventItem() complete_url = url item['url'] = 'http://www.aiche.org' + complete_url item['title'] = titles[num] request = scrapy.Request(item['url'], callback=self.parse_event, meta={'item': item}) yield request # break def parse_event(self, response): sel = response.css('html') content = sel.css('div.conference-lead-info') item = response.meta['item'] item['source'] = response.request.url item['event_website'] = '' # item['title'] = content.css('div.conference-lead-info h1.title::text').extract() try: item['description'] = content.css('div.field-name-body div.field-item p:first-child::text').extract()[0].strip() except Exception as e: item['description'] = '' pass item['addressRegion'] = '' item['postalCode'] = '' item['price_range'] = '' item['addressLocality'] = content.css('div.field-name-field-conf-venues a::text').extract()[0].strip() item['location'] = '' item['category'] = 'Conference' item['industry'] = 'Event' item['date'] = content.css('div.conference-dates div div::text').extract()[1] self.get_event_date(item) loc = self.google_maps.search(location=item['addressLocality']) my_location = loc.first() try: item['country_code'] = my_location.country_shortcut item['addressCountry'] = my_location.country_shortcut item['city'] = my_location.city item['longitude'] = my_location.lng item['latitude'] = my_location.lat item['streetAddress'] = my_location.formatted_address except Exception as e: item['country_code'] = '' item['city'] = '' item['longitude'] = '' item['latitude'] = '' item['streetAddress'] = '' pass yield item def init_Month(self): self.month = {'January': '01', 'February': '02', 'March': '03', 'April': '04', 'May': '05', 'June': '06', 'July': '07', 'August': '08', 'September': '09', 'October': '10', 'November': '11', 'December': '12'} def get_event_date(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' raw_date = item['date'] try: year_date = raw_date.split(',') month_days = (year_date[0]).split('-') from_date = (month_days[0]).split(' ') from_month = (from_date[0]).strip() from_day = (from_date[1]).strip() to_date = (month_days[1]).split(' ') if len(to_date) == 1: to_day = (to_date[0]).strip() to_month = from_month else: to_month = (to_date[0]).strip() to_day = (to_date[1]).strip() year = (year_date[1]).strip() from_month = self.month[from_month] to_month = self.month[to_month] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' print from_date + ' ' print to_date + ' ' event_start = datetime.strptime(from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime(to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass
class EventEyeSpider(scrapy.Spider): google_maps = '' month = [] name = "aiche" allowed_domains = ["aiche.org"] start_urls = ['http://www.aiche.org/resources/conferences'] def __init__(self, **kwargs): super(EventEyeSpider, self).__init__(**kwargs) self.init_Month() self.google_maps = GoogleMaps( api_key='AIzaSyCaxLzZ2r7AbCJiIy5RtJ4jOQXcOlDbeV0') def parse(self, response): titles = response.css( 'div.view-content div.views-row article h3 a::text').extract() urls = response.css( 'div.view-content div.views-row article h3 a::attr(href)').extract( ) for num in range(len(urls)): url = urls[num] item = EventItem() complete_url = url item['url'] = 'http://www.aiche.org' + complete_url item['title'] = titles[num] request = scrapy.Request(item['url'], callback=self.parse_event, meta={'item': item}) yield request # break def parse_event(self, response): sel = response.css('html') content = sel.css('div.conference-lead-info') item = response.meta['item'] item['source'] = response.request.url item['event_website'] = '' # item['title'] = content.css('div.conference-lead-info h1.title::text').extract() try: item['description'] = content.css( 'div.field-name-body div.field-item p:first-child::text' ).extract()[0].strip() except Exception as e: item['description'] = '' pass item['addressRegion'] = '' item['postalCode'] = '' item['price_range'] = '' item['addressLocality'] = content.css( 'div.field-name-field-conf-venues a::text').extract()[0].strip() item['location'] = '' item['category'] = 'Conference' item['industry'] = 'Event' item['date'] = content.css( 'div.conference-dates div div::text').extract()[1] self.get_event_date(item) loc = self.google_maps.search(location=item['addressLocality']) my_location = loc.first() try: item['country_code'] = my_location.country_shortcut item['addressCountry'] = my_location.country_shortcut item['city'] = my_location.city item['longitude'] = my_location.lng item['latitude'] = my_location.lat item['streetAddress'] = my_location.formatted_address except Exception as e: item['country_code'] = '' item['city'] = '' item['longitude'] = '' item['latitude'] = '' item['streetAddress'] = '' pass yield item def init_Month(self): self.month = { 'January': '01', 'February': '02', 'March': '03', 'April': '04', 'May': '05', 'June': '06', 'July': '07', 'August': '08', 'September': '09', 'October': '10', 'November': '11', 'December': '12' } def get_event_date(self, item): item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' raw_date = item['date'] try: year_date = raw_date.split(',') month_days = (year_date[0]).split('-') from_date = (month_days[0]).split(' ') from_month = (from_date[0]).strip() from_day = (from_date[1]).strip() to_date = (month_days[1]).split(' ') if len(to_date) == 1: to_day = (to_date[0]).strip() to_month = from_month else: to_month = (to_date[0]).strip() to_day = (to_date[1]).strip() year = (year_date[1]).strip() from_month = self.month[from_month] to_month = self.month[to_month] from_date = year + '-' + from_month + '-' + from_day + 'T10:00:00' to_date = year + '-' + to_month + '-' + to_day + 'T10:00:00' print from_date + ' ' print to_date + ' ' event_start = datetime.strptime( from_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") event_end = datetime.strptime( to_date, "%Y-%m-%dT%H:%M:%S").strftime("%Y-%m-%d %H:%M:%S") item['event_start'] = event_start item['event_end'] = event_end except Exception as e: item['event_start'] = '0000-00-00 00:00:0' item['event_end'] = '0000-00-00 00:00:0' pass
class GeolocationFinder: """ Class for retrieving geolocation when given an address. Class creates a cache of geolocations to minimise calls to geopy library, reducing the risk of hitting rate limit """ def __init__(self): self.location_cache = {} self.geolocator = Nominatim() self.geolocation = None self.backup_geolocator = GoogleMaps(api_key=google_map_api_key) self.lat = None self.long = None def get_location(self, location): """ gets location attributes give a location :param location: string :return: address:string, latitude:float, longitude:float """ if location is None: return None, None, None elif location in self.location_cache: # Check cache for location self.set_geolocation_from_cache(location) address, latitude, longitude = self.get_addr_lat_long() return address, latitude, longitude else: # Location not cached so fetch from geolocator self.set_geolocation_from_geolocator(location) if self.geolocation is not None or self.lat is not None: address, latitude, longitude = self.get_addr_lat_long() return address, latitude, longitude return None, None, None def set_geolocation_from_cache(self, location): """ sets class geolocation attribute to geolocation from the cache :param location: string """ self.geolocation = self.location_cache[location] def set_geolocation_from_geolocator(self, location): """ Sets class geolocation attribute to geolocation from the geopy library and adds new eolocation to cache :param location: string """ self.geolocation = None self.lat = None self.long = None try: self.geolocation = self.geolocator.geocode(location, timeout=None) except GeocoderTimedOut: logger.logging.warning('GeolocationFinder: geolocator timeout') print("geologger timeout") except GeocoderUnavailable: print("geologger unavailable") except GeocoderInsufficientPrivileges: # geopy doesnt work: go to backup geolocation finder logger.logging.error("GeolocationFinder: Geocoder insufficient privileges") self.getGeolocationInfoFromBackupGeolocator(location) if self.geolocation is not None: self.location_cache[location] = self.geolocation def getGeolocationInfoFromBackupGeolocator(self, location): try: google_geolocation = self.backup_geolocator.search(location=location) loc = google_geolocation.first() self.lat = loc.lat self.long = loc.lng self.address = location except: self.geolocation = None def get_addr_lat_long(self): """ Gets address, latitude, longitude from the class geolocation attribute :return: address:string, latitude:float, longitude:float """ try: if self.geolocation is None: address = self.address latitude = self.lat longitude = self.long else: address = self.geolocation.address latitude = self.geolocation.latitude longitude = self.geolocation.longitude except AttributeError: logger.logging.exception('Attribute Error:GeolocationFinder.get_addr_lat_long in geolocation_finder') address, latitude, longitude = None, None, None self.geolocation = None return address, latitude, longitude
while True: if (time.time() - start < 10): address = 'New York City Wall Street 12' time.sleep(2) elif (time.time() - start > 10 and time.time() < 20): address = 'New York City Wall Street 12' time.sleep(2) elif (time.time() - start > 20 and time.time() < 30): address = 'New York City Wall Street 12' time.sleep(2) elif (time.time() > 30): start = time.time() google_maps = GoogleMaps(api_key='AIzaSyCqBaEO3JOEwpdTb31ImhXBU6t_7KBJWT8') location_info = google_maps.search(address) location_info = location_info.first() print location_info.all() # return list of all location. data['lat'] = [] data['lat'].append({'val': location_info.lat}) data['lon'] = [] data['lon'].append({'val': location_info.lng}) with open('/home/linaro/HackGSU18/data.json', 'w') as outfile: json.dump(data, outfile) subprocess.call( "scp /home/linaro/HackGSU18/data.json [email protected]:/var/www/html", shell=True)
from geolocation.main import GoogleMaps address = "Thane Wood Street Kenora " google_maps = GoogleMaps(api_key='AIzaSyCIIOxzvcIvagBNbLs7xN_WR3rYPnEf-0A') location = google_maps.search(location=address) # sends search to Google Maps. print(location.all()) # returns all locations. my_location = location.first() # returns only first location. print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("{}: {} ({})".format(administrative_area.area_type, administrative_area.name, administrative_area.short_name)) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat) print(my_location.lng) # reverse geocode
class GeolocationTest(unittest.TestCase): def setUp(self): self.google_maps = GoogleMaps(api_key=TEST_API_KEY) def test_query(self): address = "New York City Wall Street 15" location = self.google_maps.search(address) self.assertIsNotNone(location.all()) def test_city(self): address = "New York City Wall Street 14" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('New York', my_location.city.decode('utf-8')) def test_route(self): address = "New York City Wall Street 12" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('Wall Street', my_location.route.decode('utf-8')) def test_country(self): address = "New York City Wall Street 110" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('United States', my_location.country.decode('utf-8')) def test_country_shortcut(self): address = "New York City Wall Street 2" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('US', my_location.country_shortcut.decode('utf-8')) def test_lat(self): address = "New York City Wall Street 1" location = self.google_maps.search(address) my_location = location.first() self.assertAlmostEqual(40.7060081, my_location.lat, 2) def test_lng(self): address = "New York City Wall Street 19" location = self.google_maps.search(address) my_location = location.first() self.assertAlmostEqual(-74.0134436, my_location.lng, 2) def test_formatted_address(self): address = "New York City Wall Street 124" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('Charging Bull, Broadway, New York, NY 10004, USA', my_location.formatted_address) def test_administrative_area_level_1(self): address = "New York City Wall Street 125" location = self.google_maps.search(address) my_location = location.first() self.assertEqual( 'New York', my_location.administrative_area[0].name.decode('utf-8')) self.assertEqual( 'NY', my_location.administrative_area[0].short_name.decode('utf-8')) def test_administrative_area_level_2(self): address = "New York City Wall Street 126" location = self.google_maps.search(address) my_location = location.first() self.assertEqual( 'New York County', my_location.administrative_area[1].name.decode('utf-8')) self.assertEqual( 'New York County', my_location.administrative_area[1].short_name.decode('utf-8')) def test_coding(self): address = "São Paulo" my_location = self.google_maps.search(address).first() self.assertEqual(u"São Paulo", my_location.city.decode('utf-8')) def test_latlng(self): lat = 37.4229210 lng = -122.0852112 my_location = self.google_maps.search(lat=lat, lng=lng).first() self.assertEqual('New York', my_location.city.decode('utf-8')) def test_administrative_area_resets(self): address = "São Paulo" sao_paulo = self.google_maps.search(address).first() address = "Houston, TX" houston = self.google_maps.search(address).first() self.assertNotEqual(sao_paulo, houston)
google_maps = GoogleMaps(api_key='AIzaSyARs71g1TIgn9VYqK4xmdLN64AbwZxBXn0') error = 0 breakvar = 0 total = 0 listLat = [] listLng = [] csvfile = open('sandend.csv', 'w') fieldnames = ['slatitude', 'slongitude', 'elatitude', 'elongitude'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in reader: try: location1 = google_maps.search(location=((row['Route']).split('-'))[0]) my_location1 = location1.first() location2 = google_maps.search(location=((row['Route']).split('-'))[1]) my_location2 = location2.first() writer.writerow({ 'slatitude': my_location1.lat, 'slongitude': my_location1.lng, 'elatitude': my_location2.lat, 'elongitude': my_location2.lng }) total += 1 if breakvar > 20: break breakvar += 1
class TwitterTransformer(): def __init__(self): self.gender_detector = GenderDetector() self.googlemaps_api = GoogleMaps( api_key=app_settings.SERVICES_CREDENTIALS['google_api_key']) def process(self, ds, **kwargs): raw_records = self.__fetch_tweets() print "{} new tweets have been analyzed".format(len(raw_records)) conn_db_lake = db_handler.get_connection('data_lake') cur_db_lake = conn_db_lake.cursor() for record in raw_records: tweet = record[2] clean_tweet = self.__tweet_cleaner(tweet['text']) print clean_tweet polarity, sentiment = self.__get_sentiment(clean_tweet) coordinates = self.__get_go_points(tweet['user']['location']) gender = self.__guess_gender(tweet['user']['name'].split()[0]) tweet_tokens = self.__tokenizer(clean_tweet) processed_tweet = { "author": tweet["user"]["screen_name"], "tweet_geo": tweet['geo'], "tweet_lang": tweet['lang'], "tweet_place": tweet['place'], "user_description": tweet['user']['description'], "user_followers_count": tweet['user']['followers_count'], "user_friends_count": tweet['user']['friends_count'], "user_lang": tweet['user']['lang'], "user_name": tweet['user']['name'], "user_location_name": tweet['user']['location'], "user_location_coordinate": { "lat": coordinates[0], "lon": coordinates[1] } if coordinates else None, "user_status_count": tweet['user']['statuses_count'], "tweet_created_at": str(parser.parse(tweet['created_at'])), "user_created_at": str(parser.parse(tweet['user']['created_at'])), "tweet_tokens": tweet_tokens, 'bigrams': ["_".join(x) for x in bigrams(tweet_tokens)], 'trigrams': ["_".join(x) for x in trigrams(tweet_tokens)], "polarity": polarity, "sentiment": sentiment, "gender": gender, } try: update_query = """ UPDATE records SET is_analyzed=TRUE WHERE id={}; """.format(record[0]) query = """INSERT INTO tweets (data, created_at) VALUES ('{}', '{}')""".format( json.dumps(processed_tweet).replace("'", "''"), record[3]) cur_db_lake.execute(query) cur_db_lake.execute(update_query) conn_db_lake.commit() except Exception as ex: conn_db_lake.rollback() raise ex def __fetch_tweets(self): try: conn_db_lake = db_handler.get_connection('data_lake') cur_db_lake = conn_db_lake.cursor() query = """ SELECT * FROM records WHERE type='tweet' AND is_analyzed = false """ cur_db_lake.execute(query) return cur_db_lake.fetchall() except Exception as ex: conn_db_lake.rollback() raise ex def __guess_gender(self, name): gender = None try: gender = self.gender_detector.guess(name) return gender except Exception as e: print('error in gender detector') def __get_go_points(self, address): if not address: return None coordinate = None try: res = self.googlemaps_api.search( address.strip(string.punctuation + ' ')).first() if res: coordinate = [res.lat, res.lng] except Exception as ex: print("Err in geo location convertor") return coordinate def __tweet_cleaner(self, tweet): # Convert to lower case tweet = tweet.lower() # Convert www.* or https?://* to empty string tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))', '', tweet) # Convert @username to empty string tweet = re.sub('@[^\s]+', '', tweet) # Remove additional white spaces tweet = re.sub('[\s]+', ' ', tweet) # Replace #word with word tweet = re.sub(r'#([^\s]+)', r'\1', tweet) # trim tweet = tweet.strip('\'"') return tweet def __get_sentiment(self, tweet): res = TextBlob(tweet) polarity = res.sentiment.polarity if polarity < 0: sentiment = 'negative' elif polarity == 0: sentiment = 'neutral' else: sentiment = 'positive' return (polarity, sentiment) def __tokenizer(self, tweet): tokens = [] for word in tweet.split(): if len(word) > 3 and word not in stopwords.words( 'english') and wordnet.synsets(word): tokens.append(word) return list(set(tokens))
class TwitterStream(StreamListener): __solr = None __core = None __count = 0 __count_retweet = 0 __google_maps = None def __init__(self, google_api_key): super().__init__() self.__solr = SolrClient(iu.solr_url) self.__core = iu.solr_core_tweets self.__google_maps = GoogleMaps(api_key=google_api_key) # self.__ml_model=util.load_ml_model(ml_model_file) # self.__selected_features = mutil.read_preselected_features(False, ml_selected_features) def ignoreRetweet(self, status_text): if "rt @" in status_text.lower(): self.__count_retweet += 1 if IGNORE_RETWEETS: return True return False def on_data(self, data): self.__count += 1 #print(self.__count) if self.__count % 200 == 0: code = urllib.request. \ code = iu.commit(iu.solr_core_tweets) now = datetime.datetime.now() print("{} processed: {}, where {} are retweets".format( now, self.__count, self.__count_retweet)) logger.info("{} processed: {}, where {} are retweets".format( now, self.__count, self.__count_retweet)) jdata = None try: jdata = json.loads(data) if jdata is not None and "id" in jdata.keys() \ and not self.ignoreRetweet(jdata["text"]): # created_at_time str_created_at = jdata["created_at"] time = datetime.datetime.strptime(str_created_at, TWITTER_TIME_PATTERN) str_solr_time = time.utcnow().strftime(SOLR_TIME_PATTERN) # entities hashtags hashtags = jdata["entities"]["hashtags"] hashtag_list = [] for hashtag in hashtags: hashtag_list.append(hashtag["text"].lower()) # entities urls urls = jdata["entities"]["urls"] url_list = [] for url in urls: url_list.append(url["expanded_url"]) # entities symbols symbols = jdata["entities"]["symbols"] symbols_list = [] for symbol in symbols: symbols_list.append(symbol["text"]) # entities user_mentions user_mentions = jdata["entities"]["user_mentions"] user_mention_list = [] for um in user_mentions: user_mention_list.append(um["id"]) # quoted status id if exists if "quoted_status_id" in jdata: quoted_status_id = jdata["quoted_status_id"] else: quoted_status_id = None # place exists place = jdata["place"] if place is not None: place_full_name = place["full_name"] place_coordinates = place['bounding_box']['coordinates'][ 0][0] else: place_full_name = None place_coordinates = None coordinates = jdata["coordinates"] # user_location, only compute geocode if other means have failed geocode_coordinates_of_user_location = [] str_user_loc = jdata["user"]["location"] if str_user_loc is not None and "," in str_user_loc: str_user_loc = str_user_loc.split(",")[0].strip() if str_user_loc is not None and len( str_user_loc ) < 25 and coordinates is None and place_full_name is None: geocode_obj = None if str_user_loc in LOCATION_COORDINATES.keys(): geocode_obj = LOCATION_COORDINATES[str_user_loc] else: # geocode_obj=None #currently the api for getting geo codes seems to be unstable try: geocode_obj = self.__google_maps.search( location=str_user_loc) if (geocode_obj is not None): geocode_obj = geocode_obj.first() LOCATION_COORDINATES[str_user_loc] = geocode_obj if geocode_obj is not None: geocode_coordinates_of_user_location.append( geocode_obj.lat) geocode_coordinates_of_user_location.append( geocode_obj.lng) except Exception as exc: #traceback.print_exc(file=sys.stdout) print("\t\t gmap error={}".format( str_user_loc, exc)) logger.error("\t\t gmap: {}".format(str_user_loc)) try: geocode_obj = geolocator.geocode(str_user_loc) LOCATION_COORDINATES[ str_user_loc] = geocode_obj if geocode_obj is not None: geocode_coordinates_of_user_location.append( geocode_obj.latitude) geocode_coordinates_of_user_location.append( geocode_obj.longitude) except Exception as exc: #traceback.print_exc(file=sys.stdout) print("\t\t GeoPy error={}".format( str_user_loc, exc)) logger.error( "\t\t GeoPy {}".format(str_user_loc)) # ml_tag=util.ml_tag(jdata['text'], feat_vectorizer,self.__ml_model, self.__selected_features, # SCALING_STRATEGY, self.__sysout, logger) ml_tag = '0' if random.random() < 0.2 else '2' tweet_risk = random.uniform(0, 1.0) if coordinates == None: coordinates = place_coordinates if coordinates == None: coordinates = geocode_coordinates_of_user_location coord_lat = None coord_lon = None if coordinates is not None and len(coordinates) > 0: coord_lat = coordinates[0] coord_lon = coordinates[1] docs = [{ 'id': jdata["id"], 'created_at': str_solr_time, 'coordinate_lat': coord_lat, 'coordinate_lon': coord_lon, 'favorite_count': jdata["favorite_count"], 'in_reply_to_screen_name': jdata["in_reply_to_screen_name"], 'in_reply_to_status_id': jdata["in_reply_to_status_id"], 'in_reply_to_user_id': jdata["in_reply_to_user_id"], 'lang': jdata["lang"], 'place_full_name': place_full_name, 'place_coordinates': place_coordinates, 'retweet_count': jdata["retweet_count"], 'retweeted': jdata["retweeted"], 'quoted_status_id': quoted_status_id, 'status_text': jdata["text"], 'entities_hashtag': hashtag_list, 'entities_symbol': symbols_list, 'entities_url': url_list, 'entities_user_mention': user_mention_list, 'user_id': jdata["user"]["id"], 'user_screen_name': jdata["user"]["screen_name"], 'user_statuses_count': jdata["user"]["statuses_count"], 'user_friends_count': jdata["user"]["friends_count"], 'user_followers_count': jdata["user"]["followers_count"], 'user_location': str_user_loc, 'user_location_coordinates': geocode_coordinates_of_user_location, 'ml_tag': ml_tag, "tweet_risk": tweet_risk }] self.__solr.index(self.__core, docs) except Exception as exc: traceback.print_exc(file=sys.stdout) print( "Error encountered for {}, error:{} (see log file for details)" .format(self.__count, exc)) if jdata is not None and "id" in jdata.keys(): tweet_id = jdata["id"] else: tweet_id = "[failed to parse]" logger.info( "Error encountered for counter={}, tweet={}, error:{} (see log file for details)" .format(self.__count, tweet_id, exc)) if jdata is not None: file = LOG_DIR + "/" + str(tweet_id) + ".txt" logger.info("\t input data json written to {}".format(file)) with open(file, 'w') as outfile: json.dump(jdata, outfile) pass return (True) def on_error(self, status): print(status) def on_status(self, status): print(status.text)
# -*- coding: utf-8 -*- from geolocation.main import GoogleMaps if __name__ == "__main__": address = "Wall Street 12" google_maps = GoogleMaps(api_key='your_google_maps_key') location = google_maps.search(location=address) print(location.all()) my_location = location.first() print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code) for administrative_area in my_location.administrative_area: print("%s: %s" % (administrative_area.area_type, administrative_area.name)) print(my_location.country) print(my_location.country_shortcut) print(my_location.formatted_address) print(my_location.lat) print(my_location.lng)
# -- coding: utf-8 -- from geolocation.main import GoogleMaps from geolocation.distance_matrix.client import DistanceMatrixApiClient address = "New York City Wall Street 12" google_maps = GoogleMaps(api_key='your_google_maps_key') location = google_maps.search(location=address) # sends search to Google Maps. print(location.all()) # returns all locations. my_location = location.first() # returns only first location. print(my_location.city) print(my_location.route) print(my_location.street_number) print(my_location.postal_code)
class GeolocationTest(unittest.TestCase): def setUp(self): self.google_maps = GoogleMaps(api_key=TEST_API_KEY) def test_query(self): address = "New York City Wall Street 15" location = self.google_maps.search(address) self.assertIsNotNone(location.all()) def test_city(self): address = "New York City Wall Street 14" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('New York', my_location.city.decode('utf-8')) def test_route(self): address = "New York City Wall Street 12" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('Wall Street', my_location.route.decode('utf-8')) def test_country(self): address = "New York City Wall Street 110" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('United States', my_location.country.decode('utf-8')) def test_country_shortcut(self): address = "New York City Wall Street 2" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('US', my_location.country_shortcut.decode('utf-8')) def test_lat(self): address = "New York City Wall Street 1" location = self.google_maps.search(address) my_location = location.first() self.assertAlmostEqual(40.7008854, my_location.lat, delta=0.1) def test_lng(self): address = "New York City Wall Street 19" location = self.google_maps.search(address) my_location = location.first() self.assertAlmostEqual(-73.986969, my_location.lng, delta=0.1) def test_formatted_address(self): address = "New York City 70 Pine Street" location = self.google_maps.search(address) my_location = location.first() self.assertEqual('70 Pine St, New York, NY 10270, USA', my_location.formatted_address) def test_administrative_area_level_1(self): address = "New York City Wall Street 125" location = self.google_maps.search(address) my_location = location.first() self.assertEqual( 'New York', my_location.administrative_area[0].name.decode('utf-8')) self.assertEqual( 'NY', my_location.administrative_area[0].short_name.decode('utf-8')) def test_administrative_area_level_2(self): address = "New York City Wall Street 126" location = self.google_maps.search(address) my_location = location.first() self.assertEqual( 'New York County', my_location.administrative_area[1].name.decode('utf-8')) self.assertEqual( 'New York County', my_location.administrative_area[1].short_name.decode('utf-8')) def test_coding(self): address = "São Paulo" my_location = self.google_maps.search(address).first() self.assertEqual(u"São Paulo", my_location.city.decode('utf-8')) def test_latlng(self): lat = 37.4229210 lng = -122.0852112 my_location = self.google_maps.search(lat=lat, lng=lng).first() self.assertEqual('New York', my_location.city.decode('utf-8')) def test_administrative_area_resets(self): address = "São Paulo" sao_paulo = self.google_maps.search(address).first() address = "Houston, TX" houston = self.google_maps.search(address).first() self.assertNotEqual(sao_paulo, houston)
x = google_format(width) if width_direction.values != 'N': x = -x y = google_format(length) if length_direction.values != 'E': y = -y print('x =', x, '-', width_direction) print('y =', y, '-', length_direction, '\n') gps = 0 while gps == 0: try: google_maps = GoogleMaps(api_key='Your api key') gps = google_maps.search(lat=x, lng=y) except ApiClientException: key = generate_key(40) localization = gps.first() country = localization.country.decode('utf-8') city = localization.city.decode('utf-8') print('Country:', country) print('City:', city + '\n') print('------------------------------------------------------\n') os.rename(path_prim + name_file[1], path_prim + country + "-" + city + "-" + name_file[1]) print('Gotowe!') os.system("PAUSE")