forked from g8rswimmer/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
104 lines (86 loc) · 2.34 KB
/
main.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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))
}