line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mediafire::Api; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
574
|
use 5.008001; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
19
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
14
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
7
|
1
|
|
|
1
|
|
411
|
use open qw(:std :utf8); |
|
1
|
|
|
|
|
953
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
116
|
use Carp qw/croak carp/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
9
|
1
|
|
|
1
|
|
358
|
use URI::Escape; |
|
1
|
|
|
|
|
1192
|
|
|
1
|
|
|
|
|
47
|
|
10
|
1
|
|
|
1
|
|
540
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
35907
|
|
|
1
|
|
|
|
|
33
|
|
11
|
1
|
|
|
1
|
|
10
|
use File::Basename; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
12
|
1
|
|
|
1
|
|
5
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
13
|
1
|
|
|
1
|
|
593
|
use JSON::XS; |
|
1
|
|
|
|
|
2556
|
|
|
1
|
|
|
|
|
44
|
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
387
|
use Mediafire::Api::UploadFile; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
16
|
1
|
|
|
1
|
|
297
|
use Mediafire::Api::DownloadFile; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1373
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
############################ PRIVATE METHODS ############################################ |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $openMainPage = sub { |
23
|
|
|
|
|
|
|
# Open main page for set cookies |
24
|
|
|
|
|
|
|
my ($self) = @_; |
25
|
|
|
|
|
|
|
my $url = 'https://www.mediafire.com'; |
26
|
|
|
|
|
|
|
my $res = $self->{ua}->get($url); |
27
|
|
|
|
|
|
|
my $code = $res->code; |
28
|
|
|
|
|
|
|
if ($code ne '200') { |
29
|
|
|
|
|
|
|
croak "Wrong response code from url: '$url'. Code: $code"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
return 1; |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $getSessionToken = sub { |
35
|
|
|
|
|
|
|
my ($self) = @_; |
36
|
|
|
|
|
|
|
my $url = 'https://www.mediafire.com/apps/myfiles/?shared=0&multikeys=0'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my %headers = ( |
39
|
|
|
|
|
|
|
'referer' => 'https://www.mediafire.com/', |
40
|
|
|
|
|
|
|
'upgrade-insecure-requests' => '1', |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $res = $self->{ua}->get($url, %headers); |
44
|
|
|
|
|
|
|
my $code = $res->code; |
45
|
|
|
|
|
|
|
if ($code ne '200') { |
46
|
|
|
|
|
|
|
croak "Can't get session_token by url: '$url'. Code: $code"; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
my $body = $res->decoded_content; |
49
|
|
|
|
|
|
|
if ($body =~ /token=(.+?)['&"]/) { |
50
|
|
|
|
|
|
|
return $1; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
croak "Can't found session_token from response. Url: '$url'"; |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $getLoginSecurityValue = sub { |
56
|
|
|
|
|
|
|
my ($self) = @_; |
57
|
|
|
|
|
|
|
my $ua = $self->{ua}; |
58
|
|
|
|
|
|
|
my $url = 'https://www.mediafire.com/login/'; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $res = $ua->get($url); |
61
|
|
|
|
|
|
|
my $code = $res->code; |
62
|
|
|
|
|
|
|
if ($code ne '200') { |
63
|
|
|
|
|
|
|
croak "Can't get login_security_value. Code: $code"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
if ($res->decoded_content =~ /<(input [^<>]*name="security".+?)>/) { |
66
|
|
|
|
|
|
|
my $tag = $1; |
67
|
|
|
|
|
|
|
if ($tag =~ /value="(.+?)"/) { |
68
|
|
|
|
|
|
|
return $1; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
croak "Can't find tag with name 'securyty' or can't get value of tag"; |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
######################################################################################## |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub new { |
77
|
0
|
|
|
0
|
1
|
|
my ($class) = @_; |
78
|
0
|
|
|
|
|
|
my $self = {}; |
79
|
0
|
|
|
|
|
|
bless $self, $class; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
$self->{ua} = LWP::UserAgent->new ( |
82
|
|
|
|
|
|
|
agent => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36', |
83
|
|
|
|
|
|
|
cookie_jar => {}, |
84
|
|
|
|
|
|
|
); |
85
|
0
|
|
|
|
|
|
$self->{ua}->default_header('accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'); |
86
|
0
|
|
|
|
|
|
$self->{ua}->default_header('accept-encoding' => 'gzip, deflate, br'); |
87
|
0
|
|
|
|
|
|
$self->{ua}->default_header('accept-language' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $self; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub login { |
93
|
0
|
|
|
0
|
1
|
|
my ($self, %opt) = @_; |
94
|
0
|
|
0
|
|
|
|
$self->{login} = $opt{-login} // croak "You must specify '-login' param"; |
95
|
0
|
|
0
|
|
|
|
$self->{password} = $opt{-password} // croak "You must specify '-password' param"; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
$self->$openMainPage(); |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
my $security_value = $self->$getLoginSecurityValue(); |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my ($res, $code); |
102
|
0
|
|
|
|
|
|
my $ua = $self->{ua}; |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $url = 'https://www.mediafire.com/dynamic/client_login/mediafire.php'; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my %param = ( |
107
|
|
|
|
|
|
|
security => $security_value, |
108
|
|
|
|
|
|
|
login_email => $self->{login}, |
109
|
|
|
|
|
|
|
login_pass => $self->{password}, |
110
|
0
|
|
|
|
|
|
login_remember => 'on', |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my %headers = ( |
114
|
|
|
|
|
|
|
'accept-language' => 'ru,en-US;q=0.9,en;q=0.8', |
115
|
|
|
|
|
|
|
'cache-control' => 'max-age=0', |
116
|
|
|
|
|
|
|
'content-type' => 'application/x-www-form-urlencoded', |
117
|
|
|
|
|
|
|
'origin' => 'https://www.mediafire.com', |
118
|
|
|
|
|
|
|
'referer' => 'https://www.mediafire.com/login/', |
119
|
|
|
|
|
|
|
'upgrade-insecure-requests' => '1', |
120
|
|
|
|
|
|
|
); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
$res = $ua->post($url, \%param, %headers); |
123
|
0
|
|
|
|
|
|
$code = $res->code; |
124
|
0
|
0
|
|
|
|
|
if ($code ne '200') { |
125
|
0
|
|
|
|
|
|
croak "Wrong response code on login url: '$url'. Code: $code"; |
126
|
|
|
|
|
|
|
} |
127
|
0
|
|
|
|
|
|
my $cookie = $res->header('set-cookie'); |
128
|
|
|
|
|
|
|
# If logged in, server set 'session' cookie |
129
|
0
|
0
|
|
|
|
|
if (not $cookie =~ /session=/) { |
130
|
0
|
|
|
|
|
|
croak "Can't login to mediafire.com"; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$self->renewSessionToken(); |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub renewSessionToken { |
139
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
140
|
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
|
if (not $self->{session_token}) { |
142
|
0
|
|
|
|
|
|
$self->{session_token} = $self->$getSessionToken(); |
143
|
0
|
|
|
|
|
|
return 1; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my %headers = ( |
147
|
|
|
|
|
|
|
'referer' => 'https://www.mediafire.com/widgets/notifications.php?event_load=1', |
148
|
|
|
|
|
|
|
'x-requested-with' => 'XMLHttpRequest', |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $url = 'https://www.mediafire.com/api/1.4/user/renew_session_token.php?r=jrko&session_token=' . $self->{session_token} . '&response_format=json'; |
152
|
0
|
|
|
|
|
|
my $res = $self->{ua}->get($url, %headers); |
153
|
0
|
|
|
|
|
|
my $code = $res->code; |
154
|
0
|
0
|
|
|
|
|
if ($code ne '200') { |
155
|
0
|
|
|
|
|
|
croak "Can't renew session token by url: '$url'. Code: $code"; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $json_res = eval { |
159
|
0
|
|
|
|
|
|
decode_json($res->decoded_content); |
160
|
|
|
|
|
|
|
}; |
161
|
0
|
0
|
|
|
|
|
if ($@) { |
162
|
0
|
|
|
|
|
|
croak "Can't decode response to json: $res->decoded_content"; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
my $response_result = $json_res->{response}->{result}; |
166
|
0
|
0
|
|
|
|
|
if ($response_result eq 'Success') { |
167
|
0
|
|
|
|
|
|
$self->{session_token} = $json_res->{response}->{session_token}; |
168
|
0
|
|
|
|
|
|
return 1; |
169
|
|
|
|
|
|
|
} |
170
|
0
|
|
|
|
|
|
croak "Wrong result from response for renewSessionToken. Result: $response_result"; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub uploadFile { |
174
|
0
|
|
|
0
|
0
|
|
my ($self, %opt) = @_; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
$self->renewSessionToken(); |
177
|
|
|
|
|
|
|
my $upload_file = Mediafire::Api::UploadFile->new( |
178
|
|
|
|
|
|
|
-ua => $self->{ua}, |
179
|
|
|
|
|
|
|
-session_token => $self->{session_token}, |
180
|
0
|
|
|
|
|
|
); |
181
|
0
|
|
|
|
|
|
my $mediafire_file = $upload_file->uploadFile(%opt); |
182
|
0
|
|
|
|
|
|
return $mediafire_file; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub findFileByName { |
186
|
0
|
|
|
0
|
1
|
|
my ($self, %opt) = @_; |
187
|
0
|
|
0
|
|
|
|
my $filename = $opt{-filename} // croak "You must specify '-filename' param"; |
188
|
|
|
|
|
|
|
|
189
|
0
|
|
|
|
|
|
my @mediafire_files; |
190
|
0
|
|
|
|
|
|
my $url = 'https://www.mediafire.com/api/1.4/folder/search.php'; |
191
|
|
|
|
|
|
|
my %param = ( |
192
|
|
|
|
|
|
|
'r' => 'qzpa', |
193
|
|
|
|
|
|
|
'search_text' => $filename, |
194
|
|
|
|
|
|
|
'version' => '2.5', |
195
|
|
|
|
|
|
|
'search_all' => 'yes', |
196
|
|
|
|
|
|
|
'folder_key' => '', |
197
|
|
|
|
|
|
|
'filter' => 'all', |
198
|
|
|
|
|
|
|
'session_token' => $self->{session_token}, |
199
|
0
|
|
|
|
|
|
'response_format' => 'json', |
200
|
|
|
|
|
|
|
); |
201
|
0
|
|
|
|
|
|
my $full_url = $url . '?' . join('&', map {"$_=" . uri_escape($param{$_})} keys %param); |
|
0
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
my $res = $self->{ua}->get($full_url); |
203
|
0
|
|
|
|
|
|
my $code = $res->code; |
204
|
0
|
0
|
|
|
|
|
if ($code ne '200') { |
205
|
0
|
|
|
|
|
|
croak "Can't search file. Wrong response code on url '$full_url'. Code: $code"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
my $json_res = eval { |
209
|
0
|
|
|
|
|
|
decode_json($res->decoded_content); |
210
|
|
|
|
|
|
|
}; |
211
|
0
|
0
|
|
|
|
|
if ($@) { |
212
|
0
|
|
|
|
|
|
croak "Can't decode response to json. Response: '" . $res->decoded_content . "'"; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
|
|
|
for my $f (@{$json_res->{response}->{results}}) { |
|
0
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# If not file |
217
|
0
|
0
|
|
|
|
|
if ($f->{type} ne 'file') { |
218
|
0
|
|
|
|
|
|
next; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# If in Trash folder |
222
|
0
|
0
|
|
|
|
|
if ($f->{parent_name} eq 'Trash') { |
223
|
0
|
|
|
|
|
|
next; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
push @mediafire_files, |
227
|
|
|
|
|
|
|
Mediafire::Api::File->new( |
228
|
|
|
|
|
|
|
-size => $f->{size}, |
229
|
|
|
|
|
|
|
-key => $f->{quickkey}, |
230
|
0
|
|
|
|
|
|
); |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
return \@mediafire_files; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
sub downloadFile { |
237
|
0
|
|
|
0
|
1
|
|
my ($self, %opt) = @_; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
my $download_file = Mediafire::Api::DownloadFile->new( |
240
|
|
|
|
|
|
|
-ua => $self->{ua}, |
241
|
0
|
|
|
|
|
|
); |
242
|
0
|
|
|
|
|
|
$download_file->downloadFile(%opt); |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
1; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
__END__ |