def im_moody(mood_words): """ Main mood searching def. This will ping the twitter search api every [SLEEP_TIME] seconds and find tweets that display a mood, and have a location """ # Found it easier to store the parameters as a list # so I can update the since_id params_list = [ "q=query", "rpp=100", "since_id=1" ] # Get the SimpleGeo Client client = Client(SIMPLE_GEO_TOKEN, SIMPLE_GEO_SECRET) # update the query to the mood words params_list[0] = "q=%s" % '+OR+'.join(mood_words) while True: # Init an empty list of records records = [] # Search twitter json = get_twitter_search_json('&'.join(params_list)) # if we got something back if json: # store the results results = json["results"] # set the since_id so we don't search more than we need params_list[2] = "since_id=%s" % json['max_id'] # if we got at least 1 result if len(results) > 0: # get the SimpleGeo records from the api records = get_records(results) else: print "API Error: No data returned" # Save 90 records at a time for chunk in chunker(records, 90): # add records to the SimpleGeo Layer client.add_records(SIMPLE_GEO_LAYER, chunk) # how many records added print "%s %s records added" % (len(chunk), mood_words[0]) # Wait x seconds before continuing time.sleep(SLEEP_TIME)
def im_moody(mood_words): """ Main mood searching def. This will ping the twitter search api every [SLEEP_TIME] seconds and find tweets that display a mood, and have a location """ # Found it easier to store the parameters as a list # so I can update the since_id params_list = ["q=query", "rpp=100", "since_id=1"] # Get the SimpleGeo Client client = Client(SIMPLE_GEO_TOKEN, SIMPLE_GEO_SECRET) # update the query to the mood words params_list[0] = "q=%s" % '+OR+'.join(mood_words) while True: # Init an empty list of records records = [] # Search twitter json = get_twitter_search_json('&'.join(params_list)) # if we got something back if json: # store the results results = json["results"] # set the since_id so we don't search more than we need params_list[2] = "since_id=%s" % json['max_id'] # if we got at least 1 result if len(results) > 0: # get the SimpleGeo records from the api records = get_records(results) else: print "API Error: No data returned" # Save 90 records at a time for chunk in chunker(records, 90): # add records to the SimpleGeo Layer client.add_records(SIMPLE_GEO_LAYER, chunk) # how many records added print "%s %s records added" % (len(chunk), mood_words[0]) # Wait x seconds before continuing time.sleep(SLEEP_TIME)
def main(): tweet_ids = [] client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET) my_tweets = tweepy.api.user_timeline(MY_TWITTER_USERNAME) print "\n\n" for tweet in my_tweets: if (tweet.geo): lat = tweet.geo['coordinates'][0] lng = tweet.geo['coordinates'][1] else: lat = 0 lng = 0 if (tweet.place): place_name = tweet.place['full_name'] place_URL = tweet.place['url'] else: place_name = "None" place_URL = "None" tweet_URL = "http://twitter.com/" + tweet.user.screen_name + "/" + str(tweet.id) created_t = tweet.created_at - datetime.timedelta(seconds=7*60*60) created_t = int(time.mktime(created_t.timetuple())) if (tweet.geo): record = Record( layer=MY_LAYER, id=str(tweet.id), lat=lat, lon=lng, created=created_t, text=tweet.text, URL=tweet_URL, type="object" ) print "\nCreated record with ID: %s" % record.id records.append(record) for chunk in chunker(records, 90): client.add_records(MY_LAYER, chunk) print "\n%s records added" % len(chunk)