None of the advice on perlunifaq or elsewhere can both
* Clear the "Wide character in print" warning, and
* Leave the output non doubly encoded.
#!/usr/bin/perl
# How to test this program:
# $ export restriction=TW; PERLLIB=$HOME/perl5/lib/perl5 ./ytpl jidanni2 > /tmp/o
# $ cat /tmp/o
# That will show you any problems it has.
# Print out YouTube playlists. Usage:
# Example: $0 YouTubeUserID
# Example: restriction=TW $0 jidanni2
# Copyright :
http://www.fsf.org/copyleft/gpl.html
# Author : Dan Jacobson --
http://jidanni.org/
# Created On : Wed Mar 2 08:35:33 2011
# Last Modified On: Fri Nov 2 13:23:16 2012
# Update Count : 830
use strict;
#use Encode;
#use warnings FATAL => 'all';
#binmode STDIN, ":utf8";
#binmode STDOUT, ':encoding(UTF-8)';
#binmode STDIN, ':encoding(UTF-8)';
#binmode(STDOUT);
#binmode STDERR, ":utf8";binmode STDOUT, ":utf8";binmode STDIN, ":utf8";
#use utf8;
#use open qw/:std :encoding(utf8)/;
##use diagnostics;
#use Data::Dumper;
use WebService::GData::Constants qw(:all);
use WebService::GData::YouTube;
die 'Specify a user please.' unless my $user = shift;
my ( %checklist, %vids, $playlists, );
my $yt = new WebService::GData::YouTube();
$yt->connection->env_proxy;
##$yt->connection->enable_compression(TRUE); #disaster
$yt->query->max_results(50);
#if the number of your playlist is superior to 50, you will need to
#loop via the result like you used to do before with the video results
#(start_index+items_per_page). there is no other easy way to do this
#yet.
eval { $playlists = $yt->get_user_playlists($user) } or die $@->content;
@$playlists = sort { $a->title cmp $b->title } @$playlists;
for ( $ENV{restriction} ) { $yt->query()->restriction($_) if $_ }
for my $playlist (@$playlists) {
my @missing = (undef) x $playlist->count_hint;
my $entries;
while (
eval {
## can't use compression starting here:
$entries = $yt->get_user_playlist_by_id( $playlist->playlist_id );
}
)
{
die $@->content if $@;
for my $entry (@$entries) {
my $IDP = ( split( /:/, $entry->id ) )[-1] or die;
if ( $entry->appcontrol_state
&& $entry->appcontrol_state eq "requesterRegion" )
{
# print "yy$IDP ", $entry->id, "\n";
next;
}
##
http://code.google.com/intl/en/apis/youtube/2.0/reference.html#youtube_data_api_tag_yt:state
## Also one day could use
## my $string = $entry->denied_countries;
## my @matches = $string=~m/(TW|US)/g;
delete $missing[ $entry->position - 1 ];
my $v = sprintf "%03d|%s|%s|%s", $entry->position, $entry->video_id,
$IDP,
$entry->title;
if ( $entry->media_player ) {
# use Data::Dumper;
push @{ $vids{1}{ $playlist->playlist_id } }, $v;
# print STDERR Dumper("紅",$playlist->title), "紅", $playlist->title;
# die;
unless ( $playlist->title eq '英文歌詞 English lyrics' ) {
push @{ $checklist{ $entry->video_id } }, join "|",
$playlist->title,
$v;
}
}
else {
push @{ $vids{0}{ $playlist->playlist_id } },
"# $v|" . $entry->appcontrol_state;
# print STDERR "xx$IDP\n";
}
}
}
for ( 0 .. $playlist->count_hint - 1 ) {
if ( exists $missing[$_] ) {
push @{ $vids{0}{ $playlist->playlist_id } },
sprintf "# %03d|Problem!", $_ + 1;
## try watching it in a browser when logged out to find out what was wrong
}
}
}
{
my ( $total, $list ) = ( 0, 'Duplicates' );
printf "\n%d playlists, %d videos.\n:::: $list:\n", scalar @$playlists,
scalar keys %checklist;
for ( keys %checklist ) {
if ( $#{ $checklist{$_} } ) {
for ( @{ $checklist{$_} } ) { print "$_\n"; $total++ }
}
}
print "Total $list: $total\n";
}
for my $playlist (@$playlists) {
push @{ $vids{0}{ $playlist->playlist_id } }, "Empty playlist!"
unless $vids{1}{ $playlist->playlist_id };
}
{
my @list = qw/Unavailable Available/;
for ( 0, 1 ) {
print "\n:::: $list[$_]:\n";
my $total = 0;
for my $playlist (@$playlists) {
next unless $vids{$_}{ $playlist->playlist_id };
## print '====
http://www.youtube.com/my_playlists?p=',
## print '====
http://www.youtube.com/playlist?action_edit=1&list=PL',
## print '====
http://www.youtube.com/playlist?list=PL',
print '====
http://www.youtube.com/playlist?list=',
$playlist->playlist_id, ' |', $playlist->title, "\n";
for ( sort @{ $vids{$_}{ $playlist->playlist_id } } ) {
# print decode_utf8( $_ ), "\n";
print $_, "\n";
$total++;
}
}
print "Total $list[$_]: $total\n";
}
}