Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple script to save your entire Pocket list (unread + archived) in PDF form.
Pocket Export.rb
My first 'real' Ruby script (hello world)!
More info here:
pocket_export requires the following gems:
pocket_export requires the following packages (install them with your system's package manager):
wkhtmltopdf -
**make sure that dependencies are installed first**
Go to, and download the HTML file with your pocket data. Then, run this script with
the full path to the HTML file supplied as an argument (e.g. ~/Downloads/ril_export.html). The script will begin downloading
items immediately, and will save download files in ./pocket_export_data. Errors, if encountered, are logged in pocket_export_errors.log
This process can potentially be fairly CPU-intensive, as all pages are downloaded and rendered as PDFs. If you have many items in your list, the process is
going to take a while.
require 'curb'
require 'open-uri'
require 'nokogiri'
if ARGV.length < 1
abort("pocket_export.rb /path/to/ril_export.html")
pocket_data = ARGV[0]
Dir.mkdir("./pocket_export_data/") unless File.exists?("./pocket_export_data/")
Nokogiri::HTML(open(pocket_data)).css('a').each { |link|
# Set link to value of href attribute of <a> tag.
link = link['href']
# Follow any redirects until final destination is found (url shorteners etc).
curl = Curl::Easy.perform(link.gsub("\n",'')) do |curl|
curl.head = true
curl.follow_location = true
# Fetch the webpage title for use in the filename.
title = Nokogiri::HTML(open(curl.last_effective_url)).at('title').text.gsub("'", "").gsub('"','')
puts "\n\n\n***Downloading #{title} (#{link})..."
# Run wkhtmltopdf
system("wkhtmltopdf '#{link}' ./pocket_export_data/'#{title}.pdf'")
# Catch and log any exceptions.
puts "\n\n\n!!!Downloading #{link} FAILED!!\n\n\n"'./pocket_export_data/pocket_export_errors.log', 'a') { |errorlog|
errorlog.write("Error: " << a << "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment