StreamFinder

 view release on metacpan or  search on metacpan

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

						push @epiStreams, $stream;
						push @epiTitles, $title;
						$ep1id ||= $1  if ($stream =~ m#episode\/(\d\d\d\d\d\d\d\d)\b#o);  #EXTRACT 1ST EPISODE ID!
					}
				}
			}
			if ($ep1id) {   #WE FOUND AN EPISODE, SO RETRY (TO FETCH THE EPISODE PAGE):
				++$tried;
				$url2fetch = $ep1id;
				print STDERR "-!!!!- RETRY w/XML EPISODE ID2=$url2fetch=\n"  if ($DEBUG);
				goto TRYIT;
			}
		} else {   #EPISODE PAGE ID (NOW GET THE DETAILED EPISODE METADATA & WE'RE DONE):
			my %fh = (qw(title title  published_at year  download_url Url  large_url iconurl 
					big_url imageurl description description  site_url albumartist
					name genre  fullname artist));
			foreach my $f (keys %fh) {
				($self->{$fh{$f}} = $1) =~ s#\\##g  if ($html =~ s#\"$f\"\:\"([^\"]+)\"##);
			}
			%fh = (qw(large_url articonurl  site_url albumartist  title album));
			foreach my $f (keys %fh) {
				($self->{$fh{$f}} = $1) =~ s#\\##g  if ($html =~ s#\"$f\"\:\"([^\"]+)\"##);
			}
			$self->{'created'} = $self->{'year'};
			$self->{'year'} =~ s#^(\d\d\d\d).+$#$1#;
			if ($self->{'Url'} =~ /^http/) {
				push @{$self->{'streams'}}, $self->{'Url'};
				$self->{'cnt'}++;
			}
		}
	} elsif ($episodeID) {   #EXTRACT EPISODE DATA FROM api.spreaker.com:
		$html = HTML::Entities::decode_entities($html);
		$html = uri_unescape($html);
		$html =~ s#\\\/#\/#gs;
		$self->{'title'} = ($html =~ s#\"title\"\:\"([^\"]+)##s) ? $1 : '';
		$self->{'description'} = ($html =~ m#\"description\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'description'} = HTML::Entities::decode_entities($self->{'description'});
#x		$self->{'description'} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
		$self->{'description'} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
		#FIX QUOTES:
		$self->{'description'} =~ s/\\u201([89cd])/($1 =~ m#[89]#o) ? "'" : '"'/egs;
		$self->{'description'} =~ s#\\$##gs;
		$self->{'album'} = ($html =~ m#\"title\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'iconurl'} = ($html =~ s#\"medium\_url\"\:\"([^\"]+)##s) ? $1 : '';
		$self->{'articonurl'} = ($html =~ m#\"medium\_url\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'imageurl'} = ($html =~ m#\"(?:image\_original|big)\_url\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'artimageurl'} = ($html =~ m#\"cover\_url\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'artist'} = ($html =~ m#\"fullname\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'genre'} = ($html =~ m#\"category\_id\"\:\d+\,\"name\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'created'} = ($html =~ m#\"published\_at\"\:\"([^\"]+)\"#s) ? $1 : '';
		$self->{'year'} = $1  if ($self->{'created'} =~ /(\d\d\d\d)/o);
		my $stream = ($html =~ m#\"download\_url\"\:\"([^\"]+)#s) ? $1 : '';
		if ($stream) {
			unless ($self->{'secure'} && $stream !~ /^https/o) {
				push @epiTitles, $self->{'title'};
				push @epiStreams, $stream;
				push @{$self->{'streams'}}, $stream;
				$self->{'cnt'}++;
			}
		}
	} else {    #EXTRACT PODCAST PAGE DATA FROM EMBEDDED, ENCODED JSON:
		$html = HTML::Entities::decode_entities($html);
		$html = uri_unescape($html);
		$html =~ s#\\\/#\/#gs;
		$self->{'album'} = ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:title\"\s+content\=\"([^\"]+)\"\s*\/\>#s) ? $1 : '';
		$self->{'articonurl'} = ($html =~ m#\<link\s+rel\=\"image\_src\"\s+href\=\"([^\"]+)#s) ? $1 : '';
		$self->{'articonurl'} ||= ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:image\"\s+content\=\"([^\"]+)\"\s*\/\>#s) ? $1 : '';
		$self->{'artist'} = ($html =~ m#\"fullname\"\:\"([^\"]+)#s) ? $1 : '';
		$self->{'genre'} = ($html =~ m#\"category\_id\"\:\d+\,\"name\"\:\"([^\"]+)#s) ? $1 : '';

		my $episodes = $1  if ($html =~ m#\"episodes\"\:\[(.+)$#s);

		#EXTRACT PLAYLIST DATA FOR ALL EPISODES & ALL DATA FOR THE FIRST/LATEST EPISODE:
		my $haveit = 0;  #TRUE WHEN WE HAVE DETAILED DATA FOR THE LATEST EPISODE.
		while ($episodes =~ s#^.+?\{\"miniplayer_data\"\:##s) {
			(my $ephtml = $episodes) =~ s/\{\"miniplayer_data\"\:.*$//so;
			next  unless ($ephtml =~ /\"episode_id\"\:(\d+)/so);
			my $epiID = $1;
			my $stream = ($ephtml =~ /\"playback\_url\"\:\"([^\"]+)/so) ? $1 : '';
			my $title = ($stream && $ephtml =~ /\"title\"\:\"([^\"]+)/so) ? $1 : '';
			if ($title) {
				unless ($self->{'secure'} && $stream !~ /^https/o) {
					push @epiTitles, $title;    #ADD ALL EPISODES TO THE PLAYLIST:
					push @epiStreams, $stream;
					print STDERR "--haveit=$haveit= epiID=$epiID= episodeID=$episodeID=\n"  if ($DEBUG);
					if (!$haveit && (!$episodeID || $episodeID eq $epiID)) { #GRAB 1ST/LATEST EPISODE DETAILS:
						$episodeID = $epiID;
						$haveit = 1;
						$self->{'title'} = $title;
						print STDERR "---GRABBED EPI=$epiID= title=$title=\n"  if ($DEBUG);
						$self->{'description'} = $1  if ($ephtml =~ m#\"description\"\:\"([^\"]+)\"#so);
						$self->{'description'} = HTML::Entities::decode_entities($self->{'description'});
#						$self->{'description'} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
						$self->{'description'} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
						#FIX QUOTES:
						$self->{'description'} =~ s/\\u201([89cd])/($1 =~ m#[89]#o) ? "'" : '"'/egs;
						$self->{'description'} =~ s#\\$##gs;
						$self->{'iconurl'} = $1  if ($ephtml =~ m#\"image\_url\"\:\"([^\"]+)\"#so);
						$self->{'imageurl'} = $1  if ($ephtml =~ m#\"image\_original\_url\"\:\"([^\"]+)\"#so);
						$self->{'iconurl'} ||= $self->{'imageurl'};
						$self->{'imageurl'} ||= $self->{'iconurl'};
						$self->{'created'} = $1 if ($ephtml =~ m#\"published\_at\"\:\"([^\"]+)\"#so);
						$self->{'year'} = $1  if ($self->{'created'} =~ /(\d\d\d\d)/o);
						push @{$self->{'streams'}}, $stream;
						$self->{'cnt'}++;
					}
				}
			}
		}
	}

	$self->{'total'} = $self->{'cnt'};
	$self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : '';

	#GENERATE EXTENDED-M3U PLAYLIST:

	if ($self->{'cnt'} > 0) {
		$self->{'playlist'} = "#EXTM3U\n";
		if ($#epiStreams >= 0) {  #PLAYLIST PAGE SPECIFIED, ADD ALL EPISODES TO PLAYLIST:
			$self->{'playlist_cnt'} = scalar @epiStreams;
			for (my $i=0;$i<=$#epiStreams;$i++) {



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