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 = 'qualification' 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): consent = models.BooleanField(initial=0) data_received = models.IntegerField(initial=0) # on which page user data is received (0 represent not received) workerid = models.StringField() 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']