from twython import Twython from urllib.request import urlopen import calendar import json class Reader: def __init__(self): self.TWITTER_APP_KEY = 'YT17H8JArRtK4XPpusXHn8AW6' #supply the appropriate value self.TWITTER_APP_KEY_SECRET = 'NrEazP4L1AAWQ64Pl5rx9L97BBuGKxfuvxGX4nRdnX9sr4FzVK' self.TWITTER_ACCESS_TOKEN = '734805952898072580-OZ4mrfyWrJDllVkwVD7K7GzrT8hFumw' self.TWITTER_ACCESS_TOKEN_SECRET = 'OEejoUGnb2D8Vieoz3GcacYRumMICdYHEhS10Vhqr8KeJ' gist = "https://goo.gl/xQ7Tdl" self.words = [] self.log = open('log.txt', 'ab') data = urlopen(gist) for line in data: self.words.append(line[:-1]) self.verified_tweets = [] def get_tweets(self, hashtag = None, numTweets = 100): passed = True self.verified_tweets = [] t = Twython(app_key=self.TWITTER_APP_KEY, app_secret=self.TWITTER_APP_KEY_SECRET, oauth_token=self.TWITTER_ACCESS_TOKEN, oauth_token_secret=self.TWITTER_ACCESS_TOKEN_SECRET) search = t.search(q='#'+hashtag, #**supply whatever query you want here** count=numTweets, lang='en', result_type='mixed') tweets = search['statuses'] for tweet in tweets: passed = True text = tweet.get('text').lower().encode('utf-8') for word in self.words: if text.find(word) != -1: passed = False break if passed: self.verified_tweets.append(tweet) return self.verified_tweets def print_tweets(self, tweets): try: for tweet in tweets: self.print_tweet(tweet) except BaseException as e: print(e, file=self.log) print("***********Print Failed***********\n") def print_tweet(self, tweet): hashtags = [] if tweet.get("entities", None) and tweet["entities"].get("hashtags", None): for hashtag in tweet["entities"]["hashtags"]: if hashtag.get("text", None): hashtags.append(hashtag["text"].encode("utf-8").decode("utf-8")) try: print("{") print(" id: " + str(tweet["id_str"])) print(" created_at: " + str(tweet["created_at"])) print(" text: " + tweet["text"].encode('utf-8').decode('utf-8')) print(" hashtags: " + (", ".join(hashtags) if len(hashtags) > 0 else "none")) print(" user: {") print(" author: " + str(tweet["user"]["name"])) print(" id: " + str(tweet["user"]["id"])) print(" statuses: " + str(tweet["user"]["statuses_count"])) print(" favorites: " + str(tweet["user"]["favourites_count"])) print(" friends: " + str(tweet["user"]["friends_count"])) print(" followers: " + str(tweet["user"]["followers_count"])) print(" following: " + str(tweet["user"]["following"])) print(" }") print("}\n") except BaseException as e: print(e, file=self.log) print("***********Print Failed***********\n") def get(self, tweet, key): if str(key) == 'author': return tweet["user"]["name"] if str(key) == 'hashtags': hashtags = [] if tweet.get("entities", None) and tweet["entities"].get("hashtags", None): for hashtag in tweet["entities"]["hashtags"]: if hashtag.get("text", None): hashtags.append(hashtag["text"].encode("utf-8").decode("utf-8")) return hashtags if str(key) == 'time': t = tweet["created_at"] t = t.split(" ") month = list(calendar.month_abbr).index(str(t[1])) if month < 10: month = '0' + str(month) month = str(month) time = month + '/' + str(t[2]) + '/' + str(t[5]) + ' ' + str(t[3]) return time if str(key) == 'text': return tweet['text'].encode('utf-8').decode('utf-8') return "Invalid Parameter, must provide a tweet, and one of: author, hashtags, time"