Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hackingbutlegal/93a0ea6e0a3bacb587266c5f7e283347 to your computer and use it in GitHub Desktop.
Save hackingbutlegal/93a0ea6e0a3bacb587266c5f7e283347 to your computer and use it in GitHub Desktop.
import tweepy
import time
import datetime
import requests
import uuid
import pymongo
from pymongo import MongoClient
def limit_handler(cursor):
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
time.sleep(15 * 60)
def fabssentimentanalysis(currTweet):
response = requests.get(
"https://[REDACTED].azurewebsites.net/api/WhatDidYouSay?[REDACTED]==&Text=" + currTweet)
print(response.text)
if len(response.text) > 2:
return response.text
else:
return False
auth = tweepy.OAuthHandler('[REDACTED]', '[REDACTED]')
auth.set_access_token('33642599-[REDACTED]',
'[REDACTED]')
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
user = api.me()
# print(user)
# print(user.screen_name)
"""
for follower in tweepy.Cursor(api.followers).items():
print(follower.name)
"""
fabsmongoinstance = MongoClient('mongodb://localhost:27017')
db = fabsmongoinstance['fabsplaypenalpha']
collection = db['tweets_isvs']
def persistTweetsWithSentiments(tweeter,loc,ts,original,msg,score):
try:
post = {'_id': str(uuid.uuid4()), 'author': tweeter, 'location': loc, 'timesent': ts, 'tweetoriginal': original, 'tweeetmsg': msg, 'sentimentscore': score}
result = collection.insert_one(post)
return True
except ValueError as e:
print('Error caught: ' + str(e))
return False
def processtweets():
searchTerm = 'SharePoint OR AzureFunctions OR CognitiveServices OR AzureML OR Microsoft365 OR MicrosoftGraph'
number_of_tweets = 1000
curindex = 1
print('...about to start')
for tweet in tweepy.Cursor(api.search, q=searchTerm, lang='en').items(number_of_tweets):
try:
sentimentresult = fabssentimentanalysis(tweet.text)
if sentimentresult == False:
print('nothing to show')
else:
if float(sentimentresult) > .9:
print('Excellent Tweet Score :' + str(sentimentresult) + ' for Tweet: ' + str(tweet.text))
tweet.retweet()
saveStatus = persistTweetsWithSentiments(tweet.user.screen_name, tweet.user.location, tweet.created_at, True, tweet.text, float(sentimentresult))
print('MongoDb Save Status: ' + str(saveStatus))
print('Num ' + str(curindex) + ' Tweet was Liked: ' + str(tweet.text))
time.sleep(60 * 5)
curindex += 1
elif .85 < float(sentimentresult) < .89:
print('Satisfactory Tweet Score :' + str(sentimentresult) + ' for Tweet: ' + str(tweet.text))
tweet.favorite()
saveStatus = persistTweetsWithSentiments(tweet.user.screen_name, tweet.user.location, tweet.created_at, True, tweet.text, float(sentimentresult))
print('MongoDb Save Status: ' + str(saveStatus))
print('Num ' + str(curindex) + ' Tweet was Liked: ' + str(tweet.text))
time.sleep(60 * 3)
curindex += 1
else:
print('Potentially Unsatisfactory Tweet Sentiment :' + str(sentimentresult) + ' for Tweet: ' + str(tweet.text))
saveStatus = persistTweetsWithSentiments(tweet.user.screen_name, tweet.user.location, tweet.created_at, True, tweet.text, float(sentimentresult))
print('MongoDb Save Status: ' + str(saveStatus))
except tweepy.TweepError as e:
if '139' in e.reason:
time.sleep(5)
elif '429' in e.reason:
print('429 Error Caught: ' + e.reason)
time.sleep(60 * 15)
else:
print('Error Caught: ' + e.reason)
except StopIteration:
break
while True:
processtweets()
# print('Processing Tweets ended with ' + str(processtweets.totalcount) + ' tweets')
print('Going to sleep now: ' + str(datetime.datetime.now()))
# do it all over again in 3 hours
time.sleep(60 * 180)
print('Waking up at: ' + str(datetime.datetime.now()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment