def get_latest_job_id(): """ Get the ID of the most recent job. :return: the latest job ID :rtype: str """ r = requests.get(secrets.CF_JOBS_URL, params={"key": secrets.CF_KEY}) log_request_data(r, logger) r.raise_for_status() # The API call returns the 10 latest jobs # No way to set a parameter, so return the first element of the list return r.json()[0]["id"]
def get_latest_job_id(): """ Get the ID of the most recent job. :return: the latest job ID :rtype: str """ r = requests.get(secrets.CF_JOBS_URL, params={'key': secrets.CF_KEY}) log_request_data(r, logger) r.raise_for_status() # The API call returns the 10 latest jobs # No way to set a parameter, so return the first element of the list return r.json()[0]['id']
def tag_job(job_id, tags): """ Tag a given job. :param str job_id: job ID registered in CrowdFlower :param list tags: list of tags :return: True on success :rtype: boolean """ params = {'key': secrets.CF_KEY} data = {"tags": tags} r = requests.post(secrets.CF_JOB_TAG_URL % job_id, params=params, data=data) log_request_data(r, logger) r.raise_for_status() return r.ok
def download_full_report(job_id): """ Download the full CSV report of the given job. See https://success.crowdflower.com/hc/en-us/articles/202703075-Guide-to-Reports-Page-and-Settings-Page#full_report Raises any HTTP error that may occur. :param str job_id: job ID registered in CrowdFlower """ params = {"key": secrets.CF_KEY, "type": "full"} r = requests.get(secrets.CF_JOB_RESULTS_URL % job_id, params=params) log_request_data(r, logger) r.raise_for_status() zipped_report = ZipFile(StringIO(r.content)) zipped_report.extractall() return 0
def download_full_report(job_id): """ Download the full CSV report of the given job. See https://success.crowdflower.com/hc/en-us/articles/202703075-Guide-to-Reports-Page-and-Settings-Page#full_report Raises any HTTP error that may occur. :param str job_id: job ID registered in CrowdFlower """ params = {'key': secrets.CF_KEY, 'type': 'full'} r = requests.get(secrets.CF_JOB_RESULTS_URL % job_id, params=params) log_request_data(r, logger) r.raise_for_status() zipped_report = ZipFile(StringIO(r.content)) zipped_report.extractall() return 0
def upload_units(job_id, csv_data): """ Upload the job data units to the given job. Raises any HTTP error that may occur. :param str job_id: job ID registered in CrowdFlower :param file csv_data: file handle pointing to the data units CSV :return: the uploaded job response object, as per https://success.crowdflower.com/hc/en-us/articles/201856229-CrowdFlower-API-API-Responses-and-Messaging#job_response on success, or an error message :rtype: dict """ headers = {'Content-Type': 'text/csv'} params = {'key': secrets.CF_KEY} r = requests.put(secrets.CF_JOB_UPLOAD_URL % job_id, data=csv_data, headers=headers, params=params) log_request_data(r, logger) r.raise_for_status() return r.json()
def activate_gold(job_id): """ Activate gold units in the given job. Corresponds to the 'Convert Uploaded Test Questions' UI button. :param str job_id: job ID registered in CrowdFlower :return: True on success :rtype: boolean """ params = {'key': secrets.CF_KEY} r = requests.put(secrets.CF_JOB_ACTIVATE_GOLD_URL % job_id, params=params) log_request_data(r, logger) # Inconsistent API: returns 406, but actually sometimes works (!!!) if r.status_code == 406: return r.json() else: r.raise_for_status()
def convert_gold(job_id): """ Activate gold units in the given job. Corresponds to the 'Convert Uploaded Test Questions' UI button. :param str job_id: job ID registered in CrowdFlower :return: True on success :rtype: boolean """ params = {'key': secrets.CF_KEY} r = requests.put(secrets.CF_JOB_ACTIVATE_GOLD_URL % job_id, params=params) log_request_data(r, logger) # Inconsistent API: returns 406, but actually sometimes works (!!!) if r.status_code == 406: return r.json() else: r.raise_for_status()
def config_job(job_id): """ Setup a given CrowdFlower job with default settings. See :const: JOB_SETTINGS :param str job_id: job ID registered in CrowdFlower :return: the uploaded job response object, as per https://success.crowdflower.com/hc/en-us/articles/201856229-CrowdFlower-API-API-Responses-and-Messaging#job_response on success, or an error message :rtype: dict """ params = {'key': secrets.CF_KEY} # Manually prepare the body, due to multiple included countries # i.e., requests will ignore dicts with the same key headers = {'Content-Type': 'application/x-www-form-urlencoded'} data = '&'.join("job[included_countries][]=%s" % c for c in INCLUDED_COUNTRIES) + '&' + \ '&'.join('%s=%s' % param for param in JOB_SETTINGS.iteritems()) r = requests.put(secrets.CF_JOB_CONFIG_URL % job_id, headers=headers, params=params, data=data) log_request_data(r, logger) r.raise_for_status() return r.json()
def create_job(title, instructions, cml, custom_js): """ Create an empty CrowdFlower job with the specified title and instructions. Raise any HTTP error that may occur. :param str title: plain text title :param str instructions: instructions, can contain HTML :param str cml: worker interface CML template. See https://success.crowdflower.com/hc/en-us/articles/202817989-CML-CrowdFlower-Markup-Language-Overview :param str custom_js: JavaScript code to be injected into the job :return: the created job response object, as per https://success.crowdflower.com/hc/en-us/articles/201856229-CrowdFlower-API-API-Responses-and-Messaging#job_response on success, or an error message :rtype: dict """ data = { "key": secrets.CF_KEY, "job[title]": title, "job[instructions]": instructions, "job[cml]": cml, "job[js]": custom_js, } r = requests.post(secrets.CF_JOBS_URL, data=data) log_request_data(r, logger) r.raise_for_status() return r.json()