From 5195ff8e122c2966fc7c65dd16edc50a57a79232 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 13 Oct 2020 22:28:52 +0200 Subject: [PATCH] Web: Add new @location command, to follow HTTP redirects. Useful to un-tinify URLs. --- plugins/Web/plugin.py | 15 +++++++++++++++ plugins/Web/test.py | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/plugins/Web/plugin.py b/plugins/Web/plugin.py index 201b2b619..762456846 100644 --- a/plugins/Web/plugin.py +++ b/plugins/Web/plugin.py @@ -275,6 +275,21 @@ class Web(callbacks.PluginRegexp): finally: fd.close() + @wrap(['httpUrl']) + @catch_web_errors + @fetch_sandbox + def location(self, irc, msg, args, url): + """ + + If the is redirected to another page, returns the URL of that + page. This works even if there are multiple redirects. + Only HTTP urls are valid. + Useful to "un-tinify" URLs.""" + timeout = self.registryValue('timeout') + (target, text) = utils.web.getUrlTargetAndContent(url, size=60, + timeout=timeout) + irc.reply(target) + _doctypeRe = re.compile(r'(]+>)', re.M) @wrap(['httpUrl']) @catch_web_errors diff --git a/plugins/Web/test.py b/plugins/Web/test.py index b8c74d77e..297d764c9 100644 --- a/plugins/Web/test.py +++ b/plugins/Web/test.py @@ -37,6 +37,13 @@ class WebTestCase(ChannelPluginTestCase): self.assertError('headers ftp://ftp.cdrom.com/pub/linux') self.assertNotError('headers http://www.slashdot.org/') + def testLocation(self): + self.assertError('location ftp://ftp.cdrom.com/pub/linux') + self.assertResponse( + 'location http://limnoria.net/', 'https://limnoria.net/') + self.assertResponse( + 'location https://www.limnoria.net/', 'https://limnoria.net/') + def testDoctype(self): self.assertError('doctype ftp://ftp.cdrom.com/pub/linux') self.assertNotError('doctype http://www.slashdot.org/')