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 import time # 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 def duplicateChoiceEmpty(x): y = [] for e in x: y = y + [[e, ' ']] return y 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') log = models.LongStringField(label='', 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=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollowers = models.StringField(label='Average person that follows you on Twitter', choices=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollow = models.StringField(label='Average person that you follow on Twitter', choices=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative', 'Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyNews = models.StringField(label='Your account', choices=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative','Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollowersNews = models.StringField(label='Average person that follows you on Twitter',choices=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate','Slightly conservative', 'Conservative','Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_ideologyTwitterFollowNews = models.StringField(label='Average person that you follow on Twitter', choices=duplicateChoiceEmpty(['Extremely liberal', 'Liberal', 'Slightly liberal', 'Moderate', 'Slightly conservative', 'Conservative','Extremely conservative']), widget=widgets.RadioSelectHorizontal) debrief_why_publisher_slant = models.StringField(label='The ideology of the publishers', choices=duplicateChoiceEmpty(['Yes','More or less','No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_popularity = models.StringField(label='The popularity of the publishers', choices=duplicateChoiceEmpty(['Yes','More or less','No']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_truthsworthy = models.StringField(label='The trustworthiness of the publishers', choices=duplicateChoiceEmpty(['Yes','More or less','No']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_ideology = models.StringField(label='How close or far the publishers are from my ideology',choices=duplicateChoiceEmpty(['Yes', 'More or less', 'No']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_ideology_friends = models.StringField(label='How close or far the publishers are from the ideology of my friends',choices=duplicateChoiceEmpty(['Yes', 'More or less', 'No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_ideology_followers = models.StringField(label='How close or far the publishers are from the ideology of my followers',choices=duplicateChoiceEmpty(['Yes', 'More or less', 'No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_ideology_followees = models.StringField(label='How close or far the publishers are from the ideology of the people that I follow',choices=duplicateChoiceEmpty(['Yes', 'More or less', 'No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_own_close = models.StringField(label='I selected publishers that are close to my ideology', choices=duplicateChoiceEmpty(['Yes', 'More or less', 'No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_own_close = models.StringField(label='I selected publishers that are close to my ideology', choices=duplicateChoiceEmpty(['Yes','More or less','No']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_own_far = models.StringField(label='I selected publishers that oppose my ideology', choices=duplicateChoiceEmpty(['Yes','More or less','No']), widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_peers_close = models.StringField(label='I selected publishers closer to my friends', choices=duplicateChoiceEmpty(['Yes','More or less','No']),widget=widgets.RadioSelectHorizontal, blank=True) debrief_why_publisher_peers_far = models.StringField(label='I selected publishers in the opposite direction of my friends', choices=duplicateChoiceEmpty(['Yes','More or less','No']),widget=widgets.RadioSelectHorizontal, blank=True) email = models.StringField(label='Email address',blank=True) debrief_why_publisher = models.StringField(label='If you had to choose one, which one was the most important factor when selecting publishers?', choices=duplicateChoice( [ 'How close or far the publishers are from my ideology', 'How close or far the publishers are from the ideology of my followers', 'How close or far the publishers are from the ideology of the people that I follow', '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 publishers?', blank=True) 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=duplicateChoiceEmpty(['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 any of the the publishers you were suggested?', blank=True) debrief_retweet_scale = models.StringField( label='On the same scale, how comfortable would you feel retweeting information about a publisher you follow?', choices=duplicateChoiceEmpty(['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 your news diet summary?', choices=duplicateChoiceEmpty(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']), widget=widgets.RadioSelectHorizontal, blank=True) timeFamily = models.StringField(label='... with family or friends?', choices=duplicateChoiceEmpty(['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) timeFriends = models.StringField(label='...spending time with friends?', choices=duplicateChoiceEmpty(['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) timeTwitter = models.StringField(label='...using Twitter?', choices=duplicateChoiceEmpty( ['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) timeSocialMedia = models.StringField(label='...using social media apps other than Twitter?', choices=duplicateChoiceEmpty( ['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) timeNews = models.StringField(label='...reading or listening to news about politics?', choices=duplicateChoiceEmpty( ['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) timeAlone = models.StringField(label=' ...watching TV or movies by yourself?', choices=duplicateChoiceEmpty( ['0 minutes', 'Between 1 and 30 minutes', 'Between 31 minutes and 1 hour', 'Between 1 and 2 hours', 'Between 2 and 3 hours', 'More than 3 hours']), widget=widgets.RadioSelectHorizontal) guess_lottery1 = models.IntegerField(label='The first lottery?',min=0,max=100) guess_lottery2 = models.IntegerField(label='The second lottery?',min=0, max=100) def live_handler(self, data): if data == 'next': time.sleep(0.1) return {self.participant.id_in_session: 'continue'} # 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) 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