jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[FEAT] Users might want to know only the count of their watchlist

In a case where we have a large list of watchlist items and the terminal
can't scroll to the top due to buffer overflow, users might want to know
just the number of items/pages in their watchlist.

This feature introduces -count and -count:all options to return total number
of pages in the watchlist the bot has access to (for that wiki or all wikis
respectively).

In addition, to avoid a run-time error I encountered that says:
“RuntimeError: dictionary changed size during iteration", I had to make
a copy of the keys of the config usernames via `list(config.usernames)`.

Change-Id: Ie535f12a51f9ebf3b1b2dd11ef4d3bf544c42929
---
M scripts/watchlist.py
1 file changed, 38 insertions(+), 3 deletions(-)

diff --git a/scripts/watchlist.py b/scripts/watchlist.py
index 61131c5..72122b2 100755
--- a/scripts/watchlist.py
+++ b/scripts/watchlist.py
@@ -7,12 +7,16 @@

Syntax:

- python pwb.py watchlist [-all | -new]
+ python pwb.py watchlist [-all | -count | -count:all | -new]

Command line options:

-all Reloads watchlists for all wikis where a watchlist is already
present
+-count Count only the total number of pages on the watchlist of the
+ account the bot has access to
+-count:all Count only the total number of pages on all wikis watchlists
+ that the bot is connected to.
-new Load watchlists for all wikis where accounts is setting in
user-config.py
"""
@@ -39,6 +43,27 @@
return watchlist


+def count_watchlist(site=None):
+ """Count only the total number of page(s) in watchlist for this wiki."""
+ if site is None:
+ site = pywikibot.Site()
+ watchlist_count = len(refresh(site))
+ pywikibot.output('There are {} page(s) in the watchlist.'
+ .format(watchlist_count))
+
+
+def count_watchlist_all():
+ """Count only the total number of page(s) in watchlist for all wikis."""
+ wl_count_all = 0
+ pywikibot.output('Counting pages in watchlists of all wikis...')
+ for family in config.usernames:
+ for lang in config.usernames[family]:
+ site = pywikibot.Site(lang, family)
+ wl_count_all += len(refresh(site))
+ pywikibot.output('There are a total of {} page(s) in the watchlists'
+ 'for all wikis.'.format(wl_count_all))
+
+
def isWatched(pageName, site=None):
"""Check whether a page is being watched."""
watchlist = get(site)
@@ -87,19 +112,29 @@
"""
opt_all = False
opt_new = False
+ opt_count = False
+ opt_count_all = False
for arg in pywikibot.handle_args(args):
if arg in ('-all', '-update'):
opt_all = True
elif arg == '-new':
opt_new = True
+ elif arg == '-count':
+ opt_count = True
+ elif arg == '-count:all':
+ opt_count_all = True
if opt_all:
refresh_all()
elif opt_new:
refresh_new()
+ elif opt_count:
+ count_watchlist()
+ elif opt_count_all:
+ count_watchlist_all()
else:
site = pywikibot.Site()
- watchlist = refresh(site)
- pywikibot.output('{} pages in the watchlist.'.format(len(watchlist)))
+ count_watchlist(site)
+ watchlist = list(site.watched_pages(force=True))
for page in watchlist:
try:
pywikibot.stdout(page.title())

To view, visit change 597276. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie535f12a51f9ebf3b1b2dd11ef4d3bf544c42929
Gerrit-Change-Number: 597276
Gerrit-PatchSet: 13
Gerrit-Owner: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa@seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)