From 8643c0593feaa1487c3d9a97b024b5235f9f966b Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Fri, 7 Feb 2014 13:39:48 +0100 Subject: quvi: Move soup request function to seperate lua lib --- .../share/libquvi-scripts/common/soup_request.lua | 49 ++++++++++++++++++++++ .local/share/libquvi-scripts/media/xvidstage.lua | 37 +--------------- 2 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 .local/share/libquvi-scripts/common/soup_request.lua 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:]*>(.-)') @@ -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: -- cgit