def handle_tweepy(self): #begin Tweepy code #http://tweepy.readthedocs.org/en/v3.2.0/ import tweepy from tweepy import OAuthHandler #create file map/settings.py and place your keys there from map.settings import consumer_key, consumer_secret, access_token, access_token_secret auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) #search query examples: https://dev.twitter.com/rest/public/search #max search = 100, according to API search_results = api.search(q='from:p2000rotterdam', count=100) count_tweets = 0 twitter = Source.objects.get(name='Twitter') for tweet in search_results: try: #only store tweets with lat/lng data if tweet.coordinates is not None: #build the data for the DataPoint model tweet_content = DataPoint( title = tweet.text, latitude = tweet.coordinates['coordinates'][1], longtitude = tweet.coordinates['coordinates'][0], reference_id = tweet.id, pub_date = tweet.created_at, source = twitter ) tweet_content.save() count_tweets +=1 except Exception as exeption: print (exeption) print (str(count_tweets) + ' tweets saved')
def handle_untappd(self): #import API keys from map.settings import client_id, client_secret #import modules needed for json and reading the API URL import requests import json request = requests.get('https://api.untappd.com/v4/thepub/local?client_id=' + client_id + '&client_secret='+ client_secret +'&lat=51.916666&lng=4.5&radius=5') results = request.json() count_untappd = 0 untappd = Source.objects.get(name='Untappd') for checkin in results['response']['checkins']['items']: try: #only store tweets with lant/lng data if checkin['venue']['location']['lat'] is not '': #build the data for the DataPoint model untappd_content = DataPoint( title = checkin['beer']['beer_name'], latitude = checkin['venue']['location']['lat'], longtitude = checkin['venue']['location']['lng'], reference_id = checkin['checkin_id'], pub_date = datetime.strptime(checkin['created_at'][0:-6], '%a, %d %b %Y %H:%M:%S'), source = untappd ) untappd_content.save() count_untappd +=1 except Exception as exeption: print (exeption) print (str(count_untappd) + ' untappd checkins saved')