Dumping URLs into Pocket

I use Pocket as my morning paper for my commute and centralized place for collecting things to read, with URLs fed into it from IFTTT, Feedly, plus Safari on Mac and iOS8. For a while I even used NYTimes URLs fed from IFTTT as http://www.gmodules.com/ig/proxy?url= URLs which would avoid the paywall.

I like to dump as much as possible into the reader so that I have a variety to ready from. This means any digest emails or other pages full of URLs from Mashable or Medium are ideal to throw into this. Forwarding the email to Pocket will only get you the first link – useless when there could be 10-15 links. To make this easier I created a Service with Automator called ExtractURLs2Pocket which consists of a “Run Shell Script” block feeding into a Copy To Clipboard block in case you need the output:

osascript -e ‘the clipboard as «class RTF »’ | perl -ne ‘print chr foreach unpack(“C*”,pack(“H*”,substr($_,11,-3)))’ | ~/Dropbox/rtf2html.pl -noimages – | ~/Dropbox/geturls.pl | /usr/bin/egrep -v ‘(w3.org|opt|unsub|email-settings)’ | /usr/bin/sort -u | ~/Dropbox/add2pocket.pl

What this does is grab the clipboard as RTF and convert it to a text format, which I then convert to HTML, grab the href URLs, grep to filter out any URLs that might be unsubscribe links, sort and uniq the list, then feed that to my Pocket add script.

rtf2html.pl is the sample script in RTF::HTMLConverter from https://github.com/lvu/rtf2html

geturls.pl uses the list_uris function of URI::Find::Simple:

#!/usr/bin/perl

use URI::Find::Simple qw( list_uris );
my @urllist;
my $url;
while (<>) {
@urllist = list_uris($_);
foreach $url (@urllist)
{
print “$url\n”;
}
}

add2pocket.pl uses WebService::Pocket to use the Pocket API:

#!/usr/bin/perl
use WebService::Pocket;

my $line;
my $url_hash;
my @urls_array;
my $p = WebService::Pocket->new(
username => ‘pocket_username’,
password => ‘pocket_password’,
);

while (<>)
{

$line = $_;
chomp($line);
print “$line\n”;
$url_hash = {url => $line};
push(@urls_array,$url_hash);
}

#break array into chunks of 9 URLs so the add won’t choke
while (my @subarr = splice @urls_array, 0, 9)
{
$p->add([@subarr]);
}

Once you have this in place, just highlight some text anywhere on the Mac, copy it, right-click, select Service -> ExtractURLs2Pocket. It’s even pretty fast!

Advertisements