This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengagement_api.py
114 lines (107 loc) · 2.74 KB
/
engagement_api.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
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
105
106
107
108
109
110
111
112
113
114
import json
from tweepy.binder import bind_api
from tweepy.parsers import JSONParser
class EngagementAPI:
def __init__(
self,
auth=None,
host='data-api.twitter.com',
api_root='/insights/engagement',
cache=None,
compression=True,
parser=None,
retry_count=0,
retry_delay=0,
retry_errors=None,
timeout=60,
wait_on_rate_limit=False,
wait_on_rate_limit_notify=False,
proxy='',
):
self.auth = auth
self.host = host
self.api_root = api_root
self.cache = cache
self.compression = compression
self.retry_count = retry_count
self.retry_delay = retry_delay
self.retry_errors = retry_errors
self.timeout = timeout
self.wait_on_rate_limit = wait_on_rate_limit
self.wait_on_rate_limit_notify = wait_on_rate_limit_notify
self.parser = parser or JSONParser()
self.proxy = {}
if proxy:
self.proxy['https'] = proxy
def _request(
self,
path,
*,
tweet_ids,
engagement_types,
groupings,
start=None,
end=None
):
headers = {"Content-Type": "application/json", "Content-type": "application/json"}
payload = {
'tweet_ids': tweet_ids,
'engagement_types': engagement_types,
'groupings': groupings,
}
#if start:
# payload['start'] = start.isoformat()
#if end:
# payload['end'] = end.isoformat()
print(path)
print(payload)
return bind_api(
api=self,
method='POST',
path=path,
payload_type='json',
require_auth=True,
)(post_data=json.dumps(payload), headers=headers)
def historical(
self,
*,
tweet_ids,
engagement_types,
groupings,
start=None,
end=None
):
return self._request(
'/historical',
tweet_ids=tweet_ids,
engagement_types=engagement_types,
groupings=groupings,
start=start,
end=end,
)
def twentyeighthour(
self,
*,
tweet_ids,
engagement_types,
groupings
):
return self._request(
'/28hr',
tweet_ids=tweet_ids,
engagement_types=engagement_types,
groupings=groupings,
)
def totals(
self,
*,
tweet_ids,
engagement_types,
groupings
):
return self._request(
'/totals',
tweet_ids=tweet_ids,
engagement_types=engagement_types,
groupings=groupings,
)