/
wiki-commons-mit-bayerischem-denkmalschutz----edelmauswaldgeist-lookup-commonscat-plus-id.pl
288 lines (200 loc) · 8.64 KB
/
wiki-commons-mit-bayerischem-denkmalschutz----edelmauswaldgeist-lookup-commonscat-plus-id.pl
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
#!/usr/bin/perl
use strict;
# Crypt::SSLeay muss installiert sein fuer SSL-Support
# apt-get install libwww-mechanize-perl libio-socket-ssl-perl libnet-ssleay-perl
use WWW::Mechanize;
use Data::Dumper;
use HTML::FormatText;
#http://glennf.com/writing/hexadecimal.url.encoding.html
# This pattern takes the hex characters and decodes them back into real characters. The function hex() turns a hex number into decimal; there is no dec() that does the reverse in perl. The "e" at the end of the regexp means "evaluate the replacement pattern as an expression."
sub URLDecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!--(.|\n)*-->//g;
return $theURL;
}
my $USER_AGENT='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36';
# See the autocheck parameter, which turns all HTTP errors into fatal errors. When you turn autocheck off, you will need to do all error checking yourself.
my $agent = WWW::Mechanize->new( agent => $USER_AGENT, autocheck => 0 );
# https://de.wikipedia.org/wiki/Spezial:Neue_Seiten
# 'Edelmauswaldgeist',
# 'Plozessor',
# 'Störfix'
# 'Reinhardhauke',
# 'Ricardalovesmonuments',
# 'Rufus46',
my @userlist = (
# 'Plozessor',
# 'Rufus46',
# 'Edelmauswaldgeist',
'Reinhardhauke',
);
my $limit = 300;
my $limit = 1000;
# 'Flussar',
# 'I.+Berger',
# 'Georg Karl Ell',
# my $newOnly = 1; # nur neue kategorien
my $newOnly = 0; # auch bestehende kategorien
my $URL = "";
foreach my $username (@userlist) {
my $URL = 'https://commons.wikimedia.org/w/index.php?title=Special:Contributions&limit='.$limit.'&target='.$username.'&namespace=14&tagfilter=&newOnly='.$newOnly.'&start=&end=';
# print "URL:$URL:\n";
# }
# exit;
$agent->get($URL);
# print Dumper($agent);
# print "===========================\n";
my $result = $agent->content;
# print "RESULT:$result:";
my @links = $agent->find_all_links( url_regex => qr/\/Category\:/ );
# my @links = $agent->find_all_links( url_regex => qr/\/wiki\/Benutzer\:/ );
my $count = 0;
for my $link ( @links ) {
my $url = $link->url_abs;
if ($url =~ /(in|ex)terior/i) { next; }
if ($url =~ /Cultural_heritage_monuments/i) { next; }
if ($url =~ /Churches_in/i) { next; }
# print "============================\n";
# print "LINK:$url:\n";
$count++;
# https://commons.wikimedia.org/wiki/Category:M%C3%BCnchner_Torturm_(M%C3%BChldorf_am_Inn)?action=raw
#test
# $url = 'https://commons.wikimedia.org/wiki/Category:Mangfallpark_S%C3%BCd_(Rosenheim)';
# $url = 'https://commons.wikimedia.org/wiki/Category:Kapelle_Startzfeld_(P%C3%B6sling)';
# $url ='https://commons.wikimedia.org/wiki/Category:St._Johann_Baptist_(Fischbach)';
# $url ='https://commons.wikimedia.org/wiki/Category:M%C3%BChlennebengeb%C3%A4ude_(Weidach)';
# $url ='https://commons.wikimedia.org/wiki/Category:Ried_3_(Dietramszell)';
my $lemma = "";
my $lemma_decode = "";
if ($url =~ /Category:(.*)$/) {
$lemma = $1;
$lemma_decode = URLDecode($lemma);
$lemma_decode =~ s/_/ /og;
}
# print "LEMMA:$lemma:DECODE:$lemma_decode:\n";
my $check_url = $url.'?action=raw';
# print "check URL:$url:\n";
$agent->get( $check_url );
my $result2 = $agent->content;
# print "RESULT:$result2:\n";
if ($result2 =~ /Baudenkmal Bayern/i) {
# print "Lemma $check_url hat Baudenkmal-ID\n";
if ($result2 =~ /xxxx Wikidata infobox/i) {
# print "Lemma $check_url hat Wikidata Infobox - do nothing\n";
} else {
my $blfd_id = "";
# if ($result2 =~ /(\s*)\{\{Baudenkmal Bayern(\s*)\|(\s*)(D[\-0-9]+)/i) {
if ($result2 =~ /(\s*)\{\{Baudenkmal Bayern(\s*)\|(1=)?(\s*)(D[\-0-9]+)/i) {
# print "*** HIER: $1:$2:$3:$4:5:$5:\n";
$blfd_id = $5;
}
# print "Lemma $check_url hat Baudenkmal-ID $blfd_id:\n";
# print "Lookup Q-ID for BLFD_ID:$blfd_id:\n";
# if ($count == 5) { exit ; }
if ($blfd_id ne "") {
# haswbstatement:P4244=D-1-89-159-2
# SELECT ?item WHERE { ?item, P373 wdt:P4244 "D-1-89-159-2" . }
# SELECT ?item ?commonscat WHERE { ?item wdt:P4244 "D-1-63-000-18" . OPTIONAL{?item wdt:P373 ?commonscat .} }
# my $wd_url = 'https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20WHERE%20%7B%20%3Fitem%20wdt%3AP4244%20%22'.$blfd_id.'%22%20.%20%7D';
# my $wd_url = 'https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20WHERE%20%7B%20%3Fitem%20wdt%3AP4244%20%22'.$blfd_id.'%22%20.%20%7D';
my $wd_url = 'https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20%3Fcommonscat%20WHERE%20%7B%20%3Fitem%20wdt%3AP4244%20%22'.$blfd_id.'%22%20.%20%20OPTIONAL%7B%3Fitem%20wdt%3AP373%20%3Fcommonscat%20.%7D%20%20%20%7D';
# print "WD_URL:$wd_url:\n";
# "Accept: application/json"
# "Accept: text/csv"
# 'Accept' => 'application/json'
$agent->add_header(
'Accept' => 'text/csv'
);
$agent->get( $wd_url );
my $result3 = $agent->content;
# print "RESULT:$result3:\n";
# print "LINE2:".@result3[1]."\n";
my $qid = 0; my $wd_commonscat = "";
if ($result3 =~ /wikidata.org\/entity\/Q([0-9]+),(.*)/i) {
# print "QID:$1:CAT:$2\n";
$qid = $1;
$wd_commonscat = $2;
$wd_commonscat =~ s/\s//og;
}
if ($wd_commonscat ne "") {
# print "lemma $lemma hat bereits commonscat $wd_commonscat - do nothing\n";
} else {
if ($qid > 0 ) {
# Wenn Q-ID gefunden, dann quickstatement ausgeben
# print "QUICKSTATEMENT for Q$qid\n";
# Q41269749 P373 "Ringstraße 13 Hofkapelle (Übersee, Chiemgau)"
# Q41269749 Scommonswiki "Category:Ringstraße 13 Hofkapelle (Übersee, Chiemgau)"
print "Q".$qid."\t".'P373'."\t".'"'.$lemma_decode.'"'."\n";
print "Q".$qid."\t".'Scommonswiki'."\t".'"Category:'.$lemma_decode.'"'."\n";
}
}
}
}
} else {
#print "======================\n";
# print "Lemma $check_url - Keine Baudenkmal-ID\n";
# print "LEMMA:$lemma:DECODE:$lemma_decode:\n";
if ($result2 =~ /Wikidata infobox/i) {
# print "Lemma $check_url hat Wikidata Infobox - do nothing\n";
} else {
my $lemma_encode = $lemma_decode;
$lemma_encode =~ s/ /%20/og;
$lemma_encode =~ s/\(/\\\(/og;
$lemma_encode =~ s/\)/\\\)/og;
my $check_url = 'https://de.wikipedia.org/w/index.php?search=insource%3A%2F'.$lemma_encode.'%2F&title=Spezial%3ASuche&go=Artikel&ns0=1&ns9=1&ns11=1';
# print "check URL:$check_url:\n";
$agent->get( $check_url );
my $result2 = $agent->content;
# print "RESULT:$result2:\n";
my $QID= 0;
my $BLFID = 0;
if ($result2 =~ /Wikidata=Q([0-9]+)/) {
$QID = $1;
# print "QID gefunden:$lemma_decode:QID:$QID:\n";
print "Q".$QID."\t".'P373'."\t".'"'.$lemma_decode.'"'."\n";
print "Q".$QID."\t".'Scommonswiki'."\t".'"Category:'.$lemma_decode.'"'."\n";
} else {
if ($result2 =~ /Nummer=D([0-9-]+)/) {
$BLFID = 'D'.$1;
# print "SUCHE BLFID:$lemma_decode:BLFID:$BLFID:\n";
####################
my $wd_url = 'https://query.wikidata.org/sparql?query=SELECT%20%3Fitem%20%3Fcommonscat%20WHERE%20%7B%20%3Fitem%20wdt%3AP4244%20%22'.$BLFID.'%22%20.%20%20OPTIONAL%7B%3Fitem%20wdt%3AP373%20%3Fcommonscat%20.%7D%20%20%20%7D';
$agent->add_header(
'Accept' => 'text/csv'
);
$agent->get( $wd_url );
my $result3 = $agent->content;
my $qid = 0; my $wd_commonscat = "";
if ($result3 =~ /wikidata.org\/entity\/Q([0-9]+),(.*)/i) {
# print "QID:$1:CAT:$2\n";
$qid = $1;
$wd_commonscat = $2;
$wd_commonscat =~ s/\s//og;
}
if ($wd_commonscat ne "") {
# print "lemma $lemma hat bereits commonscat $wd_commonscat - do nothing\n";
} else {
if ($qid > 0 ) {
print "Q".$qid."\t".'P373'."\t".'"'.$lemma_decode.'"'."\n";
print "Q".$qid."\t".'Scommonswiki'."\t".'"Category:'.$lemma_decode.'"'."\n";
}
}
####################
} # BLFID-Lookup
}
# |Nummer=D-1-73-118-138
# |Wikidata=Q41226085
# |Bild=<span class="searchmatch">Ried 3 Dietramszell</span> 1.jpg
# |Commonscat=
#### FIXME
# lookup commonscat -> ID
# https://de.wikipedia.org/w/index.php?search=insource%3A%2FHochreiterweg+2+%28Oberelkofen%29%2F&title=Spezial%3ASuche&go=Artikel&ns0=1&ns9=1&ns11=1
# https://de.wikipedia.org/w/index.php?search=insource%3A%2FRied+3+%28Dietramszell%29%2F&title=Spezial%3ASuche&profile=advanced&fulltext=1&ns0=1&ns9=1&ns11=1
}
}
# exit;
}
}
exit 0;