-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarks_bot.py
103 lines (80 loc) · 2.6 KB
/
bookmarks_bot.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
import irc.bot
import irc.strings
import re
import requests
import json
import os
from irc.bot import ServerSpec
from dotenv import load_dotenv
load_dotenv()
class BookmarksBot(irc.bot.SingleServerIRCBot):
def __init__(self, nickname, server):
irc.bot.SingleServerIRCBot.__init__(self, [server], nickname, nickname)
def __url_in_message(self, e):
pattern = re.compile(r"(https://osu.ppy.sh/(?:b|beatmapsets|beatmaps)/\d+)")
res = pattern.search(e.arguments[0])
if res:
return res.group(1)
return None
def bookmarkSong(self, c, e):
api_baseurl = os.getenv("API_BASEURL")
url = self.__url_in_message(e)
if not url:
return None
nick = e.source.nick
mutation = """
mutation ($input: SongInput!){
addSong (input: $input) {
osuUserId,
bookmarksUrl,
beatmapset {
osuBeatmapsetId,
title,
artist,
}
}
}"""
mutation_input = {"userName": nick, "mapUrl": url}
query = {"query": mutation, "variables": {"input": mutation_input}}
print(query)
try:
resp = requests.post(api_baseurl + "/graphql", json=query)
except requests.exceptions.ConnectionError:
c.privmsg(nick, "Something went wrong :(")
return
print(resp.status_code)
print(resp.text)
if resp.status_code != 200:
c.privmsg(nick, "Something went wrong :(")
return
song = json.loads(resp.text)
try:
song = song["data"]["addSong"]
except KeyError:
c.privmsg(nick, "Something went wrong :(")
return
if not song:
return
bookmarks_url = api_baseurl + song["bookmarksUrl"]
beatmap = song["beatmapset"]
msg = "[https://osu.ppy.sh/beatmapsets/{} {} - {}] added to [{} your list]"
msg = msg.format(
beatmap["osuBeatmapsetId"],
beatmap["artist"],
beatmap["title"],
bookmarks_url,
)
c.privmsg(nick, msg)
def on_action(self, c, e):
self.bookmarkSong(c, e)
def on_privmsg(self, c, e):
self.bookmarkSong(c, e)
def main():
nickname = os.getenv("BOT_NICKNAME")
server = "irc.ppy.sh"
port = 6667
spec = ServerSpec(server, port, os.getenv("BOT_PASSWORD"))
bot = BookmarksBot(nickname, spec)
bot.start()
if __name__ == "__main__":
main()