1+ import re
12from collections import defaultdict
23from datetime import datetime
34
1011from cloudbot .util .pager import paginated_list
1112
1213search_pages = defaultdict (dict )
14+ user_re = re .compile (r'^(?:/?u(?:ser)?/)?(?P<name>.+)$' , re .IGNORECASE )
15+ sub_re = re .compile (r'^(?:/?r/)?(?P<name>.+)$' , re .IGNORECASE )
1316
1417user_url = "http://reddit.com/user/{}/"
1518subreddit_url = "http://reddit.com/r/{}/"
1619# This agent should be unique for your cloudbot instance
1720agent = {"User-Agent" : "gonzobot a cloudbot (IRCbot) implementation for snoonet.org by /u/bloodygonzo" }
1821
1922
23+ def test_get_user ():
24+ assert get_sub ('test' ) == 'test'
25+ assert get_sub ('r/test' ) == 'test'
26+ assert get_sub ('/r/test' ) == 'test'
27+
28+
29+ def test_get_sub ():
30+ assert get_user ('test' ) == 'test'
31+ assert get_user ('/u/test' ) == 'test'
32+ assert get_user ('u/test' ) == 'test'
33+ assert get_user ('/user/test' ) == 'test'
34+ assert get_user ('user/test' ) == 'test'
35+
36+
37+ def get_user (text ):
38+ match = user_re .match (text )
39+ if match :
40+ return match .group ('name' )
41+
42+
43+ def get_sub (text ):
44+ match = sub_re .match (text )
45+ if match :
46+ return match .group ('name' )
47+
48+
2049def statuscheck (status , item ):
2150 """since we are doing this a lot might as well return something more meaningful"""
2251 if status == 404 :
@@ -60,7 +89,7 @@ def moremod(text, chan, conn):
6089@hook .command ("subs" , "moderates" , singlethread = True )
6190def moderates (text , chan , conn , reply ):
6291 """<username> - This plugin prints the list of subreddits a user moderates listed in a reddit users profile. Private subreddits will not be listed."""
63- user = text
92+ user = get_user ( text )
6493 r = requests .get (user_url .format (user ) + "moderated_subreddits.json" , headers = agent )
6594 try :
6695 r .raise_for_status ()
@@ -87,7 +116,7 @@ def moderates(text, chan, conn, reply):
87116@hook .command ("karma" , "ruser" , singlethread = True )
88117def karma (text , reply ):
89118 """<reddituser> - will return the information about the specified reddit username"""
90- user = text
119+ user = get_user ( text )
91120 url = user_url + "about.json"
92121 r = requests .get (url .format (user ), headers = agent )
93122 try :
@@ -133,7 +162,7 @@ def karma(text, reply):
133162@hook .command ("cakeday" , singlethread = True )
134163def cake_day (text , reply ):
135164 """<reddituser> - will return the cakeday for the given reddit username."""
136- user = text
165+ user = get_user ( text )
137166 url = user_url + "about.json"
138167 r = requests .get (url .format (user ), headers = agent )
139168
@@ -171,11 +200,7 @@ def time_format(numdays):
171200@hook .command ("submods" , "mods" , "rmods" , singlethread = True )
172201def submods (text , chan , conn , reply ):
173202 """<subreddit> - prints the moderators of the specified subreddit."""
174- sub = text
175- if sub .startswith ('/r/' ):
176- sub = sub [3 :]
177- elif sub .startswith ('r/' ):
178- sub = sub [2 :]
203+ sub = get_sub (text )
179204 url = subreddit_url + "about/moderators.json"
180205 r = requests .get (url .format (sub ), headers = agent )
181206
@@ -211,11 +236,7 @@ def submods(text, chan, conn, reply):
211236@hook .command ("subinfo" , "subreddit" , "sub" , "rinfo" , singlethread = True )
212237def subinfo (text , reply ):
213238 """<subreddit> - fetches information about the specified subreddit."""
214- sub = text
215- if sub .startswith ('/r/' ):
216- sub = sub [3 :]
217- elif sub .startswith ('r/' ):
218- sub = sub [2 :]
239+ sub = get_sub (text )
219240 url = subreddit_url + "about.json"
220241 r = requests .get (url .format (sub ), headers = agent )
221242
0 commit comments