Skip to content

Commit ceb77b8

Browse files
committed
Clean up some code
1 parent 2cbfbf4 commit ceb77b8

2 files changed

Lines changed: 42 additions & 44 deletions

File tree

cloudbot/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import warnings
32

43
# check python version
54
if sys.version_info < (3, 4, 0):

plugins/twitch.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,19 @@
11
import re
22
import html
3-
3+
44
from cloudbot import hook
55
from cloudbot.util import http
6-
6+
7+
78
twitch_re = re.compile(r'(.*:)//(twitch.tv|www.twitch.tv)(:[0-9]+)?(.*)', re.I)
89
multitwitch_re = re.compile(r'(.*:)//(www.multitwitch.tv|multitwitch.tv)/(.*)', re.I)
9-
10-
11-
def test(s):
10+
11+
12+
def test_name(s):
1213
valid = set('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/')
1314
return set(s) <= valid
14-
1515

16-
17-
@hook.regex(multitwitch_re)
18-
def multitwitch_url(match):
19-
usernames = match.group(3).split("/")
20-
out = ""
21-
for i in usernames:
22-
if not test(i):
23-
print("Not a valid username")
24-
return None
25-
if out == "":
26-
out = twitch_lookup(i)
27-
else:
28-
out = out + " \x02|\x02 " + twitch_lookup(i)
29-
return out
30-
31-
32-
@hook.regex(twitch_re)
33-
def twitch_url(match):
34-
bit = match.group(4).split("#")[0]
35-
location = "/".join(bit.split("/")[1:])
36-
if not test(location):
37-
print("Not a valid username")
38-
return None
39-
return twitch_lookup(location)
40-
41-
42-
@hook.command('twitchviewers')
43-
@hook.command()
44-
def twviewers(text):
45-
text = text.split("/")[-1]
46-
if test(text):
47-
location = text
48-
else:
49-
return "Not a valid channel name."
50-
return twitch_lookup(location).split("(")[-1].split(")")[0].replace("Online now! ", "")
51-
52-
16+
5317
def twitch_lookup(location):
5418
locsplit = location.split("/")
5519
if len(locsplit) > 1 and len(locsplit) == 3:
@@ -93,3 +57,38 @@ def twitch_lookup(location):
9357
playing = data['game']
9458
viewers = "\x034\x02Offline\x02\x0f"
9559
return html.unescape(fmt.format(title, channel, playing, viewers))
60+
61+
62+
@hook.regex(multitwitch_re)
63+
def multitwitch_url(match):
64+
usernames = match.group(3).split("/")
65+
out = ""
66+
for i in usernames:
67+
if not test_name(i):
68+
print("Not a valid username")
69+
return None
70+
if out == "":
71+
out = twitch_lookup(i)
72+
else:
73+
out = out + " \x02|\x02 " + twitch_lookup(i)
74+
return out
75+
76+
77+
@hook.regex(twitch_re)
78+
def twitch_url(match):
79+
bit = match.group(4).split("#")[0]
80+
location = "/".join(bit.split("/")[1:])
81+
if not test_name(location):
82+
print("Not a valid username")
83+
return None
84+
return twitch_lookup(location)
85+
86+
87+
@hook.command('twitch', 'twviewers')
88+
def twitch(text):
89+
text = text.split("/")[-1]
90+
if test_name(text):
91+
location = text
92+
else:
93+
return "Not a valid channel name."
94+
return twitch_lookup(location).split("(")[-1].split(")")[0].replace("Online now! ", "")

0 commit comments

Comments
 (0)