Ejemplo n.º 1
0
  /**
   * Orders the job given by its jobid. Pay is per assigment, which is by default 5 units.
   *
   * @param job as CrowdJob
   * @param channels : a vector of channels, in which the job should be made available
   * @param units : number of units to order
   * @param payPerAssigment : pay in (dollar) cents for each assignments
   * @return JsonNode that Crowdflower returns
   */
  JsonNode orderJob(CrowdJob job, Vector<String> channels, int units, int payPerAssigment) {
    Log LOG = LogFactory.getLog(getClass());

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    MultiValueMap<String, String> argumentMap = new LinkedMultiValueMap<String, String>();

    argumentMap.add(debitKey, String.valueOf(units));
    for (String channel : channels) {
      argumentMap.add(channelKey + "[]", channel);
    }

    updateVariable(job, jobPaymentKey, String.valueOf(payPerAssigment));

    LOG.info(
        "Order Job: #"
            + job.getId()
            + " with "
            + units
            + " judgments for "
            + payPerAssigment
            + " cents (dollar) per assigment.");
    JsonNode result =
        restTemplate.postForObject(orderJobURL, argumentMap, JsonNode.class, job.getId(), apiKey);

    return result;
  }
Ejemplo n.º 2
0
  /**
   * Upload the data vector as JSON to the specified Crowdflower job
   *
   * @param job
   * @param data - Generic vector of data, containing ordinary java classes. They should be
   *     annotated so that Jackson understands how to map them to JSON.
   */
  void uploadData(CrowdJob job, List<?> data) {
    Log LOG = LogFactory.getLog(getClass());

    // Crowdflower wants a Multi-line JSON, with each line having a new JSON object
    // Thus we have to map each (raw) object in data individually to a JSON string

    ObjectMapper mapper = new ObjectMapper();
    String jsonObjectCollection = "";

    StringBuilder jsonStringBuilder = new StringBuilder();
    int count = 0;
    for (Object obj : data) {
      count++;
      JsonNode jsonData = mapper.convertValue(obj, JsonNode.class);
      jsonStringBuilder.append(jsonData.toString());
      jsonStringBuilder.append("\n");
    }

    jsonObjectCollection = jsonStringBuilder.toString();

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> request = new HttpEntity<String>(jsonObjectCollection, headers);

    String result = "";

    if (job == null) {
      LOG.info("Upload new data and create new job: " + String.valueOf(count) + " data items");
      result = restTemplate.postForObject(uploadDataURL, request, String.class, apiKey);
    } else {
      LOG.info(
          "Uploading new data to job: "
              + job.getId()
              + ": "
              + String.valueOf(count)
              + " data items");
      result =
          restTemplate.postForObject(
              uploadDataWithJobURL, request, String.class, job.getId(), apiKey);
    }
    LOG.info("Upload response:" + result);

    // set gold? this is what i would like to do...
    // updateVariable(job, "https://api.crowdflower.com/v1/jobs/{jobid}/gold?key={apiKey}",
    // "set_standard_gold", "TRUE");
  }
Ejemplo n.º 3
0
  /**
   * Sets a single key in Crowdflowers job with a given value
   *
   * @param job
   * @param key
   * @param value
   */
  void updateVariable(CrowdJob job, String Url, String key, String value) {
    MultiValueMap<String, String> argumentMap = new LinkedMultiValueMap<String, String>();
    argumentMap.add(key, value);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    restTemplate.put(Url, argumentMap, job.getId(), apiKey);
  }
Ejemplo n.º 4
0
  /**
   * Create a new job on Crowdflower using supplied job class. If this class had been retrieved with
   * retrieveJob(String jobid), then you copied the job without its data. Can be used to copy jobs
   * across different accounts if API key is changed between the call to retrieveJob
   *
   * @param job - job class that should be replicated as new job on Crowdflower
   * @return job class which represents the new job
   * @throws HttpServerErrorException
   */
  CrowdJob createNewJob(CrowdJob job) throws HttpServerErrorException {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    JsonNode result =
        restTemplate.postForObject(newJobURL, job.getArgumentMap(), JsonNode.class, apiKey);

    return new CrowdJob(result);
  }
Ejemplo n.º 5
0
  /**
   * Retrieves raw judgments for a given job
   *
   * @param job
   * @return raw judgments as string
   * @throws IOException
   * @throws UnsupportedEncodingException
   */
  String retrieveRawJudgments(CrowdJob job) throws UnsupportedEncodingException, IOException {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    // Crowdflower sends back a zip file with a single JSON file, which make things a bit awkward
    // here:
    byte[] resultJsonZip =
        restTemplate.getForObject(judgmentsURL, byte[].class, job.getId(), apiKey);

    if (resultJsonZip != null && resultJsonZip.length > 0) {
      String resultJsonString = new String(unzip(resultJsonZip), "UTF-8");
      return resultJsonString;
    } else {
      return "";
    }
  }
Ejemplo n.º 6
0
  /**
   * Update the list of allowed countries for this job to Crowdflower (the list is set in the job)
   *
   * @param job
   * @throws HttpServerErrorException
   */
  void updateAllowedCountries(CrowdJob job) throws HttpServerErrorException {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new FormHttpMessageConverter());

    if (job.getExcludedCountries().size() > 0) {
      restTemplate.put(baseJobURL, job.getExcludedCountriesMap(), job.getId(), apiKey);
    }

    if (job.getIncludedCountries().size() > 0) {
      restTemplate.put(baseJobURL, job.getIncludedCountriesMap(), job.getId(), apiKey);
    }
  }