Skip to content

Instantly share code, notes, and snippets.

@falsefalse
Created November 20, 2011 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falsefalse/1379572 to your computer and use it in GitHub Desktop.
Save falsefalse/1379572 to your computer and use it in GitHub Desktop.
Add all LaterLoop unread links to Instapaper
node_modules
#!/usr/bin/env coffee
###
To clear your instapaper account run from dev tools console:
window.confirm = function() { console.log(arguments[0]); return true }
[].slice.call( document.querySelectorAll('#bookmark_list a.deleteLink'), 0).forEach( function(link) { ajax2_delete.call(link) })
###
http = require "http"
querystring = require "querystring"
jsdom = require "jsdom"
class Laterloop
getName = (href) -> (href.match /\/go\/(.+?)\//)[1]
constructor: (options) ->
# watch for trailing slash, it issues 301 redirect otherwise
@url = "http://m.laterloop.com/m/#{options.key}/"
fetch: (callback) ->
console.log "Fetching #{@url}..."
jsdom.env
html: @url
features:
QuerySelector: true
FetchExternalResources: false
ProcessExternalResources: false
done: (errors, window) =>
links = window.document.querySelectorAll('#links .link a')
console.log "Found #{links.length} link(s)"
@links = ({
name: getName link.href
title: link.innerHTML
resolve: resolve
} for link in links)
callback @links
# link private method
resolve = (callback) ->
req = http.request
method: "GET"
host: "laterloop.com"
path: "/go/#{this.name}"
req.on "response", (response) =>
if response.headers and response.headers.location
this.link = response.headers.location
callback this if callback
console.log "Resolved #{this.name}\nto #{this.link}"
req.end()
class Instapaper
constructor: (@options) ->
add: (link) =>
post = querystring.stringify(
username: @options.username
password: @options.password
url: link.link
title: link.title
)
req = http.request
host: "www.instapaper.com"
path: "/api/add"
method: "POST"
headers:
"Content-Type": "application/x-www-form-urlencoded"
"Content-Length": post.length
req.write post
req.on "response", (response) ->
if response.statusCode == 201
console.log "=> Saved #{link.link}"
else
console.log "=> Status Code: #{response.statusCode},\nLink: #{link}"
req.end()
KEY_NOTE = """
To obtain Laterloop Mobile key open http://m.laterloop.com,
copy logo URL, you need http://m.laterloop.com/m/<this part>/
"""
argv = (require "optimist")
.usage("Usage: $0 -k [string] -u [string] -p [string]\n\n#{KEY_NOTE}")
.options({
k:
alias: "key"
desc: "Laterloop Mobile key"
u:
alias: "username"
desc: "Instapaper user name"
p:
alias: "password"
desc: "Instapaper password"
})
.demand(["k", "u", "p"])
.argv
ll = new Laterloop
key: argv.key
ip = new Instapaper
username: argv.username
password: argv.password
ll.fetch (links) -> link.resolve ip.add for link in links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment