def main(): logger = Logger(debug=False) twitter = TwitterAPI(logger) actions = [Actions.TweetActions.space_gif, Actions.TweetActions.retweet_astro_timpeake, Actions.TweetActions.retweet_thom_astro, Actions.TweetActions.retweet_astropeggy, Actions.TweetActions.retweet_astro_kimbrough ] while True: for action in actions: if action == Actions.TweetActions.space_gif: if twitter.giphy_tweet(): break else: word_whitelist = None word_blacklist = None if action == Actions.TweetActions.retweet_scott_kelly: word_whitelist = ["#YearInSpace"] if twitter.retweet_astronaut(action, word_blacklist, word_whitelist): break logger.log('Going to sleep for 30mins', emoji='😴') time.sleep(1800) # repeat every 30 mins
# example using python twitter API # # for testing only, in your case you need to handle callback url and grab oauth_token and oauth_verifier # for get access token # # for authentication grant access # will return authentication url for grant access from twitterAPI import TwitterAPI # create object # TwitterAPI('CONSUMER SECRET', 'CONSUMER KEY') t_api = TwitterAPI('rUJ8MepSitKOYoSmaIJS', '49BcA9HPSaD') # request token # need callback parameter # callback is must same with your application callback in twitter apps request_token = t_api.request_token( 'http://127.0.0.1:8888/p/authenticate/twitter') # return token will give dictionary output if operation success # example output in my case {'oauth_callback_confirmed': 'true', 'oauth_token': 'ITL82qNEmWkh3Uze', 'oauth_token_secret': 'M1XwCvmMffnTD'} # we will use oauth_token and oauth_token_secret for next step when we request access token # then use oauth_token for get authentication url # request authentication url # use oauth_token as parameter print(t_api.request_authenticate_url(request_token.get('oauth_token')))
def main(): api = TwitterAPI() while True: follow_followers(api) logger.info("Waiting...") time.sleep(60)
def mentions_main(): api = TwitterAPI() since_id = 1 while True: since_id = check_mentions(api, "arkiword", since_id) logger.info("Waiting for mentions...")
# import twitter api from twitterAPI import TwitterAPI # create object # input your oauth_token, oauth_token_secret (from previous step) and oauth_verifier from callback url to request access token t_api = TwitterAPI('rUJ8MepSitKOYoSmaIJS', '49BcA9HPSaD') # request_access_token('oauth_token', 'oauth_token_secret', 'oauth_verifier') print(t_api.request_access_token('ITL82qNEmWkh3Uze', 'M1XwCvmMffnTD', 'QTLTHWAF52trN07q1')) # in my case it will give an output # {'oauth_token_secret': 'VN8gw5ESX8eBExLzS8pp', 'user_id': '299', 'oauth_token': '299-2cim9m4d630UKb', 'screen_name': 'sikilkuinc'} # save it and store it in text file or database, because we can use oauth_token_secret and oauth_token for request an api # no need step step 1 and step 2 if you already have oauth_token and oauth_token_secret
""" from twitterAPI import TwitterAPI from sklearn.feature_extraction.text import TfidfTransformer, TfidfVectorizer from sklearn.cluster import KMeans from sklearn import metrics from sklearn.metrics import silhouette_score import numpy as np # Read trends from csv file trends = [] with open('trends.csv') as f: for t in f.read().split(','): trends.append((t.strip().lower())) # Print ">> Retrieving tweets". api = TwitterAPI() corpus = [] for hashtag in trends: corpus.append('. '.join(api.search(hashtag))) vectorizer = TfidfVectorizer() term_document_matrix = vectorizer.fit_transform(corpus) tf_transformer = TfidfTransformer(use_idf=True).fit(term_document_matrix) term_document_matrix_tf = tf_transformer.transform(term_document_matrix) # Set lower bound by rule of thumb for determining k using the silhouette coefficient. num_clusters_lower = int(np.sqrt(len(trends) / 2)) num_clusters_upper = int(len(trends)) print "lower k bound is: %s and upper k bound is %s" % (num_clusters_lower, num_clusters_upper) best_k = num_clusters_lower
# call REST API # call request api # Returns a collection of the most recent Tweets posted by the user indicated by the screen_name or user_id parameters. # read twitter rest api for more params optional # ex: params = {'param':'value', 'param':'value'} # api_type is key of api will called # ex: api_type='api_statuses_mentions_timeline' # def request_api(self, oauth_token, oauth_token_secret, api_type, params={}): from twitterAPI import TwitterAPI t_api = TwitterAPI('rUJ8MepSitKOYoSmaIJS', '49BcA9HPSaD') # use api_type='api_statuses_user_timeline' for request https://api.twitter.com/1.1/statuses/user_timeline.json # request_api(oauth_token, oauth_token_secret, api_type, params={}) # params in dictionary # param key is same with twitter api # ex i want to get user_timeline with user_id='299' and count='2' # all parameter should be in string format # in my case I will use this information from previous step {'oauth_token_secret': 'VN8gw5ESX8eBExLzS8pp', 'user_id': '299', 'oauth_token': '299-2cim9m4d630UKb', 'screen_name': 'sikilkuinc'} params = {'user_id': '299', 'count': '2'} print( t_api.request_api('299-2cim9m4d630UKb', 'VN8gw5ESX8eBExLzS8pp', 'api_statuses_mentions_timeline', params)) # output will be in json format
def main(): api = TwitterAPI() tweet_and_word = Tweet_and_AddWord(api) tweet_and_word.tweet_words()
# import twitter api from twitterAPI import TwitterAPI # create object # input your oauth_token, oauth_token_secret (from previous step) and oauth_verifier from callback url to request access token t_api = TwitterAPI('rUJ8MepSitKOYoSmaIJS', '49BcA9HPSaD') # request_access_token('oauth_token', 'oauth_token_secret', 'oauth_verifier') print( t_api.request_access_token('ITL82qNEmWkh3Uze', 'M1XwCvmMffnTD', 'QTLTHWAF52trN07q1')) # in my case it will give an output # {'oauth_token_secret': 'VN8gw5ESX8eBExLzS8pp', 'user_id': '299', 'oauth_token': '299-2cim9m4d630UKb', 'screen_name': 'sikilkuinc'} # save it and store it in text file or database, because we can use oauth_token_secret and oauth_token for request an api # no need step step 1 and step 2 if you already have oauth_token and oauth_token_secret
# example using python twitter API # # for testing only, in your case you need to handle callback url and grab oauth_token and oauth_verifier # for get access token # # for authentication grant access # will return authentication url for grant access from twitterAPI import TwitterAPI # create object # TwitterAPI('CONSUMER SECRET', 'CONSUMER KEY') t_api = TwitterAPI('rUJ8MepSitKOYoSmaIJS', '49BcA9HPSaD') # request token # need callback parameter # callback is must same with your application callback in twitter apps request_token = t_api.request_token('http://127.0.0.1:8888/p/authenticate/twitter') # return token will give dictionary output if operation success # example output in my case {'oauth_callback_confirmed': 'true', 'oauth_token': 'ITL82qNEmWkh3Uze', 'oauth_token_secret': 'M1XwCvmMffnTD'} # we will use oauth_token and oauth_token_secret for next step when we request access token # then use oauth_token for get authentication url # request authentication url # use oauth_token as parameter print(t_api.request_authenticate_url(request_token.get('oauth_token')))