from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import boto3 from os import environ author = 'Huanren Zhang' doc = """ MTurk Qualification HIT """ class Constants(BaseConstants): name_in_url = 'recruitment' players_per_group = None num_rounds = 1 wait_minutes = 1 class Subsession(BaseSubsession): def creating_session(self): pass class Group(BaseGroup): pass class Player(BasePlayer): # t10 = models.BooleanField(initial=0,blank=True) # t15 = models.BooleanField(initial=0,blank=True) # t20 = models.BooleanField(initial=0,blank=True) # t25 = models.BooleanField(initial=0,blank=True) # t30 = models.BooleanField(initial=0,blank=True) # t35 = models.BooleanField(initial=0,blank=True) # t40 = models.BooleanField(initial=0,blank=True) # t45 = models.BooleanField(initial=0,blank=True) # t50 = models.BooleanField(initial=0,blank=True) session_time = models.IntegerField( choices=[ # [27, '16:00 ET / 15:00 CT / 13:00 PT on Friday, August 27'], # [28, '14:00 ET / 13:00 CT / 11:00 PT on Saturday, August 28'], # [29, '14:00 ET / 13:00 CT / 11:00 PT on Sunday, August 29'], # [32, '14:00 ET / 13:00 CT / 11:00 PT on Monday, August 30'], [33, '15:00 ET / 14:00 CT / 12:00 PT on Monday, August 30'], ], widget=widgets.RadioSelect ) workerid = models.StringField() consent = models.BooleanField(initial=0) data_received = models.IntegerField(initial=0) # on which page user data is received (0 represent not received) machine = models.StringField() browser = models.StringField() agent_string = models.StringField() ip = models.StringField(blank=True) language = models.StringField(blank=True) region = models.StringField(blank=True) locality = models.StringField(blank=True) city = models.StringField(blank=True) postcode = models.StringField(blank=True) network = models.StringField(blank=True) timeZone = models.StringField(blank=True) utcOffset = models.StringField(blank=True) latitude = models.FloatField(blank=True) longitude = models.FloatField(blank=True) tor = models.BooleanField(blank=True) vpn = models.BooleanField(blank=True) proxy = models.BooleanField(blank=True) def workerid_error_message(self, value): print('value is', value, self.session.config['access_key_id']) if not self.session.config.get('debug',False): access_key_id = self.session.config['access_key_id'] secret_access_key = self.session.config['secret_access_key'] print('access_key_id',access_key_id, environ.get('AWS_ACCESS_KEY_ID')) print('secret_access_key',secret_access_key, environ.get('AWS_SECRET_ACCESS_KEY')) if self.session.config.get('sandbox',False): # sandbox Qid = self.session.config['Qid_sandbox'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', endpoint_url="https://mturk-requester-sandbox.us-east-1.amazonaws.com", ) else: # live production Qid = self.session.config['Qid'] mturk = boto3.client( service_name='mturk', aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, region_name='us-east-1', ) try: mturk.associate_qualification_with_worker( QualificationTypeId=Qid, WorkerId=value, IntegerValue=0, SendNotification=False ) except Exception as e: print(e, value,Qid,) return 'Please input a valid Worker ID.' def live_method(self, data): user_agent_string = data['userAgent'] geo_data = data['geoData'] print('received agent data for player%d'%self.participant.id_in_session) self.data_received = data['page'] self.agent_string = user_agent_string # print('User agent string:', user_agent_string) # # This part checks for the operating system if 'Macintosh' in user_agent_string: self.machine = 'Macintosh' elif 'Windows' in user_agent_string: self.machine = 'Windows' elif 'X11' in user_agent_string: self.machine = 'UNIX' elif 'Linux' in user_agent_string: self.machine = 'Linux' else: self.machine = 'Other' # This part checks for the browser if 'Chrome' in user_agent_string: self.browser = 'Chrome' elif 'Edge' in user_agent_string: self.browser = 'Edge' elif 'Firefox' in user_agent_string: self.browser = 'Firefox' elif 'Safari' in user_agent_string and 'Chrome' not in user_agent_string and 'Edge' not in user_agent_string: self.browser = 'Safari' elif 'MSIE' in user_agent_string: self.browser = 'InternetExplorer' elif 'Opera' in user_agent_string: self.browser = 'Opera' else: self.browser = 'Other' self.ip = geo_data['ip'] self.language = geo_data['localityLanguageRequested'] self.region = geo_data['location']['isoPrincipalSubdivision'] self.city = geo_data['location']['city'] self.locality = geo_data['location']['localityName'] self.postcode = geo_data['location']['postcode'] self.latitude = geo_data['location']['latitude'] self.longitude = geo_data['location']['longitude'] self.timeZone = geo_data['location']['timeZone']['effectiveTimeZoneShort'] self.utcOffset = geo_data['location']['timeZone']['utcOffset'] self.network = geo_data['network']['organisation'] self.tor = geo_data['hazardReport']['isKnownAsTorServer'] self.vpn = geo_data['hazardReport']['isKnownAsVpn'] self.proxy = geo_data['hazardReport']['isKnownAsProxy']