Skip to content

Commit d632ec8

Browse files
authored
Merge pull request #17 from joshishiv4/pr-879-archiveBot
bot: add archive bot
2 parents eec0ef2 + 632e00d commit d632ec8

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Note: this bot was written by Kimi K2.
3+
"""
4+
5+
import re
6+
7+
import waybackpy
8+
9+
10+
class ArchiveBotHandler:
11+
def usage(self) -> str:
12+
return """
13+
This bot listens for messages containing URLs, saves each URL to the
14+
Internet Archive, and replies with the fresh archive.org link(s).
15+
Mention the bot or send it a PM that contains a URL.
16+
"""
17+
18+
def handle_message(self, message: dict, bot_handler) -> None:
19+
content = message["content"]
20+
urls = re.findall(r"https?://[^\s]+", content)
21+
if not urls:
22+
return
23+
24+
replies = []
25+
for url in urls:
26+
try:
27+
archive_url = waybackpy.Url(url).save()
28+
replies.append(f"Archived: {archive_url}")
29+
except Exception as exc:
30+
replies.append(f"Failed to archive {url}{exc}")
31+
32+
if replies:
33+
bot_handler.send_reply(message, "\n".join(replies))
34+
35+
36+
handler_class = ArchiveBotHandler
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
waybackpy

0 commit comments

Comments
 (0)