ºÇ¿· RSS

horiday blog

2011/6/2

¢£ [perl] perl ¤Ç youtube ¤Î¸¡º÷

youtube ¤ÎÆ°²è¤ò¸¡º÷¤¹¤ë¤Ë¤Ï¤É¤¦¤·¤¿¤éÎɤ¤¤Î¤«¤Èʹ¤«¤ì¤¿¤Î¤Ç¡¤¥µ¥ó¥×¥ë¥³¡¼¥É¤òºî¤Ã¤Æ¤ß¤Þ¤·¤¿¡¥API ¤Ç¸¡º÷¤ò¤¹¤ë¤Ë¤Ï¡¤Ç§¾Ú¤Ê¤É¤¬ÉÔÍפʤ褦¤Ç¤¹¡¥

¥ê¥Õ¥¡¥ì¥ó¥¹ ¥¬¥¤¥É: Data API ¥×¥í¥È¥³¥ë¤ò¸«¤ë¤È¡¤

Æ°²è¤ò¸¡º÷¤¹¤ë¤Ë¤Ï¡¢¼¡¤Î URL ¤Ë¥ê¥¯¥¨¥¹¥È¤Ë¹ç¤ï¤»¤Æ¥¯¥¨¥ê ¥Ñ¥é¥á¡¼¥¿¤òÉղä·¡¢HTTP GET ¥ê¥¯¥¨¥¹¥È¤òÁ÷¿®¤·¤Þ¤¹¡£

http://gdata.youtube.com/feeds/api/videos

¤¿¤È¤¨¤Ð¼¡¤Î URL ¤Ø¤Î¥ê¥¯¥¨¥¹¥È¤Ç¤Ï¡¢¸¡º÷¥­¡¼¥ï¡¼¥É¡Öfootball¡×¤Ë°ìÃפ·¡¢¥­¡¼¥ï¡¼¥É¡Ösoccer¡×¤Ë¤Ï°ìÃפ·¤Ê¤¤ºÇ¶á¥¢¥Ã¥×¥í¡¼¥É¤µ¤ì¤¿Æ°²è¤Î 10 Ëܤº¤Ä¤Î¥»¥Ã¥È¤Î 2 ÈÖÌܤΥ»¥Ã¥È¤ò¸¡º÷¤·¤Þ¤¹¡£

http://gdata.youtube.com/feeds/api/videos?
    vq=football+-soccer
    &orderby=published
    &start-index=11
    &max-results=10

¤È¤¤¤¦¤³¤È¤Ç¥µ¥ó¥×¥ë¥³¡¼¥É¤Ï²¼µ­¤Î¤è¤¦¤Ë¤Ê¤ê¤Þ¤·¤¿¡¥atom ¤Ç¼èÆÀ¤·¤è¤¦¤È¤·¤¿¤Î¤Ç¤¹¤¬¡¤°·¤¤Êý¤¬Îɤ¯¤ï¤«¤é¤Ê¤«¤Ã¤¿¤Î¤Ç¡¤rss ¤Ë¤·¤Þ¤·¤¿¡¥

#!/usr/bin/env perl
# search-youtube.pl
use strict;
use Data::Dumper;
use URI;
use XML::Simple;
use LWP::Simple;
use Unicode::RecursiveDowngrade;
use Unicode::Japanese;
use Jcode;

sub main {
  my $kw = "¹áÀîÂç³Ø";
  my $h = search_youtube($kw);

  print Dumper $h;
}

sub search_youtube {
  my $kw = shift;
  my $h;

  my $proxy = 'http://gdata.youtube.com/feeds/api/videos';
  my $uri   = URI->new( $proxy );

  my $u_kw = Jcode::convert($kw , "utf8", "euc-jp" );

  $uri->query_form(
		   vq => $u_kw,
		   orderby => "relevance",
		   alt => 'rss'
		  );

  my $xml = get($uri->as_string);
  my $xs  = XML::Simple->new();
  my $ref = $xs->XMLin( $xml, forcearray => 1 );

  my $count = 0;
  foreach my $i (@{$ref->{channel}[0]{item}}) {
    $h->{$count}{title} = shift @{$i->{title}};
    $h->{$count}{link} = shift @{$i->{link}};
    $h->{$count}{description} = $i->{'description'}[0];
    $h->{$count}{keywords} = $i->{'media:group'}[0]{'media:keywords'};
    $h->{$count}{author} = $i->{author}[0];
    $h->{$count}{viewcount} = $i->{'yt:statistics'}[0]{viewCount};
    $h->{$count}{pubdate} = $i->{pubDate}[0];
    $h->{$count}{thumb} = $i->{'media:group'}[0]{'media:thumbnail'}[1];
    $count++;
  }

  my $rd = Unicode::RecursiveDowngrade->new;
  $rd->filter(sub {
                Unicode::Japanese->new(shift, 'utf8')->euc
                });
  $h = $rd->downgrade($h);

  return $h;
}

main;

¾åµ­¤Î¥³¡¼¥É¤ò search-youtube.pl ¤È¤·¤ÆÊݸ¤·¤Æ¡¤¼Â¹Ô¤¹¤ë¤È²¼µ­¤Î¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡¥

% perl youtube-search.pl
$VAR1 = {
          '11' => {
                    'link' => 'http://www.youtube.com/watch?v=F_abi4096VQ&feature=youtube_gdata',
                    'viewcount' => '1697',
                    'pubdate' => 'Tue, 08 Feb 2011 17:51:56 +0000',
                    'author' => 'BBRarkaic3',
                    'description' => '¹áÀîÂç³ØÂè42²óÄê´ü±éÁÕ²ñ¡¢­·stage¥á¥¤¥ó¶Ê. ¤Ó¤Ó¤ëÂç¹¥¤­¥¹¥¤¡¼¥È¤Ê¥á¥í¥Ç¥£¤È¡¢¤­¤é¤Ó¤ä¤«¤Ê¥ê¥º¥à¡£ ÆüËÜ°ì¤Î¥Þ¥ó¥É¥ê¥ó¥¯¥é¥Ö¤È¼«É餷³¤±¤Þ¤¹¡£ ÁÇÀ²¤é¤·¤¤¡ª¡ª¡ª¡ª »Ø´ø¡§»Í¹ñ¤ÎÅÁÀâ¡¢ºÙÀî°ì¼ù»á(¹áÀºß½»㊟2011ǯ¸½ºß) ¢¨¹áÀîÂç³Ø±éÁդΥǥâCD¤Ê¤É¤Î¹ØÆþÅù¤ÎÌ䤤¹ç¤ï¤»¤Ï¡¢¹áÀîÂç³Ø¥Þ¥ó¥É¥ê¥ó¥¯¥é¥Ö¤Þ¤Ç¤Ó¤Ó¤ëÂç¹¥¤­',
                     'thumb' => {
                                 'width' => '120',
                                 'time' => '00:03:45.250',
                                 'url' => 'http://i.ytimg.com/vi/F_abi4096VQ/1.jpg',
                                 'height' => '90'
                               },
                    'keywords' => [
                                    '¥Þ¥ó¥É¥ê¥ó, ¹¾Æ¬'
                                  ],
                    'title' => '¡Ö±À¤Î¹ÔÊý¡× ¹áÀîÂç³ØÂ裴£²²óÄê´ü±éÁÕ²ñ 3-3final¥¹¥Æ¡¼¥¸ 2010ǯ12/18'
                 },
...
  • ¥­¥à¥ß¥®¥å(Kim Migyu)¤µ¤ó¤Î¤ªÏÃ


´ØÏ¢¥ê¥ó¥¯