如何让您在维基百科中编辑最多的贡献者

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

我想找出上周编辑与“Jake”相同页面的编辑最多或最近编辑 100 次或类似内容的编辑。

我知道我的查询,但我不知道我需要哪些表,因为维基媒体数据库布局一团糟。

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

用户名 发生次数 页面
麦基 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,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:我可以访问数据库副本,我想这比 Wikimedia API 或 Pywikibot 快得多

tym520 回答:如何让您在维基百科中编辑最多的贡献者

您可以使用时间戳参数过滤要检索的数据。这大大减少了所需的时间。请参阅 documentation 以了解其用法。以下是使用时间戳获取 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 函数具有相同的内容。

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

本文链接:https://www.f2er.com/268635.html

大家都在问