import time 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, ) def duplicateChoice(x): y = [] for e in x: y = y + [[e, e]] return y class Constants(BaseConstants): name_in_url = 'Newspapers' players_per_group = None num_rounds = 1 num_peers_to_sample = 5 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): selected_publisher = models.StringField() log = models.LongStringField(label='', blank=True) debrief_why_publisher_slant = models.StringField(label='I selected this publisher based on its slant', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_popularity = models.StringField(label='I selected the most popular publisher', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_truthsworthy = models.StringField(label='I selected the publisher that looked more trustworthy', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_own_close = models.StringField(label='I selected the publisher that is closest to my political ideology', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_own_far = models.StringField(label='I selected a publisher different than my ideology in order to be exposed to new information', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_peers_close = models.StringField(label='I selected a publisher in the direction to my friends', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_peers_far = models.StringField(label='I selected a publisher in the opposite direction of my friends', choices=duplicateChoice(['Yes', 'No', 'Maybe', 'I am not sure I understand']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher = models.StringField(label='If you had to choose one, which one was the most important factor to select this publisher?', choices=duplicateChoice( [ 'I selected this publisher based on its slant', 'I selected the most popular publisher', 'I selected the publisher that looked more trustworthy', 'I selected the publisher that is closest to my political ideology', 'I selected a publisher different than my ideology in order to be exposed to new information', 'I selected a publisher in the direction to my friends', 'I selected a publisher in the opposite direction of my friends', 'Is there any other factor that that you want to share with us and think influence your decision to select this publisher?', 'None of the above' ]), widget=widgets.RadioSelect, blank=True) debrief_why_publisher_any_other = models.LongStringField(label='Are there any other factors that you want to share with us that influence your decision to select this publisher?', blank=True) debrief_technical_glitches = models.LongStringField(label='Please explain any technical glitches you may have encountered.', blank=True) debrief_instructions = models.LongStringField(label='Were the experimental instructions clear? If not, how could they be improved?', blank=True) debrief_summary_stat = models.StringField(label='How well do you feel you understood how the summary of your news diet was calculated?', choices=['Not at all', 'Limited understanding', 'Moderate understanding', 'Great understanding'], widget=widgets.RadioSelectHorizontal, blank=True) debrief_other = models.LongStringField(label='Additional feedback.', blank=True) debrief_ideology = models.StringField(label='Yourself', choices=duplicateChoice(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollowers = models.StringField(label='People that are following you on Twitter', choices=duplicateChoice(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollow = models.StringField(label='People that you follow on Twitter', choices=duplicateChoice(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_tweet_scale = models.StringField(label='On a scale from 0 to 10 where 0 "None" and 10 means "A lot", how much do you care about what others think of the content you tweet and retweet?',choices=duplicateChoice(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_tweet_question = models.StringField(label='What of the following statements is closest to your view when deciding what to tweet?',choices=duplicateChoice(["I only care about what my followers would think", "I only care about what the people that I follow think", 'I only care about what the people that I interact the most on Twitter with would think',"I don't care about how others perceive my content"]), widget=widgets.RadioSelect, blank=True) debrief_tweet_why = models.LongStringField(label='Could you tell us why (or why not) you decided to follow on Twitter the publisher that you selected?', blank=True) debrief_retweet_scale = models.StringField( label='On a scale from 0 to 10 where 0 "Not at all" and 10 means "A lot", how comfortable would you feel retweeting information about a publisher you follow?', choices=duplicateChoice(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']), widget=widgets.RadioSelectHorizontal, blank=True ) debrief_retweet_scale_selected = models.StringField( label='On the same scale, how comfortable would you feel retweeting information about the publisher you selected?', choices=duplicateChoice(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']), widget=widgets.RadioSelectHorizontal, blank=True) def live_handler(self, data): # handles logs if isinstance(self.log, type(None)): self.log = '' self.log = self.log + ';' + json.dumps(data) if 'USERNAME' in os.environ and os.environ['USERNAME'] == 'moehring': print('\n\n\n\n') print(data) if 'selected_publisher' in data: self.selected_publisher = data['selected_publisher'].replace(';', '') self.participant.vars['selected_publisher'] = data['selected_publisher'].replace(';', '') time.sleep(0.1) return {self.participant.id_in_session: 'continue'} 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 None self.participant.vars['peer_info'] = peer_info return peer_info