-
Notifications
You must be signed in to change notification settings - Fork 2
/
subdb.pl
executable file
·209 lines (149 loc) · 5.46 KB
/
subdb.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/perl
###############################################################################
# DESsubdb - download subtitles for your movies from the thesubdb database
# Copyright (C) 2012 David Santiago
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
use warnings;
use strict;
use utf8;
use diagnostics;
use Digest::MD5 qw(md5_hex);
use LWP::UserAgent;
use URI::Escape;
use File::Basename;
use Getopt::Long;
use Data::Dumper;
use 5.014;
use constant ALLOWED_FILE_EXTENSIONS_REGEXP => "\.[^.]*";
use constant BLOCK_SIZE=>65536;# 64K
use constant SUBDB_API_URL=>'http://api.thesubdb.com/?';
use constant USER_AGENT => 'SubDB/1.0 (DESsubdb/0.1; http://sourceforge.net/projects/dessubdb)';
use constant SUBTITLES_LANGUAGE => $ENV{ 'SUBDB_LANGS' } ? ( split /\s+/, $ENV{ 'SUBDB_LANGS' } ) : qw(en pt);
use constant ALLOWED_VIDEO_FILE_EXTENSIONS => qw(.avi .m4v .mkv .mp4 .ogv .flv);
use constant ALLOWED_SUB_FILE_EXTENSIONS => qw(.srt .sub);
my $browser = LWP::UserAgent->new;
$browser->agent(USER_AGENT);
my @USER_LANGUAGES;
GetOptions("lang=s"=>\@USER_LANGUAGES);
for my $file (@ARGV){
if( ! -e $file ){
say "File not found= $file, skipped!";
next;
}
my @fileData = fileparse($file, ALLOWED_FILE_EXTENSIONS_REGEXP);
say "File extension= ",$fileData[2];
if(grep (/$fileData[2]/, ALLOWED_SUB_FILE_EXTENSIONS)){
say "Subtitles File!";
uploadSubs($file);
}elsif(grep /$fileData[2]/, ALLOWED_VIDEO_FILE_EXTENSIONS){
say "Video File!";
searchAndDownloadSubs( $file);
}
}
#upload subtitles
sub uploadSubs{
my $file = shift;
my $fileSize = -s $file;
my @fileData = fileparse($file, ALLOWED_FILE_EXTENSIONS_REGEXP);
for my $extension (ALLOWED_VIDEO_FILE_EXTENSIONS){
my $videoFile = $fileData[1].$fileData[0].$extension;
if( -e $videoFile){
say "Found $videoFile";
uploadSubtitle($file, @fileData[0..1],$extension);
} else {
my $lang = (split(/\./, $fileData[0]))[-1];
(my $fileName = $fileData[0]) =~ s/\.$lang$//;
if (-e $fileData[1].$fileName.$extension) {
say "Found subtitle language: $lang";
say "Found $fileData[1]$fileName$extension";
uploadSubtitle($file, $fileName, $fileData[1], $extension);
}
}
}
}
sub uploadSubtitle {
my ($subtitleFile, $fileName, $path, $extension) = @_;
my $file = "$path$fileName$extension";
my $fileHash = getFileHash($file);
my %url_parameters = ("action"=>"upload",);
my %url_data = (
Content_Type => 'multipart/form-data',
"content"=>[
"hash"=> $fileHash,
"file"=>[$subtitleFile, $fileName, (Content_Type => "application/octet-stream")] ,
],
);
my $response = $browser->post(SUBDB_API_URL.create_query(%url_parameters), %url_data );
say "Subtitles Upload result= ".$response->status_line;
}
# search and download subtitles
sub searchAndDownloadSubs{
my $file = shift;
my %url_parameters = (
"hash"=>getFileHash( $file),
"action"=>"search",
);
say "Querying the url: ".SUBDB_API_URL.create_query(%url_parameters);
my $response = $browser->get(SUBDB_API_URL.create_query(%url_parameters));
print "Response = ".$response->status_line;
if ($response->is_success){
my $availableLanguages = $response->content;
say " (Available languages: $availableLanguages)";
my @languagesToDownload = ();
for my $lang ($#USER_LANGUAGES==0?@USER_LANGUAGES:SUBTITLES_LANGUAGE){
if( $availableLanguages =~ /$lang/ ){
push @languagesToDownload, $lang;
}
}
if(@languagesToDownload >= 1){
$url_parameters{"action"} = "download";
$url_parameters{"language"} = join ',', @languagesToDownload;
my @fileData = fileparse($file, ALLOWED_FILE_EXTENSIONS_REGEXP);
$response = $browser->get(SUBDB_API_URL.create_query(%url_parameters), ':content_file'=>$fileData[1].$fileData[0].".srt");
if($response->is_success){
say "Subtitles for $file downloaded!";
}
}
}
}
# calculates the file hash, as specified in the thesubdb site
sub getFileHash{
my $filename = shift or die 'A filename is required!';
my $filesize = -s $filename;
die "The file is too small! Wrong file?!? " unless $filesize > BLOCK_SIZE*2;
open (my $fh, "<", $filename) or die "Couldn't open file $filename: $!";
binmode $fh;
my $buffer;
my $data;
read $fh, $data, BLOCK_SIZE;
$buffer = $data;
seek $fh, -1*BLOCK_SIZE, 2;
read $fh, $data, BLOCK_SIZE;
$buffer .=$data;
close $fh;
say "Hash= ".md5_hex($buffer);
md5_hex($buffer);
}
# i copied this from somewhere on the internet. I don't know the credits. If You do, please tell me so i can
# put them here.
sub create_query{
my %hash = @_;
my @pairs;
for my $key (keys %hash) {
push @pairs, join "=", map { uri_escape($_) } $key, $hash{$key};
}
return join "&", @pairs;
}