-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bluebird.py
276 lines (211 loc) · 8.66 KB
/
bluebird.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import logging
from datetime import datetime
from os import environ
from random import randint
from sys import exit, stdout
from threading import Thread
from time import sleep
from typing import Self
import dotenv
from discord_webhook import DiscordEmbed, DiscordWebhook
from loguru import logger
from loguru_discord import DiscordSink
from handlers import Intercept
from services import X
class Bluebird:
"""
Monitor users on X and report new posts via Discord.
https://github.com/EthanC/Bluebird
"""
def Initialize(self: Self) -> None:
"""Initialize Bluebird and begin functionality."""
logger.info("Bluebird")
logger.info("https://github.com/EthanC/Bluebird")
if dotenv.load_dotenv():
logger.success("Loaded environment variables")
self.state: dict[str, int] = {}
# Reroute standard logging to Loguru
logging.basicConfig(handlers=[Intercept()], level=0, force=True)
if level := environ.get("LOG_LEVEL"):
logger.remove()
logger.add(stdout, level=level)
logger.success(f"Set console logging level to {level}")
if url := environ.get("LOG_DISCORD_WEBHOOK_URL"):
logger.add(
DiscordSink(url),
level=environ.get("LOG_DISCORD_WEBHOOK_LEVEL"),
backtrace=False,
)
logger.success("Enabled logging to Discord webhook")
logger.trace(url)
usersAll: list[str] = []
usersTop: list[str] = []
usersMedia: list[str] = []
if value := environ.get("USERS_ALL"):
usersAll = value.split(",")
if value := environ.get("USERS_TOP"):
usersTop = value.split(",")
if value := environ.get("USERS_MEDIA"):
usersMedia = value.split(",")
for username in usersAll:
watcher: Thread = Thread(target=Bluebird.WatchPosts, args=(self, username))
watcher.daemon = True
watcher.start()
for username in usersTop:
watcher: Thread = Thread(
target=Bluebird.WatchPosts,
args=(self, username),
kwargs={"replies": False},
)
watcher.daemon = True
watcher.start()
for username in usersMedia:
watcher: Thread = Thread(
target=Bluebird.WatchPosts,
args=(self, username),
kwargs={"media": True},
)
watcher.daemon = True
watcher.start()
# Keep the parent thread alive while the child threads run.
while True:
sleep(1)
def WatchPosts(
self: Self,
username: str,
replies: bool = True,
reposts: bool = True,
media: bool = False,
) -> None:
"""
Begin a loop for a single user that fires a notification upon
new post detection.
"""
while True:
# Watch for posts after the current moment if we haven't
# yet seen a post for this user.
if not self.state.get(username):
self.state[username] = int(datetime.now().timestamp())
logger.debug(f"[@{username}] {self.state}")
# Randomize cooldown to mimic natural behavior.
cooldownMin: int = int(environ.get("COOLDOWN_MIN_TIME", 60))
cooldownMax: int = int(environ.get("COOLDOWN_MAX_TIME", 300))
cooldown: int = randint(cooldownMin, cooldownMax)
logger.info(
f"[@{username}] Waiting {cooldown:,}s before checking for new posts"
)
sleep(cooldown)
posts: list[dict[str, int | str]] = X.GetUserPosts(
username,
includeReplies=replies,
includeReposts=reposts,
onlyMedia=media,
)
# We didn't get any posts. Try again.
if len(posts) <= 0:
continue
latest: dict[str, int | str] = posts[-1]
for post in posts:
if self.state[username] >= post["timestamp"]:
logger.debug(
f"[@{username}] Skipped post {post["postId"]} due to timestamp ({self.state[username]} >= {post["timestamp"]})"
)
logger.debug(f"https://x.com/{username}/status/{post["postId"]}")
continue
logger.success(
f"[@{username}] Detected new post {post["postId"]} ({post["timestamp"]})"
)
logger.debug(f"https://x.com/{username}/status/{post["postId"]}")
details: dict = X.GetPost(username, post["postId"])
if details:
embeds: list[DiscordEmbed] = Bluebird.BuildEmbed(username, details)
if quote := details.get("quote"):
embeds.extend(
Bluebird.BuildEmbed(username, quote, isQuote=True)
)
Bluebird.Notify(embeds)
self.state[username] = latest["timestamp"]
logger.info(
f"[@{username}] Watching for new posts after {latest["postId"]} ({latest["timestamp"]})"
)
logger.debug(f"https://x.com/{username}/status/{latest["postId"]}")
logger.debug(f"[@{username}] {self.state}")
def BuildEmbed(
username: str,
post: dict,
isReply: bool = False,
isQuote: bool = False,
isRepost: bool = False,
) -> list[DiscordEmbed]:
"""Build a Discord embed object for the provided X post."""
embeds: list[DiscordEmbed] = []
primary: DiscordEmbed = DiscordEmbed()
extras: list[DiscordEmbed] = []
postUrl: str = (
f"https://x.com/{post["author"]["screen_name"]}/status/{post["id"]}"
)
if (isReply) or (post["replying_to"]):
primary.set_title("Reply on X")
elif isQuote:
primary.set_title("Quote on X")
elif (isRepost) or (post["text"] and post["text"].startswith("RT @")):
primary.set_title("Repost on X")
else:
primary.set_title("Post on X")
primary.set_color("1D9BF0")
primary.set_author(
f"{post["author"]["name"]} (@{post["author"]["screen_name"]})",
url=f"https://x.com/{post["author"]["screen_name"]}",
icon_url=post["author"]["avatar_url"],
)
primary.set_url(postUrl)
primary.set_footer(post["source"], icon_url="https://i.imgur.com/hZbC8my.png")
primary.set_timestamp(post["created_timestamp"])
if (post["text"]) and (len(post["text"]) > 0):
primary.set_description(f">>> {post["text"]}")
if media := post.get("media"):
idx: int = 0
assets: list[dict] = media.get("all", [])
# External media is not included in the all array.
if media.get("external"):
assets.append(media["external"])
for asset in assets:
extra: DiscordEmbed = DiscordEmbed()
extra.set_url(postUrl)
match asset["type"]:
case "photo":
if idx == 0:
primary.set_image(asset["url"])
else:
extra.set_image(asset["url"])
case "gif":
if idx == 0:
primary.set_image(asset["thumbnail_url"])
else:
extra.set_image(asset["thumbnail_url"])
case "video":
if idx == 0:
primary.set_image(asset["thumbnail_url"])
else:
extra.set_image(asset["thumbnail_url"])
case _:
logger.warning(
f"[@{username}] Unknown media asset type {asset["type"]} for post {post["id"]}"
)
logger.debug(postUrl)
if idx > 0:
extras.append(extra)
idx += 1
embeds.append(primary)
embeds.extend(extras)
return embeds
def Notify(embeds: list[DiscordEmbed]) -> None:
"""Send a Discord Embed object for the specified X post."""
if not (webhook := environ.get("DISCORD_WEBHOOK_URL")):
return
DiscordWebhook(url=webhook, embeds=embeds, rate_limit_retry=True).execute()
if __name__ == "__main__":
try:
Bluebird.Initialize(Bluebird)
except KeyboardInterrupt:
exit()