Created
November 11, 2020 22:49
-
-
Save ellisonleao/b20a0136fafe7a2f696365cf92acede8 to your computer and use it in GitHub Desktop.
curl + plenary
This file contains hidden or 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
--[[ | |
curl 'https://api.outline.com/v3/parse_article?source_url=https%3A%2F%2Fpolitica.estadao.com.br%2Fnoticias%2Fgeral%2Cconta-no-twitter-divulga-dados-pessoais-de-fotografa-do-estadao%2C70003323526' \ | |
-H 'authority: api.outline.com' \ | |
-H 'accept: */*' \ | |
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36' \ | |
-H 'origin: https://outline.com' \ | |
-H 'referer: https://outline.com/' \ | |
--compressed | |
]] -- | |
local job = require("plenary.job") | |
local function show_error(err, data, _) | |
print(vim.inspect(err)) | |
print(vim.inspect(data)) | |
end | |
local function process_response(response) | |
local status = nil | |
local _, pos = string.find(response, '\r\n.+:%s.+\r\n\r\n') | |
local _headers = string.sub(response, 1, pos - 2) | |
_headers = vim.split(_headers:gsub('\r\n', '\n'), '\n') | |
local headers = {} | |
for _, header in ipairs(_headers) do | |
if string.match(header, ": ") then | |
local h = vim.split(header, ': ') | |
headers[h[1]] = h[2] | |
elseif not status then | |
status = tonumber(string.match(header, '^HTTP.+%s(%d+)%s.+$')) | |
end | |
end | |
local body = string.sub(response, pos + 1) | |
body = body:gsub('\r\n', '\n') | |
return body, status, headers | |
end | |
local function on_stdout(error, data) | |
if error then | |
print(vim.inspect((error))) | |
end | |
local b, _, _ = process_response(data) | |
print(vim.inspect(b)) | |
end | |
local args = { | |
"-i", | |
"-s", | |
"-H 'authority: api.outline.com'", | |
"-H 'accept: */*'", | |
"-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'", | |
"-H 'origin: https://outline.com'", | |
"-H 'referer: https://outline.com/'", | |
"--compressed", | |
"https://api.outline.com/v3/parse_article?source_url=https%3A%2F%2Fpolitica.estadao.com.br%2Fnoticias%2Fgeral%2Cconta-no-twitter-divulga-dados-pessoais-de-fotografa-do-estadao%2C70003323526", | |
} | |
local command = { | |
command = "curl", | |
args = args, | |
on_stdout = on_stdout, | |
on_stderr = show_error, | |
} | |
local j = job:new(command) | |
j:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment