File Coverage

lib/Mediafire/Api/DownloadFile.pm
Criterion Covered Total %
statement 35 51 68.6
branch 0 2 0.0
condition 0 18 0.0
subroutine 12 14 85.7
pod 0 2 0.0
total 47 87 54.0


line stmt bran cond sub pod time code
1             package Mediafire::Api::DownloadFile;
2              
3 1     1   14 use 5.008001;
  1         4  
4 1     1   4 use utf8;
  1         2  
  1         5  
5 1     1   18 use strict;
  1         2  
  1         24  
6 1     1   5 use warnings;
  1         7  
  1         34  
7 1     1   5 use open qw(:std :utf8);
  1         1  
  1         6  
8 1     1   129 use Carp qw/croak carp/;
  1         1  
  1         51  
9 1     1   5 use URI::Escape;
  1         2  
  1         51  
10 1     1   6 use LWP::UserAgent;
  1         2  
  1         17  
11 1     1   4 use File::Basename;
  1         2  
  1         55  
12 1     1   5 use HTTP::Request;
  1         2  
  1         24  
13 1     1   4 use JSON::XS;
  1         1  
  1         37  
14              
15 1     1   4 use Mediafire::Api::File;
  1         1  
  1         540  
16              
17             our $VERSION = '0.01';
18              
19             my $DEFAULT_BUFF_SIZE = 1048576;
20              
21             ######################### PRIVATE METHODS ##################################################
22             my ($getDonwloadLink, $download);
23              
24             $getDonwloadLink = sub {
25             my ($self, $url) = @_;
26              
27             my %headers = (
28             'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
29             'Accept-Encoding' => 'gzip, deflate',
30             'Accept-Language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
31             'Cache-Control' => 'max-age=0',
32             'Upgrade-Insecure-Requests' => '1',
33             );
34              
35             my $res = $self->{ua}->get($url, %headers);
36             my $code = $res->code;
37             if ($code ne '200') {
38             croak "Wrong response code on request to '$url'. Code: $code";
39             }
40             # Find div with download link
41             my $content = $res->decoded_content;
42             if ($content =~ /]+id="download_link".+]+aria-label="Download file"[^>]*href="(.+?)".*<\/div>/s) {
43             return $1;
44             }
45             else {
46             "Can't found download link";
47             }
48              
49             };
50              
51             $download = sub {
52             my ($self, $download_url, $dest_file) = @_;
53              
54             my %headers = (
55             ':content_file' => $dest_file,
56             ':read_size_hint' => $self->{buff_size},
57             'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
58             'Accept-Encoding' => 'gzip, deflate',
59             'Accept-Language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
60             'Upgrade-Insecure-Requests' => '1',
61             );
62              
63             my $res = $self->{ua}->get($download_url, %headers);
64             my $code = $res->code;
65             if ($code ne '200') {
66             croak "Can't download file by url '$download_url' to file '$dest_file'. Code: $code";
67             }
68             return 1;
69              
70             };
71              
72             sub new {
73 0     0 0   my ($class, %opt) = @_;
74 0           my $self = {};
75 0   0       $self->{ua} = $opt{-ua} // croak "You must specify param '-ua' for method new";
76 0   0       $self->{buff_size} = $opt{-buff_size} // $DEFAULT_BUFF_SIZE;
77 0           bless $self, $class;
78 0           return $self;
79             }
80              
81             sub downloadFile {
82 0     0 0   my ($self, %opt) = @_;
83 0   0       my $mediafire_file = $opt{-mediafire_file} // croak "You must specify '-mediafire_file' param";
84 0   0       my $dest_file = $opt{-dest_file} // croak "You must specify '-dest_file' param";
85              
86 0 0         if (ref($mediafire_file) ne 'Mediafire::Api::File') {
87 0           croak "Param '-mediafire_file' must be Mediafire::Api::File object";
88             }
89              
90 0   0       my $file_name = $mediafire_file->name // croak "Can't get file name of downloaded file";
91 0   0       my $file_key = $mediafire_file->key // croak "Can't get key from upload file '" . $mediafire_file->name . "'";
92 0           my $download_page_url = 'http://www.mediafire.com/file/' . $file_key . '/' . $file_name . '/file';
93              
94 0           my $download_link = $self->$getDonwloadLink($download_page_url);
95              
96 0           $self->$download($download_link, $dest_file);
97              
98             }
99              
100              
101              
102             1;