-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* created the batch job object * added the client * added unit tests * renamed file * added batch job functions * added batch job functions * added the unit tests for the job methods * added examples * updated the readme * updated comments
- Loading branch information
1 parent
1eeea95
commit 3d5666b
Showing
9 changed files
with
1,045 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
twitter "github.com/g8rswimmer/go-twitter/v2" | ||
) | ||
|
||
type authorize struct { | ||
Token string | ||
} | ||
|
||
func (a authorize) Add(req *http.Request) { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token)) | ||
} | ||
|
||
/** | ||
In order to run, the user will need to provide the bearer token and the list of tweet ids. | ||
**/ | ||
func main() { | ||
token := flag.String("token", "", "twitter API token") | ||
jobType := flag.String("type", "", "job type") | ||
upload := flag.String("upload", "", "upload file") | ||
flag.Parse() | ||
|
||
client := &twitter.Client{ | ||
Authorizer: authorize{ | ||
Token: *token, | ||
}, | ||
Client: http.DefaultClient, | ||
Host: "https://api.twitter.com", | ||
} | ||
|
||
fmt.Println("Compliance Batch Job Example") | ||
|
||
opts := twitter.CreateComplianceBatchJobOpts{ | ||
Name: "go twitter job example", | ||
} | ||
|
||
// 1. Create a compliance batch job | ||
fmt.Println("1. Create a compliance batch job") | ||
complianceResponse, err := client.CreateComplianceBatchJob(context.Background(), twitter.ComplianceBatchJobType(*jobType), opts) | ||
if err != nil { | ||
log.Panicf("create compliance job error: %v", err) | ||
} | ||
|
||
enc, err := json.MarshalIndent(complianceResponse, "", " ") | ||
if err != nil { | ||
log.Panicf("create compliance job error: %v", err) | ||
} | ||
fmt.Println(string(enc)) | ||
|
||
job := complianceResponse.Raw.Job | ||
|
||
// 2. Upload ids from file | ||
fmt.Println("2. Upload ids from file") | ||
f, err := os.Open(*upload) | ||
if err != nil { | ||
log.Panicf("open upload file error: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
err = job.Upload(context.Background(), f) | ||
if err != nil { | ||
log.Panicf("upload ids error: %v", err) | ||
} | ||
|
||
// 3. Check the job status | ||
fmt.Println("3. Check the job status") | ||
for { | ||
time.Sleep(time.Second) | ||
|
||
resp, err := client.ComplianceBatchJob(context.Background(), job.ID) | ||
if err != nil { | ||
log.Panicf("check status error: %v", err) | ||
} | ||
jobStatus := resp.Raw.Job | ||
fmt.Println("Status: " + jobStatus.Status) | ||
if jobStatus.Status != twitter.ComplianceBatchJobStatusInProgress { | ||
break | ||
} | ||
} | ||
|
||
// 4. Download results | ||
fmt.Println("4. Download results") | ||
downloadResponse, err := job.Download(context.Background()) | ||
if err != nil { | ||
log.Panicf("download results error: %v", err) | ||
} | ||
|
||
enc, err = json.MarshalIndent(downloadResponse, "", " ") | ||
if err != nil { | ||
log.Panicf("download results error: %v", err) | ||
} | ||
fmt.Println(string(enc)) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.