-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastodon_streaming.py
72 lines (61 loc) · 2.12 KB
/
mastodon_streaming.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
import json
import os
from dotenv import load_dotenv
from kafka_writer import send_message_to_kafka
# Load environment variables from .env file
load_dotenv()
import logging
from mastodon import Mastodon, StreamListener
# Read environment variables
instance_url = os.environ.get('MASTODON_INSTANCE_URL', 'https://mastodon.social')
access_token = os.environ.get('MASTODON_ACCESS_TOKEN', 'token1')
tag = os.environ.get('HASHTAG')
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class MyStreamListener(StreamListener):
def on_update(self, status):
"""
Handle updates from the Mastodon stream.
Args:
status: Update status from the Mastodon stream.
"""
data = {
'text': status['content'],
'id': status['id'],
'created_at': status['created_at'].strftime("%Y-%m-%d, %H:%M:%S"),
'timestamp': status['created_at'].strftime("%Y-%m-%d, %H:%M:%S"),
'language': status['language'],
'sensitive': status['sensitive'],
'user_id': status['account']['id'],
'url': status.tags[0].url,
'application': status.application['name'],
}
json_data = json.dumps(data)
send_message_to_kafka(data)
def main_streaming():
"""
Main function to set up the Mastodon streaming.
"""
# Create a Mastodon instance
mastodon = Mastodon(
access_token=access_token,
api_base_url=instance_url
)
# Create a custom listener
listener = MyStreamListener()
# Start streaming hashtags
try:
stream_result = mastodon.stream_hashtag(
tag,
listener,
local=False,
run_async=False,
timeout=300,
reconnect_async=False,
reconnect_async_wait_sec=5
)
logging.info("Streaming started for instance %s: %s", instance_url, stream_result)
except Exception as e:
logging.error("Error while starting streaming for instance %s: %s", instance_url, e)
if __name__ == '__main__':
main_streaming()