-
Notifications
You must be signed in to change notification settings - Fork 3
/
glam_stats.py
148 lines (117 loc) · 4.26 KB
/
glam_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# !/usr/bin/python
# -*- coding: utf-8 -*-
"""Save usage stats for selected GLAM categories.
Usage: python3 glam_stats.py --config se_config.json
"""
from datetime import datetime
from lxml import etree
import argparse
import json
import requests
def generate_filename():
return datetime.today().strftime('%Y-%m-%d')
def make_url(catname):
base = (
"https://tools.wmflabs.org//glamtools/glamorous.php"
"?doit=1&category={}"
"&use_globalusage=1&ns0=1&depth=9&projects[wikipedia]=1"
"&projects[wikimedia]=1&projects[wikisource]=1"
"&projects[wikibooks]=1&projects[wikiquote]=1"
"&projects[wiktionary]=1&projects[wikinews]=1"
"&projects[wikivoyage]=1&projects[wikispecies]=1"
"&projects[mediawiki]=1&projects[wikidata]=1"
"&projects[wikiversity]=1&format=xml"
)
return base.format(catname.replace(" ", "+"))
def calculate_percent(part, whole):
if float(whole) > 0:
return round(100 * float(part) / float(whole), 2)
else:
return 0
def process_data(xmlblob):
row = {}
tree = etree.fromstring(xmlblob)
no_files = tree.get("images_in_category")
row["files"] = no_files
for child_of_root in tree:
distinct_used = child_of_root.get("distinct_images")
row["percent_used"] = calculate_percent(distinct_used, no_files)
row["total_usage"] = child_of_root.get("total_usage")
row["distinct_used"] = distinct_used
return row
def analyze_single(item):
url = make_url(item["cat"])
data = download_data(url)
row = process_data(data)
row["name"] = item["name"]
row["cat"] = item["cat"].replace(" ", "_")
return row
def download_data(url):
return requests.get(url).text
def read_json_file(fname):
return json.load(open(fname))
def save_file(fname, txt):
with open(fname, 'w') as f:
f.write(txt)
print("Saved file: {}".format(fname))
def save_json_file(fname, data):
with open(fname, 'w') as f:
json.dump(data, f,
ensure_ascii=False,
sort_keys=True, indent=4)
print("Saved file: {}".format(fname))
def make_wikitable(data):
txt = '{| class="wikitable sortable"\n|-\n'
txt += "! {}\n".format("institution")
txt += "! {}\n".format("files")
txt += "! {}\n".format("used")
txt += "! {}\n".format("% used")
for r in data:
link_base = "[[:commons:Category:{}|{}]]"
link = link_base.format(r["cat"], r["name"])
txt += "|-\n"
txt += "| {}\n".format(link)
txt += "| {}\n".format(r["files"])
txt += "| {}\n".format(r["distinct_used"])
txt += "| {}\n".format(r["percent_used"])
txt += "|}\n"
return txt
def make_markdown(data):
txt = "| {} | {} | {} | {} |\n".format("institution",
"files",
"used",
"% used")
txt += "|---|---|---|---|\n"
for r in data:
link_base = "https://commons.wikimedia.org/wiki/Category:{}"
link_str = link_base.format(r["cat"])
link = "[{}]({})".format(r["name"], link_str)
txt += "| {} | {} | {} | {} |\n".format(link,
r["files"],
r["distinct_used"],
r["percent_used"])
return txt
def extract_number_files(json):
return int(json["files"])
def save_processed(rows):
fname = generate_filename()
save_json_file("{}.json".format(fname), rows)
save_file("{}.md".format(fname), make_markdown(rows))
save_file("{}.wiki".format(fname), make_wikitable(rows))
def main(f_config):
cats = read_json_file(f_config)
message = "[{}] I'm going to analyze {} cats...🐈"
print(message.format(f_config, len(cats)))
rows = []
for cat in cats:
rows.append(analyze_single(cat))
rows.sort(key=extract_number_files, reverse=True)
save_processed(rows)
if __name__ == "__main__":
arguments = {}
parser = argparse.ArgumentParser()
parser.add_argument("--config",
help='file with categories to analyze',
default="se_config.json")
args = parser.parse_args()
main(args.config)