import requests import tweepy from requests_oauthlib import OAuth1Session import os import json from NewsSocialSignaling import TwitterProcessor import datetime as dt import numpy as np with open(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'keys', 'keys.json'), 'r') as f: keys = json.load(f) keys = keys['twitter_credentials']['news_signaling_academic'] CONSUMER_KEY = keys['CONSUMER_KEY'] CONSUMER_SECRET = keys['CONSUMER_SECRET'] from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) class Constants(BaseConstants): name_in_url = 'survey' players_per_group = None num_rounds = 1 num_peers_to_sample = 5 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): verifier_code = models.StringField(label='Twitter Authorization Code') def live_handler(self, data): print(data) # if data['type'] == 'start_authenticate': print('returning') # Authentication follows tutorial from # https://bilesanmiahmad.medium.com/how-to-login-with-twitter-api-using-python-6c9a0f7165c5 # https://github.com/bilesanmiahmad/twitter-integration/blob/master/twitter.py def auth_url(self): print('Getting auth url') resource = self.twitter_get_oauth_request_token() auth_url = 'https://api.twitter.com/oauth/authenticate?oauth_token={0}'.format(resource[0]) self.participant.vars['auth_url'] = auth_url return auth_url def twitter_get_oauth_request_token(self): print('Starting get oauth request token') request_token = OAuth1Session(client_key=CONSUMER_KEY, client_secret=CONSUMER_SECRET) url = 'https://api.twitter.com/oauth/request_token' data = request_token.get(url) data_token = str.split(data.text, '&') ro_key = str.split(data_token[0], '=') ro_secret = str.split(data_token[1], '=') resource_owner_key = ro_key[1] resource_owner_secret = ro_secret[1] resource = [resource_owner_key, resource_owner_secret] self.participant.vars['auth_resource'] = resource return resource def twitter_get_oauth_token(self, verifier, ro_key, ro_secret): print('Starting get oauth token') if 'auth_access_tokens' in self.participant.vars: return self.participant.vars['auth_access_tokens'] oauth_token = OAuth1Session(client_key=CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=ro_key, resource_owner_secret=ro_secret) url = 'https://api.twitter.com/oauth/access_token' data = {"oauth_verifier": verifier} access_token_data = oauth_token.post(url, data=data) access_token_list = str.split(access_token_data.text, '&') self.participant.vars['auth_access_tokens'] = access_token_list return access_token_list def twitter_get_access_token(self, access_token_list, name): print('Starting get access token') access_token_key = str.split(access_token_list[0], '=') access_token_secret = str.split(access_token_list[1], '=') access_token_name = str.split(access_token_list[3], '=') access_token_id = str.split(access_token_list[2], '=') key = access_token_key[1] secret = access_token_secret[1] # name = access_token_name[1] # id = access_token_id[1] oauth_user = OAuth1Session(client_key=CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=key, resource_owner_secret=secret) url_user = 'https://api.twitter.com/1.1/account/verify_credentials.json' params = {"include_email": 'true'} print('Pulling user data now') user_data = oauth_user.get(url_user, params=params) # get list of who they follow and who follows them (need to use cursoring which is tricky) done_with_friends = False done_with_followers = False friends = [] followers = [] friend_cursor = -1 follower_cursor = -1 for ix in range(2): if not done_with_friends: friend_url = 'https://api.twitter.com/1.1/friends/ids.json?cursor=-1&screen_name={0}&count=5000&cursor={1}'.format(user_data.json()['screen_name'], friend_cursor) friend_ids = oauth_user.get(friend_url).json() friends += friend_ids['ids'] friend_cursor = friend_ids['next_cursor'] if not done_with_followers: follower_url = 'https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name={0}&count=5000&cursor={1}'.format(user_data.json()['screen_name'], follower_cursor) follower_ids = oauth_user.get(follower_url).json() followers += follower_ids['ids'] follower_cursor = follower_ids['next_cursor'] if friend_cursor == 0: done_with_friends = True if follower_cursor == 0: done_with_followers = True if done_with_friends and done_with_followers: break # print(friend_ids.json()) # print(follower_ids.json()) self.participant.vars['user_data_' + name] = user_data.json() self.participant.vars['user_friends_' + name] = friends self.participant.vars['user_followers_' + name] = followers self.participant.vars['user_added_' + name] = dt.datetime.utcnow() return { 'user_data': user_data.json(), 'user_friends': friend_ids, 'user_followers': follower_ids } def add_peer_info(self, user): if 'peer_info' in self.participant.vars: return self.participant.vars['peer_info'] print('Pulling network data now') if 'sample_follower_seed' not in self.participant.vars: self.participant.vars['sample_follower_seed'] = np.random.randint(1, 10000000) peer_info = TwitterProcessor.follower_summary_stat(user=user, k=Constants.num_peers_to_sample, seed=self.participant.vars['sample_follower_seed']) if isinstance(peer_info, type(None)): return self.participant.vars['peer_info'] = peer_info return peer_info