This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_historical.py
executable file
·79 lines (55 loc) · 2.82 KB
/
twitter_historical.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
#!/usr/bin/env python
# Copyright 2020 @TwitterDev
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Modifications copyright (C) 2022 @morsapaes
import logging
import requests
import json
import os
from kafka import KafkaProducer
p = KafkaProducer(bootstrap_servers='redpanda:9092')
bearer_token= os.environ['BEARER_TOKEN']
search_url = "https://api.twitter.com/2/tweets/search/recent"
# Filter the stream to include Data Council content, excluding retweets
# (but including tweets, quote tweets and replies)
query_params = {'query': '(@DataCouncilAI OR "Data Council") -is:retweet',
'expansions': 'author_id,geo.place_id',
'tweet.fields': 'author_id,created_at,in_reply_to_user_id,geo,attachments,referenced_tweets',
'place.fields': 'name,place_type',
'user.fields': 'location',
'max_results': 100}
def bearer_oauth(r):
r.headers["Authorization"] = f"Bearer {bearer_token}"
r.headers["User-Agent"] = "v2RecentSearchPython"
return r
def connect_to_endpoint(url, params):
response = requests.get(url, auth=bearer_oauth, params=params)
print(response.status_code)
logging.info(response.status_code)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()
def main():
json_response = connect_to_endpoint(search_url, query_params)
for idx, response in enumerate(json_response['data']):
if response:
p.send(topic='dc_tweets', key=json.dumps(response['id']).encode('utf8'), value=json.dumps(response, ensure_ascii=False).encode('utf-8'))
for idx, response in enumerate(json_response['includes']['users']):
if response:
p.send(topic='dc_users', key=json.dumps(response['id']).encode('utf8'), value=json.dumps(response, ensure_ascii=False).encode('utf-8'))
if 'places' in json_response['includes']:
for idx, response in enumerate(json_response['includes']['places']):
p.send(topic='dc_places', key=json.dumps(response['id']).encode('utf8'), value=json.dumps(response, ensure_ascii=False).encode('utf-8'))
p.flush()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, filename="logfile", filemode="a+",
format="%(asctime)-15s %(levelname)-8s %(message)s")
main()