forked from thewade/hrmb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routemarker.pl
202 lines (154 loc) · 5.86 KB
/
routemarker.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
#! usr/bin/perl
use warnings;
use strict;
use MediaWiki::API;
use Term::ReadKey;
# Configuration
# ===================
my $replaceme = '%num%'; # replacement string, default %num%
my $scourpath = './scour/scour.py'; # where Scour CLS is located
my $textID = 'routenum'; # object ID of the text to be converted
my $delay = 10; # delay between uploading images
my $autoupload = 0; # prompts for input between uploads (0=false, 1=true)
my $username = "Highway Route Marker Bot"; # uploaders Mediawiki username
my $apiurl = 'https://commons.wikimedia.org/w/api.php'; # Mediawiki API location
my $uploadurl = 'https://commons.wikimedia.org/w/index.php?title=Special:Upload&uploadformstyle=basic'; # Mediawiki upload location
# Variables
# ===================
# Arguments
my $svgfilename = $ARGV[0]; # the SVG filename
my $sequencefile = $ARGV[1]; # the sequence filename
my $textfilename = $ARGV[2]; # the text filename
# SVG datafiles
my $basesvg; # the SVG file
my $outfile; # the new SVG file
# Other
my $sequencetxt; # sequence values
my @sequence; # array of sequence values
my $wikitext; # the un-sequenced text
my $outfilename; # the sequenced filename
my %ref_table; # hashtable that contains filename and file comment
my $password; # uploaders Mediawiki password
my $starttime; # used to speed limit the bot
my $sleeptime; # used to speed limit the bot
# Load the files, do some checks
# ===================
# Open the SVG file
open ( FILE, $svgfilename ) || die ("you die now!");
$basesvg= do{local $/; <FILE>;};
close (FILE);
# Check for the replacement string in filename
if ($svgfilename =~ m/$replaceme/) {
print "basesvg includes the string $replaceme\n";
}
else {
die ("The basesvg file must include the string $replaceme for sequencing. Program failed");
}
my $svgfilename_esc = quotemeta($svgfilename);
# Check for text with the proper object ID in SVG
if (`inkscape $svgfilename_esc --query-id=$textID --query-x`) {
print "Object $textID found.\n";
}
else {
die ("ERROR: Text with object ID $textID not found, please check the SVG file $svgfilename. Program failed");
}
# Open the sequencing file
open ( FILE, $sequencefile ) || die ("you die now!");
while (<FILE>){
$sequencetxt .= $_;
}
close FILE;
@sequence = split(',', $sequencetxt);
# Open the Wikitext file
open ( FILE, $textfilename ) || die ("you die now!");
$wikitext= do{local $/; <FILE>;};
close (FILE);
# Loop through the sequence
# ===================
foreach (@sequence) {
my($line) = $_;
chomp($line);
$line =~ s/^\s+|\s+$//g;
# Create SVG
# =========================
(my $outfile = $basesvg) =~ s/$replaceme/$line/g;
(my $outfilename = $svgfilename) =~ s/$replaceme/$line/;
# Hacking in some code to ignore already created files, this behavoir really should be user defined
unless (-e $outfilename) {
print "\nCreating $outfilename \n";
open ( FILE, ">$outfilename" ) || die ("you die now!");
print FILE $outfile;
close (FILE);
# Text to Path & Optimize
# =========================
# You cannot use Inkscape verbs in the shell mode or without the GUI, so for now Inkscape will open and close as it does it's work
print "Converting text-to-path...";
system("inkscape", "$outfilename", "--select=$textID", "--verb=ObjectToPath;FileSave;FileClose;FileQuit", "--with-gui");
# Clean SVG with Scour
if ($scourpath) {
print "creating optimized SVG with Scour...\n\n";
system("python", $scourpath, "-i", $outfilename, "-o", "temp_" . $outfilename);
unlink ($outfilename);
rename ("temp_$outfilename", $outfilename);
}
# Fixing SVG in Inkscape
print "\nResave file with Inkscape...";
system("inkscape", $outfilename ,"--export-plain-svg=" . $outfilename);
}
# Generate Wiki text
# =======================================
print "Generating Wikitext...\n";
(my $outtext = $wikitext) =~ s/$replaceme/$line/g;
$ref_table{$outfilename} = $outtext;
}
print "\nPlease review all images before uploading, press Y to proceed...\n";
ReadMode "cbreak";
$_ = ReadKey();
ReadMode "normal";
if(lc $_ eq 'y') {
# Get user info
if (!$username) {
print "\nPlease enter your username: ";
$username = <STDIN>;
chomp($username);
}
print "$username, please enter your password: ";
$password = <STDIN>;
chomp($password);
# Upload to Mediawiki
# ==================================
# Config
my $mw = MediaWiki::API->new( { api_url => $apiurl } );
$mw->{config}->{upload_url} = $uploadurl;
$mw->login( { lgname => $username, lgpassword => $password } )
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# Cycle through images
foreach my $key (sort keys %ref_table) {
$outfilename = $key;
$wikitext = $ref_table{$key};
open ( FILE, $outfilename ) || die ("you die now!");
my $outfile= do{local $/; <FILE>;};
close (FILE);
# Upload
print ("Uploading...\n");
$starttime = time;
$mw->upload( { title => $outfilename,
summary => $wikitext,
data => $outfile } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
print ("Uploaded: http://commons.wikimedia.org/wiki/File%3A$outfilename\n");
if (!$autoupload) {
print ("Please verify that images has uploaded.\n Press 'y' if the upload was good.\n Press 'a' to abort.\n Press 's' to skip this check\n");
ReadMode "cbreak";
$_ = ReadKey();
ReadMode "normal";
if(lc $_ eq 'a') { die ("you die now!"); }
if(lc $_ eq 's') { $autoupload = 1; }
}
# Bot is speed limited due to http://commons.wikimedia.org/wiki/Commons:Bot#Bot_speed
if ($starttime + $delay > time) {
$sleeptime = $delay - (time - $starttime);
print "Need to wait $sleeptime second(s)\n";
sleep($sleeptime);
}
}
}