|
1 | 1 | import re |
2 | 2 | import html |
3 | | - |
| 3 | + |
4 | 4 | from cloudbot import hook |
5 | 5 | from cloudbot.util import http |
6 | | - |
| 6 | + |
| 7 | + |
7 | 8 | twitch_re = re.compile(r'(.*:)//(twitch.tv|www.twitch.tv)(:[0-9]+)?(.*)', re.I) |
8 | 9 | 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): |
12 | 13 | valid = set('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/') |
13 | 14 | return set(s) <= valid |
14 | | - |
15 | 15 |
|
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 | + |
53 | 17 | def twitch_lookup(location): |
54 | 18 | locsplit = location.split("/") |
55 | 19 | if len(locsplit) > 1 and len(locsplit) == 3: |
@@ -93,3 +57,38 @@ def twitch_lookup(location): |
93 | 57 | playing = data['game'] |
94 | 58 | viewers = "\x034\x02Offline\x02\x0f" |
95 | 59 | 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