This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
museidelcibo_uploader.py
92 lines (79 loc) · 2.65 KB
/
museidelcibo_uploader.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Bot to read a Musei del cibo CSV file and upload files to Commons. """
#
# (C) Federico Leva, 2016
#
# Distributed under the terms of the MIT license.
#
__version__ = '0.1.0'
import pywikibot
import pywikibot.data.api
from pywikibot import config
from upload import UploadRobot
import sys
import os
#import re
from collections import namedtuple
import unicodecsv as csv
from kitchen.text.converters import to_unicode
class CiboRobot:
def __init__(self, filename):
self.repo = pywikibot.Site('commons', 'commons')
self.filename = filename
if not os.path.exists(self.filename):
pywikibot.output('Cannot find %s. Try providing the absolute path.'
% self.filename)
sys.exit(1)
def run(self, filename):
with open(filename, 'r') as f:
source = csv.reader(f, delimiter='\t')
header = next(source)
pywikibot.output("Header of the input table: " + ', '.join(header) )
titles = namedtuple('titles', ', '.join(header))
titles = [titles._make(row) for row in source]
if not titles:
pywikibot.output("We were not able to extract the data to work on. Exiting.")
return
for row in titles:
commons = "%s - Musei del cibo - %s - %s.jpg" % (row.nome, row.museo, row.inventario)
description = u"""
{{Musei del cibo
| museo = %s
| inventario = %s
| nome = %s
| ambito = %s
| epoca = %s
| dimensioni = %s
| materia = %s
| descrizione = %s
| provenienza = %s
| note = %s
| bibliografia = %s
}}
""" % (row.museo, row.inventario, row.nome, row.ambito, row.epoca,
row.dimensioni, row.materia, row.descrizione, row.provenienza, row.note, row.biblio)
try:
upload = UploadRobot(row.inventario + ".jpg", description=description,
useFilename=commons, keepFilename=True,
verifyDescription=False, ignoreWarning=False, aborts=True)
upload.run()
except:
pywikibot.output("ERROR: The upload could not be completed.")
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
# process all global bot args
# returns a list of non-global args
for arg in pywikibot.handle_args(args):
if arg:
if arg.startswith('-file'):
filename = arg[6:]
bot = CiboRobot(filename)
bot.run(filename)
if __name__ == "__main__":
main()