Skip to content

Commit d794e5c

Browse files
authored
Merge pull request #46 from twitterdev/origin/batch-compliance
Added batch compliance code samples
2 parents caa3622 + 0d253ea commit d794e5c

19 files changed

Lines changed: 893 additions & 0 deletions

Batch-Compliance/java/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Batch Compliance sample code
2+
3+
This folder contains scripts to connect to the [Batch compliance endpoints](https://developer.twitter.com/en/docs/twitter-api/compliance/batch-compliance/introduction) in Python. Make sure you have your BEARER_TOKEN set up as an environment variable. Also, make sure you have the appropriate dependencies installed by running `mvn install` in the folder that you have the pom.xml in. Then, run the scripts in the following order:
4+
5+
## 1. Create compliance job
6+
7+
First, run the `CreateJob` file. In this file, make sure to specify your `type` as `tweets` or `users`, based on whether you will uploading a dataset with Tweet IDs or User IDs. This will give you a response like:
8+
9+
```json
10+
{
11+
"data": {
12+
"resumable": false,
13+
"upload_url": "https://storage.googleapis.com/...",
14+
"download_expires_at": "2021-08-12T15:24:31.000Z",
15+
"download_url": "https://storage.googleapis.com/...",
16+
"status": "created",
17+
"name": "my_job",
18+
"upload_expires_at": "2021-08-05T15:39:31.000Z",
19+
"id": "XXXXX",
20+
"created_at": "2021-08-05T15:24:31.000Z",
21+
"type": "tweets"
22+
}
23+
}
24+
```
25+
26+
Note the `id` from here as well as well as the upload_url, as this is the URL you will upload the file with Tweet IDs or User IDs to.
27+
28+
## 2. Upload the Tweet IDs or User IDs to check for compliance
29+
30+
Next, you will upload the file with Tweet IDs or User IDs to check for compliance. To do this, you will run the `UploadDataset` file. Make sure to replace the `uploadUrl` with your `upload_url` from the previous step and make sure to specify the path to your text file that contains the Tweet IDs or User IDs, one ID per line.
31+
32+
## 3. Check the status of your compliance job
33+
34+
Now that you have uploaded your dataset to check for compliance, your status will be in_progress. You can download the result of your compliance job, once the `status` is complete. To check for the status of your job, there are two options:
35+
36+
### Get all jobs
37+
38+
In order to get all your jobs, you can run `GetJobs`. Make sure to specify the appropriate job_type in this file i.e. tweets or users
39+
40+
### Get job by job ID
41+
42+
You can also get the job information for a job using the `id` from obtained in step 1. Run `GetJobById` and make sure to replace the jobId with your job id obtained in step 1. This will give you your job status and the `download_url`.
43+
44+
```json
45+
{
46+
"data": {
47+
"resumable": false,
48+
"upload_url": "https://storage.googleapis.com/...",
49+
"download_expires_at": "2021-08-12T02:34:13.000Z",
50+
"download_url": "https://storage.googleapis.com/...",
51+
"status": "expired",
52+
"upload_expires_at": "2021-08-05T02:49:13.000Z",
53+
"id": "XXXXX",
54+
"created_at": "2021-08-05T02:34:13.000Z",
55+
"type": "tweets"
56+
}
57+
}
58+
```
59+
60+
Once your `status` changes from `in_progress` to `complete`, note the `download_url` and go to the next step to download your results
61+
62+
## 4. Download your results
63+
64+
In order to download your results, you will need the `download_url` from the previous step (once the job status is complete). Run the `DownloadResult` file and make sure to replace the `downloadUrl` value with your appropriate value obtained from the previous step.

Batch-Compliance/java/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>batch-compliance</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.apache.httpcomponents</groupId>
13+
<artifactId>httpcore</artifactId>
14+
<version>4.4.11</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.apache.httpcomponents</groupId>
18+
<artifactId>httpclient</artifactId>
19+
<version>4.5.9</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.apache.httpcomponents</groupId>
23+
<artifactId>httpmime</artifactId>
24+
<version>4.5.2</version>
25+
</dependency>
26+
</dependencies>
27+
28+
<properties>
29+
<maven.compiler.source>8</maven.compiler.source>
30+
<maven.compiler.target>8</maven.compiler.target>
31+
</properties>
32+
33+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.IOException;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.client.HttpClient;
6+
import org.apache.http.client.config.CookieSpecs;
7+
import org.apache.http.client.config.RequestConfig;
8+
import org.apache.http.client.methods.HttpPost;
9+
import org.apache.http.entity.ByteArrayEntity;
10+
import org.apache.http.impl.client.HttpClients;
11+
import org.apache.http.util.EntityUtils;
12+
13+
/*
14+
* Sample code to create a batch compliance job
15+
* */
16+
public class CreateJob {
17+
18+
// To set your enviornment variables in your terminal run the following line:
19+
// export 'BEARER_TOKEN'='<your_bearer_token>'
20+
21+
public static void main(String args[]) throws IOException {
22+
final String bearerToken = System.getenv("BEARER_TOKEN");
23+
if (null != bearerToken) {
24+
//Specify if you want to create a job for tweets or users
25+
String response = createJob("tweets", bearerToken);
26+
System.out.println(response);
27+
} else {
28+
System.out.println("There was a problem getting you bearer token. Please make sure you set the BEARER_TOKEN environment variable");
29+
}
30+
}
31+
32+
/*
33+
* This method calls the batch compliance endpoint to create a new job
34+
* */
35+
private static String createJob(String type, String bearerToken) throws IOException {
36+
String jobResponse = null;
37+
38+
HttpClient httpClient = HttpClients.custom()
39+
.setDefaultRequestConfig(RequestConfig.custom()
40+
.setCookieSpec(CookieSpecs.STANDARD).build())
41+
.build();
42+
43+
HttpPost httpPost = new HttpPost("https://api.twitter.com/2/compliance/jobs");
44+
httpPost.setHeader("Authorization", String.format("Bearer %s", bearerToken));
45+
httpPost.setHeader("Content-Type", "application/json");
46+
47+
String body = String.format("{\n" +
48+
" \"type\": \"%s\"\n" +
49+
"}", type);
50+
HttpEntity requestEntity = new ByteArrayEntity(body.getBytes("UTF-8"));
51+
httpPost.setEntity(requestEntity);
52+
53+
HttpResponse response = httpClient.execute(httpPost);
54+
HttpEntity entity = response.getEntity();
55+
if (null != entity) {
56+
jobResponse = EntityUtils.toString(entity, "UTF-8");
57+
}
58+
return jobResponse;
59+
}
60+
61+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.IOException;
2+
import java.net.URISyntaxException;
3+
4+
import org.apache.http.HttpEntity;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.client.HttpClient;
7+
import org.apache.http.client.config.CookieSpecs;
8+
import org.apache.http.client.config.RequestConfig;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.apache.http.client.utils.URIBuilder;
11+
import org.apache.http.impl.client.HttpClients;
12+
import org.apache.http.util.EntityUtils;
13+
14+
/*
15+
* Sample code to download batch compliance result
16+
* */
17+
public class DownloadResult {
18+
19+
public static void main(String args[]) throws IOException, URISyntaxException {
20+
// Replace with your job downloadUrl below
21+
String downloadUrl = "";
22+
String response = getResults(downloadUrl);
23+
System.out.println(response);
24+
}
25+
26+
/*
27+
* This method gets the results for a batch compliance job
28+
* */
29+
private static String getResults(String downloadUrl) throws IOException, URISyntaxException {
30+
String jobResponse = null;
31+
32+
HttpClient httpClient = HttpClients.custom()
33+
.setDefaultRequestConfig(RequestConfig.custom()
34+
.setCookieSpec(CookieSpecs.STANDARD).build())
35+
.build();
36+
37+
URIBuilder uriBuilder = new URIBuilder(downloadUrl);
38+
39+
HttpGet httpGet = new HttpGet(uriBuilder.build());
40+
41+
HttpResponse response = httpClient.execute(httpGet);
42+
HttpEntity entity = response.getEntity();
43+
if (null != entity) {
44+
jobResponse = EntityUtils.toString(entity, "UTF-8");
45+
}
46+
return jobResponse;
47+
}
48+
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.io.IOException;
2+
import java.net.URISyntaxException;
3+
4+
import org.apache.http.HttpEntity;
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.client.HttpClient;
7+
import org.apache.http.client.config.CookieSpecs;
8+
import org.apache.http.client.config.RequestConfig;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.apache.http.client.utils.URIBuilder;
11+
import org.apache.http.impl.client.HttpClients;
12+
import org.apache.http.util.EntityUtils;
13+
14+
/*
15+
* Sample code to get batch compliance job by ID
16+
* */
17+
public class GetJobById {
18+
19+
// To set your enviornment variables in your terminal run the following line:
20+
// export 'BEARER_TOKEN'='<your_bearer_token>'
21+
22+
public static void main(String args[]) throws IOException, URISyntaxException {
23+
final String bearerToken = System.getenv("BEARER_TOKEN");
24+
if (null != bearerToken) {
25+
// Specify your job ID below
26+
String jobId = "";
27+
String response = getJobById(jobId, bearerToken);
28+
System.out.println(response);
29+
} else {
30+
System.out.println("There was a problem getting you bearer token. Please make sure you set the BEARER_TOKEN environment variable");
31+
}
32+
}
33+
34+
/*
35+
* This method calls the batch compliance endpoint to get job by id
36+
* */
37+
private static String getJobById(String id, String bearerToken) throws IOException, URISyntaxException {
38+
String jobResponse = null;
39+
40+
HttpClient httpClient = HttpClients.custom()
41+
.setDefaultRequestConfig(RequestConfig.custom()
42+
.setCookieSpec(CookieSpecs.STANDARD).build())
43+
.build();
44+
45+
URIBuilder uriBuilder = new URIBuilder("https://api.twitter.com/2/compliance/jobs/" + id);
46+
47+
HttpGet httpGet = new HttpGet(uriBuilder.build());
48+
httpGet.setHeader("Authorization", String.format("Bearer %s", bearerToken));
49+
httpGet.setHeader("Content-Type", "application/json");
50+
51+
HttpResponse response = httpClient.execute(httpGet);
52+
HttpEntity entity = response.getEntity();
53+
if (null != entity) {
54+
jobResponse = EntityUtils.toString(entity, "UTF-8");
55+
}
56+
return jobResponse;
57+
}
58+
59+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.IOException;
2+
import java.net.URISyntaxException;
3+
import java.util.ArrayList;
4+
5+
import org.apache.http.HttpEntity;
6+
import org.apache.http.HttpResponse;
7+
import org.apache.http.NameValuePair;
8+
import org.apache.http.client.HttpClient;
9+
import org.apache.http.client.config.CookieSpecs;
10+
import org.apache.http.client.config.RequestConfig;
11+
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.http.client.utils.URIBuilder;
13+
import org.apache.http.impl.client.HttpClients;
14+
import org.apache.http.message.BasicNameValuePair;
15+
import org.apache.http.util.EntityUtils;
16+
17+
/*
18+
* Sample code to get all batch compliance jobs
19+
* */
20+
public class GetJobs {
21+
22+
// To set your enviornment variables in your terminal run the following line:
23+
// export 'BEARER_TOKEN'='<your_bearer_token>'
24+
25+
public static void main(String args[]) throws IOException, URISyntaxException {
26+
final String bearerToken = System.getenv("BEARER_TOKEN");
27+
if (null != bearerToken) {
28+
// Specify if you want to jobs for tweets or users
29+
String response = getJobs("tweets", bearerToken);
30+
System.out.println(response);
31+
} else {
32+
System.out.println("There was a problem getting you bearer token. Please make sure you set the BEARER_TOKEN environment variable");
33+
}
34+
}
35+
36+
/*
37+
* This method calls the batch compliance endpoint to get all jobs
38+
* */
39+
private static String getJobs(String type, String bearerToken) throws IOException, URISyntaxException {
40+
String jobResponse = null;
41+
42+
HttpClient httpClient = HttpClients.custom()
43+
.setDefaultRequestConfig(RequestConfig.custom()
44+
.setCookieSpec(CookieSpecs.STANDARD).build())
45+
.build();
46+
47+
URIBuilder uriBuilder = new URIBuilder("https://api.twitter.com/2/compliance/jobs");
48+
ArrayList<NameValuePair> queryParameters;
49+
queryParameters = new ArrayList<>();
50+
queryParameters.add(new BasicNameValuePair("type", type));
51+
uriBuilder.addParameters(queryParameters);
52+
53+
HttpGet httpGet = new HttpGet(uriBuilder.build());
54+
httpGet.setHeader("Authorization", String.format("Bearer %s", bearerToken));
55+
httpGet.setHeader("Content-Type", "application/json");
56+
57+
HttpResponse response = httpClient.execute(httpGet);
58+
HttpEntity entity = response.getEntity();
59+
if (null != entity) {
60+
jobResponse = EntityUtils.toString(entity, "UTF-8");
61+
}
62+
return jobResponse;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)