From ea62395b2f906221f36d372e051e418abd0f2aba Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 19 Oct 2013 20:15:23 -0400 Subject: [PATCH] More accurate get_timestamp() --- tasks/afc_undated.py | 45 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/tasks/afc_undated.py b/tasks/afc_undated.py index 7b9115f..dfcc838 100644 --- a/tasks/afc_undated.py +++ b/tasks/afc_undated.py @@ -95,33 +95,24 @@ class AFCUndated(Task): is_talk = page.namespace in self.namespaces["talk"] if is_sub: aliases = self.aliases["submission"] - timestamps = {} + timestamp = self.get_timestamp(page) elif is_talk: aliases = self.aliases["talk"] timestamp, reviewer = self.get_talkdata(page) - if not timestamp: - return else: msg = u"[[{0}]] is undated, but in a namespace I don't know how to process" self.logger.warn(msg.format(page.title)) return + if not timestamp: + return code = mwparserfromhell.parse(page.get()) changes = 0 for template in code.filter_templates(): has_ts = template.has("ts", ignore_empty=True) - has_reviewer = template.has("reviewer", ignore_empty=True) if template.name.matches(aliases) and not has_ts: - if is_sub: - status = self.get_status(template) - if status in timestamps: - timestamp = timestamps[status] - else: - timestamp = self.get_timestamp(page, status) - timestamps[status] = timestamp - if not timestamp: - continue template.add("ts", timestamp) + has_reviewer = template.has("reviewer", ignore_empty=True) if is_talk and not has_reviewer: template.add("reviewer", reviewer) changes += 1 @@ -134,25 +125,19 @@ class AFCUndated(Task): msg = u"[[{0}]] is undated, but I can't figure out what to replace" self.logger.warn(msg.format(page.title)) - def get_status(self, template): - """Get the status code that corresponds to a given template.""" - valid = ["P", "R", "T", "D"] - if template.has(1): - status = template.get(1).value.strip().upper() - if status in valid: - return status - return "P" - - def get_timestamp(self, page, chart): + def get_timestamp(self, page): """Get the timestamp associated with a particular submission.""" - log = u"[[{0}]]: Getting timestamp for state {1}" - self.logger.debug(log.format(page.title, chart)) - search = self.statistics.search_history - user, ts, revid = search(page.pageid, chart, chart, []) - if not ts: - log = u"Couldn't find timestamp in [[{0}]] with state {1}" - self.logger.warn(log.format(page.title, chart)) + self.logger.debug(u"[[{0}]]: Getting timestamp".format(page.title)) + result = self.site.api_query( + action="query", prop="revisions", rvprop="timestamp", rvlimit=1, + rvdir="newer", titles=page.title) + data = result["query"]["pages"].values()[0] + if "revisions" not in data: + log = u"Couldn't get timestamp for [[{0}]]" + self.logger.warn(log.format(page.title)) return None + raw = data["revisions"][0]["timestamp"] + ts = datetime.strptime(raw, "%Y-%m-%dT%H:%M:%SZ") return ts.strftime("%Y%m%d%H%M%S") def get_talkdata(self, page):