#!/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 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 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 . =cut