-
Notifications
You must be signed in to change notification settings - Fork 2
/
artwork_upload.py
155 lines (123 loc) · 6.56 KB
/
artwork_upload.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
149
150
151
152
153
154
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#Autor: Antoine "0x010C" Lamielle
#Date: 18 February 2017
#License: GNU GPL v3
import pywiki
import pwutils
import csv
import os
from datetime import date
import sys
import requests
import shutil
template = """== {{int:filedesc}} ==
{{Artwork
|artist = $artist
|title = $title
|description = $description
|object type = $object_type
|date = $date
|medium = $medium
|dimensions = {{Size|cm|$width|$height}}
|institution = $institution
|inscriptions = $inscriptions
|permission = $permission
{{CC-zero}}
|source = $source - uploaded by [[User:0x010C|]]
}}
$categories
"""
mediums = ['{{Azulejo}}', '{{Bronze}}', '{{Drawing}}', '{{Engraving}}', '{{Etching}}', '{{Fresco}}', '{{Gouache}}', '{{Gouache on paper}}', '{{Hanging scroll}}', '{{Illumination on parchment}}', '{{Ink}}', '{{Landscape}}', '{{Lithography}}', '{{Mosaic}}', '{{Oil on canvas}}', '{{Oil on canvas panel}}', '{{Oil on cardboard}}', '{{Oil on copper}}', '{{Oil on panel}}', '{{Pencil on paper}}', '{{Stained glass}}', '{{Tapestry}}', '{{Tempera on canvas}}', '{{Tempera on panel}}', '{{Watercolor}}', '{{Watercolor on cardboard}}', '{{Watercolor on paper}}', '{{Woodcut}}']
def uploader(file_name, artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list):
pw = pywiki.Pywiki("commonswiki-0x010C")
pw.login()
artist = artist.strip()
title = title.strip()
raw_artist = artist
if pw.exist(['Creator:'+artist]):
artist = '{{Creator:'+artist+'}}'
elif birth and death:
artist = artist + ' (' + birth + ' - ' + death + ')'
description = ""
if description_fr:
description += '\n{{fr|'+description_fr+'}}'
if description_en:
description += '\n{{en|'+description_en+'}}'
if '{{'+medium+'}}' in mediums:
medium = '{{'+medium+'}}'
if institution:
if institution[0] == '{':
pass
elif pw.exist(['Creator:'+institution]):
institution = '{{Institution:'+institution+'}}'
if death:
permission = '{{PD-old-auto-1923|deathyear='+death+'}}'
else:
permission = '{{PD-old-auto-1923}}'
categories = ""
if pw.exist(['Category:'+raw_artist]):
categories += "\n[[Category:"+raw_artist+"]]"
if categories_list:
categories_list = categories_list.split(';')
for category in categories_list:
categories += "\n[[Category:"+category+"]]"
for category in global_categories:
categories += "\n[[Category:"+category+"]]"
text = template.replace('$artist', artist).replace('$title', title).replace('$description', description).replace('$object_type', object_type).replace('$date', art_date).replace('$medium', medium).replace('$width', width).replace('$height', height).replace('$institution', institution).replace('$inscriptions', inscriptions).replace('$permission', permission).replace('$source', source).replace('$categories', categories)
extension = file_name.split(".")[-1]
extension = extension.lower()
if extension == 'jpeg':
extension = 'jpg'
title = date.today().strftime("%Y-%m") + " " + raw_artist.decode("utf-8") + " - " + title.decode("utf-8") + "." + extension
pw.upload(title, path+file_name, text)
print "File '"+title+"' uploaded"
def info_prompt():
print "artist:\n> ",;artist = sys.stdin.readline().split("\n")[0]
print "birth:\n> ",;birth = sys.stdin.readline().split("\n")[0]
print "death:\n> ",;death = sys.stdin.readline().split("\n")[0]
print "title:\n> ",;title = sys.stdin.readline().split("\n")[0]
print "description (en):\n> ",;description_en = sys.stdin.readline().split("\n")[0]
print "description (fr):\n> ",;description_fr = sys.stdin.readline().split("\n")[0]
print "object type (e.g. 'painting'):\n> ",;object_type = sys.stdin.readline().split("\n")[0]
print "date:\n> ",;art_date = sys.stdin.readline().split("\n")[0]
print "medium (e.g. 'Oil on canvas'):\n> ",;medium = sys.stdin.readline().split("\n")[0]
print "width:\n> ",;width = sys.stdin.readline().split("\n")[0]
print "height:\n> ",;height = sys.stdin.readline().split("\n")[0]
print "institution (e.g. 'Louvre':\n> ",;institution = sys.stdin.readline().split("\n")[0]
print "inscriptions:\n> ",;inscriptions = sys.stdin.readline().split("\n")[0]
print "source:\n> ",;source = sys.stdin.readline().split("\n")[0]
print "categories (separated by a ';'):\n> ",;categories_list = sys.stdin.readline().split("\n")[0]
return (artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list)
# Main
path = ""
global_categories = []
def main():
global path, global_categories, pw
path = pwutils.arg_parser("-p", value=True)
csv_file_path = pwutils.arg_parser("-f", value=True)
global_categories = pwutils.arg_parser("-c", value=True)
if global_categories:
global_categories = global_categories.encode("utf-8").split(';')
else:
global_categories = []
if path and csv_file_path:
if path[-1] != '/':
path = path + '/'
with open(csv_file_path, 'rb') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='&', quotechar='"')
for row in csv_reader:
(file_name, artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list) = row
if os.path.isfile(path+file_name):
uploader(file_name, artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list)
else:
print "Missing file '"+file_name+"'"
url = pwutils.arg_parser("--url", value=True, required=True)
path = "./"
image_response = requests.get(url, stream=True)
with open('./tmp.jpg', 'wb') as out_file:
shutil.copyfileobj(image_response.raw, out_file)
del image_response
(artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list) = info_prompt()
uploader("tmp.jpg", artist, birth, death, title, description_en, description_fr, object_type, art_date, medium, width, height, institution, inscriptions, source, categories_list)
main()