root/trunk/plagger/bin/plagger-search

Revision 1530 (checked in by ko, 2 years ago)

use Term::Encoding to detect query and results encoding automatically.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Encode ();
6 use FindBin;
7 use Getopt::Long;
8
9 use lib File::Spec->catdir($FindBin::Bin, '..', 'lib');
10 use Plagger;
11
12 my $path = "$FindBin::Bin/../config.yaml";
13 GetOptions("--config=s", \$path);
14 Getopt::Long::Configure("bundling"); # allows -c
15
16 my $context = Plagger->new(config => $path);
17 my $query = decode($ARGV[0]);
18
19 my @feeds = $context->search($query);
20
21 my $feed = $feeds[0];
22 binmode STDOUT, ":utf8" unless $context->conf->{log}->{encoding};
23 exit unless $feed;
24 print "Search for '" . encode($query) . "': ", $feed->count , " entries found.\n\n";
25
26 for my $entry ($feed->entries) {
27     print encode($entry->title), $entry->author ? "(by " . encode($entry->author) . ")" : '', "\n";
28     print encode($entry->body); # summary
29     print $entry->permalink, "\n";
30 }
31
32 sub decode {
33     my $str = shift;
34     if ($context->conf->{log}->{encoding}) {
35         $str = Encode::decode($context->conf->{log}->{encoding}, $str);
36     } else {
37         $str = Encode::decode_utf8($str);
38     }
39     return $str;
40 }
41
42 sub encode {
43     my $str = shift;
44     if ($context->conf->{log}->{encoding}) {
45         $str = Encode::decode_utf8($str) unless utf8::is_utf8($str);
46         $str = Encode::encode($context->conf->{log}->{encoding}, $str);
47     }
48     return $str;
49 }
Note: See TracBrowser for help on using the browser.