perl

YouTubeに動画をアップロードしてみよう

と、思った。
とりあえず、スクリプト集めだな。そかそか、APIのデベロッパーキーが必要ね?
ん?クライアントIDってなんだっけ? この辺りは簡単に探せそうだな。
my $developer_key = ‘xxxxxx’;
my $client_id = ‘xxxxxx’;
 
ダウンロードして、タイトルつけてアップロードってことになるな。
 
なるほど・・・このスクリプトは、ファイル名を渡してやれば、アップロードしてくれるのか。
結構よさげだね。
 
ダウンロードもスクリプトで処理したいな、探してみる。
 
d.hatena.ne.jp/shakemid/20081221/1229868522
#!/usr/bin/perl

# YouTube upload

use strict;
use warnings;
use utf8;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use IO::All;
use Getopt::Long;
use Jcode;

# usage
sub usage {
	print <<EOM;
Usage: $0 [option(s)] filename

Options:
    -t, --title        title         (default: filename)
    -d, --description  description   (default: filename)
    -k, --keywords     keywords      (default: filename)
    -c, --category     category      (default: People)
    -m, --ctype        content-type  (default: video/x-flv)
EOM

	exit 1;
}

# account settings
my $username      = 'username';
my $password      = 'password';
my $developer_key = 'xxxxxx';
my $client_id     = 'xxxxxx';

# video settings
my $title;
my $description;
my $keywords;
my $category;
my $content_type;
my $options = GetOptions(
	"t|title=s"       => \$title,
	"d|description=s" => \$description,
	"k|keywords=s"    => \$keywords,
	"c|category=s"    => \$category,
	"m|ctype=s"       => \$content_type,
) or usage();

$content_type ||= "video/x-flv";
my $filename = shift;
usage() if ! defined $filename;

my $video_detail = video_detail(
    {
        title       => $title       || $filename,
        description => $description || $filename,
        keywords    => $keywords    || $filename,
        category    => $category,
    }
);

my $ua    = LWP::UserAgent->new( keep_alive => 1 );
my $data  = io($filename)->binary->all;
my $token = auth();
upload($token);

sub upload {
    my $token = shift;
    my $url =
      "http://uploads.gdata.youtube.com/feeds/api/users/$username/uploads";
    my $request = HTTP::Request->new( POST => $url );

    $request->header(
        "Authorization"  => "GoogleLogin auth=$token",
        "X-GData-Key"    => "key=$developer_key",
        "X-GData-Client" => $client_id,
        Slug             => $filename,
        Content_Type     => 'multipart/related; boundary=xyzzy',
    );
    $request->add_part(
        HTTP::Message->new(
            [ Content_type => 'application/atom+xml' ],
            $video_detail
        )
    );
    $request->add_part(
        HTTP::Message->new( 
			[ Content_type => $content_type, 
			  'Content-Transfer-Encoding' => 'binary' ], 
			$data ) );

    my $response = $ua->request($request);
    die $response->status_line unless ( $response->is_success );
    print $response->content;
}

sub auth {
    my $request = POST(
        "https://www.google.com/youtube/accounts/ClientLogin",
        Content_Type => 'application/x-www-form-urlencoded',
        Content      => [
            Email   => $username,
            Passwd  => $password,
            service => "youtube",
            source  => "uploads",
        ],
    );
    my $response = $ua->request($request);
    die $response->status_line unless ( $response->is_success );
    $response->content =~ /Auth=(.*?)\n/;

    return $1;
}

sub video_detail {
    my $param = shift;
    foreach my $key ( keys %$param ){
        #utf8::encode($param->{$key}) if utf8::is_utf8($param->{$key});
        $param->{$key} = Jcode->new( $param->{$key} )->utf8;
    }
    my $title       = $param->{title}       || die "title is required";
    my $description = $param->{description} || die "description is required";
    my $keywords    = $param->{keywords}    || die "keywords is required";
    my $category    = $param->{category}    || "People";

    my $video_detail = << "XML";
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
 xmlns:media="http://search.yahoo.com/mrss/"
 xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <media:group>
    <media:title type="plain">$title</media:title>
    <media:description type="plain">$description</media:description>
    <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">$category</media:category>
    <media:keywords>$keywords</media:keywords>
  </media:group>
</entry>
XML

   return $video_detail;
}

スポンサーリンク

関連記事一覧