-
Notifications
You must be signed in to change notification settings - Fork 0
/
Photo.java
301 lines (256 loc) · 8.41 KB
/
Photo.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* The MIT License
*
* Copyright 2015 Paweł Marynowski.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package pl.wikimedia.glam.uam;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
class Photo {
int id = 0;
String path = "";
String accession_number = "";
String author = "";
String comment = "";
String date = "";
String location = "";
String title = "";
ArrayList<String> tags = new ArrayList<>();
Photo(int _id) {
id = _id;
}
// sets
//
public void setAccNumber(String _accession_number) {
accession_number = _accession_number.startsWith("Nr inwent.: ") ? _accession_number.substring(12) : _accession_number;
}
public void setAuthor(String _author) {
author = _author.startsWith("Autor: ") ? _author.substring(7) : _author;
}
public void setComment(String _comment) {
if (!_comment.isEmpty()) {
comment = "{{pl|" + _comment + "}}";
}
}
public void setDate(String _date) {
String d;
if (_date.startsWith("Rok:")) {
d = _date.substring(4);
} else if (_date.startsWith("Zdjęcie wykonan")) {
d = _date.substring(17);
} else {
d = _date;
}
if (d.endsWith("r.")) {
d = d.replace("r.", "");
}
date = d.trim();
}
public void setLocation(String _location) {
if (!_location.contains("nieznane")) {
location = _location.startsWith("Lokacja:") ? _location.substring(8) : _location;
}
}
public void setPath(String _path) {
path = "http://cyfrowearchiwum.amu.edu.pl" + _path.replace("window.open('", "").replace("','_blank');", "");
}
public void setTags(Elements _elems) {
for (Element e : _elems) {
tags.add(e.text());
}
}
public void setTitle(String _title) {
if(!_title.equals("brak")) {
title = _title;
}
}
// gets
//
public String getCategories() {
Categories categories = new Categories();
String text = "";
String country = "Poland";
if(tags.contains("afganistan")) {
country = "Afghanistan";
} else if(tags.contains("afryka")) {
country = "Africa";
} else if(tags.contains("azja")) {
country = "Asia";
} else if(tags.contains("bałkany")) {
country = "the Balkans";
}
for (int i = 0; i < tags.size(); ++i) {
tags.set(i, categories.get(tags.get(i)));
}
if (!getCity().isEmpty()) {
tags.add(getCity());
}
HashSet hs = new HashSet();
hs.addAll(tags);
tags.clear();
tags.addAll(hs);
Collections.sort(tags);
if (getDate().matches("[0-9]{4}.*")) {
text += "[[Category:" + getDate().substring(0, 4) + " in " + country + "]]\n";
}
for (String tag : tags) {
if (!tag.trim().isEmpty()) {
text += tag.equals(getCity()) ?
"[[Category:" + tag + "]]\n" :
"[[Category:" + tag + " " + country + "]]\n";
}
}
if (text.isEmpty()) {
text += "{{subst:unc}}";
}
return text;
}
String getCity() {
String[] loc = location.split(",");
return loc[0];
}
String getDate() {
// date month year
if (date.matches("[0-9]{1,2} [IVX]{1,5} [0-9]{4}")) {
String[] dates = date.split(" ");
if (dates[0].length() == 1) {
dates[0] = "0" + dates[0];
}
return dates[2] + "-" + parseMonth(dates[1]) + "-" + dates[0];
} else if (date.matches("[0-9]{1,2}\\-[0-9]{1,2}\\-[0-9]{4}")) {
String[] dates = date.split("-");
return dates[2] + "-" + dates[1] + "-" + dates[0];
}
return date;
}
String getTitle() {
return title.isEmpty() ? "" : "{{pl|" + title + ".}}";
}
File getFile() {
File f = null;
try {
URL url = new URL(path);
BufferedImage bi = ImageIO.read(url);
f = new File("temp.jpg");
ImageIO.write(bi, "jpg", f);
} catch (MalformedURLException ex) {
Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
}
return f;
}
public String getName() {
String text = "";
if(!title.isEmpty()) {
text += title + " - ";
}
if(!getCity().isEmpty()) {
text += getCity() + " - ";
}
text += accession_number + ".jpg";
return text;
}
public String getWikiText() {
String text = "=={{int:filedesc}}==\n"
+ "{{Photograph\n"
+ " |photographer = {{pl|" + author + "}}\n"
+ " |title = " + getTitle() + "\n"
+ " |description = \n"
+ " |depicted people = \n"
+ " |depicted place = {{pl|" + location + "}}\n"
+ " |date = " + getDate() + "\n"
+ " |medium = {{technique|photo}}\n"
+ " |dimensions = \n"
+ " |institution = {{Institution:Institute of Ethnology and Cultural Anthropology, Adam Mickiewicz University}}\n"
+ " |references = \n"
+ " |object history = \n"
+ " |exhibition history = \n"
+ " |credit line = \n"
+ " |inscriptions = \n"
+ " |notes = " + comment + "\n"
+ " |accession number = " + accession_number + "\n"
+ " |source = http://cyfrowearchiwum.amu.edu.pl/archive/" + id + "\n"
+ " |permission = \n"
+ " |other_versions = \n"
+ "}}\n\n";
text += "=={{int:license-header}}==\n"
+ "{{cc-by-sa-3.0-pl}}\n"
+ "{{Institute of Ethnology and Cultural Anthropology, Adam Mickiewicz University partnership}}\n"
+ "{{subst:chc}}\n\n";
text += getCategories();
text += "[[Category:Images from Józef Burszta Digital Archives – needing category checks]]\n";
text = text.replaceAll(" +", " ");
return text;
}
// other
//
String parseMonth(String month) {
String m = "??";
switch (month) {
case "I": m = "01"; break;
case "II": m = "02"; break;
case "III": m = "03"; break;
case "IV": m = "04"; break;
case "V": m = "05"; break;
case "VI": m = "06"; break;
case "VII": m = "07"; break;
case "VIII": m = "08"; break;
case "IX": m = "09"; break;
case "X": m = "10"; break;
case "XI": m = "11"; break;
case "XII": m = "12"; break;
}
return m;
}
}
class Categories {
Map<String, String> map = new HashMap<>();
public Categories() {
map.put("architektura sakralna", "Religious buildings in");
map.put("budownictwo", "Buildings in");
map.put("dzieci", "Children of");
map.put("folklor", "Folklore of");
map.put("kobiety", "Women of");
map.put("narzędzia rolnicze", "Agricultural tools in");
map.put("rękodzieło", "Folk art in");
map.put("rolnictwo", "Agriculture in");
map.put("strój ludowy", "Folk national costumes of");
map.put("sztuka ludowa", "Folk art in");
}
public String get(String key) {
String value = map.get(key);
return value == null ? "" : value;
}
}