Thread Bilder Upload... (3 answers)
Opened by ohrlaeppchen at 2012-10-27 21:56

murphy
 2012-10-28 12:10
#162886 #162886
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
HTTP-Anfragen kann man z.B. mit CPAN:LWP oder CPAN:WWW::Curl absetzen, für die Analyse von Antworten im HTML-Format eignet sich z.B. CPAN:XML::LibXML.

Hier ein kleines Skript, das den fraglichen Webservice ansteuert:
more (31.3kb):
Code (perl): (dl )
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
#!/usr/bin/env perl
use 5.012;
use warnings;

use Getopt::Long;
Getopt::Long::Configure('bundling');
use Pod::Usage;

use WWW::Curl::Easy;
use WWW::Curl::Form;
use XML::LibXML;

use constant {
    UPLOAD_URL => 'http://www.directupload.net/index.php?mode=upload'
};

my $comment;
my @tags;
my $email;
my $infobar;
my @links = (0);
my $verbose = 1;
my $help = 0;
GetOptions(
    'comment|c=s' => \$comment,
    'tag|t=s'     => \@tags,
    'email|e=s'   => \$email,
    'infobar|i'   => \$infobar,
    'link|l=i'    => \@links,
    'verbose|v+'  => \$verbose,
    'help|h|?'    => \$help)
    or pod2usage(-exitstatus => 2);

if ($help) {
    pod2usage(-exitstatus => 0, -verbose => $verbose);
}
if (!@ARGV) {
    pod2usage(-exitstatus => 2);
}

my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_URL, UPLOAD_URL);

for my $file (@ARGV) {
    say "$file: Uploading..." if ($verbose > 1);
    $curl->setopt(CURLOPT_WRITEDATA, \my $response);

    my $upload = WWW::Curl::Form->new;
    if ($file =~ m(^[-+_a-z0-9]+://)i) {
        $upload->formadd(image_link => $file);
    }
    else {
        $upload->formaddfile($file, bilddatei => 'application/octet-stream');
    }
    if (@tags) {
        $upload->formadd(tag_able => 'tag_able');
        $upload->formadd(image_tags => join(" ", @tags));
    }
    $upload->formadd(image_comment => $comment) if ($comment);
    $upload->formadd(image_mail => $email) if ($email);
    $upload->formadd(showtext => 'checkbox') if ($infobar);

    $curl->setopt(CURLOPT_HTTPPOST, $upload);

    my $status = $curl->perform;
    if ($status != 0) {
        die sprintf('%s: Failed to upload: %s (%d) -- %s', $file, $curl->strerror($status), $status, $curl->errbuf);
    }

    $response = XML::LibXML->load_html(
        string => $response, URI => UPLOAD_URL, recover => 1,
        suppress_warnings => ($verbose > 2 ? 0 : 1), suppress_errors => ($verbose > 1 ? 0 : 1));
    if ($response) {
        my @fields = $response->findnodes('//input[@type = "text"]');
        say "$file: link($_): ", $fields[$_]->getAttribute('value') for (@links);
    }
    else {
        warn "$file: Upload seems to have succeeded, but the response page could not be parsed"
    }
}

say "Done" if ($verbose > 1);

__END__

=head1 NAME

directupload - Upload images to directupload.net

=head1 SYNOPSIS

directupload [OPTION ...] {FILE | URL} ...

=head1 OPTIONS

=over 4

=item B<--comment=STRING>

=item B<-c STRING>

Adds a comment to the uploaded images.

=item B<--tag=STRING>

=item B<-t STRING>

Adds a tag to the uploaded images and marks them as publically
searchable. This option can be specified multiple times, the tags are
joined by spaces.

=item B<--email=STRING>

=item B<-e STRING>

Specifies an e-mail address that should receive a notification about
the upload.

=item B<--infobar>

=item B<-i>

Indicates that an information bar should be displayed with the image.

=item B<--link INTEGER>

=item B<-l INTEGER>

Specifies an additional potential link to extract from the upload
response pages. The first potential link is always extracted.

See L</LINK NUMBERS> below for possible arguments.

=item B<--verbose>

=item B<-v>

Increases the verbosity of program output. Up to two instances of this
option currently make sense.

=item B<--help>

=item B<-h>

=item B<-?>

Shows documentation about the program. Combine with B<--verbose> to
view the entire manual page.

=back

=head1 DESCRIPTION

This program will read the given input files and send them to the
service at directupload.net. If an argument looks like an absolute URL
(ie. it starts with a scheme followed by "://"), a remote upload
operation is requested.

After each successful upload, information about the new location of
the image is printed to standard output.

Each file is associated with the same set of options, so you should
use separate invocations of the program if you need to set different
tags or comments for the uploaded images.

=head1 LINK NUMBERS

The program tries to extract display / download links to the uploaded
file from the response page received after a successful upload. It
does that by extracting all text-type input tags from the response
page and reading their value attributes. By default, only the first
such value is extracted, which is expected to point to the primary
image display page.

At the time of writing, other sensible link numbers you can specify
with the B<--link> option are:

=over 4

=item 1

BBCode markup for a preview image linking to the image display page.

=item 2

HTML markup for a preview image linking to the image display page.

=item 3

BBCode markup to embed the image.

=item 4

HTML markup to embed the image.

=item 5

Direct URL of the image file.

=back

=head1 LICENSE

Copyright (c) 2012 by Thomas Chust L<mailto:chust@web.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut
When C++ is your hammer, every problem looks like your thumb.

View full thread Bilder Upload...