Last active
February 10, 2017 10:42
-
-
Save fourdollars/6061252 to your computer and use it in GitHub Desktop.
A little LiveScript to get personal information from https://launchpad.net.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lsc | |
require! fs | |
require! request | |
require! prompt | |
{map} = require \prelude-ls | |
{spawn} = require \child_process | |
oauth_consumer_key = 'node script' | |
var oauth_token | |
var oauth_token_secret | |
oauth_check = ! (callback) -> | |
if fs.existsSync \oauth_token | |
[oauth_token, oauth_token_secret] := fs.readFileSync \oauth_token , encoding: \utf8 .split ' ' | |
callback! | |
else | |
(err, res, body) <-! request.post \https://launchpad.net/+request-token, do | |
form: do | |
oauth_consumer_key: oauth_consumer_key | |
oauth_signature_method: \PLAINTEXT | |
oauth_signature: \& | |
console.log err, res if err | |
obj = {[k, v] for _ in body.split \& when [k, v] = _.split \=} | |
oauth_token := obj.oauth_token | |
oauth_token_secret := obj.oauth_token_secret | |
xdg = spawn \xdg-open, ["https://launchpad.net/+authorize-token?oauth_token=#oauth_token"] | |
prompt.message = 'Press Enter after authorized' | |
prompt.delimiter = '' | |
prompt.colors = false | |
prompt.start! | |
(err, result) <- prompt.get <[ ... ]> | |
console.log err, result if err | |
(err, res, body) <-! request.post \https://launchpad.net/+access-token, do | |
form: do | |
oauth_consumer_key: oauth_consumer_key | |
oauth_token: oauth_token | |
oauth_signature_method: \PLAINTEXT | |
oauth_signature: "&#oauth_token_secret" | |
console.log err, res if err | |
obj = {[k, v] for _ in body.split \& when [k, v] = _.split \=} | |
oauth_token := obj.oauth_token | |
oauth_token_secret := obj.oauth_token_secret | |
fs.writeFileSync \oauth_token, "#oauth_token #oauth_token_secret" | |
callback! | |
lp_get = ! (url, collection, callback) -> | |
unless callback | |
callback = collection | |
collection := null | |
(err, res, body) <-! request do | |
url: url | |
json: true | |
followRedirect: false | |
headers: do | |
'Accept': \application/json | |
'Authorization': """OAuth realm="https://api.launchpad.net/", | |
oauth_consumer_key="#oauth_consumer_key", | |
oauth_token="#oauth_token", | |
oauth_signature_method="PLAINTEXT", | |
oauth_signature="&#oauth_token_secret", | |
oauth_timestamp="#{+new Date / 1000}", | |
oauth_nonce="#{Math.floor(Math.random! * +new Date)}", | |
oauth_version="1.0" | |
""".replace /\n/g, '' | |
console.log res.statusCode if res.statusCode is not 303 and res.statusCode is not 200 | |
return callback err, res if err | |
if res.statusCode == 503 | |
return lp_get url, collection, callback | |
if res.statusCode == 303 | |
return lp_get res.headers.location, collection, callback | |
console.log "#{collection.entries.length} of #{body.total_size} are fetched." if collection | |
if body.start == 0 and body.next_collection_link | |
delete body.start | |
lp_get body.next_collection_link, body, callback | |
else if body.start > 0 and body.next_collection_link | |
collection.entries ++= body.entries | |
lp_get body.next_collection_link, collection, callback | |
else if body.start > 0 and body.prev_collection_link | |
collection.entries ++= body.entries | |
delete collection.next_collection_link | |
console.log "#{collection.entries.length} of #{body.total_size} are fetched." if collection | |
callback err, res, collection | |
else | |
callback err, res, body | |
lp_post = ! (url, data, callback) -> | |
(err, res, body) <-! request do | |
url: url | |
method: \POST | |
form: data | |
json: true | |
followRedirect: false | |
headers: do | |
'Accept': \application/json | |
'Authorization': """OAuth realm="https://api.launchpad.net/", | |
oauth_consumer_key="#oauth_consumer_key", | |
oauth_token="#oauth_token", | |
oauth_signature_method="PLAINTEXT", | |
oauth_signature="&#oauth_token_secret", | |
oauth_timestamp="#{+new Date / 1000}", | |
oauth_nonce="#{Math.floor(Math.random! * +new Date)}", | |
oauth_version="1.0" | |
""".replace /\n/g, '' | |
return callback err, res if err | |
if res.statusCode == 303 | |
lp_post res.headers.location, data, callback | |
else | |
callback err, res, body | |
sanity_check = (body) -> | |
if body == /^Unknown access token/ | |
console.log body | |
return false | |
else if body == /^Unknown consumer/ | |
console.log body | |
return false | |
else if body == /^Expired token/ | |
console.log body | |
return false | |
else | |
return true | |
<-! oauth_check | |
## Get personal information. | |
(err, res, user) <-! lp_get \https://api.launchpad.net/1.0/people/+me | |
return unless sanity_check user | |
return if res.statusCode != 200 | |
console.log user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment