def test_get_a_bearer_token(self):
     """ Make sure the twitter.oauth2_dance() function gives us a bearer token  """
     import os
     auth_keys = twitter_tool.get_auth_keys()
     bearer_token_file = "test/test_bearer_token"
     twitter.oauth2_dance(consumer_key=auth_keys[0], consumer_secret=auth_keys[1], token_filename=bearer_token_file)
     os.remove(bearer_token_file)
Пример #2
0
def get_bearer_token(consumer_key=None, consumer_secret=None):
    if consumer_key is None or consumer_secret is None:
        app_creds = get_app_creds()
        consumer_key = app_creds['consumer_key']
        consumer_secret = app_creds['consumer_secret']

    if not os.path.exists(BEARER_TOKEN_FILENAME):
        logging.info(
            f'Bearer token is not found in "{BEARER_TOKEN_FILENAME}", requesting a new one'
        )
        oauth2_dance(consumer_key, consumer_secret, BEARER_TOKEN_FILENAME)
    else:
        logging.info(f'Reusing Bearer token from {BEARER_TOKEN_FILENAME}')

    with open(BEARER_TOKEN_FILENAME) as bearer_token_file:
        return bearer_token_file.read().rstrip()
Пример #3
0
def getAppToken():
    """This routine returns an application-based token from Twitter
       that works for inquiries not requiring a personal login"""

    key1 = '3sTM6ubGwujS3tw='
    key2 = 'otW6wqnYw7vg5Ng='
    key3 = 'tsaWt7ucouy5p9vj1Mno38zVubjawLi8zA=='
    key4 = 'xKee58ul2Kne3NC2pbLA36zJ1s644rC80tirzNO638Lexaev3byXu93b07ekzJnp2qo='

    # Evaluate a standard path filename for an app token for this application
    tokenFilename = os.path.normcase(
        os.path.expanduser('~/.twitter_appToken_{}'.format(myName)))

    # Read the needed app-token if its file exists, otherwise create and write it.
    if os.path.exists(tokenFilename):
        token = twitter.read_bearer_token_file(tokenFilename)
        print 'Application token read from {}'.format(tokenFilename)
    else:
        # token = twitter.oauth2_dance(consumer_key, consumer_secret)
        token = twitter.oauth2_dance(mapKey(mapKey(key1, key2), key3),
                                     mapKey(mapKey(key1, key2), key4))
        twitter.write_bearer_token_file(tokenFilename, token)
        print 'Application token created and written to {}'.format(
            tokenFilename)

    return token
    def __setting_oauth2(self):
        """OAuth2のための設定を行う."""
        api_key = os.environ['TWITTER_API_KEY']
        api_secret = os.environ['TWITTER_API_SECRET']

        return OAuth2(bearer_token=oauth2_dance(
            api_key,
            api_secret,
        ))
Пример #5
0
def tweets_to_csv_mention_graph(filejson,filecsv):
	df_dirty = pd.read_json(filejson)
	
	for key,values in df_dirty.iterrows():
		data = values['data']
		try:
			if data['entities']['mentions']: continue
		except (KeyError,TypeError):
			df_dirty.drop(key,inplace=True)
	
	data = df_dirty['data']
	authors = [tweet['author_id'] for tweet in data]
	mentions_list = list(map(lambda x: [y['username'] for y in x],\
			[tweet['entities']['mentions'] for tweet in data]))
			
	twitter_keys = json.load(open('info/application_keys.json'))['twitter']
	consumer_key = twitter_keys['API_key']
	consumer_secret = twitter_keys['API_secret']
	bearer_token = twitter.oauth2_dance(consumer_key=consumer_key,consumer_secret=consumer_secret)
	twitter_api = twitter.Twitter(auth=twitter.OAuth2(bearer_token=bearer_token))
	
	count = 0
	mentioned_list = []
	for mentions in mentions_list:
		mentioned_ids=[]
		for mention in mentions:
			try:
				id_str = twitter_api.users.show(screen_name=mention)['id_str']
				mentioned_ids.append(id_str)
			except TwitterHTTPError: pass
			count+=1
			if count%899 == 0: #limit by twitter API
				print('Count: {}, sleep start'.format(count))
				time.sleep(15*60)
				print('Sleep end')
		mentioned_list.append(mentioned_ids)
		
	df_csv = pd.DataFrame()
	for author,mentions in zip(authors,mentioned_list):
		for mention in mentions:
			df_csv = df_csv.append({'source': author,\
						'target': mention,\
						'weight': 1},ignore_index=True)
						
	df_csv_weight = pd.DataFrame({'weight' : \
			df_csv.groupby( [ "source", "target"] ).size()})\
			.reset_index()
	
	df_csv_weight.to_csv(filecsv,index=False)
	
	
		
		
Пример #6
0
def query_twitter():
	current_token = TwitterAccessToken.objects.get(id = 7, active = 1)

	_TwitterOAuthToken = current_token.access_token
	_TwitterOAuthTokenSecret = current_token.access_token_secret
	_TwitterConsumerKey = current_token.api_key
	_TwitterConsumerSecret = current_token.api_secret

	TWITTER_ACCESS_DETAILS = (_TwitterConsumerKey, _TwitterConsumerSecret)
	twitterapi = Twitter(auth=OAuth2(bearer_token=oauth2_dance(*TWITTER_ACCESS_DETAILS)))

	# tweets = twitterapi.statuses.user_timeline(id = 348375714,count = 5,include_rts = True)
	tweets = twitterapi.search.tweets(q="#omgindia",geocode="20.593684,78.96288,500mi",count=100)

	for tweet in tweets["statuses"]:
		pp.pprint(tweet)
	def connect_to_api(self):
		if len(self.twitterapps)>0:
			if self.appindex == len(self.twitterapps)-1:
				self.appindex = 0
			# self.appindex = random.randint(0,len(self.twitterapps)-1)
			self.twapp = self.twitterapps[self.appindex]
			current_token = self.twitterapps[self.appindex]
			_TwitterOAuthToken = current_token.access_token
			_TwitterOAuthTokenSecret = current_token.access_token_secret
			_TwitterConsumerKey = current_token.api_key
			_TwitterConsumerSecret = current_token.api_secret

			TWITTER_ACCESS_DETAILS = (_TwitterConsumerKey, _TwitterConsumerSecret)
			twitterapi = Twitter(auth=OAuth2(bearer_token=oauth2_dance(*TWITTER_ACCESS_DETAILS)))
			# limit_status = twitterapi.application.rate_limit_status(resources="statuses,users")
			# pp.pprint(limit_status)
			# pp.pprint(TWITTER_ACCESS_DETAILS)
			self.appindex += 1
			return twitterapi
		else:
			print "No Twitter Apps"
			return False
Пример #8
0
def connect_app(config):

    # OAuth credentials
    credentials_file = config.TWITTER_USER_CREDENTIALS
    check_file_access(credentials_file,
                      'TwitterBot credentials file not writeable: %r')

    # Run OAuth dance (opening a webbrowser)
    (token, token_secret) = twitter.oauth_dance(
        config.APP_NAME,
        config.TWITTER_CONSUMER_KEY,
        config.TWITTER_CONSUMER_SECRET,
        credentials_file)

    # Convert consumer key/secret to OAuth2 bearer token
    bearer_file = config.TWITTER_BEARER_CREDENTIALS
    check_file_access(bearer_file,
                      'TwitterBot bearer credentials file not writeable: %r')

    # Run OAuth2 dance and store credentials
    token = twitter.oauth2_dance(
        config.TWITTER_CONSUMER_KEY,
        config.TWITTER_CONSUMER_SECRET)
    twitter.write_bearer_token_file(bearer_file, token)
Пример #9
0
 def __init__(self, consumer_key, consumer_secret):
     bearer_token = oauth2_dance(consumer_key, consumer_secret)
     self.client = Twitter(auth=OAuth2(bearer_token=bearer_token))
Пример #10
0
        at = _parse_time(t['created_at'])
        data.append({'url': href, 'title': text, 'time': at})
    if latest_id is not None:
        state['tw_latest_id'] = latest_id
    return data[::-1]


if __name__ == '__main__':
    from sys import argv
    from netwatch.utils import load_config

    if argv[1:] and argv[1:] == 'bearer-token':
        from twitter import oauth2_dance
        from os import environ
        print(
            'Token:',
            oauth2_dance(environ['CONSUMER_KEY'], environ['CONSUMER_SECRET']))
        exit()

    cfg = load_config('feeds.yml')
    init(cfg)
    urls = argv[1:]
    for u in urls:
        blob = {'url': u}
        for v in cfg.get('tweets', []):
            if get_username(v['url']).lower() == u.lower():
                blob = v
                break

        pprint(fetch({}, blob, True))
Пример #11
0
from boto3.dynamodb.conditions import Key
import twitter
import os
import json
from base64 import b64decode

#Handle encrypted ENV variables
ENCRYPTED = os.environ['CUST_KEY']
ENCRYPTED2 = os.environ['CUST_SEC']
customer_key = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED))['Plaintext']
customer_secret = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED2))['Plaintext']

#Create Twitter connection
token = twitter.oauth2_dance(customer_key, customer_secret)
t = twitter.Twitter(auth=twitter.OAuth2(bearer_token=token))

#Create Dynamo globals
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table('Tweets')

#Boto lambda client
lambda_client = boto3.client('lambda')


def check_tweets(number):

    res = t.statuses.user_timeline(screen_name='realDonaldTrump', count=number)
    return res
Пример #12
0
def init(consumer_key, consumer_secret):
    """
    Get and store a bearer token for this session.
    """
    global BEARER_TOKEN
    BEARER_TOKEN = twitter.oauth2_dance(consumer_key, consumer_secret)
Пример #13
0
}, {
    'State_abbv': 'WY',
    'State': 'Wyoming',
    'Largest city': 'Cheyenne',
    'Middle point': 'Powder River',
    'geo_code_large': '41.1399814,-104.82024619999999,12km',
    'geo_code_middle_point': '43.0759678,-107.29028390000002,200km',
    'max_id': {
        0: '',
        1: ''
    }
}]
'''*** Twitter authentication details***'''
consumer_key = '<Your consumer key provided by Twitter>'
consumer_secret = '<Your consumer secret provided by Twitter>'
BEARER_TOKEN = twitter.oauth2_dance(consumer_key, consumer_secret)
'''Ignored fields to filter before push the data to the DB'''

ignored_fields = [
    'id_str', 'truncated', 'in_reply_to_status_id_str',
    'in_reply_to_user_id_str', 'in_reply_to_screen_name', 'place',
    'quoted_status_id', 'quoted_status_id_str', 'quoted_status', 'quote_count',
    'reply_count', 'favorite_count', 'extended_entities', 'favorited',
    'possibly_sensitive', 'filter_level', 'lang', 'matching_rules', 'scopes',
    'withheld_copyright', 'withheld_in_countries', 'withheld_scope',
    'metadata', 'contributors'
]

user_ignored_fields = [
    'id_str', 'url', 'verified', 'friends_count', 'listed_count',
    'favourites_count', 'statuses_count', 'created_at', 'utc_offset',