Last active
December 21, 2023 15:04
-
-
Save lokal-profil/98e9b645eb9d1ba3479a4ed4d1dc10d2 to your computer and use it in GitHub Desktop.
Count the number of files created per month in a Commons category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List creation times for all files in a category | |
from collections import Counter | |
import pywikibot | |
from tqdm import tqdm | |
def get_creation_month(fp): | |
creation_time = fp.oldest_revision.timestamp | |
return creation_time.isoformat().rpartition('-')[0] | |
def output_results(cat, months): | |
clean_cat = cat.replace(' ','_').split(':')[-1] | |
out_file = f'{clean_cat.replace("/", "-")}_file_count.tsv' | |
with open(out_file, 'w', encoding ='utf8') as fp: | |
fp.write(f'# File count for {clean_cat}\n') | |
fp.write('month\tnew_files\n') | |
for month, count in sorted(months.items()): | |
fp.write(f'{month}\t{count}\n') | |
pywikibot.output(f'Data saved to {out_file}') | |
def count_new_files_by_month(category_name): | |
site = pywikibot.Site('commons', 'commons') | |
cat = pywikibot.Category(site, category_name) | |
months = Counter() | |
for file_page in tqdm(cat.members(member_type=['file']), desc="Processing category members", total=cat.categoryinfo.get('files')): | |
months[get_creation_month(file_page)] += 1 | |
return months | |
category = input('category name: ') | |
out_data = count_new_files_by_month(category) | |
output_results(category, out_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tqdm>=4.66.1,<5.0 | |
pywikibot>=8.3.1,<9.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment