from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from datetime import datetime import time import boto3 author = 'Huanren Zhang' doc = """ Basic information and consent form """ class Constants(BaseConstants): name_in_url = 'negotiation' players_per_group = None num_rounds = 1 wait_minutes = 1 num_exercises = 4 class Subsession(BaseSubsession): def creating_session(self): pass class Group(BaseGroup): pass class Player(BasePlayer): name = models.StringField() signing_date = models.StringField() username = models.StringField() time_diff = models.FloatField(initial=0) # time difference between client and server in seconds 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 'You cannot participate because we cannot detect your Work ID. Contact the requester if you believe this is an error..' def live_method(self, data): if data.get('client_timestamp','') != '': self.time_diff = round((data['client_timestamp'] - time.time())*10)/10 ## returning time left to start from the server to the client experiment_starting_time = self.session.config.get('experiment_starting_time', '') time_to_start = 0 if experiment_starting_time != '': starting_timestamp = datetime.fromisoformat(experiment_starting_time).timestamp() time_to_start = starting_timestamp - time.time() return {self.id_in_group: time_to_start} if data.get('word','') != '': self.keyword = data.get('word','') # print(self.keyword) if self.keyword == 'zhang': return {self.id_in_group: 'advance'} else: 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']