#!/usr/bin/lua local http = require("socket.http") local url = require("socket.url") local mime = require("mime") local lom = require("lxp.lom") local ic = require("iconv") -- parse text node function get_node_text(item) for name, value in pairs(item) do if type(value) == "string" and not (name == "tag") then return value end end return nil end -- parse and display mogo2 statuses function display_statuses(statuses) local t = ic.open("char", "utf-8") for n1, v1 in pairs(statuses) do if v1["tag"] == "status" then local date = "" local user = "" local text = "" for n2, v2 in pairs(v1) do if v2["tag"] == "created_at" then date = get_node_text(v2) elseif v2["tag"] == "text" then text = get_node_text(v2) elseif v2["tag"] == "user" then for n3, v3 in pairs(v2) do if v3["tag"] == "screen_name" then user = get_node_text(v3) end end end end -- display print(date) print(ic.iconv(t, user .. ":" .. text)) print("") end end end -- update mogo2 status function update_status(mail, pass, status) local t = ic.open("utf-8", "char") local update_status_url = "http://api.mogo2.jp/statuses/update.xml" local auth = string.format("%s:%s", mail, pass) local chunk = {} -- create url for updating status update_status_url = string.format("%s?status=%s", update_status_url, url.escape(ic.iconv(t, status))) -- send post request b, c = http.request { method = "POST", url = update_status_url, headers = { authorization = "Basic " .. (mime.b64(auth)) .. "==" }, source = nil } if not c == 200 then return nil end end -- get self statuses in timeline function get_statuses(mail, pass) local get_statuses_url = "http://api.mogo2.jp/statuses/friends_timeline.xml" local auth = string.format("%s:%s", mail, pass) local chunk = {} -- send get request b, c = http.request { method = "GET", url = get_statuses_url, headers = { authorization = "Basic " .. (mime.b64(auth)) .. "==" }, sink = ltn12.sink.table(chunk) } if not c == 200 then return nil end local xml = table.concat(chunk) -- for debug -------------------------------------- --local f = io.open('test.xml', 'r') --local xml = f:read('*a') --f:close() return lom.parse(xml) end local mail = "yourname@yourmail.com" -- your mail address local pass = "yourpassword" -- your password -- if no specified arguments, it display statuses. -- or else, send argument as status to mogo2. -- argument should be utf-8 encoding. if not arg[1] then local statuses = get_statuses(mail, pass) if statuses then display_statuses(statuses) end else update_status(mail, pass, arg[1]) end