-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsite.pl
executable file
·230 lines (189 loc) · 5.76 KB
/
website.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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Dancer;
use DBI;
set logger => 'console';
my $dbh = DBI->connect('dbi:SQLite:dbname=queue.db', '', '');
$dbh->do(qq{
CREATE TABLE IF NOT EXISTS queue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT NOT NULL,
size TEXT NOT NULL DEFAULT '1280x800',
type TEXT NOT NULL DEFAULT 'png',
proxy TEXT NOT NULL DEFAULT '',
xpath TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'pending'
);
});
get '/' => sub {
my $url = param('url') // '';
my $id = param('id') // 0;
my $size = param('size') // '1280x800';
my $proxy = param('proxy') // '';
my $xpath = param('xpath') // '';
my $select = $dbh->prepare("SELECT * FROM queue");
$select->execute();
my $out = qq{
<html>
<head><title>Screen capture queue</title></head>
<body>
<h1>Urls to download</h1>
<table>
<tr>
<th>Id</th>
<th>Url</th>
<th>Resolution</th>
<th>Extension</th>
<th>Proxy</th>
<th>XPath</th>
<th>Status</th>
<th>Action</th>
</tr>
};
while (my $row = $select->fetchrow_hashref) {
my $id_text = $row->{id};
$id_text = "<b>*</b> $id_text" if $row->{id} == $id;
my $status_text = $row->{status};
if ($status_text eq 'done') {
$status_text = qq{<a href="view?id=$row->{id}">$status_text</a>};
}
$out .= qq{
<tr>
<td>$id_text</td>
<td><a href="$row->{url}">$row->{url}</a></td>
<td>$row->{size}</td>
<td>$row->{type}</td>
<td>$row->{proxy}</td>
<td>$row->{xpath}</td>
<td>$status_text</a></td>
<td><a href="/delete?id=$row->{id}">Delete</a></td>
</tr>
};
}
$out .= qq{
</table>
<hr/>
<form action="/add">
<table>
<tr>
<th>Url:</th>
<td>
<input type="text" name="url" value="$url" size="80"/>
</td>
</tr>
<tr>
<th>Size:</th>
<td>
<input type="text" name="size" value="$size" size="10"/> (ex: 1280x800)
</td>
</tr>
<tr>
<th>Type:</th>
<td>
<input type="radio" name="type" id="png" value="png" checked/> <label for="png">PNG</label>
<input type="radio" name="type" id="pdf" value="pdf"/> <label for="pdf">PDF</label>
</td>
</tr>
<tr>
<th>Proxy:</th>
<td>
<input type="text" name="proxy" value="$proxy" size="80"/>
</td>
</tr>
<tr>
<th>XPath:</th>
<td>
<input type="text" name="xpath" value="$xpath" size="80"/>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add"/>
</tr>
</table>
</form>
</body>
</html>
};
return $out;
};
get '/add' => sub {
my $url = param('url') or return do_error("Missing parameter 'url'");
my $size = param('size') // '';
my $type = param('type') // 'png';
my $proxy = param('proxy') // '';
my $xpath = param('xpath') // '';
foreach my $val ($url, $size, $type, $proxy, $xpath) {
$val = trim($val);
}
$dbh->begin_work();
my $id = '';
eval {
$dbh->do(
"INSERT INTO queue (url, size, type, proxy, xpath) VALUES (?, ?, ?, ?, ?)",
undef,
$url, $size, $type, $proxy, $xpath,
);
$id = $dbh->sqlite_last_insert_rowid();
$dbh->commit();
1;
} or do {
my $error = $@ || "Internal error";
warn "Error: $error";
$dbh->rollback();
return send_error "Server error";
};
return redirect "/?id=$id";
};
get '/delete' => sub {
my $id = param('id') or return do_error("Missing parameter 'id'");
$dbh->begin_work();
eval {
$dbh->do("DELETE FROM queue WHERE id = ?", undef, $id);
$dbh->commit();
1;
} or do {
my $error = $@ || "Internal error";
warn "Error: $error";
$dbh->rollback();
return send_error "Server error";
};
return redirect "/";
};
my %MIME_TYPES = (
pdf => 'application/pdf',
png => 'image/png',
);
get '/view' => sub {
my $id = param('id') or return do_error("Missing parameter 'id'");
my $select = $dbh->prepare("SELECT type FROM queue WHERE id = ? LIMIT 1");
$select->execute($id);
while (my $row = $select->fetchrow_hashref) {
my $type = $row->{type} || 'png';
my $file = "captures/$id.$type";
my $mime_type = $MIME_TYPES{$type};
debug "Showing $file ($mime_type)";
return do_error("Can't find the file $file") unless -e $file;
return send_file $file, content_type => $mime_type, system_path => 1;
}
return do_error("Can't find screenshot with id: $id");
};
sub do_error {
my ($message) = @_;
return qq{
<html>
<body>
<h1>Error</h1>
<p>$message</p>
</body>
</html>
};
}
sub trim {
my ($string) = @_;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
dance();