def index(): try: APP_KEY = "KEY"; APP_SECRET = "SECRET"; twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() text = request.form['text'] twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN) search = twitter.cursor(twitter.search,q=text,count=1000) totalSent = 0 i = 0 for result in search: if i > 500: break; totalSent += TextBlob(result['text']).sentiment.polarity i += 1 return render_template('index.html',name = "the twitter gods rated " + text, finding = str(wordOfIt(round(totalSent/i,2))),errorThing = None) except: return render_template('index.html',name = None, finding = None, errorThing = "There was an error with your input. Please try something else!")
def index(): try: APP_KEY = "KEY" APP_SECRET = "SECRET" twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() text = request.form['text'] twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN) search = twitter.cursor(twitter.search, q=text, count=1000) totalSent = 0 i = 0 for result in search: if i > 500: break totalSent += TextBlob(result['text']).sentiment.polarity i += 1 return render_template('index.html', name="the twitter gods rated " + text, finding=str(wordOfIt(round(totalSent / i, 2))), errorThing=None) except: return render_template( 'index.html', name=None, finding=None, errorThing= "There was an error with your input. Please try something else!")
def search_keyword(keyword_dic, function_name): twitter_list = [] hash_list = [] # Fill in your keys and tokens APP_KEY= '' APP_SECRET = '' OAUTH_TOKEN = '' OAUTH_TOKEN_SECRET = '' twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) SUPPORTED_LANGUAGE = ['zh', 'zh-Hant', 'en', 'fr', 'de', 'it', 'ja', 'ko', 'pt', 'es', ] keyword = keyword_dic["main_keyword"] + ' "' + keyword_dic["restriction"] +'"' try: results = twitter.cursor(twitter.search, q=keyword, result_type = 'recent' , count = COUNT, include_entities = True) if function_name == 'keyword_sentiment': MAX_TWEETS = 30 elif function_name == 'picture_list': MAX_TWEETS = COUNT * 10 else: print("wrong function name") for idx, status in enumerate(results): # 'results' is a generator. It yields tweet objects if idx < MAX_TWEETS: #print(idx) content={} content['lang'] = status['lang'] hashValue = hash(status["text"]) #if texts are identical, hash value is same flag = False if function_name == 'keyword_sentiment': flag = content['lang'] in SUPPORTED_LANGUAGE elif function_name == 'picture_list': flag = (content['lang'] in SUPPORTED_LANGUAGE) and ('media' in status['entities']) else: print("wrong function name.") if flag: if (hashValue not in hash_list) : #or (content["hash"] in twitter_list and content['text'] not in twitter_list) hash_list.append(hashValue) content["text"] = filter(status['text']) content["entities"] = status['entities'] content["retweet_count"] = status['retweet_count'] # return int content["favorite_count"] = status['favorite_count'] #return integer or Nullable twitter_list.append(content) else: break except TwythonError as e: if e.error_code == 429: print("Too many requests!") else: print(e.error_code) # print("begin sleep for 15 minutes. Please wait...") # time.sleep(60 * 15) # print("wake up!") except StopIteration: pass return twitter_list
def search_keyword(self): """return a list of tweets""" hash_list = [] # Fill in your keys and tokens twitter = Twython(self.consumer_key, self.consumer_secret, self.access_token, self.access_token_secret) SUPPORTED_LANGUAGE = ['zh', 'zh-Hant', 'en'] if len(self.keyword) == 0: self.keyword = "coronavirus" try: results = twitter.cursor(twitter.search, q=self.keyword, result_type='recent', count=COUNT, include_entities=True) MAX_TWEETS = 30 for idx, status in enumerate( results ): # 'results' is a generator. It yields tweet objects if idx < MAX_TWEETS: gap = '\n' content = {} if os.path.exists("keys"): with open("data.json", "w") as f: json.dump(status, f) else: with open("data.json", "r") as f: status = json.load(f) return status content['lang'] = status['lang'] hashValue = hash( status["text"] ) #if texts are identical, hash value is same if content['lang'] in SUPPORTED_LANGUAGE: if ( hashValue not in hash_list ): #or (content["hash"] in twitter_list and content['text'] not in twitter_list) hash_list.append(hashValue) print(status['text']) print(self.filter(status['text'])) tweet_list = list(self.filter(status['text'])) print("Success") for i in range(len(tweet_list)): if (i % 50) == 0: tweet_list.insert(i, gap) tweetText = ''.join(tweet_list) self.twitter_list.append(tweetText) else: break except TwythonError as e: if e.error_code == 429: print("Too many requests!") else: print(e.error_code) except StopIteration as s: print(s.error_code) return self.twitter_list
class YalgaarTwitterInterface: '''gets the data out from Twitter and caches it''' hashtag = '#SaveTheInternet' t = None limit = 100 #The number of tweets we will collect def __init__(self, hashtag=None, limit=50, tweet_type='mixed'): ''' :hashtag: the hashtag for searching :limit: how many tweets to collect :tweet_type: what type of tweet to collect - popular/recent/mixed. See Twitter API docs ''' if hashtag: self.hashtag = hashtag self.limit = limit self.tweet_type = tweet_type self.t = Twython(APP_KEY, access_token=ACCESS_TOKEN) def get_data(self): results = self.t.cursor( self.t.search, q=self.hashtag, count=( self.limit * 2 ), #this is to so that we have enough tweets which remain even after filters below result_type=self.tweet_type, ) try: count = 0 tweets = {} for r in results: #results is a GENERATOR! if count >= self.limit: break #filter out these tweets further #we don't want anyone's conversations #we don't want to snoop on someone's conversations if r['in_reply_to_screen_name'] is not None: continue elif r['text'].startswith('RT'): continue #We don't want any literal or direct RTs (Literal: Manually prefix 'RT', direct: Someone RT's but twitter gives us this text) else: tweets[r['text']] = r count = count + 1 return tweets except Exception as e: raise e return False
def get_sentiments(ticker): sentiment_data = "" twitter = Twython("04XLdRImNqO1SROyP9BqWEbOV", "A18ltEyce5T5Fcy14ycNxnKFEKo3kK5TLx4c0RHhIw0izr7NrI") results = twitter.cursor(twitter.search, q=ticker) count = 1 for result in results: if (count > 100): break sentiment_data = sentiment_data + " " + str(sentiment(result['text'])) count = count + 1 return sentiment_data
def search(keyword): twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) # result = twitter.search(q=keyword) # print str(result['search_metadata']['count']) + ' tweets' # print result results = twitter.cursor(twitter.search, q=keyword) for result in results: print(result)
def november(): twitter = Twython(consumer_key, consumer_secret, access_token, access_secret) tango = request.form['tango'] tweets = [] prenegative = [] prepositive = [] negative = [] neutral = [] positive = [] collection = [] count = 0 multiplier = 50 results = twitter.cursor(twitter.search, q=tango, result_type='popular', lang='en') for result in results: if count == 25: break tweet = result['text'] tweets.append(tweet) count += 1 sid2 = SentimentIntensityAnalyzer() for tweet in tweets: ss2 = sid2.polarity_scores(tweet) for key, value in ss2.items(): if key == 'neg': prenegative.append(value) if key == 'neu': neutral.append(value) if key == 'pos': prepositive.append(value) for x in prepositive: multiplier *= float(x) positive.append(multiplier) multiplier = 50 for x in prenegative: multiplier *= float(x) negative.append(multiplier) multiplier = 50 collection = tweets + positive + neutral + negative return jsonify(collection)
def request_tweets(q_list=['python', 'rock', 'food', 'joker']): # topics = [''] twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret) keyword = q_list[int(random.random() * len(q_list))] results_generator = twitter.cursor(twitter.search, q=keyword) result = next(results_generator) data = { 'username': result['user']['name'], 'content': parse_tweets_content(result['text']), 'keyword': keyword } return data
class YalgaarTwitterInterface: '''gets the data out from Twitter and caches it''' hashtag = '#SaveTheInternet' t = None limit = 100 #The number of tweets we will collect def __init__(self, hashtag = None, limit = 50): ''' :hashtag: the hashtag for searching ''' if hashtag: self.hashtag = hashtag self.limit = limit self.t = Twython(APP_KEY, access_token=ACCESS_TOKEN) def get_data(self): results = self.t.cursor(self.t.search, q = self.hashtag, count = (self.limit + 100) #this is random result_type = 'mixed' ) try: count = 0 tweets = {} for r in results: #results is a GENERATOR! if count >= self.limit: break; #filter out these tweets further #we don't want anyone's conversations #we don't want to snoop on someone's conversations if r['in_reply_to_screen_name'] is not None: continue; else: tweets[r['text']] = r count = count + 1 return tweets except Exception as e: raise e return False
class TwitterConnector(models.Model): token = models.CharField(u'token', max_length=250) def __unicode__(self): return self.token def save(self, *args, **kwargs): if TwitterConnector.objects.all().exists(): TwitterConnector.objects.all().delete() twitter = Twython(settings.APP_KEY, settings.APP_SECRET, oauth_version=2) self.token = twitter.obtain_access_token() super(TwitterConnector, self).save(*args, **kwargs) def get_results(self, *args): self.twitter = Twython(settings.APP_KEY, access_token=self.token) return self.twitter.cursor(self.twitter.search, q=" ".join(args))
def get_followerdata(self, screen_name: str): -> twitter = Twython(APP_KEY, APP_SECRET) screen_name = screen_name user = [] users = [] # 対象のユーザー名からフォロワーidリストを取得 followers_ids = [fw for fw in twitter.cursor(twitter.get_followers_ids, screen_name=screen_name)] for i in followers_ids: user.append(i) if len(user) == 100: users_data = twitter.lookup_user(user_id=user) print("ok") # users.append(users_data) users.extend(users_data) print(len(users_data)) print(users) user.clear() users_data = twitter.lookup_user(user_id=user) users.extend(users_data)
from twython import Twython APP_KEY = "h0rZYPs6iDq4orLJzBLJ0iFYl" APP_SECRET = "y6A4U5QsfdIRzbbUNY493d696BmSVZdb4NPHRvPNp8jFpzduI4" twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() twitter = Twython(APP_KEY, access_token = ACCESS_TOKEN) tag = raw_input("Enter the term to search for-->") results = twitter.cursor(twitter.search, q=tag) target = open("TwittrSolrData.txt", 'a') #target = open("TwittrSolrData.txt", 'a') For appending to the file #number_of_tweets = raw_input("Enter the number of tweets that you want to collect-->") i = 0 key = raw_input("Enter the key-->") for result in results: #target.write(result) #target.write('\n') print result target.write(str(result[key]).encode("utf-8") + '\n') #target.write(str(result) + '\n') i = i+1 if (i > 4): break #print "File has been written to" twitter.disconnect()
credentials['CONSUMER_SECRET'] = os.environ.get('INTERESTIFY_CONSUMER_SECRET', '') credentials['ACCESS_TOKEN'] = os.environ.get('INTERESTIFY_ACCESS_TOKEN', '') credentials['TOKEN_SECRET'] = os.environ.get('INTERESTIFY_TOKEN_SECRET', '') # Save the credentials object to file with open("twitter_credentials.json", "w") as credentialsFile: json.dump(credentials, credentialsFile) twitter = Twython(app_key=credentials['CONSUMER_KEY'], app_secret=credentials['CONSUMER_SECRET'], oauth_token=credentials['ACCESS_TOKEN'], oauth_token_secret=credentials['TOKEN_SECRET']) results = twitter.cursor(twitter.search, q='Game of Thrones', result_type='popular') followers = twitter.get_followers_list(screen_name="pewdiepie") tweet_list = [] try: for result in results: tweet_list.append(result) except: pass users = get_users(tweet_list) for follower in followers: print(follower)
for sqlrow in db.execute(sql): registered.append(sqlrow['from_user']) # now follow these registered users with open(r'../conf/twitter/twitter.yml') as file: conf = yaml.full_load(file) user = conf["auth"]["user"] app_key = conf["auth"]["app_key"] app_secret = conf["auth"]["app_secret"] oauth_token = conf["auth"]["oauth_token"] oauth_token_secret = conf["auth"]["oauth_token_secret"] twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret) followers = [] for fid in twitter.cursor(twitter.get_followers_ids, count=5000): followers.append(fid) print("Followers Count: ", len(followers)) limit1 = twitter.get_lastfunction_header('x-rate-limit-remaining') print("x-rate-limit-remaining: ", limit1) friends = [] for fid in twitter.cursor(twitter.get_friends_ids, count=5000): friends.append(fid) print("Friends Count: ", len(friends)) limit2 = twitter.get_lastfunction_header('x-rate-limit-remaining') print("x-rate-limit-remaining: ", limit2) pending = []
# A rate limiter to handle the Twython generator and the Twitter API's tweet retrieval limits # user id as dict key filters out duplicate tweets def rate_limit(my_gen, seconds=10): stop = 50 gen_dict = {} while stop != 500: limit = islice(my_gen, stop) for i in limit: gen_dict[i['user']['id']] = i['user']['location'] stop += 50 time.sleep(seconds) return gen_dict with open('secret_twitter_credentials.pkl', 'rb') as pkl: Twitter = pickle.load(pkl) api = Twython(app_key=Twitter['Consumer Key'], app_secret=Twitter['Consumer Secret'], oauth_token=Twitter['Access Token'], oauth_token_secret=Twitter['Access Token Secret']) results = api.cursor(api.search, q='firework conspiracy') rate_dict = rate_limit(results) with open('rate_locs.csv', 'a') as loc_file: # Filters out None returned from locate function when users tag locations with no lat or long # ie. Philly, New York State of Mind, etc. for loc in filter(None, map(locate, rate_dict.values())): loc_file.write(loc)
from twython import Twython APP_KEY = 'RDCuCPIL9O6l5hObhgpStqakU' APP_SECRET = 'mCXLR9g9oNVeoQDWSX4B5AnMzQtvUozig9muKFl55RuZaHN22o' OAUTH_TOKEN = '156980782-uxD8gQva2kM2lCTwGShBqarLovHbrXTs1S9IRr15' OAUTH_TOKEN_SECRET = 'OGahmQqSuqltN98KOHeeg91gMN3EDrWGqvUVoerM3mGqj' twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) results = twitter.cursor(twitter.search, q='Google OR google OR #google OR $GOOG OR #Google OR #GOOGLE', lang='en', count='10', until='2014-12-30') for result in results: print result
if __name__ == '__main__': from twython import Twython try: count = 1 max_id = '' creds = load_credentials() twitter = Twython(creds['credentials']['CONSUMER_KEY'], creds['credentials']['CONSUMER_SECRET'], creds['credentials']['ACCESS_TOKEN'], creds['credentials']['ACCESS_SECRET']) api_url = 'https://api.twitter.com/1.1/search/tweets.json' final = [] results = twitter.cursor(twitter.search, q='Narendra Modi', tweet_mode='extended', lang='en', count=100, max_id=max_id) for result in results: tweet = dict() tweet['id'] = result['id'] try: tweet['full_text'] = str(result['full_text'].encode('utf8')) except KeyError as e: tweet['full_text'] = str(result['text'].encode('utf8')) tweet['location'] = result['user']['location'] try: tweet['hashtags'] = result['entities']['hashtags'][0]['text'] except Exception as ee:
from twython import Twython consumer_key = "G8v4IHt7misK6qliT5eH3p1Rp" consumer_secret = "uw3O4u9GXTdS53aS9KEDuSsdbiOLV0kN7MK3H7ZpawbM7yWHh5" access_token_key = "22895708-O5NdDSJRKxtuTIWrAGxpNaWPKUHG1CTj8QJbjjilS" access_token_secret = "sx2KjzCWxPCDOmQhe4cQrYQpT3Y6w0algyBcUaKzMBZXt" twitter = Twython( app_key=consumer_key, app_secret=consumer_secret, oauth_token=access_token_key, oauth_token_secret=access_token_secret, ) print "twitter: {}".format(twitter) results = twitter.cursor(twitter.search, q="#python") for result in results: print result break
#!/usr/bin/python2 # -*- coding: utf-8 -*- from twython import Twython APP_KEY = 'QkXeFkMDm6Mx75PzcDQkqg' APP_SECRET = 'LY0tb8UM0Ye1X0Bk3Z99BsWKXd0I5zoG3A6wfiYErY' OAUTH_TOKEN = 'Pwjotx1YyaqiVce77HFeOxWJxWvk8KgXYCLykZDL9ik' OAUTH_TOKEN_SECRET='1893564546-biXRsHcfyqnsTu0Qnaz9rU4BxqeBT4PmFFhWBRH' twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) search = twitter.cursor(twitter.search, q='Energiewende') for result in search: print result
from twython import Twython APP_KEY = 'jByrnJjBK1qwXbAs5IIXlGc7Q' APP_SECRET = 'ASQsvQnu0KGpPFakTDKbezy8dzYy9YEHGtGxy2ocgww2HGe322' OAUTH_TOKEN = '747471719342841856-ID2YX5V3iM4suIxyIWMZSmfVnnlwjcZ' OAUTH_TOKEN_SECRET = 'hyL409iSVq8LWLUNDqwQdIBiySm1fHYOucYXsIhub62cs' twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) twitter.verify_credentials() twitter.get_home_timeline() # t = twitter.search(q='herramienta') results = twitter.cursor(twitter.search, q='herramienta', count=10) for result in results: print(result['text']) # print(t) # ACCESS_TOKEN = twitter.obtain_access_token()
"Num_Negative_Tweets": 0 } QUERY = "microsoft" count = 0 coords = 0 geo = 0 place = 0 tweet_text = [] tweet_coords = [] tweets = [] classifications = [] geolocations = [] print 'making request' #radius with almost all of the US. Close to getting all geotagged tweets for analysis tweets = twitter.cursor(twitter.search, q="QUERY", count=1, geocode="40.0,-100.0,6mi") print 'made request' for tweet in tweets: tweets[count] = tweet count += 1 print tweet.keys() sys.exit(0) print tweet['coordinates'] #sleep right before hitting rate limit of twitter API. Sleep for 15 minutes if count % 17 == 0: print 'sleeping' for i in range(0, len(tweets)): classifications[i] = a.predict(tweets[i]['text']) s = geocoder.reverse(tweets[i]['coordinates'])
class TwythonAPITestCase(unittest.TestCase): def setUp(self): self.api = Twython('', '', '', '') def get_url(self, endpoint): """Convenience function for mapping from endpoint to URL""" return '%s/%s.json' % (self.api.api_url % self.api.api_version, endpoint) def register_response(self, method, url, body='{}', match_querystring=False, status=200, adding_headers=None, stream=False, content_type='application/json; charset=utf-8'): """Wrapper function for responses for simpler unit tests""" # responses uses BytesIO to hold the body so it needs to be in bytes if not is_py2: body = bytes(body, 'UTF-8') responses.add(method, url, body, match_querystring, status, adding_headers, stream, content_type) @responses.activate def test_request_should_handle_full_endpoint(self): """Test that request() accepts a full URL for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request(url) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_relative_endpoint(self): """Test that request() accepts a twitter endpoint name for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request('search/tweets', version='1.1') self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_post_request_regardless_of_case(self): """Test that request() accepts the HTTP method name regardless of case""" url = 'https://api.twitter.com/1.1/statuses/update.json' self.register_response(responses.POST, url) self.api.request(url, method='POST') self.api.request(url, method='post') self.assertEqual(2, len(responses.calls)) self.assertEqual('POST', responses.calls[0].request.method) self.assertEqual('POST', responses.calls[1].request.method) @responses.activate def test_request_should_throw_exception_with_invalid_http_method(self): """Test that request() throws an exception when an invalid HTTP method is passed""" # TODO(cash): should Twython catch the AttributeError and throw a TwythonError self.assertRaises(AttributeError, self.api.request, endpoint='search/tweets', method='INVALID') @responses.activate def test_request_should_encode_boolean_as_lowercase_string(self): """Test that request() encodes a boolean parameter as a lowercase string""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'include_entities': True}) self.api.request(endpoint, params={'include_entities': False}) self.assertEqual(url + '?include_entities=true', responses.calls[0].request.url) self.assertEqual(url + '?include_entities=false', responses.calls[1].request.url) @responses.activate def test_request_should_handle_string_or_number_parameter(self): """Test that request() encodes a numeric or string parameter correctly""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'lang': 'es'}) self.api.request(endpoint, params={'count': 50}) self.assertEqual(url + '?lang=es', responses.calls[0].request.url) self.assertEqual(url + '?count=50', responses.calls[1].request.url) @responses.activate def test_request_should_encode_list_of_strings_as_string(self): """Test that request() encodes a list of strings as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = ['37.781157', '-122.39872', '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) # requests url encodes the parameters so , is %2C self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_encode_numeric_list_as_string(self): """Test that request() encodes a list of numbers as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = [37.781157, -122.39872, '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_ignore_bad_parameter(self): """Test that request() ignores unexpected parameter types""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': self}) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_file_as_parameter(self): """Test that request() pulls a file out of params for requests lib""" endpoint = 'account/update_profile_image' url = self.get_url(endpoint) self.register_response(responses.POST, url) mock_file = StringIO("Twython test image") self.api.request(endpoint, method='POST', params={'image': mock_file}) self.assertIn(b'filename="image"', responses.calls[0].request.body) self.assertIn(b"Twython test image", responses.calls[0].request.body) @responses.activate def test_request_should_put_params_in_body_when_post(self): """Test that request() passes params as data when the request is a POST""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.request(endpoint, method='POST', params={'status': 'this is a test'}) self.assertIn(b'status=this+is+a+test', responses.calls[0].request.body) self.assertNotIn('status=this+is+a+test', responses.calls[0].request.url) @responses.activate def test_get_uses_get_method(self): """Test Twython generic GET request works""" endpoint = 'account/verify_credentials' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_post_uses_post_method(self): """Test Twython generic POST request works""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.post(endpoint, params={'status': 'I love Twython!'}) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) def test_raise_twython_error_on_request_exception(self): """Test if TwythonError is raised by a RequestException""" with mock.patch.object(requests.Session, 'get') as get_mock: # mocking an ssl cert error get_mock.side_effect = requests.RequestException("hostname 'example.com' doesn't match ...") self.assertRaises(TwythonError, self.api.get, 'https://example.com') @responses.activate def test_request_should_get_convert_json_to_data(self): """Test that Twython converts JSON data to a Python object""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id": 210462857140252672}') data = self.api.request(endpoint, params={'id': 210462857140252672}) self.assertEqual({'id': 210462857140252672}, data) @responses.activate def test_request_should_raise_exception_with_invalid_json(self): """Test that Twython handles invalid JSON (though Twitter should not return it)""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id: 210462857140252672}') self.assertRaises(TwythonError, self.api.request, endpoint, params={'id': 210462857140252672}) @responses.activate def test_request_should_handle_401(self): """Test that Twython raises an auth error on 401 error""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Error"}]}', status=401) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_for_missing_auth_data(self): """Test that Twython raises an auth error on 400 error when no oauth data sent""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Bad Authentication data"}]}', status=400) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_that_is_not_auth_related(self): """Test that Twython raises a normal error on 400 error when unrelated to authorization""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Bad request"}]}', status=400) self.assertRaises(TwythonError, self.api.request, endpoint) @responses.activate def test_request_should_handle_rate_limit(self): """Test that Twython raises an rate limit error on 429""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Rate Limit"}]}', status=429) self.assertRaises(TwythonRateLimitError, self.api.request, endpoint) @responses.activate def test_get_lastfunction_header_should_return_header(self): """Test getting last specific header of the last API call works""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, adding_headers={'x-rate-limit-remaining': '37'}) self.api.get(endpoint) value = self.api.get_lastfunction_header('x-rate-limit-remaining') self.assertEqual('37', value) value2 = self.api.get_lastfunction_header('does-not-exist') self.assertIsNone(value2) value3 = self.api.get_lastfunction_header('not-there-either', '96') self.assertEqual('96', value3) def test_get_lastfunction_header_should_raise_error_when_no_previous_call(self): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') @responses.activate def test_sends_correct_accept_encoding_header(self): """Test that Twython accepts compressed data.""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(b'gzip, deflate', responses.calls[0].request.headers['Accept-Encoding']) # Static methods def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" url = 'https://api.twitter.com/1.1/search/tweets.json' constructed_url = self.api.construct_api_url(url, q='#twitter') self.assertEqual(constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter') def test_encode(self): """Test encoding UTF-8 works""" self.api.encode('Twython is awesome!') def test_cursor_requires_twython_function(self): """Test that cursor() raises when called without a Twython function""" def init_and_iterate_cursor(*args, **kwargs): cursor = self.api.cursor(*args, **kwargs) return next(cursor) non_function = object() non_twython_function = lambda x: x self.assertRaises(TypeError, init_and_iterate_cursor, non_function) self.assertRaises(TwythonError, init_and_iterate_cursor, non_twython_function)
#!/usr/bin/env python from twython import Twython from tokens import * sn="ghost_0836" twitter = Twython(app_key=APP_KEY, app_secret=APP_SECRET, oauth_token=OAUTH_TOKEN, oauth_token_secret=OAUTH_TOKEN_SECRET) followers = twitter.cursor(twitter.get_friends_ids,screen_name = sn) f=open(sn+".txt",'w') i=1 users=[] for follower in followers: users.append(str(follower)) if i==100: i=0 userlist=",".join(users) #print len(users) users=[] results=twitter.lookup_user(user_id=userlist) for result in results: f.write(result['screen_name']+"\n") f.flush() i+=1
mykeys=[] with open("keys.txt") as instream: for line in instream: mykeys.append(line) APP_KEY = mykeys[0].rstrip() APP_SECRET = mykeys[1].rstrip() OAUTH_TOKEN = mykeys[2].rstrip() OAUTH_TOKEN_SECRET = mykeys[3].rstrip() drugs=['abilify','alprazolam','amitriptyline','aplenzin','aripiprazole','atenolol','ativan', 'budeprion','bupropion','buspar','buspirone','celexa','citalopram','clonazepam','clonidine', 'cymbalta','deplin','desvenlafaxine','desyrel','diazepam','doxepin','duloxetine','effexor', 'escitalopram','fetzima','fluoxetine','forfivo','gabapentin','hydroxyzine','lamotrigine ', 'lexapro','librium','lorazepam','methylphenidate','mirtazapine','nortriptyline','olanzapine ', 'oleptro','paroxetine','paxil','pristiq','propranolol','prozac','quetiapine','remeron','seroquel', 'sertraline','tenormin','tramadol','trazodone','trintellix','valium','venlafaxine','viibryd', 'vistaril','wellbutrin','xanax','zoloft','zyprexa'] tw_auth = Twython(APP_KEY, APP_SECRET, oauth_version=2) token = tw_auth.obtain_access_token() twitter = Twython(APP_KEY, access_token=token) #twitter = Twython(APP_KEY, APP_SECRET, #OAUTH_TOKEN, OAUTH_TOKEN_SECRET) results = twitter.cursor(twitter.search, q=drugs, count='100', result_type='recent', lang='en', tweet_mode='extended') for result in results: print(result) processtweet(result)
from twython import Twython consumer_key = 'G8v4IHt7misK6qliT5eH3p1Rp' consumer_secret = 'uw3O4u9GXTdS53aS9KEDuSsdbiOLV0kN7MK3H7ZpawbM7yWHh5' access_token_key = '22895708-O5NdDSJRKxtuTIWrAGxpNaWPKUHG1CTj8QJbjjilS' access_token_secret = 'sx2KjzCWxPCDOmQhe4cQrYQpT3Y6w0algyBcUaKzMBZXt' twitter = Twython( app_key=consumer_key, app_secret=consumer_secret, oauth_token=access_token_key, oauth_token_secret=access_token_secret ) print 'twitter: {}'.format(twitter) results = twitter.cursor(twitter.search, q='#python') for result in results: print result break
saver = TweetSaver() for tweet in timeline: saver.handleTweet(tweet) # For the search API elif search_type == 3: # tweet search twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) t_keep_tweets = keep_tweets # Hi there. You are wondering if you can use more search parameters. Yes, yes you can. # Read about them here: https://dev.twitter.com/docs/api/1.1/get/search/tweets results = twitter.cursor(twitter.search,q=search_terms) maxid = "x" # Do not change this while t_keep_tweets > 100: if maxid == "x": # print "while loop initial search without maxid" results = twitter.search(q=search_terms,count=100) maxid = processTweetsSaveAPI(results) # Why is this a problem? else: # print "while loop paginated search maxid:",maxid results = twitter.search(q=search_terms,max_id=maxid,count=100) maxid = processTweetsSaveAPI(results) t_keep_tweets -= 100 if t_keep_tweets <= 100:
#!/usr/bin/python2 # -*- coding: utf-8 -*- from twython import Twython APP_KEY = 'QkXeFkMDm6Mx75PzcDQkqg' APP_SECRET = 'LY0tb8UM0Ye1X0Bk3Z99BsWKXd0I5zoG3A6wfiYErY' OAUTH_TOKEN = 'Pwjotx1YyaqiVce77HFeOxWJxWvk8KgXYCLykZDL9ik' OAUTH_TOKEN_SECRET = '1893564546-biXRsHcfyqnsTu0Qnaz9rU4BxqeBT4PmFFhWBRH' twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) search = twitter.cursor(twitter.search, q='Energiewende') for result in search: print result
urllib3.disable_warnings(urllib3.exceptions.InsecurePlatformWarning) API_KEY = 'LafZ0C6RH5Xi9wKoPUtUc2p5M' API_SEC = 'ajsmO7BNqHbwxWq7GmxpxG0Jet4ZozpUmzEXFUs5fQuxpwUGlt' twitter = Twython(API_KEY, API_SEC) user_data = twitter.show_user(user_id=user) print(user_data.text) # https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets results = twitter.cursor(twitter.search, q="#WhatIDoNow", result_type='recent', count=25, tweet_mode='extended') max_str_id = None for _result in results: str_id = _result['id_str'] if str_id > max_str_id: max_str_id = str_id # if tweet_mode='extended', use _result['full_text'] text = _result['text'] if 'text' in _result else _result['full_text'] # check if is retweet is_retweet = True if 'retweeted_status' in _result or 'quoted_status' in _result else False
class TwythonAPITestCase(unittest.TestCase): def setUp(self): self.api = Twython('', '', '', '') def get_url(self, endpoint): """Convenience function for mapping from endpoint to URL""" return '%s/%s.json' % (self.api.api_url % self.api.api_version, endpoint) def register_response(self, method, url, body='{}', match_querystring=False, status=200, adding_headers=None, stream=False, content_type='application/json; charset=utf-8'): """Wrapper function for responses for simpler unit tests""" # responses uses BytesIO to hold the body so it needs to be in bytes if not is_py2: body = bytes(body, 'UTF-8') responses.add(method, url, body, match_querystring, status, adding_headers, stream, content_type) @responses.activate def test_request_should_handle_full_endpoint(self): """Test that request() accepts a full URL for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request(url) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_relative_endpoint(self): """Test that request() accepts a twitter endpoint name for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request('search/tweets', version='1.1') self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_post_request_regardless_of_case(self): """Test that request() accepts the HTTP method name regardless of case""" url = 'https://api.twitter.com/1.1/statuses/update.json' self.register_response(responses.POST, url) self.api.request(url, method='POST') self.api.request(url, method='post') self.assertEqual(2, len(responses.calls)) self.assertEqual('POST', responses.calls[0].request.method) self.assertEqual('POST', responses.calls[1].request.method) @responses.activate def test_request_should_throw_exception_with_invalid_http_method(self): """Test that request() throws an exception when an invalid HTTP method is passed""" # TODO(cash): should Twython catch the AttributeError and throw a TwythonError self.assertRaises(AttributeError, self.api.request, endpoint='search/tweets', method='INVALID') @responses.activate def test_request_should_encode_boolean_as_lowercase_string(self): """Test that request() encodes a boolean parameter as a lowercase string""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'include_entities': True}) self.api.request(endpoint, params={'include_entities': False}) self.assertEqual(url + '?include_entities=true', responses.calls[0].request.url) self.assertEqual(url + '?include_entities=false', responses.calls[1].request.url) @responses.activate def test_request_should_handle_string_or_number_parameter(self): """Test that request() encodes a numeric or string parameter correctly""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'lang': 'es'}) self.api.request(endpoint, params={'count': 50}) self.assertEqual(url + '?lang=es', responses.calls[0].request.url) self.assertEqual(url + '?count=50', responses.calls[1].request.url) @responses.activate def test_request_should_encode_list_of_strings_as_string(self): """Test that request() encodes a list of strings as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = ['37.781157', '-122.39872', '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) # requests url encodes the parameters so , is %2C self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_encode_numeric_list_as_string(self): """Test that request() encodes a list of numbers as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = [37.781157, -122.39872, '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_ignore_bad_parameter(self): """Test that request() ignores unexpected parameter types""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': self}) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_file_as_parameter(self): """Test that request() pulls a file out of params for requests lib""" endpoint = 'account/update_profile_image' url = self.get_url(endpoint) self.register_response(responses.POST, url) mock_file = StringIO("Twython test image") self.api.request(endpoint, method='POST', params={'image': mock_file}) self.assertIn(b'filename="image"', responses.calls[0].request.body) self.assertIn(b"Twython test image", responses.calls[0].request.body) @responses.activate def test_request_should_put_params_in_body_when_post(self): """Test that request() passes params as data when the request is a POST""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.request(endpoint, method='POST', params={'status': 'this is a test'}) self.assertIn(b'status=this+is+a+test', responses.calls[0].request.body) self.assertNotIn('status=this+is+a+test', responses.calls[0].request.url) @responses.activate def test_get_uses_get_method(self): """Test Twython generic GET request works""" endpoint = 'account/verify_credentials' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_post_uses_post_method(self): """Test Twython generic POST request works""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.post(endpoint, params={'status': 'I love Twython!'}) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) def test_raise_twython_error_on_request_exception(self): """Test if TwythonError is raised by a RequestException""" with mock.patch.object(requests.Session, 'get') as get_mock: # mocking an ssl cert error get_mock.side_effect = requests.RequestException( "hostname 'example.com' doesn't match ...") self.assertRaises(TwythonError, self.api.get, 'https://example.com') @responses.activate def test_request_should_get_convert_json_to_data(self): """Test that Twython converts JSON data to a Python object""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id": 210462857140252672}') data = self.api.request(endpoint, params={'id': 210462857140252672}) self.assertEqual({'id': 210462857140252672}, data) @responses.activate def test_request_should_raise_exception_with_invalid_json(self): """Test that Twython handles invalid JSON (though Twitter should not return it)""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id: 210462857140252672}') self.assertRaises(TwythonError, self.api.request, endpoint, params={'id': 210462857140252672}) @responses.activate def test_request_should_handle_401(self): """Test that Twython raises an auth error on 401 error""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Error"}]}', status=401) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_for_missing_auth_data(self): """Test that Twython raises an auth error on 400 error when no oauth data sent""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response( responses.GET, url, body='{"errors":[{"message":"Bad Authentication data"}]}', status=400) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_that_is_not_auth_related(self): """Test that Twython raises a normal error on 400 error when unrelated to authorization""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Bad request"}]}', status=400) self.assertRaises(TwythonError, self.api.request, endpoint) @responses.activate def test_request_should_handle_rate_limit(self): """Test that Twython raises an rate limit error on 429""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Rate Limit"}]}', status=429) self.assertRaises(TwythonRateLimitError, self.api.request, endpoint) @responses.activate def test_get_lastfunction_header_should_return_header(self): """Test getting last specific header of the last API call works""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, adding_headers={'x-rate-limit-remaining': '37'}) self.api.get(endpoint) value = self.api.get_lastfunction_header('x-rate-limit-remaining') self.assertEqual('37', value) value2 = self.api.get_lastfunction_header('does-not-exist') self.assertIsNone(value2) value3 = self.api.get_lastfunction_header('not-there-either', '96') self.assertEqual('96', value3) def test_get_lastfunction_header_should_raise_error_when_no_previous_call( self): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') @responses.activate def test_sends_correct_accept_encoding_header(self): """Test that Twython accepts compressed data.""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(b'gzip, deflate', responses.calls[0].request.headers['Accept-Encoding']) # Static methods def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" url = 'https://api.twitter.com/1.1/search/tweets.json' constructed_url = self.api.construct_api_url(url, q='#twitter') self.assertEqual( constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter') def test_encode(self): """Test encoding UTF-8 works""" self.api.encode('Twython is awesome!') def test_cursor_requires_twython_function(self): """Test that cursor() raises when called without a Twython function""" def init_and_iterate_cursor(*args, **kwargs): cursor = self.api.cursor(*args, **kwargs) return next(cursor) non_function = object() non_twython_function = lambda x: x self.assertRaises(TypeError, init_and_iterate_cursor, non_function) self.assertRaises(TwythonError, init_and_iterate_cursor, non_twython_function)
class TwythonAPITestCase(unittest.TestCase): def setUp(self): client_args = { 'headers': { 'User-Agent': '__twython__ Test' }, 'allow_redirects': False } oauth2_client_args = { 'headers': { } # This is so we can hit coverage that Twython sets User-Agent for us if none is supplied } self.api = Twython(app_key, app_secret, oauth_token, oauth_token_secret, client_args=client_args) self.oauth2_api = Twython(app_key, access_token=access_token, client_args=oauth2_client_args) def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" url = 'https://api.twitter.com/1.1/search/tweets.json' constructed_url = self.api.construct_api_url(url, q='#twitter') self.assertEqual( constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter') def test_get(self): """Test Twython generic GET request works""" self.api.get('account/verify_credentials') def test_post(self): """Test Twython generic POST request works, with a full url and with just an endpoint""" update_url = 'https://api.twitter.com/1.1/statuses/update.json' status = self.api.post( update_url, params={'status': 'I love Twython! %s' % int(time.time())}) self.api.post('statuses/destroy/%s' % status['id_str']) def test_get_lastfunction_header(self): """Test getting last specific header of the last API call works""" self.api.get('statuses/home_timeline') self.api.get_lastfunction_header('x-rate-limit-remaining') def test_get_lastfunction_header_not_present(self): """Test getting specific header that does not exist from the last call returns None""" self.api.get('statuses/home_timeline') header = self.api.get_lastfunction_header('does-not-exist') self.assertEqual(header, None) def test_get_lastfunction_header_no_last_api_call(self): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') def test_cursor(self): """Test looping through the generator results works, at least once that is""" search = self.api.cursor(self.api.search, q='twitter', count=1) counter = 0 while counter < 2: counter += 1 result = next(search) new_id_str = int(result['id_str']) if counter == 1: prev_id_str = new_id_str time.sleep( 1) # Give time for another tweet to come into search if counter == 2: self.assertTrue(new_id_str > prev_id_str) def test_encode(self): """Test encoding UTF-8 works""" self.api.encode('Twython is awesome!') def test_html_for_tweet(self): """Test HTML for Tweet returns what we want""" tweet_text = self.api.html_for_tweet(test_tweet_object) self.assertEqual(test_tweet_html, tweet_text) def test_html_for_tweet_expanded_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, use_expanded_url=True) # Make sure full url is in HTML self.assertTrue('http://google.com' in tweet_text) def test_html_for_tweet_short_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, False) # Make sure HTML doesn't contain the display OR exapanded url self.assertTrue(not 'http://google.com' in tweet_text) self.assertTrue(not 'google.com' in tweet_text) def test_raise_error_on_bad_ssl_cert(self): """Test TwythonError is raised by a RequestException when an actual HTTP happens""" self.assertRaises(TwythonError, self.api.get, 'https://example.com') # Timelines def test_get_mentions_timeline(self): """Test returning mentions timeline for authenticated user succeeds""" self.api.get_mentions_timeline() def test_get_user_timeline(self): """Test returning timeline for authenticated user and random user succeeds""" self.api.get_user_timeline() # Authenticated User Timeline self.api.get_user_timeline( screen_name='twitter') # Random User Timeline def test_get_protected_user_timeline_following(self): """Test returning a protected user timeline who you are following succeeds""" self.api.get_user_timeline(screen_name=protected_twitter_1) def test_get_protected_user_timeline_not_following(self): """Test returning a protected user timeline who you are not following fails and raise a TwythonAuthError""" self.assertRaises(TwythonAuthError, self.api.get_user_timeline, screen_name=protected_twitter_2) def test_retweeted_of_me(self): """Test that getting recent tweets by authenticated user that have been retweeted by others succeeds""" self.api.retweeted_of_me() def test_get_home_timeline(self): """Test returning home timeline for authenticated user succeeds""" self.api.get_home_timeline() # Tweets def test_get_retweets(self): """Test getting retweets of a specific tweet succeeds""" self.api.get_retweets(id=test_tweet_id) def test_show_status(self): """Test returning a single status details succeeds""" self.api.show_status(id=test_tweet_id) def test_update_and_destroy_status(self): """Test updating and deleting a status succeeds""" status = self.api.update_status( status='Test post just to get deleted :( %s' % int(time.time())) self.api.destroy_status(id=status['id_str']) def test_get_oembed_tweet(self): """Test getting info to embed tweet on Third Party site succeeds""" self.api.get_oembed_tweet(id='99530515043983360') def test_get_retweeters_ids(self): """Test getting ids for people who retweeted a tweet succeeds""" self.api.get_retweeters_ids(id='99530515043983360') # Search def test_search(self): """Test searching tweets succeeds""" self.api.search(q='twitter') # Direct Messages def test_get_direct_messages(self): """Test getting the authenticated users direct messages succeeds""" self.api.get_direct_messages() def test_get_sent_messages(self): """Test getting the authenticated users direct messages they've sent succeeds""" self.api.get_sent_messages() def test_send_get_and_destroy_direct_message(self): """Test sending, getting, then destory a direct message succeeds""" message = self.api.send_direct_message(screen_name=protected_twitter_1, text='Hey d00d! %s' % int(time.time())) self.api.get_direct_message(id=message['id_str']) self.api.destroy_direct_message(id=message['id_str']) def test_send_direct_message_to_non_follower(self): """Test sending a direct message to someone who doesn't follow you fails""" self.assertRaises(TwythonError, self.api.send_direct_message, screen_name=protected_twitter_2, text='Yo, man! %s' % int(time.time())) # Friends & Followers def test_get_user_ids_of_blocked_retweets(self): """Test that collection of user_ids that the authenticated user does not want to receive retweets from succeeds""" self.api.get_user_ids_of_blocked_retweets(stringify_ids=True) def test_get_friends_ids(self): """Test returning ids of users the authenticated user and then a random user is following succeeds""" self.api.get_friends_ids() self.api.get_friends_ids(screen_name='twitter') def test_get_followers_ids(self): """Test returning ids of users the authenticated user and then a random user are followed by succeeds""" self.api.get_followers_ids() self.api.get_followers_ids(screen_name='twitter') def test_lookup_friendships(self): """Test returning relationships of the authenticating user to the comma-separated list of up to 100 screen_names or user_ids provided succeeds""" self.api.lookup_friendships(screen_name='twitter,ryanmcgrath') def test_get_incoming_friendship_ids(self): """Test returning incoming friendship ids succeeds""" self.api.get_incoming_friendship_ids() def test_get_outgoing_friendship_ids(self): """Test returning outgoing friendship ids succeeds""" self.api.get_outgoing_friendship_ids() def test_create_friendship(self): """Test creating a friendship succeeds""" self.api.create_friendship(screen_name='justinbieber') def test_destroy_friendship(self): """Test destroying a friendship succeeds""" self.api.destroy_friendship(screen_name='justinbieber') def test_update_friendship(self): """Test updating friendships succeeds""" self.api.update_friendship(screen_name=protected_twitter_1, retweets='true') self.api.update_friendship(screen_name=protected_twitter_1, retweets=False) def test_show_friendships(self): """Test showing specific friendship succeeds""" self.api.show_friendship(target_screen_name=protected_twitter_1) def test_get_friends_list(self): """Test getting list of users authenticated user then random user is following succeeds""" self.api.get_friends_list() self.api.get_friends_list(screen_name='twitter') def test_get_followers_list(self): """Test getting list of users authenticated user then random user are followed by succeeds""" self.api.get_followers_list() self.api.get_followers_list(screen_name='twitter') # Users def test_get_account_settings(self): """Test getting the authenticated user account settings succeeds""" self.api.get_account_settings() def test_verify_credentials(self): """Test representation of the authenticated user call succeeds""" self.api.verify_credentials() def test_update_account_settings(self): """Test updating a user account settings succeeds""" self.api.update_account_settings(lang='en') def test_update_delivery_service(self): """Test updating delivery settings fails because we don't have a mobile number on the account""" self.assertRaises(TwythonError, self.api.update_delivery_service, device='none') def test_update_profile(self): """Test updating profile succeeds""" self.api.update_profile(include_entities='true') def test_update_profile_colors(self): """Test updating profile colors succeeds""" self.api.update_profile_colors(profile_background_color='3D3D3D') def test_list_blocks(self): """Test listing users who are blocked by the authenticated user succeeds""" self.api.list_blocks() def test_list_block_ids(self): """Test listing user ids who are blocked by the authenticated user succeeds""" self.api.list_block_ids() def test_create_block(self): """Test blocking a user succeeds""" self.api.create_block(screen_name='justinbieber') def test_destroy_block(self): """Test unblocking a user succeeds""" self.api.destroy_block(screen_name='justinbieber') def test_lookup_user(self): """Test listing a number of user objects succeeds""" self.api.lookup_user(screen_name='twitter,justinbieber') def test_show_user(self): """Test showing one user works""" self.api.show_user(screen_name='twitter') def test_search_users(self): """Test that searching for users succeeds""" self.api.search_users(q='Twitter API') def test_get_contributees(self): """Test returning list of accounts the specified user can contribute to succeeds""" self.api.get_contributees(screen_name='TechCrunch') def test_get_contributors(self): """Test returning list of accounts that contribute to the authenticated user fails because we are not a Contributor account""" self.assertRaises(TwythonError, self.api.get_contributors, screen_name=screen_name) def test_remove_profile_banner(self): """Test removing profile banner succeeds""" self.api.remove_profile_banner() def test_get_profile_banner_sizes(self): """Test getting list of profile banner sizes fails because we have not uploaded a profile banner""" self.assertRaises(TwythonError, self.api.get_profile_banner_sizes) # Suggested Users def test_get_user_suggestions_by_slug(self): """Test getting user suggestions by slug succeeds""" self.api.get_user_suggestions_by_slug(slug='twitter') def test_get_user_suggestions(self): """Test getting user suggestions succeeds""" self.api.get_user_suggestions() def test_get_user_suggestions_statuses_by_slug(self): """Test getting status of suggested users succeeds""" self.api.get_user_suggestions_statuses_by_slug(slug='funny') # Favorites def test_get_favorites(self): """Test getting list of favorites for the authenticated user succeeds""" self.api.get_favorites() def test_create_and_destroy_favorite(self): """Test creating and destroying a favorite on a tweet succeeds""" self.api.create_favorite(id=test_tweet_id) self.api.destroy_favorite(id=test_tweet_id) # Lists def test_show_lists(self): """Test show lists for specified user""" self.api.show_lists(screen_name='twitter') def test_get_list_statuses(self): """Test timeline of tweets authored by members of the specified list succeeds""" self.api.get_list_statuses( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_create_update_destroy_list_add_remove_list_members(self): """Test create a list, adding and removing members then deleting the list succeeds""" the_list = self.api.create_list(name='Stuff %s' % int(time.time())) list_id = the_list['id_str'] self.api.update_list(list_id=list_id, name='Stuff Renamed %s' % int(time.time())) screen_names = ['johncena', 'xbox'] # Multi add/delete members self.api.create_list_members(list_id=list_id, screen_name=screen_names) self.api.delete_list_members(list_id=list_id, screen_name=screen_names) # Single add/delete member self.api.add_list_member(list_id=list_id, screen_name='justinbieber') self.api.delete_list_member(list_id=list_id, screen_name='justinbieber') self.api.delete_list(list_id=list_id) def test_get_list_memberships(self): """Test list of memberhips the authenticated user succeeds""" self.api.get_list_memberships() def test_get_list_subscribers(self): """Test list of subscribers of a specific list succeeds""" self.api.get_list_subscribers( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_subscribe_is_subbed_and_unsubscribe_to_list(self): """Test subscribing, is a list sub and unsubbing to list succeeds""" self.api.subscribe_to_list( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) # Returns 404 if user is not a subscriber self.api.is_list_subscriber( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name, screen_name=screen_name) self.api.unsubscribe_from_list( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_is_list_member(self): """Test returning if specified user is member of a list succeeds""" # Returns 404 if not list member self.api.is_list_member(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name, screen_name='themattharris') def test_get_list_members(self): """Test listing members of the specified list succeeds""" self.api.get_list_members( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_get_specific_list(self): """Test getting specific list succeeds""" self.api.get_specific_list( slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_get_list_subscriptions(self): """Test collection of the lists the specified user is subscribed to succeeds""" self.api.get_list_subscriptions(screen_name='twitter') def test_show_owned_lists(self): """Test collection of lists the specified user owns succeeds""" self.api.show_owned_lists(screen_name='twitter') # Saved Searches def test_get_saved_searches(self): """Test getting list of saved searches for authenticated user succeeds""" self.api.get_saved_searches() def test_create_get_destroy_saved_search(self): """Test getting list of saved searches for authenticated user succeeds""" saved_search = self.api.create_saved_search(query='#Twitter') saved_search_id = saved_search['id_str'] self.api.show_saved_search(id=saved_search_id) self.api.destroy_saved_search(id=saved_search_id) # Places & Geo def test_get_geo_info(self): """Test getting info about a geo location succeeds""" self.api.get_geo_info(place_id='df51dec6f4ee2b2c') def test_reverse_geo_code(self): """Test reversing geocode succeeds""" self.api.reverse_geocode(lat='37.76893497', long='-122.42284884') def test_search_geo(self): """Test search for places that can be attached to a statuses/update succeeds""" self.api.search_geo(query='Toronto') def test_get_similar_places(self): """Test locates places near the given coordinates which are similar in name succeeds""" self.api.get_similar_places(lat='37', long='-122', name='Twitter HQ') # Trends def test_get_place_trends(self): """Test getting the top 10 trending topics for a specific WOEID succeeds""" self.api.get_place_trends(id=1) def test_get_available_trends(self): """Test returning locations that Twitter has trending topic information for succeeds""" self.api.get_available_trends() def test_get_closest_trends(self): """Test getting the locations that Twitter has trending topic information for, closest to a specified location succeeds""" self.api.get_closest_trends(lat='37', long='-122') # Help def test_get_twitter_configuration(self): """Test getting Twitter's configuration succeeds""" self.api.get_twitter_configuration() def test_get_supported_languages(self): """Test getting languages supported by Twitter succeeds""" self.api.get_supported_languages() def test_privacy_policy(self): """Test getting Twitter's Privacy Policy succeeds""" self.api.get_privacy_policy() def test_get_tos(self): """Test getting the Twitter Terms of Service succeeds""" self.api.get_tos() def test_get_application_rate_limit_status(self): """Test getting application rate limit status succeeds""" self.oauth2_api.get_application_rate_limit_status()
# Authorization - 'Permanent' Tokens twitter = Twython(API_KEY, API_SECRET_KEY, authorized_tokens['oauth_token'], authorized_tokens['oauth_token_secret']) twitter.verify_credentials() # %% [markdown] # ## Exericse 1. Do a search for a hashtag of your choosing, and retrieve at least 1000 tweets. Show that you have at least 1000 tweets. # %% import itertools maxTweets = 1000 cursor = twitter.cursor(twitter.search, q='#Watchmen', count=100, result_type='mixed') tweets = list(itertools.islice(cursor, maxTweets)) len(tweets) #%% # Create Graph import networkx as nx G = nx.DiGraph() # %% [markdown] # ## 2. For each tweet, if the tweet is a retweet, add an edge **from the *retweeted* user to the *retweeting* user**. for t in tweets: if 'retweeted_status' in t: # Retweeter & Author
class TwythonAPITestCase(unittest.TestCase): def setUp(self): self.api = Twython('', '', '', '') def get_url(self, endpoint): """Convenience function for mapping from endpoint to URL""" return '%s/%s.json' % (self.api.api_url % self.api.api_version, endpoint) def register_response(self, method, url, body='{}', match_querystring=False, status=200, adding_headers=None, stream=False, content_type='application/json; charset=utf-8'): """Wrapper function for responses for simpler unit tests""" # responses uses BytesIO to hold the body so it needs to be in bytes if not is_py2: body = bytes(body, 'UTF-8') responses.add(method, url, body, match_querystring, status, adding_headers, stream, content_type) @responses.activate def test_request_should_handle_full_endpoint(self): """Test that request() accepts a full URL for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request(url) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_relative_endpoint(self): """Test that request() accepts a twitter endpoint name for the endpoint argument""" url = 'https://api.twitter.com/1.1/search/tweets.json' self.register_response(responses.GET, url) self.api.request('search/tweets', version='1.1') self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_post_request_regardless_of_case(self): """Test that request() accepts the HTTP method name regardless of case""" url = 'https://api.twitter.com/1.1/statuses/update.json' self.register_response(responses.POST, url) self.api.request(url, method='POST') self.api.request(url, method='post') self.assertEqual(2, len(responses.calls)) self.assertEqual('POST', responses.calls[0].request.method) self.assertEqual('POST', responses.calls[1].request.method) @responses.activate def test_request_should_throw_exception_with_invalid_http_method(self): """Test that request() throws an exception when an invalid HTTP method is passed""" # TODO(cash): should Twython catch the AttributeError and throw a TwythonError self.assertRaises(AttributeError, self.api.request, endpoint='search/tweets', method='INVALID') @responses.activate def test_request_should_encode_boolean_as_lowercase_string(self): """Test that request() encodes a boolean parameter as a lowercase string""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'include_entities': True}) self.api.request(endpoint, params={'include_entities': False}) self.assertEqual(url + '?include_entities=true', responses.calls[0].request.url) self.assertEqual(url + '?include_entities=false', responses.calls[1].request.url) @responses.activate def test_request_should_handle_string_or_number_parameter(self): """Test that request() encodes a numeric or string parameter correctly""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'lang': 'es'}) self.api.request(endpoint, params={'count': 50}) self.assertEqual(url + '?lang=es', responses.calls[0].request.url) self.assertEqual(url + '?count=50', responses.calls[1].request.url) @responses.activate def test_request_should_encode_list_of_strings_as_string(self): """Test that request() encodes a list of strings as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = ['37.781157', '-122.39872', '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) # requests url encodes the parameters so , is %2C self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_encode_numeric_list_as_string(self): """Test that request() encodes a list of numbers as a comma-separated string""" endpoint = 'search/tweets' url = self.get_url(endpoint) location = [37.781157, -122.39872, '1mi'] self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': location}) self.assertEqual(url + '?geocode=37.781157%2C-122.39872%2C1mi', responses.calls[0].request.url) @responses.activate def test_request_should_ignore_bad_parameter(self): """Test that request() ignores unexpected parameter types""" endpoint = 'search/tweets' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.request(endpoint, params={'geocode': self}) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_request_should_handle_file_as_parameter(self): """Test that request() pulls a file out of params for requests lib""" endpoint = 'account/update_profile_image' url = self.get_url(endpoint) self.register_response(responses.POST, url) mock_file = StringIO("Twython test image") self.api.request(endpoint, method='POST', params={'image': mock_file}) self.assertIn(b'filename="image"', responses.calls[0].request.body) self.assertIn(b"Twython test image", responses.calls[0].request.body) @responses.activate def test_request_should_put_params_in_body_when_post(self): """Test that request() passes params as data when the request is a POST""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.request(endpoint, method='POST', params={'status': 'this is a test'}) self.assertIn(b'status=this+is+a+test', responses.calls[0].request.body) self.assertNotIn('status=this+is+a+test', responses.calls[0].request.url) @responses.activate def test_get_uses_get_method(self): """Test Twython generic GET request works""" endpoint = 'account/verify_credentials' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) @responses.activate def test_post_uses_post_method(self): """Test Twython generic POST request works""" endpoint = 'statuses/update' url = self.get_url(endpoint) self.register_response(responses.POST, url) self.api.post(endpoint, params={'status': 'I love Twython!'}) self.assertEqual(1, len(responses.calls)) self.assertEqual(url, responses.calls[0].request.url) def test_raise_twython_error_on_request_exception(self): """Test if TwythonError is raised by a RequestException""" with mock.patch.object(requests.Session, 'get') as get_mock: # mocking an ssl cert error get_mock.side_effect = requests.RequestException( "hostname 'example.com' doesn't match ...") self.assertRaises(TwythonError, self.api.get, 'https://example.com') @responses.activate def test_request_should_get_convert_json_to_data(self): """Test that Twython converts JSON data to a Python object""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id": 210462857140252672}') data = self.api.request(endpoint, params={'id': 210462857140252672}) self.assertEqual({'id': 210462857140252672}, data) @responses.activate def test_request_should_raise_exception_with_invalid_json(self): """Test that Twython handles invalid JSON (though Twitter should not return it)""" endpoint = 'statuses/show' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"id: 210462857140252672}') self.assertRaises(TwythonError, self.api.request, endpoint, params={'id': 210462857140252672}) @responses.activate def test_request_should_handle_401(self): """Test that Twython raises an auth error on 401 error""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Error"}]}', status=401) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_for_missing_auth_data(self): """Test that Twython raises an auth error on 400 error when no oauth data sent""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response( responses.GET, url, body='{"errors":[{"message":"Bad Authentication data"}]}', status=400) self.assertRaises(TwythonAuthError, self.api.request, endpoint) @responses.activate def test_request_should_handle_400_that_is_not_auth_related(self): """Test that Twython raises a normal error on 400 error when unrelated to authorization""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Bad request"}]}', status=400) self.assertRaises(TwythonError, self.api.request, endpoint) @responses.activate def test_request_should_handle_rate_limit(self): """Test that Twython raises an rate limit error on 429""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, body='{"errors":[{"message":"Rate Limit"}]}', status=429) self.assertRaises(TwythonRateLimitError, self.api.request, endpoint) @responses.activate def test_get_lastfunction_header_should_return_header(self): """Test getting last specific header of the last API call works""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url, adding_headers={'x-rate-limit-remaining': '37'}) self.api.get(endpoint) value = self.api.get_lastfunction_header('x-rate-limit-remaining') self.assertEqual('37', value) value2 = self.api.get_lastfunction_header('does-not-exist') self.assertIsNone(value2) value3 = self.api.get_lastfunction_header('not-there-either', '96') self.assertEqual('96', value3) def test_get_lastfunction_header_should_raise_error_when_no_previous_call( self): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') @responses.activate def test_sends_correct_accept_encoding_header(self): """Test that Twython accepts compressed data.""" endpoint = 'statuses/home_timeline' url = self.get_url(endpoint) self.register_response(responses.GET, url) self.api.get(endpoint) self.assertEqual(b'gzip, deflate', responses.calls[0].request.headers['Accept-Encoding']) # Static methods def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" url = 'https://api.twitter.com/1.1/search/tweets.json' constructed_url = self.api.construct_api_url(url, q='#twitter') self.assertEqual( constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter') def test_encode(self): """Test encoding UTF-8 works""" self.api.encode('Twython is awesome!') def test_html_for_tweet(self): """Test HTML for Tweet returns what we want""" tweet_text = self.api.html_for_tweet(test_tweet_object) self.assertEqual(test_tweet_html, tweet_text) def test_html_for_tweet_reply(self): """Test HTML for Tweet links the replied-to username.""" tweet_text = self.api.html_for_tweet(test_tweet_reply) self.assertEqual( tweet_text, u'<span class="twython-tweet-prefix"><a href="https://twitter.com/philgyford" class="twython-mention">@philgyford</a> </span>Here’s a test tweet that goes on as much as possible and includes an image. Hi to my fans in testland!<span class="twython-tweet-suffix"> https://t.co/tzhyk2QWSr</span>' ) def test_html_for_tweet_expanded_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, use_expanded_url=True) # Make sure full url is in HTML self.assertTrue('http://google.com' in tweet_text) def test_html_for_tweet_short_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, False) # Make sure HTML doesn't contain the display OR expanded url self.assertTrue('http://google.com' not in tweet_text) self.assertTrue('google.com' not in tweet_text) def test_html_for_tweet_identical_urls(self): """If the 'url's for different url entities are identical, they should link correctly.""" tweet_text = self.api.html_for_tweet(test_tweet_identical_urls) self.assertEqual( tweet_text, u'Use Cases, Trials and Making 5G a Reality <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a> #5G #innovation via @5GWorldSeries <a href="https://t.co/W0uArTMk9N" class="twython-url">buff.ly/2sEhrgO</a>' ) def test_html_for_tweet_symbols(self): tweet_text = self.api.html_for_tweet(test_tweet_symbols_object) # Should only link symbols listed in entities: self.assertTrue( '<a href="https://twitter.com/search?q=%24AAPL" class="twython-symbol">$AAPL</a>' in tweet_text) self.assertTrue( '<a href="https://twitter.com/search?q=%24ANOTHER" class="twython-symbol">$ANOTHER</a>' not in tweet_text) def test_html_for_tweet_no_symbols(self): """Should still work if tweet object has no symbols list""" tweet = test_tweet_symbols_object # Save a copy: symbols = tweet['entities']['symbols'] del tweet['entities']['symbols'] tweet_text = self.api.html_for_tweet(tweet) self.assertTrue('symbols: $AAPL and' in tweet_text) self.assertTrue('and $ANOTHER and $A.' in tweet_text) # Put the symbols back: test_tweet_symbols_object['entities']['symbols'] = symbols def test_html_for_tweet_compatmode(self): tweet_text = self.api.html_for_tweet(test_tweet_compat_object) # link to compat web status link self.assertTrue( u'<a href="https://t.co/SRmsuks2ru" class="twython-url">twitter.com/i/web/status/7…</a>' in tweet_text) def test_html_for_tweet_extendedmode(self): tweet_text = self.api.html_for_tweet(test_tweet_extended_object) # full tweet rendered with suffix self.assertEqual(test_tweet_extended_html, tweet_text) def test_cursor_requires_twython_function(self): """Test that cursor() raises when called without a Twython function""" def init_and_iterate_cursor(*args, **kwargs): cursor = self.api.cursor(*args, **kwargs) return next(cursor) non_function = object() non_twython_function = lambda x: x self.assertRaises(TypeError, init_and_iterate_cursor, non_function) self.assertRaises(TwythonError, init_and_iterate_cursor, non_twython_function)
from twython import Twython statuses ={} import config as c twitter = Twython(c.CONSUMER_KEY,c.CONSUMER_SECRET, c.ACCESS_TOKEN,c.ACCESS_SECRET) results = twitter.cursor(twitter.search, q='python',count=10) print results[statuses]
class TweetBot: def __init__(self, key_source='values', consumer_key=None, consumer_secret=None, access_token=None, access_token_secret=None): if key_source not in ('values', 'files'): raise ValueError('key_value should be one of values, files or env') consumer_keys = (consumer_key, consumer_secret) access_keys = (access_token, access_token_secret) if key_source == 'files': consumer_keys = list(map(read_key_from_file, consumer_keys)) access_keys = list(map(read_key_from_file, access_keys)) self.api = Twython(*(consumer_keys + access_keys)) user = self.api.verify_credentials() self.screen_name = user['screen_name'] print('Connected to ' + self.screen_name) def post_photo(self, tweet_text, from_camera=False, media=None, many=False, **kwargs): if from_camera: from .camera import EasyCamera ec = EasyCamera() photo_path = ec.take_photo(path.join(self.screen_name, 'Photos')) ids = [self.upload_media(photo_path, many=True)] else: ids = self.upload_media(media, many=many) tweet = self.api.update_status(status=tweet_text, media_ids=ids, **kwargs) print('Posted to {screen_name}'.format(screen_name=self.screen_name)) return tweet def post_video(self, tweet_text, duration_secs=10, **kwargs): from .camera import EasyCamera ec = EasyCamera() video_path = ec.record_video(path.join(self.screen_name, 'Videos'), duration_secs=duration_secs) with open(video_path, 'rb') as video: upload = self.api.upload_video(media=video, media_type='video/mp4', check_progress=True) self.api.update_status(status=tweet_text, media_ids=[upload['media_id']], **kwargs) print('Video at {video_path} posted to {screen_name}'.format( video_path=video_path, screen_name=self.screen_name)) def upload_media(self, media, many=True, **kwargs): if not many: media = [media] ids = [] for m in media: with open(m, 'rb') as fp: resp = self.api.upload_media(media=fp, **kwargs) ids.append(resp['media_id']) return ids def full_user_timeline(self, **kwargs): if 'user_id' not in kwargs and 'screen_name' not in kwargs: raise ValueError( 'get_full_timeline requires either a screen_name or user_id ' 'to search for') for page in self.api.cursor(self.api.get_user_timeline, **kwargs): yield page
def TwitterSearch(query, count): logger.info("starting search") t = Twython(app_key=TWITTER_APP_KEY, app_secret=TWITTER_APP_KEY_SECRET, oauth_token=TWITTER_ACCESS_TOKEN, oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET) search = t.cursor(t.search, q=query, count=count, lang='en') # count = 3 graph = Graph("http://localhost:7474/db/data/") p.set_options(p.OPT.URL, p.OPT.EMOJI, p.OPT.SMILEY, p.OPT.MENTION, p.OPT.NUMBER) i = 0 for tweet in search: try: i += 1 print i logger.info(i) t1 = Tweet() u = User() t1.id_str = tweet['id_str'] t1.text = tweet['text'] t1.retweet_count = tweet['retweet_count'] t1.favorite_count = tweet['favorite_count'] t1.created_at = tweet['created_at'] t1.lemm = pre.preprocess_tweet(tweet['text']) # print t1.lemm sentiment_tweet(t1.lemm) for source in tweet['source']: s = Source() s.name = tweet['source'] graph.push(s) t1.USING.add(s) for url in tweet['entities']['urls']: ul = Url() ul.tweet_url = url['url'] graph.push(ul) t1.CONTAINS.add(ul) t1.RETWEETS.add(t1) t1.REPLY_TO.add(t1) graph.push(t1) for hashtag in tweet['entities']['hashtags']: h = Hashtag() t1 = Tweet() h.hashtag = hashtag['text'] h.TAGS.add(t1) graph.push(h) for user in tweet['user']: u = User() t1 = Tweet() u.id = tweet['user']['id'] u.id_str = tweet['user']['id_str'] u.name = tweet['user']['name'] u.screen_name = tweet['user']['screen_name'] u.location = tweet['user']['location'] u.favourites_count = tweet['user']['favourites_count'] u.followers_count = tweet['user']['followers_count'] u.friends_count = tweet['user']['friends_count'] u.lang = tweet['user']['lang'] u.geo_enabled = tweet['user']['geo_enabled'] u.description = tweet['user']['description'] u.POSTS.add(t1) graph.push(u) mentions = [] for user_t in tweet['entities']['user_mentions']: if tweet['user']['screen_name'] != user_t['screen_name']: user = t.show_user(screen_name=user_t['screen_name']) u1 = User() u1.id = user['id'] u1.id_str = user['id_str'] u1.name = user['name'] u1.screen_name = user['screen_name'] u1.location = user['location'] u1.favourites_count = user['favourites_count'] u1.followers_count = user['followers_count'] u1.friends_count = user['friends_count'] u1.lang = user['lang'] u1.geo_enabled = user['geo_enabled'] u1.description = user['description'] t1.MENTIONS.add(u1) graph.push(u1) # # is reply if tweet['in_reply_to_status_id']: status = t.show_status(id=tweet['in_reply_to_status_id']) t2 = Tweet() u1 = User() t2.text = status['text'] t2.retweet_count = status['retweet_count'] t2.favorite_count = status['favorite_count'] t2.created_at = status['created_at'] t2.lemm = pre.preprocess_tweet(tweet['text']) # t2.RETWEETS.add(t1) t1.REPLY_TO.add(t2) graph.push(t2) # t12.MENTIONS.add(u1) user = t.show_user(screen_name=tweet[ 'in_reply_to_screen_name']) u1.id = user['id'] u1.id_str = user['id_str'] u1.name = user['name'] u1.screen_name = user['screen_name'] u1.location = user['location'] u1.favourites_count = user['favourites_count'] u1.followers_count = user['followers_count'] u1.friends_count = user['friends_count'] u1.lang = user['lang'] u1.geo_enabled = user['geo_enabled'] u1.description = user['description'] u1.POSTS.add(t2) graph.push(u1) # if i==count: # pass except TwythonRateLimitError: remainder = float(t.get_lastfunction_header( header='x-rate-limit-reset')) - time.time() t.disconnect() time.sleep(remainder) t = Twython(app_key=TWITTER_APP_KEY, app_secret=TWITTER_APP_KEY_SECRET, oauth_token=TWITTER_ACCESS_TOKEN, oauth_token_secret=TWITTER_ACCESS_TOKEN_SECRET) continue
# # STEP 2: Save the returned tweets # for result in results['statuses']: # # tweet_text = result['text'] # tweets.append(result) # # STEP 3: Get the next max_id # try: # # Parse the data returned to get max_id to be passed in consequent call. # next_results_url_params = results['search_metadata']['next_results'] # next_max_id = next_results_url_params.split('max_id=')[1].split('&')[0] # except: # # No more next pages # break count = 0 results = twitter.cursor(twitter.search, q='strava', count=5000) for result in results: if result not in tweets: tweets.append(result) # print result count += 1 results = twitter.cursor(twitter.search, q='#strava', count=5000) for result in results: if result not in tweets: tweets.append(result) # print result count += 1 results = twitter.cursor(twitter.search, q='Strava', count=5000) for result in results:
from textblob import TextBlob from twython import Twython APP_KEY = "KEY" APP_SECRET = "SECRET" twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN) print 'Write what you want to search!' search = twitter.cursor(twitter.search, q='obama', count=10) totalSent = 0 i = 0 for result in search: if i > 2: break totalSent += TextBlob(result['text']).sentiment.polarity i += 1 print str(totalSent / i)
from textblob import TextBlob from twython import Twython APP_KEY = "KEY"; APP_SECRET = "SECRET"; twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2) ACCESS_TOKEN = twitter.obtain_access_token() twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN) print 'Write what you want to search!' search = twitter.cursor(twitter.search,q='obama',count=10) totalSent = 0 i = 0 for result in search: if i > 2: break; totalSent += TextBlob(result['text']).sentiment.polarity i += 1 print str(totalSent/i)
# remove linebreak which is the last character of the string current_place = line[:-1] # add item to the list repliedTo.append(current_place) #Print Program Status print(colour.BLUE, colour.BOLD, 'Opened Replied To File', colour.END) #Start Loop (run forever) #Print Program Status print(colour.BLUE, colour.BOLD, 'Starting Loop', colour.END) while True: # Search Tweets print(colour.BLUE, colour.BOLD, 'Searching Tweets', colour.END) search_term = '@GetBadJokes #GiveMeAJoke' results = twitter.cursor(twitter.search, q=search_term) # Reply To Each Result for result in results: #Define User Metadata #Print Program Status print(colour.BLUE, colour.BOLD, 'Defining User Metadata', colour.END) name = result['user'] screen_name = name['screen_name'] creation_date = result['created_at'] tweet_txt = result['text'] id = result['id'] print(colour.BLUE, colour.BOLD, 'User Tweet:', tweet_txt, colour.END) #Check for Skip Reasons #Print Program Status
class TwythonAPITestCase(unittest.TestCase): def setUp(self): client_args = { 'headers': { 'User-Agent': '__twython__ Test' }, 'allow_redirects': False } oauth2_client_args = { 'headers': {} # This is so we can hit coverage that Twython sets User-Agent for us if none is supplied } self.api = Twython(app_key, app_secret, oauth_token, oauth_token_secret, client_args=client_args) self.oauth2_api = Twython(app_key, access_token=access_token, client_args=oauth2_client_args) def test_construct_api_url(self): """Test constructing a Twitter API url works as we expect""" url = 'https://api.twitter.com/1.1/search/tweets.json' constructed_url = self.api.construct_api_url(url, q='#twitter') self.assertEqual(constructed_url, 'https://api.twitter.com/1.1/search/tweets.json?q=%23twitter') def test_get(self): """Test Twython generic GET request works""" self.api.get('account/verify_credentials') def test_post(self): """Test Twython generic POST request works, with a full url and with just an endpoint""" update_url = 'https://api.twitter.com/1.1/statuses/update.json' status = self.api.post(update_url, params={'status': 'I love Twython! %s' % int(time.time())}) self.api.post('statuses/destroy/%s' % status['id_str']) def test_get_lastfunction_header(self): """Test getting last specific header of the last API call works""" self.api.get('statuses/home_timeline') self.api.get_lastfunction_header('x-rate-limit-remaining') def test_get_lastfunction_header_not_present(self): """Test getting specific header that does not exist from the last call returns None""" self.api.get('statuses/home_timeline') header = self.api.get_lastfunction_header('does-not-exist') self.assertEqual(header, None) def test_get_lastfunction_header_no_last_api_call(self): """Test attempting to get a header when no API call was made raises a TwythonError""" self.assertRaises(TwythonError, self.api.get_lastfunction_header, 'no-api-call-was-made') def test_cursor(self): """Test looping through the generator results works, at least once that is""" search = self.api.cursor(self.api.search, q='twitter', count=1) counter = 0 while counter < 2: counter += 1 result = next(search) new_id_str = int(result['id_str']) if counter == 1: prev_id_str = new_id_str time.sleep(1) # Give time for another tweet to come into search if counter == 2: self.assertTrue(new_id_str > prev_id_str) def test_encode(self): """Test encoding UTF-8 works""" self.api.encode('Twython is awesome!') def test_html_for_tweet(self): """Test HTML for Tweet returns what we want""" tweet_text = self.api.html_for_tweet(test_tweet_object) self.assertEqual(test_tweet_html, tweet_text) def test_html_for_tweet_expanded_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, use_expanded_url=True) # Make sure full url is in HTML self.assertTrue('http://google.com' in tweet_text) def test_html_for_tweet_short_url(self): """Test using expanded url in HTML for Tweet displays full urls""" tweet_text = self.api.html_for_tweet(test_tweet_object, False) # Make sure HTML doesn't contain the display OR exapanded url self.assertTrue(not 'http://google.com' in tweet_text) self.assertTrue(not 'google.com' in tweet_text) def test_raise_error_on_bad_ssl_cert(self): """Test TwythonError is raised by a RequestException when an actual HTTP happens""" self.assertRaises(TwythonError, self.api.get, 'https://example.com') # Timelines def test_get_mentions_timeline(self): """Test returning mentions timeline for authenticated user succeeds""" self.api.get_mentions_timeline() def test_get_user_timeline(self): """Test returning timeline for authenticated user and random user succeeds""" self.api.get_user_timeline() # Authenticated User Timeline self.api.get_user_timeline(screen_name='twitter') # Random User Timeline def test_get_protected_user_timeline_following(self): """Test returning a protected user timeline who you are following succeeds""" self.api.get_user_timeline(screen_name=protected_twitter_1) def test_get_protected_user_timeline_not_following(self): """Test returning a protected user timeline who you are not following fails and raise a TwythonAuthError""" self.assertRaises(TwythonAuthError, self.api.get_user_timeline, screen_name=protected_twitter_2) def test_retweeted_of_me(self): """Test that getting recent tweets by authenticated user that have been retweeted by others succeeds""" self.api.retweeted_of_me() def test_get_home_timeline(self): """Test returning home timeline for authenticated user succeeds""" self.api.get_home_timeline() # Tweets def test_get_retweets(self): """Test getting retweets of a specific tweet succeeds""" self.api.get_retweets(id=test_tweet_id) def test_show_status(self): """Test returning a single status details succeeds""" self.api.show_status(id=test_tweet_id) def test_update_and_destroy_status(self): """Test updating and deleting a status succeeds""" status = self.api.update_status(status='Test post just to get deleted :( %s' % int(time.time())) self.api.destroy_status(id=status['id_str']) def test_get_oembed_tweet(self): """Test getting info to embed tweet on Third Party site succeeds""" self.api.get_oembed_tweet(id='99530515043983360') def test_get_retweeters_ids(self): """Test getting ids for people who retweeted a tweet succeeds""" self.api.get_retweeters_ids(id='99530515043983360') # Search def test_search(self): """Test searching tweets succeeds""" self.api.search(q='twitter') # Direct Messages def test_get_direct_messages(self): """Test getting the authenticated users direct messages succeeds""" self.api.get_direct_messages() def test_get_sent_messages(self): """Test getting the authenticated users direct messages they've sent succeeds""" self.api.get_sent_messages() def test_send_get_and_destroy_direct_message(self): """Test sending, getting, then destory a direct message succeeds""" message = self.api.send_direct_message(screen_name=protected_twitter_1, text='Hey d00d! %s' % int(time.time())) self.api.get_direct_message(id=message['id_str']) self.api.destroy_direct_message(id=message['id_str']) def test_send_direct_message_to_non_follower(self): """Test sending a direct message to someone who doesn't follow you fails""" self.assertRaises(TwythonError, self.api.send_direct_message, screen_name=protected_twitter_2, text='Yo, man! %s' % int(time.time())) # Friends & Followers def test_get_user_ids_of_blocked_retweets(self): """Test that collection of user_ids that the authenticated user does not want to receive retweets from succeeds""" self.api.get_user_ids_of_blocked_retweets(stringify_ids=True) def test_get_friends_ids(self): """Test returning ids of users the authenticated user and then a random user is following succeeds""" self.api.get_friends_ids() self.api.get_friends_ids(screen_name='twitter') def test_get_followers_ids(self): """Test returning ids of users the authenticated user and then a random user are followed by succeeds""" self.api.get_followers_ids() self.api.get_followers_ids(screen_name='twitter') def test_lookup_friendships(self): """Test returning relationships of the authenticating user to the comma-separated list of up to 100 screen_names or user_ids provided succeeds""" self.api.lookup_friendships(screen_name='twitter,ryanmcgrath') def test_get_incoming_friendship_ids(self): """Test returning incoming friendship ids succeeds""" self.api.get_incoming_friendship_ids() def test_get_outgoing_friendship_ids(self): """Test returning outgoing friendship ids succeeds""" self.api.get_outgoing_friendship_ids() def test_create_friendship(self): """Test creating a friendship succeeds""" self.api.create_friendship(screen_name='justinbieber') def test_destroy_friendship(self): """Test destroying a friendship succeeds""" self.api.destroy_friendship(screen_name='justinbieber') def test_update_friendship(self): """Test updating friendships succeeds""" self.api.update_friendship(screen_name=protected_twitter_1, retweets='true') self.api.update_friendship(screen_name=protected_twitter_1, retweets=False) def test_show_friendships(self): """Test showing specific friendship succeeds""" self.api.show_friendship(target_screen_name=protected_twitter_1) def test_get_friends_list(self): """Test getting list of users authenticated user then random user is following succeeds""" self.api.get_friends_list() self.api.get_friends_list(screen_name='twitter') def test_get_followers_list(self): """Test getting list of users authenticated user then random user are followed by succeeds""" self.api.get_followers_list() self.api.get_followers_list(screen_name='twitter') # Users def test_get_account_settings(self): """Test getting the authenticated user account settings succeeds""" self.api.get_account_settings() def test_verify_credentials(self): """Test representation of the authenticated user call succeeds""" self.api.verify_credentials() def test_update_account_settings(self): """Test updating a user account settings succeeds""" self.api.update_account_settings(lang='en') def test_update_delivery_service(self): """Test updating delivery settings fails because we don't have a mobile number on the account""" self.assertRaises(TwythonError, self.api.update_delivery_service, device='none') def test_update_profile(self): """Test updating profile succeeds""" self.api.update_profile(include_entities='true') def test_update_profile_colors(self): """Test updating profile colors succeeds""" self.api.update_profile_colors(profile_background_color='3D3D3D') def test_list_blocks(self): """Test listing users who are blocked by the authenticated user succeeds""" self.api.list_blocks() def test_list_block_ids(self): """Test listing user ids who are blocked by the authenticated user succeeds""" self.api.list_block_ids() def test_create_block(self): """Test blocking a user succeeds""" self.api.create_block(screen_name='justinbieber') def test_destroy_block(self): """Test unblocking a user succeeds""" self.api.destroy_block(screen_name='justinbieber') def test_lookup_user(self): """Test listing a number of user objects succeeds""" self.api.lookup_user(screen_name='twitter,justinbieber') def test_show_user(self): """Test showing one user works""" self.api.show_user(screen_name='twitter') def test_search_users(self): """Test that searching for users succeeds""" self.api.search_users(q='Twitter API') def test_get_contributees(self): """Test returning list of accounts the specified user can contribute to succeeds""" self.api.get_contributees(screen_name='TechCrunch') def test_get_contributors(self): """Test returning list of accounts that contribute to the authenticated user fails because we are not a Contributor account""" self.assertRaises(TwythonError, self.api.get_contributors, screen_name=screen_name) def test_remove_profile_banner(self): """Test removing profile banner succeeds""" self.api.remove_profile_banner() def test_get_profile_banner_sizes(self): """Test getting list of profile banner sizes fails because we have not uploaded a profile banner""" self.assertRaises(TwythonError, self.api.get_profile_banner_sizes) # Suggested Users def test_get_user_suggestions_by_slug(self): """Test getting user suggestions by slug succeeds""" self.api.get_user_suggestions_by_slug(slug='twitter') def test_get_user_suggestions(self): """Test getting user suggestions succeeds""" self.api.get_user_suggestions() def test_get_user_suggestions_statuses_by_slug(self): """Test getting status of suggested users succeeds""" self.api.get_user_suggestions_statuses_by_slug(slug='funny') # Favorites def test_get_favorites(self): """Test getting list of favorites for the authenticated user succeeds""" self.api.get_favorites() def test_create_and_destroy_favorite(self): """Test creating and destroying a favorite on a tweet succeeds""" self.api.create_favorite(id=test_tweet_id) self.api.destroy_favorite(id=test_tweet_id) # Lists def test_show_lists(self): """Test show lists for specified user""" self.api.show_lists(screen_name='twitter') def test_get_list_statuses(self): """Test timeline of tweets authored by members of the specified list succeeds""" self.api.get_list_statuses(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_create_update_destroy_list_add_remove_list_members(self): """Test create a list, adding and removing members then deleting the list succeeds""" the_list = self.api.create_list(name='Stuff %s' % int(time.time())) list_id = the_list['id_str'] self.api.update_list(list_id=list_id, name='Stuff Renamed %s' % int(time.time())) screen_names = ['johncena', 'xbox'] # Multi add/delete members self.api.create_list_members(list_id=list_id, screen_name=screen_names) self.api.delete_list_members(list_id=list_id, screen_name=screen_names) # Single add/delete member self.api.add_list_member(list_id=list_id, screen_name='justinbieber') self.api.delete_list_member(list_id=list_id, screen_name='justinbieber') self.api.delete_list(list_id=list_id) def test_get_list_memberships(self): """Test list of memberhips the authenticated user succeeds""" self.api.get_list_memberships() def test_get_list_subscribers(self): """Test list of subscribers of a specific list succeeds""" self.api.get_list_subscribers(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_subscribe_is_subbed_and_unsubscribe_to_list(self): """Test subscribing, is a list sub and unsubbing to list succeeds""" self.api.subscribe_to_list(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) # Returns 404 if user is not a subscriber self.api.is_list_subscriber(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name, screen_name=screen_name) self.api.unsubscribe_from_list(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_is_list_member(self): """Test returning if specified user is member of a list succeeds""" # Returns 404 if not list member self.api.is_list_member(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name, screen_name='themattharris') def test_get_list_members(self): """Test listing members of the specified list succeeds""" self.api.get_list_members(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_get_specific_list(self): """Test getting specific list succeeds""" self.api.get_specific_list(slug=test_list_slug, owner_screen_name=test_list_owner_screen_name) def test_get_list_subscriptions(self): """Test collection of the lists the specified user is subscribed to succeeds""" self.api.get_list_subscriptions(screen_name='twitter') def test_show_owned_lists(self): """Test collection of lists the specified user owns succeeds""" self.api.show_owned_lists(screen_name='twitter') # Saved Searches def test_get_saved_searches(self): """Test getting list of saved searches for authenticated user succeeds""" self.api.get_saved_searches() def test_create_get_destroy_saved_search(self): """Test getting list of saved searches for authenticated user succeeds""" saved_search = self.api.create_saved_search(query='#Twitter') saved_search_id = saved_search['id_str'] self.api.show_saved_search(id=saved_search_id) self.api.destroy_saved_search(id=saved_search_id) # Places & Geo def test_get_geo_info(self): """Test getting info about a geo location succeeds""" self.api.get_geo_info(place_id='df51dec6f4ee2b2c') def test_reverse_geo_code(self): """Test reversing geocode succeeds""" self.api.reverse_geocode(lat='37.76893497', long='-122.42284884') def test_search_geo(self): """Test search for places that can be attached to a statuses/update succeeds""" self.api.search_geo(query='Toronto') def test_get_similar_places(self): """Test locates places near the given coordinates which are similar in name succeeds""" self.api.get_similar_places(lat='37', long='-122', name='Twitter HQ') # Trends def test_get_place_trends(self): """Test getting the top 10 trending topics for a specific WOEID succeeds""" self.api.get_place_trends(id=1) def test_get_available_trends(self): """Test returning locations that Twitter has trending topic information for succeeds""" self.api.get_available_trends() def test_get_closest_trends(self): """Test getting the locations that Twitter has trending topic information for, closest to a specified location succeeds""" self.api.get_closest_trends(lat='37', long='-122') # Help def test_get_twitter_configuration(self): """Test getting Twitter's configuration succeeds""" self.api.get_twitter_configuration() def test_get_supported_languages(self): """Test getting languages supported by Twitter succeeds""" self.api.get_supported_languages() def test_privacy_policy(self): """Test getting Twitter's Privacy Policy succeeds""" self.api.get_privacy_policy() def test_get_tos(self): """Test getting the Twitter Terms of Service succeeds""" self.api.get_tos() def test_get_application_rate_limit_status(self): """Test getting application rate limit status succeeds""" self.oauth2_api.get_application_rate_limit_status()