jenkins-bot has submitted this change and it was merged.
Change subject: [IMPR] use wikihistory to get main authors
......................................................................
[IMPR] use wikihistory to get main authors
- rename afd-notice.py to afd_notice.py
- Add some tests
Change-Id: Iccfa7105e55a58a36361bb80826b3e6a87b508c8
---
R afd_notice.py
A tests/afd_notice_tests.py
2 files changed, 104 insertions(+), 32 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/afd-notice.py b/afd_notice.py
similarity index 86%
rename from afd-notice.py
rename to afd_notice.py
index f041b34..ac9df94 100644
--- a/afd-notice.py
+++ b/afd_notice.py
@@ -24,12 +24,13 @@
#
import pickle
+import re
import time
-from collections import Counter
import pywikibot
from pywikibot import config, pagegenerators, textlib
from pywikibot.bot import SingleSiteBot
+from pywikibot.comms.http import fetch
msg = u'{{ers:user:xqbot/LD-Hinweis|%(page)s|%(action)s}}'
opt_out = u'Benutzer:Xqbot/Opt-out:LD-Hinweis'
@@ -165,15 +166,16 @@
"""
Process a given page.
- Get the creator of the page and calculate the main authors.
+ Get the creator of the page and get the main authors from wikihistory.
@param pagename: page title
@type pagename: str
"""
page = pywikibot.Page(pywikibot.Link(pagename))
if not page.exists():
+ pywikibot.output('Page %s does not exist. Skipping' % page)
return
- cnt = Counter()
+
# read the oldest_revision with content
old_rev = next(page.revisions(total=1, reverse=True, content=True))
@@ -191,44 +193,39 @@
# either he tagged the deletion request or he saw it
latest = next(page.revisions(total=1)).user
- # evtl. anonyme/unregistrierte von Zählung ausnehmen
- for rev in page.revisions():
- if rev.minor:
- cnt[rev.user] += 0.2
- else:
- cnt[rev.user] += 1
-
- s = sum(cnt.values())
- s2 = sum(i ** 2 for i in cnt.values())
- n = float(len(cnt))
- x_ = s / n
- # avg + stdabw
- # faktor von 1 auf 1,5 erhöht für bessere Trennschärfe
- # (siehe Bem. von Gestumblindi)
- limit = max(5, (s2 / n - x_ ** 2) ** 0.5 * 1.5 + x_)
- # main, main_cnt = cnt.most_common(1)[0]
-
# inform creator
if creator and creator != latest and creator not in self.ignoreUser:
user = pywikibot.User(self.site, creator)
if user.isRegistered() and not (user.isBlocked() or
'bot' in user.groups()):
- pywikibot.output(u'>>> Creator is ' + creator)
+ pywikibot.output(u'\n>>> Creator is ' + creator)
self.inform(user, page=page.title(), action=u'angelegte')
# inform main authors
- for main, main_cnt in cnt.most_common(7):
- if main_cnt < limit:
+ url = ('https://tools.wmflabs.org/wikihistory/dewiki/getauthors.php?'
+ 'page_id=%s' % page._pageid)
+ r = fetch(url)
+ if r.status not in (200, ):
+ pywikibot.warning('wikihistory request status is %d' % r.status)
+ return # TODO: try again?
+ pattern = r'>(?P<author>.+?)</a>\s\((?P<percent>\d{1,3})&'
+ count = 0
+ for main, main_cnt in re.findall(pattern, r.content):
+ main_cnt = int(main_cnt)
+ count += main_cnt
+ if ' weitere' in main:
break
- if main != latest and main != creator and \
- main not in self.ignoreUser:
- user = pywikibot.User(self.site, main)
- if user.isRegistered() and not (user.isBlocked() or
- 'bot' in user.groups()):
- pywikibot.output(u'>>> Main author %s with %d Edits'
- % (main, main_cnt))
- self.inform(user, page=page.title(),
- action=u'stark überarbeitete')
+ if main == latest or main == creator or main in self.ignoreUser:
+ continue
+ user = pywikibot.User(self.site, main)
+ if user.isRegistered() and not (user.isBlocked() or
+ 'bot' in user.groups()):
+ pywikibot.output('>>> Main author %s with %d %% edits'
+ % (main, main_cnt))
+ self.inform(user, page=page.title(),
+ action='stark überarbeitete')
+ if count > 50:
+ break
def inform(self, user, **param):
"""
diff --git a/tests/afd_notice_tests.py b/tests/afd_notice_tests.py
new file mode 100644
index 0000000..845bd20
--- /dev/null
+++ b/tests/afd_notice_tests.py
@@ -0,0 +1,75 @@
+# -*- coding: utf-8 -*-
+"""Test imagereview modules."""
+#
+# (C) xqt, 2016
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, print_function, unicode_literals
+
+__version__ = '$Id $'
+
+import inspect
+import os
+import sys
+import unittest
+
+currentdir = os.path.dirname(
+ os.path.abspath(inspect.getfile(inspect.currentframe())))
+parentdir = os.path.dirname(currentdir)
+sys.path.insert(0, parentdir)
+
+import afd_notice
+
+from pywikibot import config
+
+
+class TestBot(afd_notice.AFDNoticeBot):
+
+ """Test class of AFDNoticeBot."""
+
+ def __init__(self, **kwargs):
+ """Constructor."""
+ super(TestBot, self).__init__(**kwargs)
+ self.users = []
+
+ def inform(self, user, **param):
+ """Redefine inform method to get informed user as list."""
+ self.users.append(user.title(withNamespace=False))
+
+
+class TestAFDNoticeBot(unittest.TestCase):
+
+ """Test CheckImageBot."""
+
+ @classmethod
+ def setUpClass(cls):
+ """Setup Class."""
+ config.family = 'wikipedia'
+ config.mylang = 'de'
+ cls.bot = TestBot()
+ cls.bot.treat('Hydraulik')
+
+ def test_creator(self):
+ """Test creator."""
+ if not self.bot.users:
+ self.skipTest('No entries in self.bot.users')
+ self.assertEqual(self.bot.users[0], 'Ulfb')
+
+ def test_authors(self):
+ """Test main author."""
+ if not self.bot.users:
+ self.skipTest('No entries in self.bot.users')
+ self.assertEqual(self.bot.users[1], 'Xqt')
+
+ def test_length(self):
+ """Test wikihistory length."""
+ self.assertGreater(len(self.bot.users), 0)
+ self.assertLessEqual(len(self.bot.users), 6)
+
+
+if __name__ == '__main__':
+ try:
+ unittest.main()
+ except SystemExit:
+ pass
--
To view, visit https://gerrit.wikimedia.org/r/286311
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iccfa7105e55a58a36361bb80826b3e6a87b508c8
Gerrit-PatchSet: 24
Gerrit-Project: pywikibot/bots/xqbot
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits