# $Id: UploadingCom 11668 2010-07-13 14:04:38Z sparky $
# Get::UploadingCom - File getter plugin for rsget.pl
#
# 2009-2010 (c) Przemysław Iskra <sparky@pld-linux.org>
#		This program is free software,
# you may distribute it under GPL v2 or newer.

name: UploadingCom
short: UC
web: "http://uploading.com/"
tos: "http://uploading.com/terms/"
uri: qr{uploading\.com/(../)?files/(get/)?[A-Za-z0-9]{8}}
cookie: !uc
slots: !1
status: OK 2010-07-13

unify:
	return "http://uploading.com/files/$1/"
		if m{uploading\.com/(?:../)?files/(?:get/)?([A-Za-z0-9]{8})/?};

start:
	COOKIE( [ (qw(.uploading.com TRUE / FALSE), time() + 43200, 'lang', 1 ) ] );

	GET( $-{_uri} );

	RESTART( -irand( 60, 300 ), "free limit reached: $1" )
		if /(Sorry, you can download only one file per \d+ minutes)/;
	RESTART( -irand( 30, 120 ), "temporarily unavailable" )
		if m{<h1>Service Not Available</h1>};
	ERROR( "file not found" ) if />The requested file is not found</;
	MULTI() if />Parallel Download</;

	! m{.*<h2>(.+?)</h2><br/>\s*File size: <b>($STDSIZE)</b>}so;
	my $name = $1; my $size = $2;
	if ( $name =~ /\.\..{1,5}$/ ) {
		! m{<title>Download (.+?) for free on uploading\.com</title>};
		$name = $1;
	}
	INFO( name => $name, asize => $size );

	my $form = $self->form( id => "downloadform" );
	! $-{file_id} = $form->get( "file_id" );
	! $-{file_code} = $form->get( "code" );

	CLICK( $form->post() );

	if ( m{var file_link = '(.+?)'} ) {
		$-{file_uri} = $1;
		GOTO stage_download;
	}
	! /CONF\s*=\s*{\s*host:\s*'(.*?)'/;
	$-{host} = $1;

	! m{timer_count\s*=\s*(\d+);};
	WAIT( -$1, "expecting download link" );

	GET( $-{host} . "files/get/?JsHttpRequest=" . jstime() . "-xml",
		post => "action=get_link&file_id=$-{file_id}&code=$-{file_code}&pass=",
		headers => [ "Content-Type: application/octet-stream" ],
		keep_referer => 1,
	);
stage_download:

	unless ( $-{file_uri} ) {
		! m{"link"\s*:\s*"(.*?)"};
		$-{file_uri} = $1;
		$-{file_uri} =~ s/\\(.)/$1/g;
	}

	CLICK_DOWNLOAD( $-{file_uri} );

	MULTI() if /Your IP address is currently downloading a file/;

# vim: filetype=perl:ts=4:sw=4
