0

我正在做一个游戏化网络应用程序来帮助维基媒体的社区健康。

我想找出哪些编辑在上周编辑了与“Jake”相同的页面最多或最后一次编辑 100 次或类似的内容。

我知道我的查询,但我不知道我需要什么表,因为 Wikimedia DB 布局一团糟。

所以,我想获得类似的东西

用户名 出现次数 页面
米奇 13 奥巴马,...

所以查询将类似于(我正在接受建议):

  1. 获取用户“Jake”在上周编辑的页面。
  2. 获取上周该页面的贡献者。
  3. 对于这些贡献者中的每一个,获取他们在上周编辑的页面,看看它们是否与“Jake”编辑的页面匹配并计算它们。

我尝试在Pywikibot中做一些更简单的事情,但它非常非常慢(Jake 的最后 500 次贡献需要 20 秒)。

我只获取编辑过的页面并获取该页面的贡献者并仅计算它们,这非常慢。

我的 pywikibot 代码是:

site = Site(langcode, 'wikipedia')
user = User(site, username)
contributed_pages = set()
for page, oldid, ts, comment in user.contributions(total=100, namespaces=[0]):
    contributed_pages.add(page)

return get_contributor_ocurrences(contributed_pages,site, username)

和功能

def get_contributor_ocurrences(contributed_pages, site,username):
contributors = []
for page in contributed_pages:

    for editor in page.contributors():
        if APISite.isBot(self= site,username=editor) or editor==username:
            continue
        contributors.append(editor)

return Counter(contributors)

PS:我可以访问DB 副本,我想这比 Wikimedia API 或 Pywikibot 快得多

4

1 回答 1

1

您可以使用时间戳参数过滤要检索的数据。这大大减少了所需的时间。有关它们的用法,请参阅文档以下是使用Pywikibot使用时间戳获取数据的代码片段:

from collections import Counter
from datetime import timedelta
import pywikibot
from pywikibot.tools import filter_unique
site = pywikibot.Site()
user = pywikibot.User(site, username)  # username must be a string

# Setup the Generator for the last 7 days.
# Do not care about the timestamp format if using pywikibot.Timestamp
stamp = pywikibot.Timestamp.now() - timedelta(days=7)
contribs = user.contributions(end=stamp)

contributors= []

# filter_unique is used to remove duplicates.
# The key uses the page title
for page, *_ in filter_unique(contribs, key=lambda x: str(x[0])):
    # note: editors is a Counter
    editors = page.contributors(endtime=stamp)
    print('{:<35}: {}'.format(page.title(), editors))
    contributors.extend(editors.elements())

total = Counter(contributors)

这将打印一个页面列表,并为每个页面显示给定时间范围内的编辑者及其贡献计数器。最后应该与上面的函数total具有相同的内容。get_contributor_ocurrences

它需要一些额外的工作才能获得您上面提到的表格。

于 2021-07-12T15:55:10.673 回答