Migrating Twitter Favorites to Instapaper in Mac OSX.

Online bookmarks have become an unmanageable mess.  Between Delicious being sold, Twitter Favorites, Instapaper, a variety of browser bookmark systems, I’ve found that there are a lot of orphaned links laying around.  I seem to keep up with Instapaper more than any other because most of the apps on my iPhone will use it as a Read Later tool.  Thus Instapaper seemed like a logical place to put all my links.

Next I Googled for any Tools that either Twitter or Instapaper might have to import and export.  Strangely, Instapaper provides a way to export your links, but not import anything.  A nice service I found that will help me in the future is http://tweetgrabber.com/ which will look for new Twitter favorites and add an Instapaper link.  However, this only helped for future Tweet Favorites.

My search for a single script that would grab the Twitter Favorites and import into Instapaper was also fruitless, so I broke the problem down and grabbed some code to do each.  First, I found this nice script with comes in ruby and functions well under OSX without any additions. Call this script twitterfavs.rb.

#!/usr/bin/ruby -w
# author: rgollapudi
# description: Script to fetch all your twitter favorites and write them to a csv file

require 'open-uri'
require 'rss/1.0'
require 'rss/2.0'

# For reading rss from a url:
# refer http://benwoodall.com/2010/10/ruby-rss-parser/
#

# Open a file for exporting all favorites from the rss
$i = 1
$csv = File.open('mytwitfavs.txt', 'w')
$username = 'TwitterUserName'

#
# gets 20 favorites from twitter rss
#
def getfavorites
  url = "http://twitter.com/favorites/#{$username}.rss"
  unless $i == 1
    url = url+"?page=#{$i}"
  end
  puts "url : " + url
  content = ''
  open(url) do |parse|
    content = parse.read
  end

  rss = RSS::Parser.parse(content, false)
  return rss
end

def writefavorites(rss)
  # Output the feeds title, URL, and number of entries
  puts "=================================="
  puts "Title: #{rss.channel.title}"
  puts "URL: #{rss.channel.link}"
  puts "Total Entries: #{rss.items.size}"
  puts "=================================="

  # Setup to read feed and parse all items
  # Change for this: parse top 5 posts, ask if more, (Y/N?), get/end

  rss.items.each do |item|
     puts "Title: #{item.title}"
     puts "Published: #{item.date}"
     puts "Description: #{item.description}"
     puts "=================================="
#     $csv << ["#{item.title}", ";; #{item.link}", ";; #{item.date}\n"]
     $csv << ["#{item.link}\n"]
  end
end
#
# => write the fetched rss to a file
#
rss = getfavorites() # => call once to initialize the loop
size = rss.items.size
writefavorites(rss)
while size > 0
  $i += 1
  rss = getfavorites()
  size = rss.items.size
  writefavorites(rss)
end

# close the file handle
$csv.close()

I don’t code in ruby, but it was easy to see that it was grabbing RSS and outputing some of the XML. I only changed this script to output the link and nothing more. Note that it seems to say there is a 20 link limit in what is output by Twitter’s RSS. Change the TwitterUserName to match your Twitter account. This is assuming your Twitter Favorites are publicly accessible.
Now I had a file called mytwitfavs.txt full of URLs. I needed some way of submitting this to Instapaper. I found a line of curl code which I then placed into the shell script below called heatinstapaper.sh to output the required curl command for each url line in the mytwitfavs.txt file.

#!/bin/sh
# Set the field seperator to a newline
IFS="
"

username="email@address.com"
password="UR_p455werd"

password=`./urlencode.php $password`
# Loop through the file
for line in `cat mytwitfavs.txt`;do
#encode the link and then send it through to instapaper
url=`./urlencode.php $line`

cmd='/usr/bin/curl -v -d username="'$username'" -d password="'$password'" -d url="'$url'" "http://www.instapaper.com/api/add"'
echo $cmd
echo

done

Note that this script is not doing anything with the output, just printing.  Change the username and password lines to match yours for Instapaper.  Complex passwords require the urlencode.  But OSX had no urlencode!  I whipped this up and called it urlencode.php.

#!/usr/bin/php
<?php
$mystr = $argv[1];
echo urlencode($mystr) . "\n";
?>

And now we have a way of calling the script.  The shell already knows to call it from the same folder, and with the php extension.  Make all these files executable with a chmod +x and pipe the output of heatinstapaper.sh into /bin/sh and everything should work out with a 201 response for each line.  All of my Twitter Favorites are now in Instapaper and Tweetgrabber will take care of the rest.

Advertisements

One thought on “Migrating Twitter Favorites to Instapaper in Mac OSX.

Comments are closed.