local TWITTER_CREDS = {
oauth={
consumertoken='
',
consumersecret='',
accesstoken='',
tokensecret=''
}
}
local underscore = require 'underscore'
local username = request.form.username
-- Twitter usernames can't contain spaces
if username == nil or string.find(username, ' ') then return 400 end
local id = request.form.id
if id == nil then return 400 end
local from = request.form.from
if from ~= nil and string.find(from, ' ') then return 400 end
-- fetch JSON-encoded compliments
local data = json.parse(http.request {
url = 'http://www.flatterist.com/compliments.json'
}.content)
-- find the matching compliment
-- JSON for a compliment looks like ["", ""]
local compliment = underscore.detect(data.compliments,
function(compliment) return compliment[1] == id end)
if compliment == nil then return 400 end
local tweet = string.format(
'@%s %s http://www.flatterist.com/#%s',
username, compliment[2], id)
if from ~= nil and from ~= '' then tweet = tweet .. ' (from @'..from..')' end
local response = http.request {
url = 'https://api.twitter.com/1.1/statuses/update.json',
data = { status = tweet },
auth = TWITTER_CREDS
}
if response.statuscode == 200 then
return 302, '', {
Location = 'https://twitter.com/flatterist/status/'
.. json.parse(response.content).id_str
}
else
return 400, 'Tweet failed. :-('
end
Post a Comment Blogger Facebook