mirror of
https://github.com/vrag86/Mediafire-Api
synced 2026-01-13 13:24:28 -08:00
Added dowmloadFile() method
This commit is contained in:
Binary file not shown.
@@ -13,6 +13,7 @@ use HTTP::Request;
|
|||||||
use JSON::XS;
|
use JSON::XS;
|
||||||
|
|
||||||
use Mediafire::Api::UploadFile;
|
use Mediafire::Api::UploadFile;
|
||||||
|
use Mediafire::Api::DownloadFile;
|
||||||
|
|
||||||
use Data::Printer;
|
use Data::Printer;
|
||||||
|
|
||||||
@@ -234,6 +235,17 @@ sub findFileByName {
|
|||||||
return \@mediafire_files;
|
return \@mediafire_files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub downloadFile {
|
||||||
|
my ($self, %opt) = @_;
|
||||||
|
|
||||||
|
my $download_file = Mediafire::Api::DownloadFile->new(
|
||||||
|
-ua => $self->{ua},
|
||||||
|
);
|
||||||
|
$download_file->downloadFile(%opt);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
=pod Вынести в отдельный класс с проверкой наличия такой директории
|
=pod Вынести в отдельный класс с проверкой наличия такой директории
|
||||||
sub createDir {
|
sub createDir {
|
||||||
my ($self, %opt) = @_;
|
my ($self, %opt) = @_;
|
||||||
@@ -300,6 +312,14 @@ B<Mediafire::Api> - Upload and Download files from mediafire.com file sharing
|
|||||||
# Get uploaded file key
|
# Get uploaded file key
|
||||||
print "Uploaded file key: " . $mediafire_file->getDouploadKey() . "\n";
|
print "Uploaded file key: " . $mediafire_file->getDouploadKey() . "\n";
|
||||||
|
|
||||||
|
# Find file on mediafire.com by name. Return arrayref to Mediafire::Api::File objects
|
||||||
|
my $find_result = $mediafire->findFileByName(
|
||||||
|
-filename => 'file_to_find.txt',
|
||||||
|
);
|
||||||
|
if (@$find_result) {
|
||||||
|
print "Found files: " . join(' ', map {$_->name()} @$find_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
=head1 Upload Files to server
|
=head1 Upload Files to server
|
||||||
@@ -309,6 +329,44 @@ B<Mediafire::Api> - Upload and Download files from mediafire.com file sharing
|
|||||||
|
|
||||||
=head2 login(%opt)
|
=head2 login(%opt)
|
||||||
|
|
||||||
|
=head1 Mediafire::Api::File
|
||||||
|
|
||||||
|
=head2 name
|
||||||
|
|
||||||
|
Set/Get name of file
|
||||||
|
$mediafire_file->name("New name");
|
||||||
|
my $name = $mediafire->name;
|
||||||
|
|
||||||
|
=head2 key
|
||||||
|
|
||||||
|
Set/Get download key of file
|
||||||
|
|
||||||
|
$mediafire_file->key("downloadfilekey");
|
||||||
|
my $key = $mediafire_file->key;
|
||||||
|
|
||||||
|
=head2 size
|
||||||
|
|
||||||
|
Set/Get size of file
|
||||||
|
|
||||||
|
$mediafire->size(2343);
|
||||||
|
my $size = $mediafire->size;
|
||||||
|
|
||||||
|
=head2 hash
|
||||||
|
|
||||||
|
Set/Get sha256sum hashsum of file
|
||||||
|
|
||||||
|
$mediafire_file->hash('dffdf');
|
||||||
|
my $hash = $mediafire_file->hash;
|
||||||
|
|
||||||
|
=head1 Find files on mediafire.com
|
||||||
|
|
||||||
|
=head2 findFileByName(%opt)
|
||||||
|
|
||||||
|
Return arrayref with Mediafire::Api::file objects
|
||||||
|
|
||||||
|
%opt:
|
||||||
|
-filename => Name of file to find
|
||||||
|
|
||||||
=head1 DEPENDENCE
|
=head1 DEPENDENCE
|
||||||
|
|
||||||
L<LWP::UserAgent>, L<JSON::XS>, L<URI::Escape>, L<Encode>, L<HTTP::Request>, L<Carp>, L<File::Basename>, L<MIME::Detect>
|
L<LWP::UserAgent>, L<JSON::XS>, L<URI::Escape>, L<Encode>, L<HTTP::Request>, L<Carp>, L<File::Basename>, L<MIME::Detect>
|
||||||
|
|||||||
Binary file not shown.
102
lib/Mediafire/Api/DownloadFile.pm
Normal file
102
lib/Mediafire/Api/DownloadFile.pm
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package Mediafire::Api::DownloadFile;
|
||||||
|
|
||||||
|
use 5.008001;
|
||||||
|
use utf8;
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use open qw(:std :utf8);
|
||||||
|
use Carp qw/croak carp/;
|
||||||
|
use URI::Escape;
|
||||||
|
use LWP::UserAgent;
|
||||||
|
use File::Basename;
|
||||||
|
use HTTP::Request;
|
||||||
|
use JSON::XS;
|
||||||
|
|
||||||
|
use Mediafire::Api::File;
|
||||||
|
|
||||||
|
our $VERSION = '0.01';
|
||||||
|
|
||||||
|
my $DEFAULT_BUFF_SIZE = 1048576;
|
||||||
|
|
||||||
|
######################### PRIVATE METHODS ##################################################
|
||||||
|
my ($getDonwloadLink, $download);
|
||||||
|
|
||||||
|
$getDonwloadLink = sub {
|
||||||
|
my ($self, $url) = @_;
|
||||||
|
|
||||||
|
my %headers = (
|
||||||
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
||||||
|
'Accept-Encoding' => 'gzip, deflate',
|
||||||
|
'Accept-Language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
|
||||||
|
'Cache-Control' => 'max-age=0',
|
||||||
|
'Upgrade-Insecure-Requests' => '1',
|
||||||
|
);
|
||||||
|
|
||||||
|
my $res = $self->{ua}->get($url, %headers);
|
||||||
|
my $code = $res->code;
|
||||||
|
if ($code ne '200') {
|
||||||
|
croak "Wrong response code on request to '$url'. Code: $code";
|
||||||
|
}
|
||||||
|
# Find div with download link
|
||||||
|
my $content = $res->decoded_content;
|
||||||
|
if ($content =~ /<div[^>]+id="download_link".+<a[^>]+aria-label="Download file"[^>]*href="(.+?)".*<\/div>/s) {
|
||||||
|
return $1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
"Can't found download link";
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
$download = sub {
|
||||||
|
my ($self, $download_url, $dest_file) = @_;
|
||||||
|
|
||||||
|
my %headers = (
|
||||||
|
':content_file' => $dest_file,
|
||||||
|
':read_size_hint' => $self->{buff_size},
|
||||||
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
||||||
|
'Accept-Encoding' => 'gzip, deflate',
|
||||||
|
'Accept-Language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
|
||||||
|
'Upgrade-Insecure-Requests' => '1',
|
||||||
|
);
|
||||||
|
|
||||||
|
my $res = $self->{ua}->get($download_url, %headers);
|
||||||
|
my $code = $res->code;
|
||||||
|
if ($code ne '200') {
|
||||||
|
croak "Can't download file by url '$download_url' to file '$dest_file'. Code: $code";
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
sub new {
|
||||||
|
my ($class, %opt) = @_;
|
||||||
|
my $self = {};
|
||||||
|
$self->{ua} = $opt{-ua} // croak "You must specify param '-ua' for method new";
|
||||||
|
$self->{buff_size} = $opt{-buff_size} // $DEFAULT_BUFF_SIZE;
|
||||||
|
bless $self, $class;
|
||||||
|
return $self;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub downloadFile {
|
||||||
|
my ($self, %opt) = @_;
|
||||||
|
my $mediafire_file = $opt{-mediafire_file} // croak "You must specify '-mediafire_file' param";
|
||||||
|
my $dest_file = $opt{-dest_file} // croak "You must specify '-dest_file' param";
|
||||||
|
|
||||||
|
if (ref($mediafire_file) ne 'Mediafire::Api::File') {
|
||||||
|
croak "Param '-mediafire_file' must be Mediafire::Api::File object";
|
||||||
|
}
|
||||||
|
|
||||||
|
my $file_name = $mediafire_file->name // croak "Can't get file name of downloaded file";
|
||||||
|
my $file_key = $mediafire_file->key // croak "Can't get key from upload file '" . $mediafire_file->name . "'";
|
||||||
|
my $download_page_url = 'http://www.mediafire.com/file/' . $file_key . '/' . $file_name . '/file';
|
||||||
|
|
||||||
|
my $download_link = $self->$getDonwloadLink($download_page_url);
|
||||||
|
|
||||||
|
$self->$download($download_link, $dest_file);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
1;
|
||||||
@@ -8,22 +8,17 @@ use open qw(:std :utf8);
|
|||||||
use Carp qw/croak carp/;
|
use Carp qw/croak carp/;
|
||||||
use URI::Escape;
|
use URI::Escape;
|
||||||
use LWP::UserAgent;
|
use LWP::UserAgent;
|
||||||
use LWP::ConnCache;
|
|
||||||
use File::Basename;
|
use File::Basename;
|
||||||
use HTTP::Request;
|
use HTTP::Request;
|
||||||
use JSON::XS;
|
use JSON::XS;
|
||||||
use MIME::Detect;
|
use MIME::Detect;
|
||||||
use Crypt::Digest::SHA256 qw/sha256_hex/;
|
use Crypt::Digest::SHA256 qw/sha256_hex/;
|
||||||
use Time::HiRes qw/gettimeofday/;
|
use Time::HiRes qw/gettimeofday/;
|
||||||
use IO::Socket::SSL;
|
|
||||||
|
|
||||||
use Mediafire::Api::File;
|
use Mediafire::Api::File;
|
||||||
|
|
||||||
use Data::Printer;
|
|
||||||
|
|
||||||
our $VERSION = '0.01';
|
our $VERSION = '0.01';
|
||||||
|
|
||||||
|
|
||||||
my $DEFAULT_BUFF_SIZE = 1048576;
|
my $DEFAULT_BUFF_SIZE = 1048576;
|
||||||
|
|
||||||
############################ PRIVATE METHODS ############################################
|
############################ PRIVATE METHODS ############################################
|
||||||
@@ -82,8 +77,6 @@ $checkUploadFile = sub {
|
|||||||
croak "Can't checkUploadFile. Storage limit exceeded";
|
croak "Can't checkUploadFile. Storage limit exceeded";
|
||||||
}
|
}
|
||||||
|
|
||||||
p $response;
|
|
||||||
|
|
||||||
my $file_key = $response->{preemptive_quickkey} // $response->{duplicate_quickkey};
|
my $file_key = $response->{preemptive_quickkey} // $response->{duplicate_quickkey};
|
||||||
$self->{file}->key($file_key);
|
$self->{file}->key($file_key);
|
||||||
$self->{upload_url} = $response->{upload_url}->{resumable};
|
$self->{upload_url} = $response->{upload_url}->{resumable};
|
||||||
@@ -256,7 +249,7 @@ sub new {
|
|||||||
my $self = {};
|
my $self = {};
|
||||||
$self->{ua} = $opt{-ua} // croak "You must specify param '-ua' for method new";
|
$self->{ua} = $opt{-ua} // croak "You must specify param '-ua' for method new";
|
||||||
$self->{session_token} = $opt{-session_token} // croak "You must specify '-session_token' param";
|
$self->{session_token} = $opt{-session_token} // croak "You must specify '-session_token' param";
|
||||||
$self->{buff_size} = $opt{-buff_zize} // $DEFAULT_BUFF_SIZE;
|
$self->{buff_size} = $opt{-buff_size} // $DEFAULT_BUFF_SIZE;
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|||||||
17
t/001_test.t
17
t/001_test.t
@@ -35,6 +35,7 @@ use_ok('Mediafire::Api');
|
|||||||
my $LOGIN = $ENV{MEDIAFIRE_LOGIN};
|
my $LOGIN = $ENV{MEDIAFIRE_LOGIN};
|
||||||
my $PASSWORD = $ENV{MEDIAFIRE_PASSWORD};
|
my $PASSWORD = $ENV{MEDIAFIRE_PASSWORD};
|
||||||
my $UPLOAD_FILE = File::Spec->catfile($CURR_DIR, 't', 'test_upload3.f');
|
my $UPLOAD_FILE = File::Spec->catfile($CURR_DIR, 't', 'test_upload3.f');
|
||||||
|
my $DEST_DOWNLOAD_FILE = File::Spec->catfile($CURR_DIR, 't', 'downloaded_test_upload3.f');
|
||||||
|
|
||||||
|
|
||||||
SKIP: {
|
SKIP: {
|
||||||
@@ -53,8 +54,9 @@ SKIP: {
|
|||||||
skip $@;
|
skip $@;
|
||||||
}
|
}
|
||||||
|
|
||||||
testUploadFile($mediafire, $UPLOAD_FILE);
|
my $mediafire_file = testUploadFile($mediafire, $UPLOAD_FILE);
|
||||||
testFindFileByName($mediafire, basename($UPLOAD_FILE));
|
testFindFileByName($mediafire, basename($UPLOAD_FILE));
|
||||||
|
testDownloadFile($mediafire, $mediafire_file, $DEST_DOWNLOAD_FILE);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -80,6 +82,7 @@ sub testUploadFile {
|
|||||||
|
|
||||||
my $doupload_key = $upload_file->key;
|
my $doupload_key = $upload_file->key;
|
||||||
ok($doupload_key, "Test upload file. DouploadKey: $doupload_key");
|
ok($doupload_key, "Test upload file. DouploadKey: $doupload_key");
|
||||||
|
return $upload_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub testFindFileByName {
|
sub testFindFileByName {
|
||||||
@@ -94,4 +97,16 @@ sub testFindFileByName {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub testDownloadFile {
|
||||||
|
my ($mediafire, $mediafire_file, $dest_file) = @_;
|
||||||
|
|
||||||
|
unlink($dest_file);
|
||||||
|
$mediafire->downloadFile(
|
||||||
|
-mediafire_file => $mediafire_file,
|
||||||
|
-dest_file => $dest_file,
|
||||||
|
);
|
||||||
|
|
||||||
|
ok (-f $dest_file, 'Test downloadFile()');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user