-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons-category-downloader.py
31 lines (24 loc) · 1 KB
/
commons-category-downloader.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
import pywikibot
from pywikibot import pagegenerators
import argparse
from pathlib import Path
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('category', type=str, help='''Name of the category. Prefixing "Category:" is optional.''')
parser.add_argument('-p', '--path', type=str, default="./images", help='Output path')
parser.add_argument('-q', '--quiet', action='store_true', help='Print nothing')
args = parser.parse_args()
cat_name = args.category
if not cat_name.startswith("Category:"):
cat_name = "Category:" + cat_name
download_path = args.path
Path(download_path).mkdir(parents=True, exist_ok=True)
quiet = args.quiet
site = pywikibot.Site("commons", "commons")
cat = pywikibot.Category(site, cat_name)
gen = pagegenerators.CategorizedPageGenerator(cat)
for page in gen:
filename = page.title()[5:]
if not quiet:
print(filename)
page.download(f'{download_path}/{filename}')