StreamFinder

 view release on metacpan or  search on metacpan

lib/StreamFinder/BrandNewTube.pm  view on Meta::CPAN

			print STDERR "---FOUND 1ST EPISODE URL=$url2fetch= IN CHANNEL PAGE, RUN WITH THAT!\n"  if ($DEBUG);
		}
	}

	my $html = '';
	if ($url2fetch =~ m#\/watch\/([^\/\.]+)#) {
		$self->{'id'} = $1;
		print STDERR "-FETCHING HTML URL=$url2fetch= ID=".$self->{'id'}."=\n"  if ($DEBUG);
		$response = $ua->get($url2fetch);
		if ($response->is_success) {
			$html = $response->decoded_content;
		} else {
			print STDERR $response->status_line  if ($DEBUG);
			my $no_wget = system('wget','-V');
			unless ($no_wget) {
				print STDERR "\n..trying wget...\n"  if ($DEBUG);
				$html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `;
			}
		}
		if ($html) {
			$self->{'title'} = ($html =~ m#\<title\>\s*([^\<]+)\<\/title\>#s) ? $1 : '';
			$self->{'title'} ||= $1  if ($html =~ m#\<meta\s+(?:name|property)\=\"?(?:og|twitter)\:title\"?\s+content\=\"(.+?)\"\s*\/?\>#s);
			$self->{'title'} =~ s#\s+$##;
			$self->{'title'} =~ s#\s*\-\s+Brand New Tube$##;

			if ($html =~ m#\<div\s+class\=\"video\-date\"\>(.+?)\<\/div\>#s) {
				($self->{'created'} = $1) =~ s/^\s+//s;
				$self->{'created'} =~ s/\s+$//s;
				if ($self->{'created'} =~ /(\d\d\d\d)/) {
					$self->{'year'} = $1;
				} else {
					#YES, THEY DO STUPID STUFF LIKE THIS!:
					my @timestuff = localtime(time);
					my $curyear = $timestuff[5] + 1900;
					if ($self->{'created'} =~ /(\d)\s+years?\s+ago/) {
						$self->{'year'} = $curyear - $1;
					} elsif ($self->{'created'} =~ /months?\s+ago/) {
						$self->{'year'} = $curyear;
					}
				}
			}
			$self->{'description'} = $self->{'title'};
			$self->{'imageurl'} = $1  if ($html =~ m#\bdata\-poster\=\"([^\"]+)#s);
			#THIS IS UGLY:
			if ($html =~ m#\<div\s+class\=\"col\-md\-1\s+col\-sm\-2\s+col\-2\s+pb\-3\"\>(.+?)\<\/div\>#s) {
				my $channelstuff = $1;
				$self->{'albumartist'} = $1  if ($channelstuff =~ m#\<a\s+href\=\"([^\"]+)#s);
				$self->{'articonurl'} = $1  if ($channelstuff =~ m#\<img\s+src\=\"([^\"]+)#s);
			}
			if ($html =~ m#\<a\s+href\=\"([^\"]+)\"\s+class\=\"video\-username\"\>\s*([^\<]+)#s) {
				$self->{'albumartist'} ||= $1;
				($self->{'artist'} = $2) =~ s#\s+$##s;
			}

			#TRY TO FETCH STREAMS:
			while ($html =~ s#\<source\s+.*?src\=\"([^\"]+)\"##s) {
				my $one = $1;
				push @{$self->{'streams'}}, $one  unless ($self->{'secure'} && $one !~ /^https/);
				$self->{'cnt'}++;
			}
			#STEP 2:  TRY FETCHING THE EMBED URL UNLESS WE FOUND STREAM(S):
#DEPRECIATED?			$url2fetch = $1  if ($self->{'cnt'} <= 0
#DEPRECIATED?					&& $html =~ m#\<iframe\s+src\=\"($baseURL\/embed\/[^\"]+)#s);
			print STDERR "*** EMBED PAGE URL($1) FOUND!\n"  if ($self->{'cnt'} <= 0
					&& $html =~ m#\<iframe\s+src\=\"($baseURL\/embed\/[^\"]+)#s);
		}
	}

	$self->{'cnt'} = scalar @{$self->{'streams'}};
	$self->{'title'} =~ s/\s+\-\s+$self->{'artist'}\s*$//;  #CONVERT "Title - Artist" => "Title"
	foreach my $field (qw(description artist title)) {
		$self->{$field} = HTML::Entities::decode_entities($self->{$field});
		$self->{$field} = uri_unescape($self->{$field});
		$self->{$field} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/eg;
	}
	#SOME TITLES HAVE WIDE CHARACTERS, REMOVE 'EM!:
	$self->{'title'} =~ s/(.)/'\x'.sprintf('%02x',ord($1))/eg;
	$self->{'title'} =~ s/\\x[0-9a-f][0-9a-f][0-9a-f]+//g;
	$self->{'title'} =~ s/\\x\{?([0-9A-Fa-f]{2})\}?/chr(hex($1))/eg;

	$self->{'iconurl'} ||= $self->{'imageurl'};
	$self->{'iconurl'} ||= $self->{'articonurl'}  if ($self->{'articonurl'});
	$self->{'artimageurl'} ||= $self->{'articonurl'};
	foreach my $field (qw(iconurl imageurl articonurl artimageurl)) {
		$self->{$field} =~ s/\&amp\;/\&/g;
	}
	$self->{'total'} = $self->{'cnt'};
	$self->{'Url'} = ($self->{'cnt'} > 0) ? $self->{'streams'}->[0] : '';
	if ($DEBUG) {
		foreach my $f (sort keys %{$self}) {
			print STDERR "--KEY=$f= VAL=$$self{$f}=\n";
		}
		print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n"  if ($self->{'cnt'} > 0);;
	}

	$self->_log($url);

	bless $self, $class;   #BLESS IT!

	return $self;
}

1



( run in 0.831 second using v1.01-cache-2.11-cpan-71847e10f99 )