Changeset 104

Show
Ignore:
Timestamp:
04/29/06 23:42:04 (3 years ago)
Author:
miya
Message:

some fixes and new features.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plagger/trunk/lib/Plagger/Plugin/CustomFeed/YouTube.pm

    r86 r104  
    4040    $context->log( info => 'Getting YouTube search results.' ); 
    4141    my $ua = Plagger::UserAgent->new; 
    42     my $res = $ua->mirror("http://youtube.com/results?search=$q&sort=video_date_uploaded" => $file); 
    43  
    44     if($res->is_error){ 
    45         $context->log( error => $res->status_line ); 
    46         return; 
    47     } 
    48  
    49     open my $fh, "<:encoding(utf-8)", $file 
    50         or return $context->log(error => "$file: $!"); 
    5142 
    5243    my $feed = Plagger::Feed->new; 
    5344    $feed->type('youtubesearch'); 
    54  
    55     my (@videos, $data); 
    56     while (<$fh>) { 
    57         m!<div class="moduleTitle">(.*)</div>! 
    58             and $feed->title($1); 
    59         m!<img src="(.*)" class="moduleEntryThumb" width="(\d+)" height="(\d+)">! 
    60             and do { 
    61                 $data->{image}->{url}    = $1; 
    62                 $data->{image}->{width}  = $2; 
    63                 $data->{image}->{height} = $3; 
     45    $feed->title('YouTube Search - ' . $self->conf->{query}); 
     46 
     47    my $page = $self->conf->{page} || 1; 
     48    my $sort = $self->conf->{sort} || 'video_date_uploaded'; 
     49    for ( 1 .. $page ){ 
     50        my $res = $ua->mirror("http://youtube.com/results?search=$q&sort=$sort&page=$_" => $file); 
     51 
     52        if($res->is_error){ 
     53            $context->log( error => $res->status_line ); 
     54            return; 
     55        } 
     56 
     57        open my $fh, "<:encoding(utf-8)", $file 
     58            or return $context->log(error => "$file: $!"); 
     59 
     60        my (@videos, $data, $title_flag, $tag_flag); 
     61        while (<$fh>) { 
     62            # get title 
     63            m!<div class="title">! 
     64                and $title_flag = 1; 
     65            m!<a href="/watch\?v=([^&]+)&search=[^>]+">(.+)</a>! 
     66                and do { 
     67                    if($title_flag){ 
     68                        $data->{title} = $2; 
     69                        $data->{id} = $1; 
     70                        $title_flag = 0; 
     71                    } 
     72                }; 
     73            m!<img src="(http://static\d+.youtube.com/[^">]+/1.jpg)" class="vimgSm" />! 
     74                and $data->{image}->{url} = $1; 
     75            m!<div class="desc">(.*)</div>! 
     76                and $data->{description} = $1; 
     77            m!<td><span class="grayText">Tags:</span></td>! 
     78                and $tag_flag = 1; 
     79            m!(<td><a href="/results\?search=.*)! 
     80                and do { 
     81                    if($tag_flag){ 
     82                        $data->{tags} = $1; 
     83                        $tag_flag = 0; 
     84                    } 
     85                }; 
     86            m!profile\?user=([^"]+)! 
     87                and do { 
     88                    $context->log( info => 'Got ' . $data->{title}); 
     89                    $data->{author} = $1; 
     90                    my $entry = Plagger::Entry->new; 
     91                    $entry->title($data->{title}); 
     92                    $entry->link('http://youtube.com/watch?v=' . $data->{id}); 
     93                    $entry->icon({ 
     94                        url    => $data->{image}->{url}, 
     95                        #width  => $data->{image}->{width}, 
     96                        #height => $data->{image}->{height}, 
     97                    }); 
     98                    $entry->summary($data->{description}); 
     99                    $entry->body($data->{description}); 
     100                    $entry->author($data->{author}); 
     101 
     102                    # tags 
     103                    while( $data->{tags} =~ /<a href="\/results\?search=.*">(.*)<\/a>/gms){ 
     104                        $entry->add_tag($1); 
     105                    } 
     106 
     107                    # enclosure 
     108                    my $res = $ua->fetch($entry->link); 
     109                    if($res->is_error){ 
     110                        $context->log( error => $res->status_line ); 
     111                        return; 
     112                    } 
     113                    if($res->content =~ /&t=([^&]+)/gms){ 
     114                        $entry->meta->{enclosure}->{url} = 'http://youtube.com/get_video?video_id=' . $data->{id} . "&t=$1"; 
     115                        $entry->meta->{enclosure}->{type} = 'application/x-shockwave-flash'; 
     116                    } 
     117 
     118                    $feed->add_entry($entry); 
     119                    $data = {}; 
    64120            }; 
    65         m!<div class="moduleEntryTitle"><a href="/(watch\?v=.*)&search=.*">(.*)</a> </div>! 
    66             and do { 
    67                 $data->{title} = $2; 
    68                 $data->{path} = $1; 
    69             }; 
    70         m!<div class="moduleEntryDescription">(.*)<br>! 
    71             and $data->{description} = $1; 
    72         m!Tags // (.*)! 
    73             and $data->{tags} = $1; 
    74         m!profile\?user=([^"]+)! 
    75             and do { 
    76                 $data->{author} = $1; 
    77                 my $entry = Plagger::Entry->new; 
    78                 $entry->title($data->{title}); 
    79                 $entry->link('http://youtube.com/' . $data->{path}); 
    80                 $entry->icon({ 
    81                     url    => $data->{image}->{url}, 
    82                     width  => $data->{image}->{width}, 
    83                     height => $data->{image}->{height}, 
    84                 }); 
    85                 $entry->summary($data->{description}); 
    86                 $entry->body($data->{description}); 
    87                 $entry->author($data->{author}); 
    88  
    89                 # tags 
    90                 while( $data->{tags} =~ /<a href="\/results\?search=.*">(.*)<\/a>/gms){ 
    91                     push @{$entry->tags}, $1; 
    92                 } 
    93  
    94                 # enclosure 
    95                 $context->log( info => 'Getting http://youtube.com/' . $data->{path}); 
    96                 my $res = $ua->fetch('http://youtube.com/' . $data->{path}); 
    97                 if($res->is_error){ 
    98                     $context->log( error => $res->status_line ); 
    99                     return; 
    100                 } 
    101                 if($res->content =~ /embed src=&quot;([^&]*)&quot/gms){ 
    102                     $entry->meta->{enclosure}->{url} = $1; 
    103                     $entry->meta->{enclosure}->{type} = 'application/x-shockwave-flash'; 
    104                 } 
    105  
    106                 $feed->add_entry($entry); 
    107                 $data = {}; 
    108             }; 
     121        } 
    109122    } 
     123 
    110124    $context->update->add($feed); 
    111125} 
     
    113127sub tag { 
    114128    my($self, $context, $args) = @_; 
    115  
    116     $XML::RSS::AUTO_ADD = 1; 
    117129 
    118130    my $q = $self->conf->{query}; 
     
    167179  - module: CustomFeed::YouTube 
    168180    config: 
    169       type: search|tag 
     181      feed_type: search|tag 
    170182      query: keyword|tag 
     183      sort: video_date_uploaded 
     184      page: 5 
    171185 
    172186=head1 DESCRIPTION 
     
    178192=over 4 
    179193 
    180 =item type 
     194=item feed_type 
    181195 
    182196'search' fetches the the result of YouTube search.'tag' fetches the rss of a specified tag. 
     
    186200Specify search queries or rss tag name. 
    187201 
     202=item sort 
     203 
     204Set sort condition.Available condisions are below.Default is video_date_uploaded. 
     205 
     206 relevance 
     207 video_date_uploaded 
     208 title_sort 
     209 video_view_count 
     210 rating_sort 
     211 
     212=item page 
     213 
     214Number of pages of search result you get.(Valid when feed_typs is search.) 
     215Default is 1. 
     216 
    188217=back 
    189218