def getTweets(politician_id, searchOnlySexistWords): try: politician = Politician.objects.get(id=politician_id) politician_names = [ politician.first_name + " " + politician.last_name, politician.username ] tso = TwitterSearchOrder() searchTerms = [] if searchOnlySexistWords: sexistWords = CONFIG['SEXISTWORDS'] for word in sexistWords: for politician_name in politician_names: searchTerms.append(word + ' ' + politician_name) elif searchOnlySexistWords is False: searchTerms = politician_names tso.set_keywords(searchTerms, or_operator=True) tso.set_language("en") tso.set_include_entities(False) querystr = tso.create_search_url() tso.set_search_url(querystr + "&tweet_mode=extended") ts = TwitterSearch(consumer_key=CONFIG['CONSUMER_KEY'], consumer_secret=CONFIG['CONSUMER_SECRET'], access_token=CONFIG['ACCESS_TOKEN'], access_token_secret=CONFIG['ACCESS_TOKEN_SECRET']) print("**Processing tweets for " + str(politician.first_name + " " + politician.last_name) + "**") if searchOnlySexistWords: tweets = ts.search_tweets_iterable(tso) return tweets else: # will limit to 100 if not only searching sexist words tweets = ts.search_tweets(tso) return tweets['content']['statuses'] except TwitterSearchException as e: logging.exception("Unable to get new tweets because of" + str(e))
def getStats(self, url, proxy, headers, timeout): """returns (retweet + favorite count) count from twitter API , url is url that could be in a tweet, proxy is 'ip:port' in string, headers should contain user-agent in as an item in dictionary, timeout is maximum time while waiting for response and is an int""" count = 0 tso = TwitterSearchOrder() tso.set_search_url('q=' + url) tso.set_result_type(result_type='mixed') tso.set_include_entities(False) tso.set_count(100) ts = TwitterSearch(consumer_key=self.ConsumerKey, consumer_secret=self.ConsumerSecret, access_token=self.AccessTokenKey, access_token_secret=self.AccessTokenSecret, proxy=proxy) for tweet in ts.search_tweets_iterable(tso): count += tweet['retweet_count'] + tweet['favorite_count'] return count
def getTweets(politician_id): try: politician = Politician.objects.get(id=politician_id) politician_names = [politician.first_name + " " + politician.last_name, politician.last_name, politician.username] print("Getting Tweets for " + str(politician.first_name + " " + politician.last_name)) tso = TwitterSearchOrder() sexistWords = ['bitch', 'skank', 'rape'] searchTerms = [] for word in sexistWords: for politician in politician_names: searchTerms.append(word + ' ' + politician) tso.set_keywords(searchTerms, or_operator=True) print(searchTerms) tso.set_language("en") tso.set_include_entities(False) querystr = tso.create_search_url() tso.set_search_url(querystr + "&tweet_mode=extended") ts = TwitterSearch( consumer_key = os.environ.get('CONSUMER_KEY', CONFIG['CONSUMER_KEY']), consumer_secret = os.environ.get('CONSUMER_SECRET', CONFIG['CONSUMER_SECRET']), access_token = os.environ.get('ACCESS_TOKEN', CONFIG['ACCESS_TOKEN']), access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET', CONFIG['ACCESS_TOKEN_SECRET']) ) return ts.search_tweets_iterable(tso) except TwitterSearchException as e: logging.exception("Unable to get new tweets because of" + str(e)) # if __name__ == "__main__": # getTweets()
try: tso = TwitterSearchOrder() tso.set_language('en') tso.set_locale('en') tso.set_keywords(['airline mergers']) url = "https://twitter.com/search"+tso.create_search_url() print url response = urllib2.urlopen('http://www.baidu.com/') html = response.read() print html except TwitterSearchException as e: print(e) tso2 = TwitterSearchOrder() tso2.set_search_url(querystr + '&result_type=mixed&include_entities=true') tso2.set_locale('en') print(tso2.create_search_url()) tso = TwitterSearchOrder() keywords = open("/usr/python/progect/feature/"+filename+".txt",'r+').read() keywords = keywords.split("\n") tso.set_keywords("airline mergers") ts = TwitterSearch( consumer_key='ooRo1YonXsAvS7RsgNYSCdxws', consumer_secret='0zX2rbeNiz5qxnUC9MB9uUErTB9EGDSG4OtU57tuPCsNI5u6M2', access_token='3107850807-zCmoMZSwaRCvjuw9wybqBoyrGIDIkRoF7RU2am3', access_token_secret='FrHzLCkIHTXk8h62E5Pyl5ISNfk6ux3hvJDQiiEt9JqRE' )