root/trunk/plagger/lib/Plagger/Plugin/Subscription/Planet.pm

Revision 1479 (checked in by miyagawa, 14 years ago)

BIG CHANGE: decode YAML from UTF-8 to Unicode when we load config files.
Fixes #103

Line 
1 package Plagger::Plugin::Subscription::Planet;
2 use strict;
3 use base qw( Plagger::Plugin::Subscription::Config );
4
5 use Encode;
6 use URI::Escape;
7
8 sub load {
9     my($self, $context) = @_;
10
11     my $lang = $self->conf->{lang} || 'default';
12     $lang = [ $lang ] unless ref $lang;
13
14     $self->load_assets(
15         File::Find::Rule->file->name([ map "$_.yaml", @$lang ]),
16         sub {
17             my($file) = @_;
18             my $data = YAML::LoadFile($file);
19             push @{ $self->{engines} }, @{ $data->{engines} };
20         },
21     );
22
23     for my $site (@{ $self->{engines} }) {
24         my $site_url = $site; # copy
25
26         # use eval ... die to skip if there's no url/keyword
27         eval {
28             $site_url =~ s{{([\w\-\:]+)}}{
29                 my($key, $encoding) = split /:/, $1;
30
31                 my $data = $self->conf->{$key} or die "$key is not there";
32                 if ($encoding && $encoding ne 'utf-8') {
33                     $data = Encode::encode($encoding, $data);
34                 } else {
35                     $data = Encode::encode_utf8($data);
36                 }
37
38                 my $chunk = URI::Escape::uri_escape($data);
39                 $chunk =~ s/%20/+/g; # hack
40                 $chunk;
41             }eg;
42             push @{$self->conf->{feed}}, { url => $site_url }
43         };
44     }
45
46     $self->SUPER::load($context);
47 }
48
49 1;
50
51 __END__
52
53 =head1 NAME
54
55 Plagger::Plugin::Subscription::Planet - Ego search subscription
56
57 =head1 SYNOPSIS
58
59   - module: Subscription::Planet
60     config:
61       keyword: Plagger
62       lang: en
63
64 =head1 DESCRIPTION
65
66 This plugin gives a handy way to subscribe to dozens of feed / web
67 search engine results by just supplying keywords.
68
69 =head1 CONFIG
70
71 =over 4
72
73 =item keyword
74
75 The keyword to use as a query in web search engines. Required.
76
77 =item lang
78
79 Language code to either 1) specify list of search engines or 2) pass
80 to search query.  Optional.
81
82 For example, technorati.jp will be added if you use I<ja>, while
83 technorati.com will be if you use I<en>. Default is to search
84 everything.
85
86 =back
87
88 =head1 EXAMPLES
89
90   # search "Plagger" on default engines
91   - module: Subscription::Planet
92     config:
93       keyword: Plagger
94
95   # search "Pokemon" on Japanese search engines
96   - module: Subscription::Planet
97     config:
98       keyword: Pokemon
99       lang: ja
100
101   # search "Plagger" and pages linking to "http://plagger.org/"
102   - module: Subscription::Planet
103     config:
104       keyword: Plagger
105       url: http://plagger.org/
106
107 =head1 AUTHOR
108
109 youpy
110
111 Tatsuhiko Miyagawa
112
113 =head1 SEE ALSO
114
115 L<Plagger>
116
117 =cut
Note: See TracBrowser for help on using the browser.