Managing your MTurk Workers with Python (Part 2)

This blog has been preoccupied with Python, Boto, and the mysteries of MTurk's API. After applying my code on a large scale, the API has only become more unusual.

If you were to bonus workers as I suggested in my first blog post , you'd find that only the first ten workers in your HIT would ever receive your payment. For reasons that aren't entirely clear to me, many of the API's methods (and, by extension, Boto's methods) return values in pages. If you asked for all the assignments associated with a HIT, you'd most likely receive only a subsection of the whole, the first of many pages. With a few lines of Python, we can get around this nuisance.

I conducted a study that involved paying a bonus to 120 participants. This required me to get all 120 assignment objects from the HIT, then pay $1 to the worker corresponding to each assignment.

  from boto.mturk.connection import MTurkConnection
  from boto.mturk.price import Price

  ACCESS_ID = your access id
  SECRET_KEY = your secret key
  HOST = 'mechanicalturk.amazonaws.com'

  mturk = MTurkConnection(aws_access_key_id=ACCESS_ID,
              aws_secret_access_key=SECRET_KEY,
              host=HOST)


  def get_all_assignments(hit):
    assignments = []

    page = 1

    while len(mturk.get_assignments(hit, page_number=str(page)))>0:

      assignments.extend(mturk.get_assignments(hit,
                           page_number=
                           str(page)))
      page += 1

    return assignments


  assignments = get_all_assignments("3JU8CV4BRLJ5U2Z???????????????")

  payment = Price(1.00)
  message = ("[message that accompanies your bonus payment]")

  for a in assignments:
    mturk.grant_bonus(a.WorkerId,a.AssignmentId,payment,message)

Boto's get_assignments method takes a page number as an argument, returns the assignments corresponding to this page, and returns zero assignments when you exceed the number of pages that are available. Given this, the get_all_assignments function simply loops through each page until the get_assignments method runs out of new assignments to give us. This is a quick way to make the get_assignments method do what you would have expected it to do in the first place: get all of the assignments.

This was the only part of the bonus giving process that gave me trouble.