diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2014-02-07 13:39:48 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2014-02-07 13:39:48 +0100 |
commit | 8643c0593feaa1487c3d9a97b024b5235f9f966b (patch) | |
tree | e4b37a8d79be9d0234b663ca5210bcc5ed7df0ea | |
parent | dcf2fe944db0664ee6d494185b4802ea23418d53 (diff) | |
download | dotfiles-8643c0593feaa1487c3d9a97b024b5235f9f966b.tar.gz dotfiles-8643c0593feaa1487c3d9a97b024b5235f9f966b.tar.bz2 dotfiles-8643c0593feaa1487c3d9a97b024b5235f9f966b.zip |
quvi: Move soup request function to seperate lua lib
-rw-r--r-- | .local/share/libquvi-scripts/common/soup_request.lua | 49 | ||||
-rw-r--r-- | .local/share/libquvi-scripts/media/xvidstage.lua | 37 |
2 files changed, 51 insertions, 35 deletions
diff --git a/.local/share/libquvi-scripts/common/soup_request.lua b/.local/share/libquvi-scripts/common/soup_request.lua new file mode 100644 index 0000000..28d4e74 --- /dev/null +++ b/.local/share/libquvi-scripts/common/soup_request.lua @@ -0,0 +1,49 @@ +-- libquvi-scripts + +local M = {} + +function M.request(url, method, params) + local lgi = require 'lgi' + local Soup = lgi.Soup + local req = lgi.Soup.Session():request_http_uri(method, Soup.URI(url)) + + function url_encode(str) + if (str) then + str = string.gsub (str, "\n", "\r\n") + str = string.gsub (str, "([^%w %-%_%.%~])", + function (c) return string.format ("%%%02X", string.byte(c)) end) + str = string.gsub (str, " ", "+") + end + return str + end + + p = {} + for k,v in pairs(params) do + table.insert(p, table.concat({ k, '=', url_encode(v) })) + end + p = table.concat(p, '&') + + if method == "POST" then + req:get_message():set_request("application/x-www-form-urlencoded", 0, p, #p) + end + + function read(stream) + local Bytes = require 'bytes' + local buffer = Bytes.new(4096) + local out = {} + + while true do + local size = stream:read(buffer) + if size <= 0 then + break; + end + table.insert(out, tostring(buffer):sub(1, size)) + end + + return table.concat(out) + end + + return read(req:send()) +end + +return M diff --git a/.local/share/libquvi-scripts/media/xvidstage.lua b/.local/share/libquvi-scripts/media/xvidstage.lua index 1312e96..a9df736 100644 --- a/.local/share/libquvi-scripts/media/xvidstage.lua +++ b/.local/share/libquvi-scripts/media/xvidstage.lua @@ -12,9 +12,10 @@ end -- Parse media URL. function parse(qargs) + local R = require 'soup_request' local p = { op = "download1", method_free = "1", id = qargs.input_url:match('xvidstage.com/([^/]+)') } - local c = Xvidstage.request(qargs.input_url, "POST", p) + local c = R.request(qargs.input_url, "POST", p) qargs.id = p.id qargs.title = c:match('Dateiname:</td><td[^>]*>(.-)</td>') @@ -89,38 +90,4 @@ function Xvidstage.can_parse_url(qargs) and true or false end -function Xvidstage.request(url, method, params) - local lgi = require 'lgi' - local Soup = lgi.Soup - local req = lgi.Soup.Session():request_http_uri(method, Soup.URI(url)) - - p = {} - for k,v in pairs(params) do - table.insert(p, table.concat({ k, '=', v })) - end - p = table.concat(p, '&') - - if method == "POST" then - req:get_message():set_request("application/x-www-form-urlencoded", 0, p, #p) - end - - function read(stream) - local Bytes = require 'bytes' - local buffer = Bytes.new(4096) - local out = {} - - while true do - local size = stream:read(buffer) - if size <= 0 then - break; - end - table.insert(out, tostring(buffer):sub(1, size)) - end - - return table.concat(out) - end - - return read(req:send()) -end - -- vim: set ts=2 sw=2 tw=72 expandtab: |