Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 94 lines (82 sloc) 2.373 kb
00f937e @dann for delicious
authored
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use utf8;
5 use Config::Pit;
6 use Carp ();
7 use LWP::UserAgent;
8 use URI::QueryParam;
9 use XML::RSS;
10 use Scalar::Util qw(reftype);
11 use List::MoreUtils qw(any);
12 use utf8;
13
14 # Config
15 my $DELICIOUS_API_ENDPINT = "http://delicious.com/rss/dann";
16 my $ENTRY_TAG = "tokindle";
17
18 # Main
19 my $INSTANT_PAPER_API_ENDPOINT = "https://www.instapaper.com/api/add";
20 my $CONFIG;
21
22 main();
23 exit;
24
25 # Logic
26 sub main {
27 init();
28 my $entries = fetch_entriies_from_hatebu();
29 post_entries_to_instantpaper($entries);
30 }
31
32 sub init {
33 $CONFIG = pit_get(
34 "delicious2instant",
35 require => {
36 "instant_username" => "your username for instantpaper",
37 "instant_password" => "your password for instantpaper",
38 }
39 );
40 die 'pit_get failed.' if !%$CONFIG;
41 }
42
43 sub fetch_entriies_from_hatebu {
44 say("--> Fetching entries from delicious");
45 my $ua = LWP::UserAgent->new;
46 $ua->agent('Mozilla/8.0');
47 my $delicious_api_endpoint = URI->new($DELICIOUS_API_ENDPINT);
48 my $res = $ua->get($delicious_api_endpoint);
49
50 my $rss = _parse_rss( $res->content );
51 return $rss->{'items'} || [];
52 }
53
54 sub _parse_rss {
55 my $rss_text = shift;
56 my $rss = XML::RSS->new;
57 eval { $rss->parse($rss_text) }
58 or Carp::croak( "Parsing content failed: " . $@ );
59 $rss;
60 }
61
62 sub post_entries_to_instantpaper {
63 my $entries = shift;
64 say("--> Posting delicious entries to InstantPaper ... ");
65 foreach my $entry (@$entries) {
66 my $tag_text = $entry->{'dc'}->{'subject'};
67 my @tags =();
68 @tags = split ' ', $tag_text if $tag_text;
69 my $contain_tag = any { $_ eq $ENTRY_TAG } @tags;
70 post_entry_to_instanpaper($entry) if $contain_tag;
71 }
72 }
73
74 sub say {
75 my $text = shift;
76 print "$text\n";
77 }
78
79 sub post_entry_to_instanpaper {
80 my $entry = shift;
81 say("==> Posting '$entry->{title}'");
82 my $ua = LWP::UserAgent->new;
83 my $endpoint_uri = URI->new($INSTANT_PAPER_API_ENDPOINT);
84 $endpoint_uri->query_form_hash(
85 username => $CONFIG->{instant_username},
86 password => $CONFIG->{instant_password},
87 url => $entry->{link},
88 );
89 my $res = $ua->get($endpoint_uri);
90 unless ($res->is_success) {
91 print STDERR $res->status_line, "\n";
92 }
93 }
Something went wrong with that request. Please try again.