From 3f1737717c0069f4398bb1c3db402e3ae4775a04 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 21 Jul 2012 17:07:38 -0400 Subject: [PATCH] More accurate handling when using get_site(lang, project). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some wikis (like frwiki), set their projects to localized forms of the expected (so "wikipedia" becomes "wikipédia"). An additional search by URL in _get_site_name_from_sitesdb() fixes this. --- earwigbot/wiki/sitesdb.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/earwigbot/wiki/sitesdb.py b/earwigbot/wiki/sitesdb.py index e2e2a8a..00d07b1 100644 --- a/earwigbot/wiki/sitesdb.py +++ b/earwigbot/wiki/sitesdb.py @@ -220,14 +220,27 @@ class SitesDB(object): def _get_site_name_from_sitesdb(self, project, lang): """Return the name of the first site with the given project and lang. + If we can't find the site with the given information, we'll also try + searching for a site whose base_url contains "{lang}.{project}". There + are a few sites, like the French Wikipedia, that set their project to + something other than the expected "wikipedia" ("wikipédia" in this + case), but we should correctly find them when doing get_site(lang="fr", + project="wikipedia"). + If the site is not found, return None. An empty sitesdb will be created if none exists. """ - query = "SELECT site_name FROM sites WHERE site_project = ? and site_lang = ?" + query1 = "SELECT site_name FROM sites WHERE site_project = ? and site_lang = ?" + query2 = "SELECT site_name FROM sites WHERE site_base_url LIKE ?" with sqlite.connect(self._sitesdb) as conn: try: - site = conn.execute(query, (project, lang)).fetchone() - return site[0] if site else None + site = conn.execute(query1, (project, lang)).fetchone() + if site: + return site[0] + else: + url = "%{0}.{1}%".format(lang, project) + site = conn.execute(query2, (url,)).fetchone() + return site[0] if site else None except sqlite.OperationalError: self._create_sitesdb()