From 10c9c893b1955e4eac3f6c46fc065ed81b03cda7 Mon Sep 17 00:00:00 2001 From: Aire-One Date: Sun, 11 Sep 2022 03:17:40 +0200 Subject: [PATCH] feat(crawler): manage `file://` protocol --- .vscode/settings.json | 1 + src/awesomewm.d.tl/crawler/init.lua | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 15c126e..18f3e6c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -23,6 +23,7 @@ "mkdir", "setopt", "Stylua", + "tablex", "tmpl", "wibox", "writefunction" diff --git a/src/awesomewm.d.tl/crawler/init.lua b/src/awesomewm.d.tl/crawler/init.lua index f2a02d4..1305b9c 100644 --- a/src/awesomewm.d.tl/crawler/init.lua +++ b/src/awesomewm.d.tl/crawler/init.lua @@ -1,10 +1,12 @@ +local file = require "pl.file" local http = require "socket.http" local inspect = require "inspect" local log = require "logger" +local tablex = require "pl.tablex" local crawler = {} -function crawler.request(url) +local function http_request(url) local body, code_or_error = http.request(url) if not body then @@ -25,8 +27,26 @@ function crawler.request(url) return body end +local function get_resource_by_protocol(url) + local protocol, resource = url:match "^(%a+)://(.*)$" + + if not not tablex.find({ "http", "https" }, protocol) then + local success, result = pcall(http_request, url) + return success, result + elseif protocol == "file" then + local res, err = file.read(resource, false) + if res then + return true, res + else + return false, err + end + else + error { "Unsupported protocol", protocol = protocol } + end +end + function crawler.fetch(url) - local success, result = pcall(crawler.request, url) + local success, result = get_resource_by_protocol(url) if not success then log:error(inspect { "Fetch failed", status = success, error = result })