Usuari:TronaBot/Python/wikistats.py
Aparença
< Usuari:TronaBot | Python
#!/usr/bin/env python
#-*- coding:utf8 -*-
# Copyleft (!C) 2013 Coet
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# 2013-05-19 01:07 CEST
# wikistats.py
# actualitza [[Plantilla:Nivell vandalisme]]
import os, re, sys, unicodedata, time
from platform import system as platfsys
on_win = platfsys().lower() == "windows"
home = on_win and r"E:\\iShare\SugarSync\My Python scripts" \
or "/home/pasqual/public_html/"
for folder in ("pywikilib", "pyuserlib"):
sys.path.append(os.path.join(home, folder))
#pyuserlib
from common import File, Date, ArgumentHandler
from common import has_repeated_string, get_diffs, get_line_diffs
from common import format_string, green, red
#pywikilib
import query as api, wikipedia as pywikilib
def statistics(lang):
params = {
"action": "query",
"meta": "siteinfo",
"siprop": "statistics"
}
site = pywikilib.getSite(lang)
data = api.GetData(params, site)
return data['query']['statistics']
def log_stats(main_lang):
f = File("statistics")
f.prepare()
langs = [lang for lang in pywikilib.Site(main_lang).languages()]
langs.remove(main_lang)
langs.sort()
f.append("lang\tpages\tarticles\tedits\timages\tusers\tactiveusers\tadmins\tjobs")
for lang in langs:
stats=statistics(lang)
stats['lang']=lang
line = (
"%(lang)s\t%(pages)i\t%(articles)i\t%(edits)i\t%(images)i\t%(users)i\t%(activeusers)i\t%(admins)i\t%(jobs)i"
% stats
)
print line
f.append(line)
f.flush()
f.close()
def numberofdata(main_lang):
text = File("statistics")
lines = text.read_lines()
header = """<!-- All open Wikipedia-projects are included. --><onlyinclude>{{#switch:{{{1}}}
| %(mainlang)s = {{#switch:{{{2}}}
| NUMBEROFARTICLES | ARTICLES = {{NUMBEROFARTICLES:R}}
| NUMBEROFFILES | FILES = {{NUMBEROFFILES:R}}
| NUMBEROFPAGES |PAGES = {{NUMBEROFPAGES:R}}
| NUMBEROFUSERS | USERS = {{NUMBEROFUSERS:R}}
| NUMBEROFACTIVEUSERS | ACTIVEUSERS = {{NUMBEROFACTIVEUSERS:R}}
| NUMBEROFADMINS | ADMINS = {{NUMBEROFADMINS:R}}
| NUMBEROFEDITS | EDITS = {{NUMBEROFEDITS:R}}
| 0 }}""" % {"mainlang": main_lang}
footer = """| 0 }}</onlyinclude><noinclude>
</noinclude>
"""
middle =""
for data in lines[1:]:
data=data.split("\t")
data = zip(["lang", "pages", "articles", "edits", "images", "users", "activeusers", "admins", "jobs"], data)
middle += """\n| %(lang)s = {{#switch:{{{2}}}
| NUMBEROFARTICLES | ARTICLES = %(articles)s
| NUMBEROFFILES | FILES = %(images)s
| NUMBEROFPAGES | PAGES = %(pages)s
| NUMBEROFUSERS | USERS = %(users)s
| NUMBEROFACTIVEUSERS | ACTIVEUSERS = %(activeusers)s
| NUMBEROFADMINS | ADMINS = %(admins)s
| NUMBEROFEDITS | EDITS = %(edits)s
| 0 }}""" % dict(data)
return "%s%s\n%s" % (header, middle, footer)
def main():
main_lang="ca"
log_stats(main_lang)
text = numberofdata(main_lang)
File("statistics-text").save(text)
page = pywikilib.Page(site,"Plantilla:Variables/dades")
page.put(text, "Bot, actualitzant plantilla")
def put():
text = File("statistics-text").read()
page = pywikilib.Page(site,"Plantilla:Variables/dades")
page.put(text, "Bot, actualitzant plantilla")
if __name__ == '__main__':
site = pywikilib.getSite(fam="wikipedia_e")
main()
pywikilib.stopme()