Skip to content

Commit 0b38ad2

Browse files
committed
Clean up url parsing
1 parent 369f94b commit 0b38ad2

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

plugins/reddit.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@
88
from cloudbot import hook
99
from cloudbot.util import timeformat, formatting
1010

11-
reddit_re = re.compile(r'.*(//((www\.)?reddit\.com/r|redd\.it)[^ ]+)', re.I)
11+
reddit_re = re.compile(
12+
r"""
13+
https? # Scheme
14+
://
15+
16+
# Domain
17+
(?:
18+
redd\.it|
19+
(?:www\.)?reddit\.com/r
20+
)
21+
22+
(?:/(?:[A-Za-z0-9!$&-.:;=@_~\u00A0-\u10FFFD]|%[A-F0-9]{2})*)* # Path
23+
24+
(?:\?(?:[A-Za-z0-9!$&-;=@_~\u00A0-\u10FFFD]|%[A-F0-9]{2})*)? # Query
25+
""",
26+
re.IGNORECASE | re.VERBOSE
27+
)
1228

1329
base_url = "https://reddit.com/r/{}"
1430
short_url = "https://redd.it/{}"
@@ -51,11 +67,11 @@ def format_output(item, show_url=False):
5167

5268
@hook.regex(reddit_re, singlethread=True)
5369
def reddit_url(match, bot):
54-
url = match.group(1)
70+
url = match.group()
5571
url = URL(url).with_scheme("https")
5672

5773
if url.host.endswith("redd.it"):
58-
response = requests.get(url)
74+
response = requests.get(url, headers={'User-Agent': bot.user_agent})
5975
response.raise_for_status()
6076
url = URL(response.url).with_scheme("https")
6177

0 commit comments

Comments
 (0)