What's in your Pocket?

Here’s a real #firstworldproblem: every afternoon, I receive Dave Pell’s excellent NextDraft newsletter and add the articles to my Pocket queue, so I can read them on the subway. The problem is, there are like 20+ links in each newsletter and adding each one to Pocket requires a ton of clicking & swiping.

Fortunately, I hacked together a clever solution using a Unqueue, a repo I found on GitHub.

Written by Jesse Shawl, Unqueue is a PHP web app that archives unread articles in your Pocket queue. Got 56 unread articles and want to delete them all? No problem, Unqueue can do it in 2 clicks.

And while that’s kind of the opposite of what I want to do, Jesse’s code was the perfect starting point for me, because it handles all the login and OAuth authentication junk required by Pocket’s API. All I really had to do was modify the queuing code.

I started by scraping the current issue of NextDraft, extracting all of the story links from the HTML, and storing them in a session variable. I used an xpath query to do all the heavy lifting:

```php <?php // get source of current issue & ignore all errors libxml_use_internal_errors(TRUE); $dom = new DOMDocument; $dom->loadHTMLFile(‘http://nextdraft.com/current’); libxml_clear_errors();

// setup xpath query // only want links from paragraph tags in blurb-content class divs $xp = new DOMXpath($dom); $links = $xp->query(‘//div[@class=”blurb-content”]/p/a/@href’);

// load up array with links & store data in session variable $payload = array(); for ($i = 0; $i < $links->length; $i++) { $payload[] = $links->item($i)->nodeValue; }

// STORE URLS FOR LATERS $_SESSION[‘to_queue’] = $payload; ?> ```

After that, I used the OAuth tokens which Unqueue obtained automagically, looped through the array of links, and made calls to Pocket’s /add API endpoint. (FYI, it’d be more efficient to use the /send end point in the future.)

That’s it, really. You can see it in action below:

Unqueue saved me lots of time futzing with OAuth. If you’re interested in hacking with Pocket, it’s the first repo you should look at.

I call my hack NextPocket and you can use it right now, provided you have a Pocket account. I may also extend it for Instapaper or build a shell script instead of a web app, but right now, this is a perfect solution for me. And isn’t that what great hacks are all about?

Comments :)
2017 Neal Shyam