-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwantedimages.pl
166 lines (132 loc) · 3.25 KB
/
wantedimages.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
use LWP::Simple;
use Try::Tiny;
binmode STDOUT, ':utf8';
#$content = get("http://speedydeletion.wikia.com/wiki/Special:WantedFiles?limit=10000");
my $offset = shift @ARGV ||0;
$content = get("http://speedydeletion.wikia.com/wiki/Special:WantedFiles?limit=500&offset=${offset}");
use MediaWiki::API;
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'http://speedydeletion.wikia.com/api.php';
my $username;
my $userpass;
open IN, "<config.cfg" or die;
while (<IN>)
{
if (/username:(.+)/)
{
$username=$1;
}
elsif (/userpass:(.+)/)
{
$userpass=$1;
}
}
close IN;
#print "User:$username Pass:$userpass\n";
die unless $username;
die unless $userpass;
$mw->login( { lgname => $username, lgpassword => $userpass } )
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
my $mw2 = MediaWiki::API->new();
$mw2->{config}->{api_url} = 'http://en.wikipedia.org/w/api.php';
#my $title=shift;
print $file;
use Encode;
use File::Copy;
#<li><s><a href="/wiki/File:
while ($content =~ m/\>File:([^<]+)</g)
{
my $n=$1;
print "looking at $n\n";
next if ($n =~ /\//); # skip slashes in the name
next if ($n =~ /\\/); # skip slashes in the name
next if ($n =~ /http:/); # skip urls
if (/<li><s><a href=\"\/wiki\/File:$n/)
{
print "Skip\n";
next;
}
if (/(<li.+File:$n)/)
{
print "Skip $n\n";
next;
}
my $f="images/$n";
if ( -f $f)
{
if ($f =~ /svg$/i)
{
print "Skipping\n";
}
else
{
$f =~ s/\s+/\ /g;
print "Uploading via python $f\n";
my $cmd = "python /home/mdupont/experiments/wikipedia/pywikipediabot/upload.py -noverify -keep -filename:\"$n\" \"$f\"";
print "$cmd\n";
my $ret = system "$cmd";
warn "ERROR:$ret" unless $ret ==0;
}
next;
}
print "check $n";
my $file = $mw2->download( { title => "File:$n" } )
|| warn "Cannot download $n " . $mw2->{error}->{code} . ': ' . $mw2->{error}->{details};
if ($file )
{
print "$url\n";
print $image;
print "got file \"$n\"\n";
my $newcontent = encode('utf-8', $file);
open OUT,">tmp1_${f}";
# binmode OUT, ':utf8';
# print OUT $newcontent;
# close OUT;
# warn "created tmp1_${f}";
############
open OUT,">tmp_${f}";
print OUT $file;
close OUT;
warn "created tmp_${f}";
############
# read it back in
open IN,"tmp_${f}";
binmode IN, ':utf8';
#print IN $file;
my $data="";
while (<IN>) {
$data .= $_;
}
close IN;
my $newcontent = encode('utf-8', $data);
print "check data " . length($newcontent);
if ($f =~ /svg$/i)
{
print "Skipping\n";
try {
$mw->upload( { title => "$n",
summary => '{{wikipedia-image|$n}}',
data => $newcontent } ) || warn "Failed upload :" . $mw->{error}->{code} . ': ' . $mw->{error}->{details};
move ("tmp_${f}","${f}");
}
catch {
warn "error! $_";
}
}
else
{
$f =~ s/\s+/\ /g;
print "Uploading via python $f\n";
my $cmd = "python /home/mdupont/experiments/wikipedia/pywikipediabot/upload.py -noverify -keep -filename:\"$n\" \"tmp_$f\"";
print "$cmd\n";
my $ret = system "$cmd";
warn "Failed $ret" unless $ret ==0;
move ("tmp_${f}","${f}");
}
}
else
{
warn "cannot download $f";
}
print "Done!\n";
}