-
Notifications
You must be signed in to change notification settings - Fork 8
/
book_histogram.py
51 lines (46 loc) · 1.29 KB
/
book_histogram.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Create a histogram of the dates when books were created
# Mike Peel 23-Apr-2022 v1 - start
import pywikibot
from pywikibot import pagegenerators
import numpy as np
import matplotlib.pyplot as plt
enwiki = pywikibot.Site('en', 'wikipedia')
hist_arr = []
years = 2000 + np.arange(4,23,1)
months = np.arange(1,13,1)
datearr = []
plot_arr = []
for year in years:
for month in months:
if month < 10:
datearr.append(str(year)+'-0'+str(month))
plot_arr.append(year+(month/12))
else:
datearr.append(str(year)+'-'+str(month))
plot_arr.append(year+(month/12))
vals = np.zeros(len(datearr))
# max_num = 100
# count = 0
cat = pywikibot.Category(enwiki,"Category:User namespace book pages")
for page in pagegenerators.CategorizedPageGenerator(cat, recurse=False):
# count += 1
history = page.getVersionHistoryTable(reverse=True)
hist = history.splitlines()
trip = 0
for line in hist:
if '||' in line and 'oldid' not in line and trip == 0:
date = line.split('||')[1].strip()[0:7]
index = datearr.index(date)
vals[index] += 1
trip = 1
# if count > max_num:
# break
plt.plot(plot_arr, vals)
print(datearr)
print(vals)
plt.title('Book creation dates on enwiki')
plt.xlabel('Time (yrs)')
plt.ylabel('Count')
plt.savefig('book_histogram.pdf')