-
Notifications
You must be signed in to change notification settings - Fork 14
/
pull_results.py
executable file
·71 lines (57 loc) · 1.84 KB
/
pull_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import logging
from sys import exit
from zipfile import ZipFile
from StringIO import StringIO
import click
import requests
from strephit.commons import secrets
from strephit.commons.logging import log_request_data
logger = logging.getLogger(__name__)
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 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
@click.command()
@click.option('--job', '-j', default='latest')
def main(job):
"""
Download the full CSV report of a CrowdFlower job.
By default, get the latest job.
"""
if job == 'latest':
latest = get_latest_job_id()
logger.info("Will fetch the report of the latest job ID: %s" % latest)
download_full_report(latest)
else:
download_full_report(job)
logger.info("Downloaded and decompressed full CSV report in the current directory")
return 0
if __name__ == '__main__':
exit(main())