Skip to content

Instantly share code, notes, and snippets.

@gfontenot
Created March 11, 2011 16:32
Show Gist options
  • Save gfontenot/866137 to your computer and use it in GitHub Desktop.
Save gfontenot/866137 to your computer and use it in GitHub Desktop.
Ruby script to archive "liked" items to Yojimbo. Modified version of original script written by Rhet Turnbull
####!/usr/bin/ruby
# Ruby script to import an RSS feed, for example from InstaPaper or delicious, into Yojimbo
# Items already in the Yojimbo Library will not be re-imported unless you delete them from Yojimbo
# To use this, you'll need to install rb-appscript with the following two Terminal commands:
# sudo gem update --system
# sudo gem install rb-appscript
#
# If you get an with gem update, you may need to update your version of rubygems as follows:
# Get latest rubygems from http://rubyforge.org/projects/rubygems/
# Open a Terminal session
# cd ~/Downloads/
# tar xzvf rubygems-1.4.2.tgz
# cd rubygems-1.4.2
# sudo ruby ./setup.rb
#
# Then proceed with gem update and gem install per above
#
# Tested with Yojimbo 3.0.1 and OS X 10.6.6
#
# Created by Rhet Turnbull, rturnbull at gmail . com
# Version 1.1, 20110108
#
# Modified to use Instapaper's text only view by Gordon Fontenot, gordon dot fontenot at gmail dot com
# 20110311
#
# Known issues: does not do any error trapping
# Will most likely fail for some articles on first run, due to hitting the rate limit.
#
# To use: modify source and tags below to appropriate values
# Run the script from the Terminal, e.g. ./rss_yj.rb
# or use the excellent FastScripts to run the script http://www.red-sweater.com/fastscripts/
#
# See instructions at end of script to create a launch agent for automatically running the script with launchd
#
require 'rubygems'
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'appscript'
require 'syslog'
require 'uri'
include Appscript
# For logging to the system log
def log(message)
# $0 is the current script name
Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning message }
end
# set source to link to your rss feed
# Go to your "Liked" page, and check the RSS feed there to get the url
# source url must start with "http://" NOT "feed://"
source = "http://Instapaper.com/starred/rss/randomdigits"
log "Starting with source #{source}"
#set tags to the tags you want added to imported items
tags = ['instapaper']
#uncomment following line for no tags
#tags = []
#get and parse the RSS feed
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
print "RSS title: ", rss.channel.title, "\n"
print "RSS link: ", rss.channel.link, "\n"
print "RSS description: ", rss.channel.description, "\n"
print "RSS publication date: ", rss.channel.date, "\n"
print "Number of RSS items: ", rss.items.size, "\n"
#get Yojimbo object
yj = app('Yojimbo')
#loop through the RSS items and add them if not already in the Yojimbo Library
i = 0
items_added = 0
rss.items.size.times do
item_title = rss.items[i].title
escaped_link = URI.escape(rss.items[i].link, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
item_link = "http://www.instapaper.com/text?u=#{escaped_link}"
puts "Checking #{item_title} : #{item_link}"
#if it's not already in the libary, then add it
#check the comments field of the item for the original link
if (not yj.collections[its.name.eq("Library")].web_archive_items[its.comments.eq(item_link)].exists)
#Item doesn't exist in the Library, so add it
print "Adding item ", item_link, "\n"
# log "Adding item #{item_link}"
#create new item in Yojimbo
#set the comments field to the original URL from the feed
#many websites normalize the link when saving the web archive
# so Yojimbo's source_url can't be trusted
new_item = yj.make( :new =>:web_archive_item,
:with_contents => item_link,
:with_properties =>{:comments => item_link})
new_item.add_tags(tags)
items_added += 1
else
print "Ignoring existing item ", item_link, "\n";
end
i += 1
end
print "Added #{items_added} / #{rss.items.size} items\n"
log "Added #{items_added} / #{rss.items.size} items\n"
### END OF SCRIPT ###
# To schedule this to run automatically, put the following in a plist file and save to ~/LaunchAgents/com.yj.InstaPaperYjSync.plist
# then at the Terminal, type launchctl load ~/Library/LaunchAgents/com.yj.InstaPaperYjSync.plist
### Cut Here for plist file ###
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
# <key>Label</key>
# <string>com.yj.InstaPaperYjSync</string>
# <key>ProgramArguments</key>
# <array>
# <string>path/to/script.rb</string>
# </array>
# <key>StartInterval</key>
# <integer>3600</integer>
# <key>RunAtLoad</key>
# <false/>
# </dict>
# </plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment