File Coverage

blib/lib/FlashVideo/Search.pm
Criterion Covered Total %
statement 9 48 18.7
branch 0 16 0.0
condition n/a
subroutine 3 6 50.0
pod 0 3 0.0
total 12 73 16.4


line stmt bran cond sub pod time code
1             # Part of get-flash-videos. See get_flash_videos for copyright.
2             package FlashVideo::Search;
3              
4 1     1   1562 use strict;
  1         2  
  1         36  
5 1     1   6 use Carp;
  1         2  
  1         60  
6 1     1   6 use FlashVideo::Utils;
  1         2  
  1         903  
7              
8             # Sites which support searching
9             my @sites_with_search = ('4oD', 'GoogleVideoSearch');
10              
11             sub search {
12 0     0 0   my ($class, $search, $max_per_site, $max_results) = @_;
13              
14             # Preload search sites
15 0           my @search_sites = map { FlashVideo::URLFinder::_load($_) } @sites_with_search;
  0            
16              
17             # If a user searches for "foo something", check to see if "foo" is a site
18             # we support. If it is, only search that site.
19 0 0         if ($search =~ /^(\w+) \w+/) {
20 0           my $possible_site = ucfirst lc $1;
21              
22 0           debug "Checking to see if '$possible_site' in '$search' is a search-supported site.";
23              
24 0           my $possible_package = FlashVideo::URLFinder::_load($possible_site);
25              
26 0 0         if ($possible_package->can("search")) {
27             # Only search this site
28 0           debug "Search for '$search' will only search $possible_site.";
29              
30             # Remove the site name from the search string
31 0           $search =~ s/^\w+ //;
32              
33 0           return search_site($possible_package, $search, "site", $max_results);
34             }
35             }
36              
37             # Check to see if any plugins have a search function defined.
38 0           my @plugins = App::get_flash_videos::get_installed_plugins();
39              
40 0           foreach my $plugin (@plugins) {
41 0           $plugin =~ s/\.pm$//;
42              
43 0           my $plugin_package = FlashVideo::URLFinder::_load($plugin);
44              
45 0 0         if ($plugin_package->can("search")) {
46 0           debug "Plugin '$plugin' has a search method.";
47              
48 0           unshift @search_sites, $plugin_package;
49             }
50             else {
51 0           debug "Plugin '$plugin' doesn't have a search method.";
52             }
53             }
54              
55             # Call each site's search method - this includes plugins and sites
56             # defined in @sites_with_search.
57 0           my @results = map { search_site($_, $search, "all", $max_per_site) } @search_sites;
  0            
58              
59             # Return all results, trimming if necessary.
60 0           trim_resultset(\@results, $max_results);
61              
62 0           return @results;
63             }
64              
65             sub search_site {
66 0     0 0   my($search_site, $search, $type, $max) = @_;
67              
68 0           debug "Searching '$search_site' for '$search'.";
69              
70 0 0         if (my @site_results = eval { $search_site->search($search, $type) }) {
  0 0          
71 0           debug "Found " . @site_results . " results for $search.";
72              
73 0           trim_resultset(\@site_results, $max);
74 0           return @site_results;
75             }
76             elsif($@) {
77 0           info "Searching '$search_site' failed with: $@";
78             }
79             else {
80 0           debug "No results found for '$search'.";
81             }
82              
83 0           return ();
84             }
85              
86             sub trim_resultset {
87 0     0 0   my ($results, $max) = @_;
88              
89 0 0         croak "Must be supplied a reference to resultset" unless ref($results) eq 'ARRAY';
90 0 0         croak "No max supplied" unless $max;
91              
92 0 0         if (@$results > $max) {
93 0           debug "Found " . @$results . " results, trimming to $max.";
94 0           splice @$results, $max;
95             }
96             }
97              
98             1;