File Coverage

blib/lib/WWW/Rediff/iShare.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WWW::Rediff::iShare;
2              
3 3     3   109363 use strict;
  3         7  
  3         125  
4              
5 3     3   16 use vars qw( $VERSION );
  3         8  
  3         219  
6             $VERSION = "0.05";
7              
8 3     3   3815 use HTML::TagParser;
  3         61136  
  3         216  
9 3     3   2836 use LWP::Simple;
  3         231717  
  3         35  
10 3     3   4581 use URI::Encode;
  3         4951  
  3         156  
11 3     3   2837 use URI::Fetch;
  3         262080  
  3         113  
12 3     3   2915 use XML::Simple;
  0            
  0            
13             use Carp qw(cluck);
14             use String::Random;
15             use FLV::ToMP3;
16              
17             sub new {
18             my ( $class, %args ) = @_;
19             my $query_link = 'http://ishare.rediff.com/';
20             my $self = {
21             base_url => $query_link,
22             %args,
23             };
24             bless $self, $class or Carp::croak "Can't bless $class: $!";
25             return $self;
26             }
27              
28             sub search {
29             my ( $self, $term, $stream_type ) = @_;
30              
31             $stream_type = $self->_validate($stream_type);
32             $self->{stream_type} = $stream_type;
33              
34             my $query_link = $self->{'base_url'} . "$stream_type/$term";
35             my $uri = URI::Encode->new();
36             my $encoded_url = $uri->encode($query_link);
37              
38             return $self->_get_result($encoded_url);
39             }
40              
41             sub get_stream_url {
42             my ( $self, $term, $stream_type ) = @_;
43             return $self->search( $term, $stream_type );
44             }
45              
46             sub _get_result {
47             my ( $self, $url ) = @_;
48              
49             my $base_url = $self->{'base_url'};
50             my $stream_type = $self->{stream_type};
51              
52             my $html = HTML::TagParser->new($url);
53             my @tag_lists = $html->getElementsByTagName("a");
54              
55             my $results;
56             my $result_found = 0;
57             foreach my $each_tag (@tag_lists) {
58             my $attr = $each_tag->attributes;
59             foreach my $key ( sort keys %$attr ) {
60             my $match_this = $base_url . $stream_type . "/";
61             if ( $attr->{'href'} =~ m/$match_this/ ) {
62             my $link = $attr->{'href'};
63             $link =~ /.com\/.*\/.*\/(.*)\/(\d+)$/ig;
64             my $title_id = $1;
65             my $file_id = $2;
66             if ( !$results->{$file_id} ) {
67             $results->{$file_id} =
68             $self->_get_file_info( $file_id, $title_id )
69             if ( $file_id && $title_id );
70             $result_found = 1;
71             }
72             }
73             }
74             }
75             if ($result_found) {
76             my @result_arr =
77             map { $results->{$_} } sort { $a <=> $b } keys %$results;
78             return \@result_arr;
79             }
80             else {
81             return "No result found";
82             }
83             }
84              
85             sub _get_file_info {
86             my ( $self, $file_id, $title_id ) = @_;
87              
88             my $stream_url = $self->_get_stream_url($file_id);
89             my $stream_xml_url = URI::Fetch->fetch($stream_url);
90             my $stream_xml_content = $stream_xml_url->content();
91             my $file_details;
92              
93             if ( $stream_xml_content !~ /XML Parsing Error|Servers are busy/ ) {
94              
95             my $xmlobj = new XML::Simple;
96             my $file_info = $xmlobj->XMLin($stream_xml_content);
97              
98             if ( $file_info->{video} ) {
99             my $video_details = $file_info->{video};
100             $title_id =~ s/-/ /ig;
101             $file_details->{path} = $video_details->{path};
102             $file_details->{time} = $video_details->{duration};
103             $file_details->{comment} = ucfirst($title_id);
104             }
105             else {
106             if ( !$title_id ) {
107             foreach my $each_song ( keys %{ $file_info->{track} } ) {
108             if ( $file_info->{track}->{$each_song}->{link} =~
109             /$file_id/ )
110             {
111             $title_id = $each_song;
112             }
113             }
114             }
115             $file_details = $file_info->{track}->{$title_id}
116             if ( $file_info->{track}->{$title_id} );
117             }
118             $file_details->{file_id} = $file_id;
119             }
120              
121             return $file_details;
122             }
123              
124             sub _get_file_stream_type {
125             my ( $self, $file_id ) = @_;
126             my $rand_pattern = new String::Random;
127             my $base_url = $self->{'base_url'};
128              
129             my $file_web_url =
130             $base_url
131             . "video/"
132             . $rand_pattern->randpattern("cccc") . "/"
133             . $rand_pattern->randpattern("cccc") . "/"
134             . $file_id;
135              
136             my $file_url = URI::Fetch->fetch($file_web_url);
137             my $file_url_content = $file_url->content();
138             my $stream_type = 'music';
139              
140             if ( $file_url_content !~ /The video you want to view does not exist./ ) {
141             $stream_type = 'video';
142             }
143              
144             $self->{stream_type} = $stream_type;
145             return $stream_type;
146             }
147              
148             sub _get_stream_url {
149             my ( $self, $file_id ) = @_;
150              
151             my $base_url = $self->{'base_url'};
152             my $stream_type = $self->_get_file_stream_type($file_id);
153              
154             return ( $stream_type eq 'video' )
155             ? $base_url . "embedplayer_config_REST.php?content_id=$file_id&x=3"
156             : $base_url . "audio_config_REST.php?audioid=$file_id&autoplay=true";
157             }
158              
159             sub get_player_embedded_code {
160             my ( $self, $file_id ) = @_;
161              
162             my $base_url = $self->{'base_url'};
163             my $stream_type = $self->_get_file_stream_type($file_id);
164              
165             my $player_type = ( $stream_type eq 'video' ) ? "splayer" : "aplayer";
166              
167             my $player_url_type = ( $stream_type eq 'video' ) ? "videoURL" : "audioURL";
168              
169             my $player_source =
170             ( $stream_type eq 'video' )
171             ? "http://ishare.rediff.com/images/svplayer_ad_20100212_2.swf"
172             : "http://ishare.rediff.com/images/saplayer2101.swf";
173              
174             my $stream_url = $self->_get_stream_url($file_id);
175              
176             my $embedded_code = "
177            
178             width=\"100%\"
179             height=\"322\"
180             wmode=\"transparent\"
181             type='\"application/x-shockwave-flash\"
182             allowfullscreen=\"true\"
183             allowscriptaccess=\"always\"
184             name=\"$player_type\"
185             flashvars=\"$player_url_type=$stream_url\"
186             src=\"$player_source\"/>";
187              
188             return $embedded_code;
189              
190             }
191              
192             sub download {
193             my ( $self, $file_id, $type ) = @_;
194              
195             my $file_info = $self->_get_file_info($file_id);
196             my $stream_type = $self->{stream_type};
197             my $download_path = $file_info->{path};
198              
199             print "Downloading file ... \n";
200             getstore( $download_path, $file_id . '.flv' );
201              
202             if ( $stream_type ne 'video' ) {
203             my $converter = FLV::ToMP3->new("$file_id.flv");
204             print "Converting to MP3 ... \n";
205             $converter->parse_flv("$file_id.flv");
206             $converter->save("$file_id.mp3");
207             unlink("$file_id.flv");
208             }
209             print "Downloading done ... \n";
210             }
211              
212             sub _validate {
213             my ( $self, $stream_type ) = @_;
214             $stream_type =~ tr/[A-Z]/[a-z]/;
215             if ( $stream_type !~ /music|video|audio/ig ) {
216             Carp::croak "Not a valid stream type : $stream_type ";
217             }
218             $stream_type = 'music' if ( $stream_type eq 'audio' );
219             return $stream_type;
220             }
221              
222             1;
223              
224             __END__