jenkins-bot has submitted this change and it was merged.
Change subject: Move watchlist functions to Site object from scripts/watchlist.py
......................................................................
Move watchlist functions to Site object from scripts/watchlist.py
It also fixes the bug in watchlist.py regarding caching.
"watchlist.py -all" failed if the cache entry wasn't a watchlistraw api
call.
Bug: T59995
Change-Id: Iba40f2eef6d161ff08500950ec4c097bac97f505
---
M pywikibot/site.py
M scripts/watchlist.py
2 files changed, 35 insertions(+), 45 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py
index c4d53ce..1b76668 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -5722,6 +5722,29 @@
data = req.submit()
return
data['flow']['view-topic']['result']['topic']
+ def watched_pages(self, sysop=False, force=False, step=None, total=None):
+ """
+ Return watchlist.
+
+ @param sysop: Returns watchlist of sysop user if true
+ @type sysop: bool
+ @param force_reload: Reload watchlist
+ @type force_reload: bool
+ @return: list of pages in watchlist
+ @rtype: list of pywikibot.Page objects
+ """
+ self.login(sysop=sysop)
+ if not total:
+ total = pywikibot.config.special_page_limit
+ if force:
+ gen = api.PageGenerator(site=self, generator='watchlistraw',
+ step=step, gwrlimit=total)
+ else:
+ gen = api.PageGenerator(
+ site=self, generator='watchlistraw', step=step,
+ expiry=pywikibot.config.API_config_expiry, gwrlimit=total)
+ return gen
+
# aliases for backwards compatibility
isBlocked = redirect_func(is_blocked, old_name='isBlocked',
class_name='APISite')
diff --git a/scripts/watchlist.py b/scripts/watchlist.py
index f3a0a76..6729e73 100755
--- a/scripts/watchlist.py
+++ b/scripts/watchlist.py
@@ -3,10 +3,7 @@
"""
Allows access to the bot account's watchlist.
-The function refresh() downloads the current watchlist and saves it to disk.
-It is run automatically when a bot first tries to save a page retrieved. The
-watchlist can be updated manually by running this script. The list will also
-be reloaded automatically once a month.
+The watchlist can be updated manually by running this script.
Syntax: python watchlist [-all | -new]
@@ -18,7 +15,7 @@
"""
#
# (C) Daniel Herding, 2005
-# (C) Pywikibot team, 2005-2014
+# (C) Pywikibot team, 2005-2015
#
# Distributed under the terms of the MIT license.
#
@@ -33,20 +30,12 @@
from pywikibot.data.api import CachedRequest
from scripts.maintenance.cache import CacheEntry
-cache = {}
-
def get(site=None):
"""Load the watchlist, fetching it if necessary."""
if site is None:
site = pywikibot.Site()
- if site in cache:
- # Use cached copy if it exists.
- watchlist = cache[site]
- else:
- # create cached copy
- watchlist = refresh(site)
- cache[site] = watchlist
+ watchlist = [p.title() for p in site.watched_pages()]
return watchlist
@@ -58,30 +47,8 @@
def refresh(site, sysop=False):
"""Fetch the watchlist."""
- if not site.logged_in(sysop=sysop):
- site.login(sysop=sysop)
-
- params = {
- 'action': 'query',
- 'list': 'watchlistraw',
- 'wrlimit': config.special_page_limit,
- }
-
pywikibot.output(u'Retrieving watchlist for %s via API.' % str(site))
- # pywikibot.put_throttle() # It actually is a get, but a heavy one.
- watchlist = []
- while True:
- req = CachedRequest(config.API_config_expiry, site=site, **params)
- data = req.submit()
- if 'error' in data:
- raise RuntimeError('ERROR: %s' % data)
- watchlist.extend([w['title'] for w in data['watchlistraw']])
-
- if 'query-continue' in data:
- params.update(data['query-continue']['watchlistraw'])
- else:
- break
- return watchlist
+ return list(site.watched_pages(sysop=sysop, force=True))
def refresh_all(sysop=False):
@@ -95,7 +62,7 @@
entry.parse_key()
entry._rebuild()
if entry.site not in seen:
- if entry._data['watchlistraw']:
+ if entry._data.get('watchlistraw'):
refresh(entry.site, sysop)
seen.append(entry.site)
@@ -106,10 +73,12 @@
'Downloading all watchlists for your accounts in user-config.py')
for family in config.usernames:
for lang in config.usernames[family]:
- refresh(pywikibot.Site(lang, family), sysop=sysop)
+ site = pywikibot.Site(lang, family)
+ refresh(site, sysop=sysop)
for family in config.sysopnames:
for lang in config.sysopnames[family]:
- refresh(pywikibot.Site(lang, family), sysop=sysop)
+ site = pywikibot.Site(lang, family)
+ refresh(site, sysop=sysop)
def main(*args):
@@ -137,12 +106,10 @@
refresh_new(sysop=sysop)
else:
site = pywikibot.Site()
- refresh(site, sysop=sysop)
-
- watchlist = get(site)
+ watchlist = refresh(site, sysop=sysop)
pywikibot.output(u'%i pages in the watchlist.' % len(watchlist))
- for pageName in watchlist:
- pywikibot.output(pageName, toStdout=True)
+ for page in watchlist:
+ pywikibot.output(page.title(), toStdout=True)
if __name__ == "__main__":
main()
--
To view, visit
https://gerrit.wikimedia.org/r/215902
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iba40f2eef6d161ff08500950ec4c097bac97f505
Gerrit-PatchSet: 21
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>