App-get_flash_videos

 view release on metacpan or  search on metacpan

lib/FlashVideo/Site/Tudou.pm  view on Meta::CPAN

use strict;
use FlashVideo::Utils;

sub find_video {
  my ($self, $browser, $embed_url) = @_;

  my $check_response = sub {
    my ( $message ) = @_;
    return if $browser->success;
    die sprintf $message, $browser->response->code;
  };

=for comment
  SD video
  Watch: http://www.tudou.com/programs/view/wo2YLr4sc44
  Embed: http://www.tudou.com/v/wo2YLr4sc44
  which redirects to:
         http://www.tudou.com/player/outside/player_outside.swf?iid=30599813&default_skin=http://js.tudouui.com/bin/player2/outside/Skin_outside_12.swf&autostart=false&rurl=

  HD video
  Watch: http://hd.tudou.com/program/15950/
  Embed: http://www.tudou.com/v/elcIFKbSjno
  which redirects to:
         http://www.tudou.com/player/outside/player_outside.swf?iid=21170065&default_skin=http://js.tudouui.com/bin/player2/outside/Skin_outside_12.swf&autostart=false&rurl=

=cut

  my $videoID = 0;

  # HD video web page URL, need to extract video ID via javascript variable
  if ( $embed_url =~ m`hd.tudou.com/program/\w+` )
  {
    ( $videoID ) = ( $browser->content =~ /iid: "(\w+)"/ );
  }

  # Otherwise, get the video from the external player URL
  else
  {
    # SD video web page URL, forward to embedded URL to extract location redirect
    if ( $embed_url =~ m`tudou.com/programs/view/(.+)$` )
    {
      $embed_url = sprintf "http://www.tudou.com/v/%s", $1;
      $browser->get( $embed_url );
    }

    # Embedded URL should be a redirect, use that as the current URL
    if ( $browser->response->code eq 302 and $embed_url =~ m`tudou.com/v/(.+)$` )
    {
      $embed_url = $browser->response->header( 'Location' );
    }

    # Video ID is in the URL if we are either at the redirected URL
    # or at the embedded link that sends the redirect
    ( $videoID ) = ( $embed_url =~ m`tudou.com/player/outside/player_outside.swf\?iid=(\d+)` );
  }

  die "Couldn't extract video ID, we are out probably out of date" unless $videoID;
  debug "Using video ID $videoID";

  # Get video info; safe keys seen: YouNeverKnowThat, IAlsoNeverKnow
  # but they're not even verifying either safekey or noCatch
  # (maybe noCatch means no cache?)
  $browser->get(
    sprintf "http://v2.tudou.com/v2/kili?safekey=%s&id=%s&noCatch=%d",
    'YouNeverKnowThat', $videoID, rand( 10000 ) );

  # Fallback URL in case the first one doesn't have our video information
  if ( not $browser->success )
  {
    debug 'Using fallback tudou link for video info';
    $browser->get(
      sprintf "http://v2.tudou.com/v2/cdn?safekey=%s&id=%s&noCatch=%d",
      'YouNeverKnowThat', $videoID, rand( 10000 ) );
  }
  $check_response->( "Couldn't grab video informaton from tudou, server response was %s" );

  # Response is a plain XML document
  return parse_video_info( $browser->content );
}

# Video info is in XML format
sub parse_video_info {
  my ( $raw_xml ) = @_;

=for comment
SD XML structure:
<v
  time="101300" vi="1" ch="5" nls="0"
  title="&#19978;&#28023;&#22320;&#38081;&#37324;&#36339;&#38050;&#31649;&#33310;"
  code="wo2YLr4sc44" enable="1" logo="0" band="1">

  <a></a>
  <b>
    <f w="1" h="0" sha1="46c7a7a5f8953b0c7e07423bfaa7e6cc80c11ee6" size="3110483">
      http://121.12.103.35/flv/030/599/453/30599453.flv?key=7b63b43d51e29716ec4a7c4a218127cb4054c3
    </f>
    <f w="1" h="0" sha1="46c7a7a5f8953b0c7e07423bfaa7e6cc80c11ee6" size="3110483">
      http://125.64.131.8/flv/030/599/453/30599453.flv?key=7b63b43d51e29716ec4a7c4a218127cb4054c3
    </f>
    <f w="1" h="0" sha1="46c7a7a5f8953b0c7e07423bfaa7e6cc80c11ee6" size="3110483">
      http://124.232.132.4/flv/030/599/453/30599453.flv?key=7b63b43d51e29716ec4a7c4a218127cb4054c3
    </f>
    <f w="1" h="0" sha1="46c7a7a5f8953b0c7e07423bfaa7e6cc80c11ee6" size="3110483">
      http://123.134.67.76/flv/030/599/453/30599453.flv?key=7b63b43d51e29716ec4a7c4a218127cb4054c3
    </f>
    <f w="1" h="0" sha1="46c7a7a5f8953b0c7e07423bfaa7e6cc80c11ee6" size="3110483">
      http://125.211.196.8/flv/030/599/453/30599453.flv?key=7b63b43d51e29716ec4a7c4a218127cb4054c3
    </f>
  </b>
</v>

HD XML structure:
<v
  time="2566570" vi="" ch="22" nls="0"
  title="&#25105;&#30340;&#38738;&#26149;&#35841;&#20570;&#20027;(1-3)AA"
  code="elcIFKbSjno" enable="1" logo="0" band="0">

  <a></a>
  <b>
    <f w="1" h="0" sha1="5aa07d5920dbc600e1d7256e2a9f90c9a3e05870" size="155830642">
      http://121.12.103.43/mp4/021/170/065/21170065.f4v?key=1d0399b5a9fa36b8b7f8004a21a241cb4054c3



( run in 1.903 second using v1.01-cache-2.11-cpan-437f7b0c052 )