예제 #1
0
def list_HITs(key, secret_key, sandboxed):
    """
    List all created HITs.

    :param key: AWS access key.
    :type key: ``str``

    :param secret_key: AWS secret key.
    :type key: ``str``

    :param sandboxed: Whether to list HITs from sandbox,
                      as opposed to production environment.
    :type  sandboxed: ``bool``

    :return:  List of HITs.
    :rtype:  ``list`` of ``HIT`` objects
    """
    Operation = 'SearchHITs'
    Service = 'AWSMechanicalTurkRequester'
    PageSize = 100
    Timestamp = \
        datetime.datetime.utcnow().strftime('%Y-%m-%dT%XZ')
    Signature = base.create_signature(secret_key=secret_key,
                                      service=Service,
                                      operation=Operation,
                                      timestamp=Timestamp)
    common_params = {
        'AWSAccessKeyId': key,
        'Version': '2014-08-15',
        'Service': Service,
        'Operation': Operation,
        'Signature': Signature,
        'Timestamp': Timestamp,
    }
    hit_params = {
        'PageSize': PageSize
    }
    hit_params.update(common_params)

    response = None

    if sandboxed:
        response = requests.get(
            'https://mechanicalturk.sandbox.amazonaws.com',
            params=hit_params
        )
    else:
        response = requests.get(
            'https://mechanicalturk.amazonaws.com',
            params=hit_params
        )

    parsed_response = xmltodict.parse(response.text)
    hit_results = parsed_response['SearchHITsResponse']['SearchHITsResult']
    hit_list = None

    if hit_results['Request']['IsValid'] == "True":
        hit_list = [HIT(hit) for hit in hit_results['HIT']]

    return hit_list
예제 #2
0
def create_external_HIT(key, secret_key, property_file,
                        question_file, sandboxed=False):
    """
    Creates an external HIT. If using sandboxed mode,
    make sure your url in your question file
    submits to the sandbox.

    :param config_file: Path to config file.
    :type config_file: ``str``

    :param property_file: Path to property file.
    :type property_file: ``str``

    :param question_file: Path to question file.
    :type question_file: ``str``

    :param sandboxed: Submit HIT to developer sandbox.
    :type sandboxed: ``bool``

    :return: ID of created HIT.
    :rtype: ``str``
    """

    # Set Common Paramaters
    operation = 'CreateHIT'
    service = 'AWSMechanicalTurkRequester'
    timestamp = \
        datetime.datetime.utcnow().strftime('%Y-%m-%dT%XZ')

    signature = base.create_signature(secret_key=secret_key,
                                      service=service,
                                      operation=operation,
                                      timestamp=timestamp)
    # Get external question XML
    with open(question_file, 'r') as f:
        ex_question = f.read()
    # Get HIT configs
    parse = ConfigParser()
    parse.read(property_file)
    common_params = {
        'AWSAccessKeyId': key,
        'Version': '2014-08-15',
        'Service': service,
        'Operation': operation,
        'Signature': signature,
        'Timestamp': timestamp,
    }
    hit_params = {
        'ResponseGroup.0': 'Minimal',
        'Title': parse.get('defaults', 'title'),
        'Description': parse.get('defaults', 'description'),
        'Question': ex_question,
        'Keywords': parse.get('defaults', 'keywords'),
        'Reward.1.Amount': parse.get('defaults', 'reward'),
        'Reward.1.CurrencyCode': 'USD',
        'MaxAssignments': parse.get('defaults', 'assignments'),
        'AssignmentDurationInSeconds': parse.get('defaults',
                                                 'assignmentduration'),
        'LifetimeInSeconds': parse.get('defaults', 'hitlifetime'),
        'AutoApprovalDelayInSeconds': parse.get('defaults',
                                                'autoapprovaldelay')
    }
    hit_params.update(common_params)

    response = None

    if sandboxed:
        response = requests.get(
            'https://mechanicalturk.sandbox.amazonaws.com',
            params=hit_params
        )
    else:
        response = requests.get(
            'https://mechanicalturk.amazonaws.com',
            params=hit_params
        )

    hit_id = None
    parsed_response = xmltodict.parse(response.text)
    hit_info = parsed_response['CreateHITResponse']['HIT']
    if hit_info['Request']['IsValid'] == 'True':
        hit_id = hit_info['HITId']

    return hit_id