def auth(): acct_name, consumer_key, consumer_secret, access_token, access_token_secret = get_account_sequential() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) auth_api = API(auth) return auth_api
def __init__(self, twitter_user=None): self.autentificacion = AutentificadorDeTwitter( ).autoentificar_app_de_twitter() self.cliente_twitter = API(self.autentificacion) self.twitter_user = twitter_user
def __init__(self): self.auth = TwitterAuthenticator().authenticate_twitter_app() self.twitter_client = API(self.auth)
###### last edit: 1 Feb 2021 ##### packages import os, time from tweepy import OAuthHandler, Stream, StreamListener, API from datetime import datetime os.chdir('') # directory to save json txt files in ##### authentification auth = OAuthHandler('', '') auth.set_access_token('', '') api = API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True) ##### stream while True: txt = 'stream_' + str(datetime.now().strftime('%Y_%m%d_%H%M_%S')) + '.txt' class Listener(StreamListener): def __init__(self, time_limit= 3600): # inset the interval at which txt files are generated/saved (e.g., 'time_limit= 3600' is 3,600 seconds = 1 hour ) self.start_time = time.time() self.limit = time_limit self.saveFile = open(txt, 'a') super(Listener, self).__init__()
def setUp(self): self.api = API(create_auth()) self.api.retry_count = 2 self.api.retry_delay = 5 self.api.retry_errors = set([401, 404, 503]) self.api.wait_on_rate_limit = True
# ---------------------------------------------------------------------------- # # API key: api_key = getenv("85ydEkXlBBciiAYLONlPMqVhk") # API secret key: api_secret = getenv("QT2RlCWJCybSM98YCAwmjXpUkPGjJvnVA7vnqputXwAv6qLXR4") # Access token: access_token = getenv("1305673102160875523-6bstDMBo5xu0UOdqea3mi5x0uKPSph") # Access token secret: access_token_secret = getenv("7gOQNzknTQDDle6IForuCTpP6WIjRXLh0Y6BrTnSafjAZ") # ---------------------------------- Tweepy ---------------------------------- # # Tweepy 0Auth 1a authentication: auth = OAuthHandler(api_key, api_secret) auth.set_access_token(access_token, access_token_secret) # API Variable: api = API(auth, wait_on_rate_limit=True) # ---------------------------------------------------------------------------- # def get_my_followers(): try: followers = api.followers() except TweepError as error: print(f"-> Error: {error.reason}") send_error_email.send_error_email(error) pass follower_id_list = [] try: for page in Cursor(api.followers_ids).pages(): follower_id_list.extend(page) sleep(10)
def get_twitter_client(self): auth = self._get_api_auth() client = API(auth, wait_on_rate_limit=True) return client
# Search filter rule set to geo baunding box MEDELLIN_BBOX = [-75.8032106603, 5.9566942289, -75.2758668684, 6.4913941464] if __name__ == '__main__': # Authentication auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) # Instantiate twitter API with auth info api = API( auth, # retry 3 times with 5 seconds delay when getting these error codes # For more details see # https://dev.twitter.com/docs/error-codes-responses retry_count=3, retry_delay=5, retry_errors=set([401, 404, 500, 503]), # monitor remaining calls and block until replenished wait_on_rate_limit=True, wait_on_rate_limit_notify=True) # create a engine to the database engine = create_engine("sqlite:///tweets.sqlite") # if the database does not exist if not database_exists(engine.url): # create a new database create_database(engine.url) # Instantiate the listener object listener = Listener(api)
def __init__(self, twitter_user=None): #default to none, which will default to my own account self.auth = TwitterAuthenticator().authenticate_twitter_app() self.twitter_client = API(self.auth) self.twitter_user = twitter_user
def main(): logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO) logger = logging.getLogger(__name__) args = parse_args() consumer_key = os.environ.get('TWEEPY_CONSUMER_KEY') consumer_secret = os.environ.get('TWEEPY_CONSUMER_SECRET') access_token = os.environ.get('TWEEPY_ACCESS_TOKEN') access_token_secret = os.environ.get('TWEEPY_ACCESS_TOKEN_SECRET') if not (consumer_key and consumer_secret and access_token and access_token_secret): logger.error('Need to specify the OAuth configuration.') sys.exit(1) user_auth = OAuthHandler(consumer_key, consumer_secret) user_auth.set_access_token(access_token, access_token_secret) user_api = API(user_auth, wait_on_rate_limit_notify=True, wait_on_rate_limit=True) api_auth = AppAuthHandler(consumer_key, consumer_secret) app_api = API(api_auth, wait_on_rate_limit_notify=True, wait_on_rate_limit=True) account_queue = RedisQueue('accounts') lookup_queue = RedisQueue('lookup') streamer_class = JSONStreamer if args.stdout: streamer_class = StdoutStreamer account_streamer = streamer_class(args.account_filename) processes = [] if args.stream: stream_process = Process(target=start_streamer, args=[user_api, account_queue, lookup_queue], kwargs={'query': args.stream_query}) processes.append(stream_process) else: logger.info('Skipping stream') if args.enum: enumerate_process = Process(target=fetch_accounts, args=[user_api, account_queue], kwargs={ 'min_id': args.min_id, 'max_id': args.max_id, 'percentage': args.enum_percentage }) processes.append(enumerate_process) else: logger.info('Skipping enum') # if args.tweets: # fetch_tweets_process = Process( # target=fetch_tweets, # args=[app_api, tweet_streamer], # kwargs={ # 'lookup_queue': lookup_queue, # 'minimum_tweets': args.min_tweets # }, # ) # processes.append(fetch_tweets_process) # else: # logger.info('Skipping tweets') lookup_account_process = Process( target=start_lookup, args=[app_api, lookup_queue, account_queue]) processes.append(lookup_account_process) for p in processes: p.start() # The main loop's job is simple - it simply fetches account dicts coming # from the various processes and saves them to the database so the tweet # fetcher can process them. try: account_count = 0 while True: try: account = account_queue.get() # Verify the account isn't already in our database if Account.exists(account['id']): continue account_count += 1 if account_count % CHECKIN_THRESHOLD == 0: logger.info( 'Accounts discovered: {}'.format(account_count)) # Add the account to our database cache Account.from_dict(account).save() # Write the account to our account streamer account_streamer.write_row(account) except Exception as e: print('Error fetching account: {}'.format(e)) except KeyboardInterrupt: print('\nCtrl+C received. Shutting down...') for p in processes: p.terminate() p.join() account_streamer.close()
def tweepy_api(auth): api = API(auth_handler=auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) return api
def authenticate(): ''' Authentication for using twitter data ''' auth = OAuthHandler(t.CONSUMER_KEY, t.CONSUMER_SECRET) auth.set_access_token(t.ACCESS_TOKEN, t.ACCESS_TOKEN_SECRET) api = API(auth) return api, auth
def connect_api(): auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = API(auth) return api
def __init__(self, twitter_user=None): self.auth = TwitterAuthenticator().authenticate_twitter_app()#To authenticate username we are going to take as input self.twitter_client = API(self.auth)#authentication on process self.twitter_user = twitter_user # for entering the username
def get_twitter_client(): auth = get_twitter_auth() client = API(auth) return client
def __init__(self, screenName): auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) self.api = API(auth) self.screenName = screenName
def get_twitter_sentiment(self): """ Analyze the tweet sentiment. :return: 1 if success else -1 """ try: print("Getting celeb data from db...") res = celeb_data = self.__get_data() if res == -1: return -1 print("Authorizing app...") # Create OAuthHandler object auth = OAuthHandler(self.__consumer_key, self.__consumer_secret) # Set access token and secret auth.set_access_token(self.__access_token, self.__access_token_secret) # Create tweepy API object to fetch tweets api = API(auth) # For each celeb in celeb list perform setiment analysis sentiment_list = list() print("\nPerforming Twitter Sentiment Analysis...Please Wait...") for celeb in list(celeb_data): """ For each celeb, get the tweets, calculate sentiment and form a dict(celeb: sentiment) """ celeb_sentiment = {} negative = 0 positive = 0 neutral = 0 celeb_name = celeb[0] # Get the last 100 tweets from twitter for the celeb. tweets = api.search(q=celeb_name, count=100) if len(tweets) == 0: celeb_sentiment[celeb_name] = "NA" continue for tweet in tweets: # get the tweets sentiment polarity tweet_polarity = self.__get_tweet_polarity(tweet.text) # decide positive negative or neutral based on polarity if tweet_polarity > 0: positive += 1 elif tweet_polarity == 0: neutral += 1 else: negative += 1 # Decide the overall sentiment for the celeb if positive >= neutral and positive >= negative: celeb_sentiment[celeb_name] = "POSITIVE" elif neutral >= positive and neutral >= negative: celeb_sentiment[celeb_name] = "NEUTRAL" else: celeb_sentiment[celeb_name] = "NEGATIVE" # Append the dict to sentiment list for later use. sentiment_list.append(celeb_sentiment) print("Twitter sentimental analysis completed...") print("Updated db with sentiment data...") res = self.__dump_data(sentiment_list) if res == -1: return -1 print("Sentiment data successfully updated in db...") return 1 except Exception as exp: print("!!! Exception: %s !!!" % exp) return -1
def load(self): auth = OAuthHandler(self.consumer_key, self.consumer_secret) auth.set_access_token(self.access_token, self.access_token_secret) self.api = API(auth) print('Successfully connected to the Twitter API.')
def __init__(self, api=None): super(MyStreamListener, self).__init__(api) self.api = api or API() self.startFile()
def setUp(self): auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret) auth.set_access_token(oauth_token, oauth_token_secret) self.api = API(auth) self.api.retry_count = 2 self.api.retry_delay = 5
def __init__(self, twitter_user=None): self.auth = TwitterAuthenticator().authenticate_twitter_app() self.client = API(self.auth, wait_on_rate_limit=True) self.twitter_user = twitter_user
app = Flask(__name__, static_folder='./client/build/') # Twitter Api Credentials consumer_key = "29t0d6bCnEPbWynevgwubCWAZ" consumer_secret = "mMCuy5v8AkkeqIuePQrHShd8GNrHF1BauHgiqq1devTkTIPeVo" access_token = "1147464618618437632-x4oiaSK6ORIySsJML05KYIKMTpyn4H" access_token_secret = "iASo6JMo7lyE4ZGAQWQhUD2ztdpmcQJCSPjijuAIiV5Cg" # CORS cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) # Authentication auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) auth_api = API(auth) # Load model for prediction with open('cluster_model.pkl', 'rb') as f: u = pickle._Unpickler(f) u.encoding = 'latin1' kmeans = u.load() #Routes @app.route('/api/getUserDetails', methods=['GET']) #get user details from twitter api def getUserDetails(): # Get twitter data twitter_name = request.args.get('twitter_name') item = auth_api.get_user(twitter_name)
# Authentication Class class TwitterAuth(): def auth_tokens(self): auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET) auth.set_access_token(twitter_credentials.ACCESS_KEY, twitter_credentials.ACCESS_SECRET) return auth # Instantiate TwitterAuthenticator twitter_auth = TwitterAuth() # Instantiate API Object api = API(twitter_auth.auth_tokens()) 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)) f_write.close() return
def __init__(self, twitter_user=None): self.auth = TwitterAuthenticator().authenticate_twitter_app() self.twitter_client = API(self.auth) self.twitter_user = twitter_user # user timeline for specified user
def post_comment(status_id, message): auth = TwitterAuthenticator.authenticate_twitter_app() comment_post = API(auth) comment_post.update_status(message, status_id)
# Setting up stopwords: from nltk.corpus import stopwords stopWords = set(stopwords.words('english')) # Setting up the serial keys and stuff: consumer_key = "skrtPlkUrqc3zqskPiup0Hl30" consumer_secret = "og2hCbNeWkhihoiLuIIJo1jY9qJcUPboHNVRO4FE6K0rTCqSLS" access_token = "785116862-zT7wQtAefZQe7RY2Ni1kODUGpfrk5rIjPPXpM2CK" access_token_secret = "xJRFeS1W40WCkRVdeqPTZaAwjKO266ndRxoaQP2R99Tnh" # Setting up authenticati--kokokon and handling: auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creating the API object: twitter_api = API(auth_handler=auth) # Setting up the file that we write the user_ids to: f = open("test.txt", "w+") # Setting up the Tweet query to use query = "I am depressed" while True: try: for item in twitter_api.search(q=query, count=100, include_rts=False): print(item.screen_name) except tweepy.TweepError as e: # In case of an error, it will re-attempt in 60 seconds. time.sleep(60) print("Some error: " + str(e)) continue
tweet_text = tweet._json['text'] if len(tag(tweet_text)) > len( tweet_text): # Filter out non timeline sentences tweets.append((tweet_date, tweet_text)) curr_tweets += 1 if curr_tweets >= max_tweets: break return tweets if __name__ == '__main__': auth = OAuthHandler(credentials['CONSUMER_KEY'], credentials['CONSUMER_SECRET']) auth.set_access_token(credentials['ACCESS_TOKEN'], credentials['ACCESS_SECRET']) api = API(auth) query = 'a' # input("Enter topic to search on twitter : ") max_tweets = 10 curr_tweets = 0 from_date = today - datetime.timedelta(days=5) from_date = from_date.strftime("%Y-%m-%d") to_date = today - datetime.timedelta(days=0) to_date = to_date.strftime("%Y-%m-%d") tweets = {"1daysago": [], "2daysago": [], "3daysago": []} for tweet in tweepy.Cursor(api.search, q=query, lang=['en'], since=from_date, until=to_date).items(): for i in range(1, 4): tweet_date = datetime.datetime.strptime(
print "starging bot..." logging.getLogger("main").setLevel(logging.DEBUG) logging.debug('Twittbot starting...') print "loading twitts..." loadfile('follows.txt', follows) loadfile('twitts.txt', twitts) atexit.register(exitfunc) warnings.filterwarnings("ignore") auth_handler = OAuthHandler(consumer_key, consumer_secret) auth_handler.set_access_token(access_token, access_token_secret) twitter_client = API(auth_handler, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True) listener = PyStreamListener() stream = Stream(auth_handler, listener) try: stream.filter(track=track) except: print "execution error..." exitfunc() # #sys.exit()
def __init__(self, twitter_user=None): self.auth = TwitterAuthenticator().authenticate_twitter_app() self.twitter_client = API(self.auth) self.twitter_user = twitter_user
def pull_down_tweets(screen_name): api = API(auth) tweets = api.user_timeline(screen_name=screen_name, count=200) for tweet in tweets: # print(json.dumps(tweet._json,indent=4)) print(tweet.text)