def api(): #APIキー CONSUMER_KEY = '取得したCONSUMER_KEY' CONSUMER_SECRET = '取得したCONSUMER_SECRET' ACCESS_TOKEN = '取得したACCESS_TOKEN' ACCESS_SECRET = '取得したACCESS_SECRET' auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) return tweepy.api(auth)
def main(): consumer_key = 'ezT5j0pRtCakw3O1pN8PjeQF7' consumer_secret = 'wVflXWNj7pC3lGwhMelaEX4AYZReUHazXlZ8R6veAGY2oeFjkt' access_key = '889485691675127809-63aEoWJytHS4TYVR6KA1MWz15yI6WV7' access_key_secret = 'rdKzsxKuYkRimPA8IFyQApll5wN0m0sXZB6f50L0XIi8x' grape = tweepy.api(consumer_key = consumer_key, consumer_secret = consumer_secret, access_token_key = access_key, access_token_secret = access_key_secret) grape.update_status("tweet test") print("tweet sent")
def fetch(self): while 1: twitterlists = [x.id for x in TwitterList.objects.filter(is_queried=False).distinct()] fetch = subscriberFetcher(TwitterLists=twitterlists) if not twitterlists: time.sleep(20) try: for tlist in twitterlists: api = tweepy.api(self.auth) list_members = [x for x in api.list_members(list_id = tlist)] for member in list_members: tuser, created = TwitterUser.objects.get_or_create(screen_name= x.screen_name, followers_count = x.followers_count, location=x.location, friends_count = x.friends_count) tuser.save() tlist.subscribers.add(tuser) tlist.subscribers.save() tlist.save() self.fetch() except Exception, e: self.handle_error(e)
import tweepy import os from dotenv import load_dotenv load_dotenv() TWITTER_API_KEY = os.getenv("TWITTER_API_KEY") TWITTER_API_SECRET = os.getenv("TWITTER_API_SECRET") TWITTER_ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN") TWITTER_ACCESS_TOKEN_SECRET = os.getenv("TWITTER_ACCESS_TOKEN_SECRET") def twitter_api_client(): auth = tweepy.OAuthHandler(TWITTER_API_KEY, TWITTER_API_SECRET) auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET) print("AUTH:", auth) api = tweepy.API(auth) print("API:" api) return api if __name__ == "__main__": api = tweepy.api() user = api.get_user("elonmusk") print("USER", user) print(user.screen_name) print(user.name) print(user.followers_count)
import tweepy from textblob import textblob consumer_key = '' consumer_secret = '' access_token = '' access_token_secret = '' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.api(auth) public_tweets = api.search('Trump') for tweet in public_tweets: print(tweet.text) analysis = TextBlob(tweet.text) print (analysis.sentiment)
import tweepy from tweepy import OAuthHandler import json from timeit import default_timer as timer # Query Twitter API for each tweet in the Twitter archive and save JSON in a text file # These are hidden to comply with Twitter's API terms and conditions consumer_key = 'YBUY2iKk7C1sRkfJ8T7pO2bOo' consumer_secret = 'R3XIHSqfbPkDbNrUr7VbGoa2wYfwJr44FeVFNYvjVocgKvztRp ' access_token = '3199394082-3h1bxiItyKbwnXftqwMMl1pYSjvjQgBFG8INGop' access_secret = 'KlbTLpKRS9uaET1tqS72SpaCLnjGQlzOdobjreugkyqIy' auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) api = tweepy.api(auth, wait_on_rate_limit=True)
import tweepy import time CONSUMER_KEY = "tkBz3ewGYoHivWKkyYcRfqk9z" CONSUMER_SECRET = "Dkwju8fPTU3LA0qt8zjCadmCPLL43DEpsQW1TglsddFXlgVIS3GH" ACCESS_KEY = "902905259231633408-1ETuKEku0dWcktANdTe7okfPzqP3Y3T" ACCESS_SECRET = "PVh7E3w3lF5jU7AU08OIAr2d69zjzRsBKcNb2LzBoNRZR" #for doing authentication auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) api #It shows <tweepy.api.API at 0x6f4a2b0> #for checking timeline tweets api.mentions_timeline() mentions = api.mentions_timeline() #stored the mentions tweets ovehere api = tweepy.api( auth, wait_on_rate_limit=True) #for avoiding rate limit exceed error mentions[0].__dict__ #converted into the dictionary(python) mentions[0].__dict__.keys() FILE_NAME = 'last_seen_id.txt' def retrieve_last_seen_id(file_name): f_read = open(file_name, 'r') last_seen_id = int(f_read.read().strip()) f_read.close() return last_seen_id def store_last_seen_id(last_seen_id, file_name): f_write = open(file_name, 'w') f_write.write(str(last_seen_id))
def get_tweepy_api(self): api = tweepy.api(self.auth) return api