/usr/share/perl5/FlashVideo/Generic.pm is in get-flash-videos 1.25~git2014.03.23-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | # Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::Generic;
use strict;
use FlashVideo::Utils;
use URI;
use FlashVideo::URLFinder;
use URI::Escape qw(uri_unescape);
use HTML::Entities qw(decode_entities);
our $VERSION = '0.01';
sub Version() { $VERSION; }
my $video_re = qr!http[-:/a-z0-9%_.?=&]+@{[EXTENSIONS]}
# Grab any params that might be used for auth..
(?:\?[-:/a-z0-9%_.?=&]+)?!xi;
sub find_video {
my ($self, $browser, $embed_url, $prefs) = @_;
# First strategy - identify all the Flash video files, and download the
# biggest one. Yes, this is hacky.
if (!$browser->success) {
$browser->get($browser->response->header('Location'));
die "Couldn't download URL: " . $browser->response->status_line
unless $browser->success;
}
my ($possible_filename, $actual_url, $title);
$title = extract_title($browser);
my @flv_urls = map {
(m{http://.+?(http://.+?@{[EXTENSIONS]})}i) ? $1 : $_
} ($browser->content =~ m{($video_re)}gi);
if (@flv_urls) {
require LWP::Simple;
require Memoize;
Memoize::memoize("LWP::Simple::head");
@flv_urls = sort { (LWP::Simple::head($a))[1] <=> (LWP::Simple::head($b))[1] } @flv_urls;
$possible_filename = (split /\//, $flv_urls[-1])[-1];
# Un-escape URLs if necessary
if ($flv_urls[-1] =~ /^http%3a%2f%2f/) {
$flv_urls[-1] = uri_unescape($flv_urls[-1])
}
$actual_url = url_exists($browser->clone, $flv_urls[-1]);
}
my $filename_is_reliable;
if(!$actual_url) {
RE: for my $regex(
qr{(?si)<embed.*?flashvars=["']?([^"'>]+)},
qr{(?si)<embed.*?src=["']?([^"'>]+)},
qr{(?si)<a[^>]* href=["']?([^"'>]+?@{[EXTENSIONS]})},
qr{(?si)<object[^>]*>.*?<param [^>]*value=["']?([^"'>]+)},
qr{(?si)<object[^>]*>(.*?)</object>},
# Attempt to handle scripts using flashvars / swfobject
qr{(?si)<script[^>]*>(.*?)</script>}) {
for my $param($browser->content =~ /$regex/gi) {
(my $url, $possible_filename, $filename_is_reliable) = find_file_param($browser->clone, $param, $prefs);
if($url) {
my $resolved_url = url_exists($browser->clone, $url);
if($resolved_url) {
$actual_url = $resolved_url;
last RE;
}
}
}
}
if(!$actual_url) {
for my $iframe($browser->content =~ /<iframe[^>]+src=["']?([^"'>]+)/gi) {
$iframe = decode_entities($iframe);
$iframe = URI->new_abs($iframe, $browser->uri);
$iframe = decode_entities($iframe);
debug "Found iframe: $iframe";
my $sub_browser = $browser->clone;
$sub_browser->get($iframe);
# Recurse!
my($package, $possible_url) = FlashVideo::URLFinder->find_package($iframe, $sub_browser);
# Before fetching the url, give the package a chance
if($package->can("pre_find")) {
$package->pre_find($sub_browser);
}
info "Downloading $iframe";
$sub_browser->get($iframe);
my($actual_url, @suggested_fnames) = eval {
$package->find_video($sub_browser, $possible_url, $prefs);
};
return $actual_url, @suggested_fnames if $actual_url;
}
}
}
my @filenames;
return $actual_url, $possible_filename if $filename_is_reliable;
$possible_filename =~ s/\?.*//;
# The actual filename, provided it looks like it might be reasonable
# (not just numbers)..
push @filenames, $possible_filename if $possible_filename
&& $possible_filename !~ /^[0-9_.]+@{[EXTENSIONS]}$/;
# The title of the page, if it isn't similar to the filename..
my $ext = substr(($actual_url =~ /(@{[EXTENSIONS]})$/)[0], 1);
push @filenames, title_to_filename($title, $ext) if
$title && $title !~ /\Q$possible_filename\E/i;
# A title with just the timestamp in it..
push @filenames, get_video_filename() if !@filenames;
return $actual_url, @filenames if $actual_url;
# As a last ditch attempt, download the SWF file as in some cases, sites
# use an SWF movie file for each FLV.
# Get SWF URL(s)
my %swf_urls;
if (eval { require URI::Find }) {
my $finder = URI::Find->new(
sub { $swf_urls{$_[1]}++ if $_[1] =~ /\.swf$/i }
);
$finder->find(\$browser->content);
}
else {
# Extract URLs in a frail way.
my $content = $browser->content;
while($content =~ m{(http://[^ "']+?\.swf)}ig) {
$swf_urls{$1}++;
}
}
if (%swf_urls) {
foreach my $swf_url (keys %swf_urls) {
if (my ($flv_url, $title) = search_for_flv_in_swf($browser, $swf_url)) {
return $flv_url, title_to_filename($title);
}
}
}
die "No URLs found";
}
sub search_for_flv_in_swf {
my ($browser, $swf_url) = @_;
$browser = $browser->clone();
$browser->get($swf_url);
if (!$browser->success) {
die "Couldn't download SWF URL $swf_url: " .
$browser->response->status_line();
}
# SWF data might be compressed.
my $swf_data = $browser->content;
if ('C' eq substr $swf_data, 0, 1) {
if (eval { require Compress::Zlib }) {
$swf_data = Compress::Zlib::uncompress(substr $swf_data, 8);
}
else {
die "Compress::Zlib is required to uncompress compressed SWF files.\n";
}
}
if ($swf_data =~ m{(http://.{10,300}?\.flv)}i) {
my $flv_url = $1;
my $filename = uri_unescape(File::Basename::basename(URI->new($flv_url)->path()));
$filename =~ s/\.flv$//i;
return ($flv_url, $filename);
}
return;
}
sub find_file_param {
my($browser, $param, $prefs) = @_;
for my $file($param =~ /(?:video|movie|file|path)_?(?:href|src|url)?['"]?\s*[=:,]\s*['"]?([^&'" ]+)/gi,
$param =~ /(?:config|playlist|options)['"]?\s*[,:=]\s*['"]?(http[^'"&]+)/gi,
$param =~ /['"=](.*?@{[EXTENSIONS]})/gi,
$param =~ /([^ ]+@{[EXTENSIONS]})/gi,
$param =~ /SWFObject\(["']([^"']+)/) {
debug "Found $file";
my ($actual_url, $filename, $filename_is_reliable) = guess_file($browser, $file, '', $prefs);
if(!$actual_url && $file =~ /\?(.*)/) {
# Maybe we have query params?
debug "Trying query param on $1";
for my $query_param(split /[;&]/, $1) {
my($query_key, $query_value) = split /=/, $query_param;
debug "Found $query_value from $query_key";
($actual_url, $filename, $filename_is_reliable)
= guess_file($browser, $query_value, '', $prefs);
last if $actual_url;
}
}
if($actual_url) {
my $possible_filename = $filename || (split /\//, $actual_url)[-1];
return $actual_url, $possible_filename, $filename_is_reliable;
}
}
if($param =~ m{(rtmp://[^ &"']+)}) {
info "This looks like RTMP ($1), no generic support yet..";
}
return;
}
sub guess_file {
my($browser, $file, $once, $prefs) = @_;
# Contains lots of URI encoding, so try escaping..
$file = uri_unescape($file) if scalar(() = $file =~ /%[A-F0-9]{2}/gi) > 3;
my $orig_uri = URI->new_abs($file, $browser->uri);
info "Guessed $orig_uri trying...";
if($orig_uri) {
my $uri = url_exists($browser->clone, $orig_uri);
if($uri) {
# Check to see if this URL is for a supported site.
my ($package, $url) = FlashVideo::URLFinder->find_package($uri,
$browser->clone);
if($package && $package ne __PACKAGE__) {
debug "$uri is supported by $package.";
(my $browser_on_supported_site = $browser->clone())->get($uri);
return $package->find_video($browser_on_supported_site, $uri, $prefs), 1;
}
my $content_type = $browser->response->header("Content-type");
if($content_type =~ m!^(text|application/xml)!) {
# Just in case someone serves the video itself as text/plain.
$browser->add_header("Range", "bytes=0-10000");
$browser->get($uri);
$browser->delete_header("Range");
if(FlashVideo::Downloader->check_magic($browser->content)
|| $uri =~ m!$video_re!) {
# It's a video..
debug "Found a video at $uri";
return $uri;
}
# If this looks like HTML we have no hope of guessing right, so
# give up now.
return if $browser->content =~ /<html[^>]*>/i;
if($browser->content =~ m!($video_re)!) {
# Found a video URL
return $1;
} elsif(!defined $once
&& $browser->content =~ m!(http[-:/a-zA-Z0-9%_.?=&]+)!i) {
# Try once more, one level deeper..
return guess_file($browser, $1, 1, $prefs);
} else {
info "Tried $uri, but no video URL found";
}
} elsif($content_type =~ m!application/! && $uri ne $orig_uri) {
# We were redirected, maybe something in the new URL?
return((find_file_param($browser, $uri))[0]);
} else {
return $uri->as_string;
}
} elsif(not defined $once) {
# Try using the location of the .swf file as the base, if it's different.
if($browser->content =~ /["']([^ ]+\.swf)/) {
my $swf_uri = URI->new_abs($1, $browser->uri);
if($swf_uri) {
my $new_uri = URI->new_abs($file, $swf_uri);
debug "Found SWF: $swf_uri -> $new_uri";
if($new_uri ne $uri) {
return guess_file($browser, $new_uri, 1, $prefs);
}
}
}
}
}
return;
}
1;
|