from __future__ import print_function from boto.mturk.connection import MTurkConnection from csv import DictReader import argparse from os.path import expanduser __author__ = 'Andrew Watts <*****@*****.**>' parser = argparse.ArgumentParser( description='Block a worker from doing your HITs on Amazon Mechanical Turk' ) parser.add_argument( '-blockfile', required=True, help="(required) File with comma separated 'worker' and 'reason' columns") parser.add_argument( '-p', '--profile', help= 'Run commands using specific aws credentials rather the default. To set-up alternative credentials see http://boto3.readthedocs.org/en/latest/guide/configuration.html#shared-credentials-file' ) args = parser.parse_args() mtc = MTurkConnection(is_secure=True, profile_name=args.profile) with open(args.blockfile, 'r') as blockfile: toblock = DictReader(blockfile) for row in toblock: print("Blocking '{}' for '{}'".format(row['workerid'], row['reason'])) mtc.block_worker(row['workerid'], row['reason'])
class MTurkClient: # SETUP # =========== def __init__(self,aws_access_key,aws_secret_key,aws_mode): self.mode = aws_mode if aws_mode == 'sandbox': self.host = 'mechanicalturk.sandbox.amazonaws.com' else: self.host = 'mechanicalturk.amazonaws.com' self.c = MTurkConnection( aws_access_key, aws_secret_key, host=self.host) default_settings = { 'lifetime': DAY, 'duration': 10 * MINUTE, 'approval_delay': DAY, 'title': "[title]", 'description': "[description]", 'keywords': [], 'reward': 0.01, 'max_assignments': 1, 'height': 700, 'qualifications': [], } # HITS # =========== def create_hit(self,url,extra_settings): "Eventually, this should take a TEMPLATE and a dictionary of INPUT data that's put into that template. This function would then create an HTML file locally (assuming we're running on a web server) by replacing template {tags} with input values, and then send the URL to the newly created page to MTurk." settings = self.default_settings.copy() settings.update(extra_settings) settings['reward'] = Price(settings['reward']) settings['qualifications'] = qualification.Qualifications(settings['qualifications']) settings['keywords'] = ','.join(settings['keywords']) height = settings.pop('height') hit = self.c.create_hit(question=ExternalQuestion(url,height),**settings)[0] #print 'Created hit %s' % hit.HITId return hit.HITId,hit.HITTypeId #hit_type=None, # Let Amazon do this automatically #annotation=None, # Optional annotation for our system to use #questions=None, # If you want to create multiple HITs at a time? Probably irrelevant for External #response_groups=None, # Unclear what this does def get_hit(self,hit_id): return self.c.get_hit(hit_id)[0] def hit_results(self,hit_id,type=None): # type in ['Submitted','Approved','Rejected',None] results = {} assignments = self.c.get_assignments(hit_id, status=None, page_size=100) for asst in assignments: results.setdefault(asst.AssignmentId,{}) answers = asst.answers[0] for qfa in answers: field, response = qfa.qid, qfa.fields[0] results[asst.AssignmentId][field] = response results[asst.AssignmentId]['worker_id'] = asst.WorkerId results[asst.AssignmentId]['accept_time'] = datetime.strptime(asst.AcceptTime,"%Y-%m-%dT%H:%M:%SZ") results[asst.AssignmentId]['submit_time'] = datetime.strptime(asst.SubmitTime,"%Y-%m-%dT%H:%M:%SZ") return results # URL of a HIT on MTurk def hit_url_turk(self,hit_id): pass def hit_url_external(self,hit_id): pass def extend_hit(self,hit_id,extras): return self.c.extend_hit(hit_id, extras) @catcherror def delete_hit(self,hit_id): self.c.disable_hit(hit_id) # Deletes all the HITS on the server. Risky! def cleanup(self): for hit in self.c.get_all_hits(): self.delete_hit(hit.HITId) # ASSIGNMENTS # =========== @catcherror def approve(self, asst_id, feedback=None): return self.c.approve_assignment(asst_id, feedback) @catcherror def reject(self, asst_id, feedback=None): return self.c.reject_assignment(asst_id, feedback) def block(self,worker_id,feedback=None): return self.c.block_worker(worker_id, feedback) def unblock(self,worker_id,feedback=None): return self.c.unblock_worker(worker_id, feedback) def bonus(self,asst,amount,feedback): return self.c.grant_bonus(asst.worker, asst.asst_id, Price(amount), feedback) # STATUS / DIAGNOSTICS # -------------------- def balance(self): return self.c.get_account_balance()[0]
return False# no bonus if already paid for return True except MTurkRequestError: print "Couldn't pay" return False def pay_bonus(worker_id, assign_id, bonus_price, reason): """pays for assignment; returns False if something went wrong, else True""" try: if bonus_price > 0: mtc.grant_bonus(worker_id, assign_id, Price(amount=bonus_price), reason) except MTurkRequestError: print "Couldn't give bonus" return False# no bonus if already paid for return True def pay_bonus_d(worker_id, assign_id, bonus_price, reason): mtc.grant_bonus(worker_id, assign_id, Price(amount=bonus_price), reason) ######## Script Starts ####### mtc = MTurkConnection(aws_access_key_id=ACCESS_ID, aws_secret_access_key=SECRET_KEY, host=HOST) if __name__ == '__main__': jerks = [] for name in jerks: mtc.block_worker(name, reason)