From 729aa04cc1f6eeea1c17af66e237702ab899dad4 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 4 Jul 2012 01:11:18 -0400 Subject: [PATCH] Site.url; some refactoring and cleanup --- docs/toolset.rst | 2 ++ earwigbot/commands/_old.py | 18 ++++++++++++------ earwigbot/commands/afc_submissions.py | 3 ++- earwigbot/wiki/page.py | 2 +- earwigbot/wiki/site.py | 21 +++++++++++++-------- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/toolset.rst b/docs/toolset.rst index 812b0c8..75f4a2f 100644 --- a/docs/toolset.rst +++ b/docs/toolset.rst @@ -80,6 +80,8 @@ following attributes: ``"en"`` - :py:attr:`~earwigbot.wiki.site.Site.domain`: the site's web domain, like ``"en.wikipedia.org"`` +- :py:attr:`~earwigbot.wiki.site.Site.url`: the site's full base URL, like + ``"https://en.wikipedia.org"`` and the following methods: diff --git a/earwigbot/commands/_old.py b/earwigbot/commands/_old.py index e22d681..3767fa9 100644 --- a/earwigbot/commands/_old.py +++ b/earwigbot/commands/_old.py @@ -9,18 +9,16 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): authy = auth(host) + + if command == "access": a = 'The bot\'s owner is "%s".' % OWNER b = 'The bot\'s admins are "%s".' % ', '.join(ADMINS_R) reply(a, chan, nick) reply(b, chan, nick) return - if command == "tock": - u = urllib.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') - info = u.info() - u.close() - say('"' + info['Date'] + '" - tycho.usno.navy.mil', chan) - return + + if command == "dict" or command == "dictionary": def trim(thing): if thing.endswith(' '): @@ -58,6 +56,8 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): reply('Sorry, no definition found.', chan, nick) else: say(result, chan) return + + if command == "ety" or command == "etymology": etyuri = 'http://etymonline.com/?term=%s' etysearch = 'http://etymonline.com/?search=%s' @@ -126,6 +126,8 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri) reply(msg, chan, nick) return + + if command == "sub" or command == "submissions": try: number = int(line2[4]) @@ -162,6 +164,8 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): report = "\x02First %s pending AfC submissions:\x0F %s" % (number, s) say(report, chan) return + + if command == "trout": try: user = line2[4] @@ -182,6 +186,8 @@ def parse(command, line, line2, nick, chan, host, auth, notice, say, reply, s): else: reply("I refuse to hurt anything with \"Earwig\" in its name :P", chan, nick) return + + if command == "notes" or command == "note" or command == "about" or command == "data" or command == "database": try: action = line2[4] diff --git a/earwigbot/commands/afc_submissions.py b/earwigbot/commands/afc_submissions.py index 2a2f6a2..2c8ce9f 100644 --- a/earwigbot/commands/afc_submissions.py +++ b/earwigbot/commands/afc_submissions.py @@ -55,5 +55,6 @@ class AFCSubmissions(Command): site = self.bot.wiki.get_site() category = site.get_category("Pending AfC submissions") - pages = ", ".join(category.get_members(use_sql=True, limit=number)) + members = category.get_members(use_sql=True, limit=number) + pages = ", ".join([member.url for member in members]) self.reply(data, "{0} pending AfC subs: {1}".format(number, pages)) diff --git a/earwigbot/wiki/page.py b/earwigbot/wiki/page.py index 98f11dd..310edad 100644 --- a/earwigbot/wiki/page.py +++ b/earwigbot/wiki/page.py @@ -498,7 +498,7 @@ class Page(CopyrightMixin): else: slug = quote(self._title.replace(" ", "_"), safe="/:") path = self._site._article_path.replace("$1", slug) - return ''.join((self._site._base_url, path)) + return ''.join((self._site.url, path)) @property def namespace(self): diff --git a/earwigbot/wiki/site.py b/earwigbot/wiki/site.py index eb818c4..a47c839 100644 --- a/earwigbot/wiki/site.py +++ b/earwigbot/wiki/site.py @@ -69,6 +69,7 @@ class Site(object): - :py:attr:`project`: the site's project name, like ``"wikipedia"`` - :py:attr:`lang`: the site's language code, like ``"en"`` - :py:attr:`domain`: the site's web domain, like ``"en.wikipedia.org"`` + - :py:attr:`url`: the site's URL, like ``"https://en.wikipedia.org"`` *Public methods:* @@ -243,14 +244,7 @@ class Site(object): e = "Tried to do an API query, but no API URL is known." raise exceptions.SiteAPIError(e) - base_url = self._base_url - if base_url.startswith("//"): # Protocol-relative URLs from 1.18 - if self._use_https: - base_url = "https:" + base_url - else: - base_url = "http:" + base_url - url = ''.join((base_url, self._script_path, "/api.php")) - + url = ''.join((self.url, self._script_path, "/api.php")) params["format"] = "json" # This is the only format we understand if self._assert_edit: # If requested, ensure that we're logged in params["assert"] = self._assert_edit @@ -548,6 +542,17 @@ class Site(object): """The Site's web domain, like ``"en.wikipedia.org"``.""" return urlparse(self._base_url).netloc + @property + def url(self): + """The Site's full base URL, like ``"https://en.wikipedia.org"``.""" + url = self._base_url + if url.startswith("//"): # Protocol-relative URLs from 1.18 + if self._use_https: + url = "https:" + url + else: + url = "http:" + url + return url + def api_query(self, **kwargs): """Do an API query with `kwargs` as the parameters.