]> git.sesse.net Git - pr0n/blob - perl/pr0n-upload.pl
Auto-create images/NN and cache/NN directories on demand.
[pr0n] / perl / pr0n-upload.pl
1 #! /usr/bin/perl
2
3 #
4 # Small multithreaded pr0n uploader, based partially on dave from HTTP::DAV.
5 # Use like
6 #
7 #   pr0n-upload.pl http://pr0n.sesse.net/webdav/upload/random/ *.JPG
8 #
9 # Adjust $threads to your own liking.
10 #
11
12 use strict;
13 use warnings;
14 use HTTP::DAV;
15 use threads;
16 use Thread::Queue;
17
18 my $threads = 16;
19 my $running_threads :shared = 0;
20 my $queue :shared = Thread::Queue->new;
21
22 # Enqueue all the images.
23 my $url = shift @ARGV;
24 $queue->enqueue(@ARGV);
25
26 # Fetch username and password, and check that they actually work.
27 my ($user, $pass) = get_credentials();
28 my $dav = init_dav($url, $user, $pass);
29
30 # Fire up the worker threads, and wait for them to finish.
31 my @threads = ();
32 for my $i (1..$threads) {
33         push @threads, threads->create(\&upload_thread);
34 }
35 while ($running_threads > 0) {
36         printf "%d threads running, %d images queued\n", $running_threads, $queue->pending;
37         sleep 1;
38 }
39 for my $thread (@threads) {
40         $thread->join();
41 }
42
43 sub upload_thread {
44         $running_threads++;
45
46         my $dav = init_dav($url, $user, $pass);
47         while (my $filename = $queue->dequeue_nb) {
48                 $dav->put(-local => $filename, -url => $url)
49                         or warn "Couldn't upload $filename: " . $dav->message . "\n";
50         }
51         
52         $running_threads--;
53 }
54
55 sub init_dav {
56         my ($url, $user, $pass) = @_;
57         my $ua = HTTP::DAV::UserAgent->new();
58         $ua->agent('pr0n-uploader/v1.0 (perldav)');
59         my $dav = HTTP::DAV->new(-useragent=>$ua);
60         $dav->credentials(-user=>$user, -pass=>$pass, -url=>$url, -realm=>'pr0n.sesse.net');
61         $dav->open(-url => $url)
62                 or die "Couldn't open $url: " . $dav->message . "\n";
63         return $dav;
64 }
65
66 sub get_credentials {
67         print "\nEnter username for $url: ";
68         chomp (my $user = <STDIN>);
69         exit if (!defined($user));
70         print "Password: ";
71         system("stty -echo");
72         chomp (my $pass = <STDIN>);
73         system("stty echo");
74         print "\n";
75
76         return ($user, $pass);
77 }