Skip to content

Instantly share code, notes, and snippets.

@djhworld
Created January 24, 2011 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djhworld/794158 to your computer and use it in GitHub Desktop.
Save djhworld/794158 to your computer and use it in GitHub Desktop.
downloads the latest edition of the excellent magazine from the guys at pragprog and pushes it to your kindle
#!/usr/bin/env ruby
# Quick rough and dirty script to push new issues of the excellent
# free monthly magazine from the guys at PragProg to your kindle
#
# Will download the latest magazine from the list and then will only
# download magazines when a new issue comes out
#
require 'date'
require 'fileutils'
require 'nokogiri'
require 'open-uri'
SETTINGS_FILE = File.expand_path('~/.pragprogmagazines2kindle-settings')
def get_last_issue_no
last_issue = 0
File.open(SETTINGS_FILE,'r') do |file|
last_issue = file.gets
end
return last_issue.to_i
end
begin
doc = Nokogiri::HTML(open('http://pragprog.com/magazines'))
issue_nos = []
doc.css('table.magazines h3').each do |issue| issue_nos << issue.content.chomp.strip.slice(6..100)[0..1].to_i end
latest_issue = issue_nos.max
puts DateTime.now.strftime('%d/%m/%Y %H:%M:%S')
puts "-"*80
puts "Latest issue is ##{latest_issue}"
last_issue = File.exists?(SETTINGS_FILE) ? get_last_issue_no : 0
puts "Last issue retrieved was ##{last_issue}" if last_issue > 0
if latest_issue > last_issue
puts "New issue found! Downloading..."
#construct a filepath
filename = latest_issue.to_s << ".mobi"
magazine_link = "http://pragprog.com/magazines/download/" << filename
filepath = File.expand_path("~/" << filename)
#download
puts "\n"
%x(wget #{magazine_link} -O #{filepath})
puts
#use kindlemail to send document
system("kindlemail #{filepath}")
#remove file from filesystem
FileUtils.rm(filepath)
File.open(SETTINGS_FILE,'w') do |file| file.write latest_issue end
else
puts "No new issues found"
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment