init
This commit is contained in:
parent
ddd745a2bb
commit
2fcc21db08
52
config/config.lua
Executable file
52
config/config.lua
Executable file
@ -0,0 +1,52 @@
|
||||
local json = require('cjson.safe')
|
||||
local _M = {}
|
||||
_M.data = {}
|
||||
_M.comments = {}
|
||||
_M.file = '' -- файл конфигурации
|
||||
local key
|
||||
|
||||
function _M.read()
|
||||
for line in io.lines(_M.file) do
|
||||
key = string.match(line, '([%w_]+)::')
|
||||
if (key) then
|
||||
_M.data[key] = string.match(line, '::(.*) #')
|
||||
_M.comments[key] = string.match(line, '#(.*)')
|
||||
if string.find(_M.data[key],'%{%"') or string.find(_M.data[key],'%[%"') then
|
||||
_M.data[key] = json.decode(_M.data[key])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function reprint(k,v)
|
||||
if type(v) == 'table' then
|
||||
for i,j in pairs(v) do
|
||||
reprint(i,j)
|
||||
end
|
||||
else
|
||||
print(k..': '..v)
|
||||
end
|
||||
end
|
||||
|
||||
function _M.data:write()
|
||||
local config_file = io.open(_M.file, 'w')
|
||||
for k,v in pairs(self) do
|
||||
if type(v) ~= 'function' then
|
||||
if type(v) == 'table' then v = json.encode(v) end
|
||||
config_file:write(k..'::'..v..' #'.._M.comments[key]..'\n')
|
||||
end
|
||||
end
|
||||
config_file:close()
|
||||
end
|
||||
|
||||
function _M.data:print()
|
||||
for k,v in pairs(self) do
|
||||
if type(v) ~= 'function' then
|
||||
if _M.comments[k] then print('\n'.._M.comments[k]:gsub("^%s*(.-)%s*$", "%1")..': \n') end
|
||||
reprint(k,v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return _M
|
||||
|
||||
5
config/replicate.lua
Executable file
5
config/replicate.lua
Executable file
@ -0,0 +1,5 @@
|
||||
local config = require('config.config')
|
||||
local _M = config
|
||||
_M.file = 'replicate.conf' -- файл конфигурации
|
||||
_M.read()
|
||||
return _M.data
|
||||
3
replicate.conf
Normal file
3
replicate.conf
Normal file
@ -0,0 +1,3 @@
|
||||
url::https://api.replicate.com/v1/ #API URL
|
||||
token::r8_e7m8rAlVxiH9ey6INuZr4nuO7fcjhKc0UlwYG #Token
|
||||
format::json #Формат результатов
|
||||
119
replicate/api.lua
Normal file
119
replicate/api.lua
Normal file
@ -0,0 +1,119 @@
|
||||
local json = require('cjson')
|
||||
local cURL = require("cURL")
|
||||
local date = require("date")
|
||||
local log = require('utils.log')
|
||||
local config = require('config.replicate')
|
||||
local url = require('utils.url')
|
||||
|
||||
local _M = {}
|
||||
|
||||
_M.result = nil
|
||||
_M.base = config.url
|
||||
_M.format = config.format
|
||||
|
||||
log.outfile = 'logs/replicate_'..os.date('%Y-%m-%d')..'.log'
|
||||
log.level = 'trace'
|
||||
|
||||
function query(data)
|
||||
if not data then return '' end
|
||||
local str = '?'
|
||||
for k,v in pairs(data) do str = str..'&'..k..'='..v end
|
||||
return str
|
||||
end
|
||||
|
||||
local function get_result(str,url)
|
||||
local result, err = pcall(json.decode,str)
|
||||
if result then
|
||||
_M.result = json.decode(str)
|
||||
if _M.result.status and _M.result.status == 'error' then
|
||||
log.error(url..':'..json.encode(_M.result.error))
|
||||
return nil, _M.result.error
|
||||
end
|
||||
else
|
||||
log.error(url..':'..err)
|
||||
return nil, err
|
||||
end
|
||||
return _M.result
|
||||
end
|
||||
|
||||
local function poster(data)
|
||||
if data then
|
||||
local result = {}
|
||||
for i,k in pairs(data) do table.insert(result, i..'='..k) end
|
||||
return table.concat(result,'&')
|
||||
end
|
||||
end
|
||||
|
||||
function _M.get(url,data)
|
||||
local str = ''
|
||||
url = _M.base..url..query(data)
|
||||
local headers = {
|
||||
'Content-type: application/json',
|
||||
'Accept: application/json'
|
||||
}
|
||||
local c = cURL.easy{
|
||||
url = url,
|
||||
httpheader = headers,
|
||||
writefunction = function(st)
|
||||
str = str..st
|
||||
collectgarbage("collect")
|
||||
return #st
|
||||
end
|
||||
}
|
||||
local ok, err = c:perform()
|
||||
local code = c:getinfo_response_code()
|
||||
c:close()
|
||||
if not ok then return nil, err end
|
||||
if code ~= 200 then
|
||||
log.error(url..':'..str)
|
||||
return nil,str
|
||||
end
|
||||
if _M.format == 'json' then
|
||||
res,err = get_result(str,url,full)
|
||||
else
|
||||
res = str
|
||||
end
|
||||
if not res then return nil,err end
|
||||
return res
|
||||
end
|
||||
|
||||
function _M.post(url,data,add)
|
||||
local str = ''
|
||||
url = _M.base..url..query(add)
|
||||
local headers = {
|
||||
'Content-type: application/x-www-form-urlencoded',
|
||||
'Accept: application/json'
|
||||
}
|
||||
local c = cURL.easy{
|
||||
url = url,
|
||||
post = true,
|
||||
postfields = poster(data),
|
||||
httpheader = headers,
|
||||
writefunction = function(st)
|
||||
str = str..st
|
||||
collectgarbage("collect")
|
||||
return #st
|
||||
end
|
||||
}
|
||||
local ok, err = c:perform()
|
||||
local code = c:getinfo_response_code()
|
||||
c:close()
|
||||
if not ok then return nil, err end
|
||||
if code ~= 200 then
|
||||
log.error(url..':'..str)
|
||||
return nil,str
|
||||
end
|
||||
if _M.format == 'json' then
|
||||
res,err = get_result(str,url,full)
|
||||
else
|
||||
res = str
|
||||
end
|
||||
if not res then return nil,err end
|
||||
return res
|
||||
end
|
||||
|
||||
function _M.predictions(data)
|
||||
return _M.post('predictions',data)
|
||||
end
|
||||
|
||||
return _M
|
||||
6
test.lua
Normal file
6
test.lua
Normal file
@ -0,0 +1,6 @@
|
||||
local api = require('replicate.api')
|
||||
local json = require('cjson')
|
||||
|
||||
print(json.encode(api.predictions()))
|
||||
|
||||
|
||||
50
utils/array.lua
Normal file
50
utils/array.lua
Normal file
@ -0,0 +1,50 @@
|
||||
local _M = {}
|
||||
|
||||
function _M.keys(t)
|
||||
local keys = {}
|
||||
for key,_ in pairs(t) do
|
||||
table.insert(keys, key)
|
||||
end
|
||||
return keys
|
||||
end
|
||||
|
||||
function _M.merge(t1,t2)
|
||||
for k,v in pairs(t2) do
|
||||
table.insert(t1,v)
|
||||
end
|
||||
return t1
|
||||
end
|
||||
|
||||
function _M.has_value(tab, val)
|
||||
for index, value in ipairs(tab) do
|
||||
if value == val then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function _M.count(t)
|
||||
local count = 0
|
||||
for key,_ in pairs(t) do
|
||||
count = count + 1
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
function _M.minkey(t)
|
||||
local a = {}
|
||||
for n in pairs(t) do table.insert(a, n) end
|
||||
table.sort(a)
|
||||
return a[1]
|
||||
end
|
||||
|
||||
function _M.implode(t,before,after,sep)
|
||||
local result = ''
|
||||
local count = _M.count(t)
|
||||
for key,val in pairs(t) do
|
||||
result = result..before..val..after
|
||||
if key < count then result = result..sep end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
90
utils/log.lua
Normal file
90
utils/log.lua
Normal file
@ -0,0 +1,90 @@
|
||||
--
|
||||
-- log.lua
|
||||
--
|
||||
-- Copyright (c) 2016 rxi
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or modify it
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local log = { _version = "0.1.0" }
|
||||
|
||||
log.usecolor = true
|
||||
log.outfile = nil
|
||||
log.level = "trace"
|
||||
|
||||
|
||||
local modes = {
|
||||
{ name = "trace", color = "\27[34m", },
|
||||
{ name = "debug", color = "\27[36m", },
|
||||
{ name = "info", color = "\27[32m", },
|
||||
{ name = "warn", color = "\27[33m", },
|
||||
{ name = "error", color = "\27[31m", },
|
||||
{ name = "fatal", color = "\27[35m", },
|
||||
}
|
||||
|
||||
|
||||
local levels = {}
|
||||
for i, v in ipairs(modes) do
|
||||
levels[v.name] = i
|
||||
end
|
||||
|
||||
|
||||
local round = function(x, increment)
|
||||
increment = increment or 1
|
||||
x = x / increment
|
||||
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
|
||||
end
|
||||
|
||||
|
||||
local _tostring = tostring
|
||||
|
||||
local tostring = function(...)
|
||||
local t = {}
|
||||
for i = 1, select('#', ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
x = round(x, .01)
|
||||
end
|
||||
t[#t + 1] = _tostring(x)
|
||||
end
|
||||
return table.concat(t, " ")
|
||||
end
|
||||
|
||||
|
||||
for i, x in ipairs(modes) do
|
||||
local nameupper = x.name:upper()
|
||||
log[x.name] = function(...)
|
||||
|
||||
-- Return early if we're below the log level
|
||||
if i < levels[log.level] then
|
||||
return
|
||||
end
|
||||
|
||||
local msg = tostring(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local lineinfo = info.short_src .. ":" .. info.currentline
|
||||
|
||||
-- Output to console
|
||||
print(string.format("%s[%-6s%s]%s %s: %s",
|
||||
log.usecolor and x.color or "",
|
||||
nameupper,
|
||||
os.date("%H:%M:%S"),
|
||||
log.usecolor and "\27[0m" or "",
|
||||
lineinfo,
|
||||
msg))
|
||||
|
||||
-- Output to log file
|
||||
if log.outfile then
|
||||
local fp = io.open(log.outfile, "a")
|
||||
local str = string.format("[%-6s%s] %s: %s\n",
|
||||
nameupper, os.date(), lineinfo, msg)
|
||||
fp:write(str)
|
||||
fp:close()
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return log
|
||||
38
utils/url.lua
Normal file
38
utils/url.lua
Normal file
@ -0,0 +1,38 @@
|
||||
local _M = {}
|
||||
|
||||
local char_to_hex = function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end
|
||||
|
||||
local hex_to_char = function(x)
|
||||
return string.char(tonumber(x, 16))
|
||||
end
|
||||
|
||||
function _M.encode(url)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub("\n", "\r\n")
|
||||
url = url:gsub("([^%w ])", char_to_hex)
|
||||
url = url:gsub(" ", "+")
|
||||
return url
|
||||
end
|
||||
|
||||
function _M.litepms_encode(url)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub(" ", char_to_hex)
|
||||
return url
|
||||
end
|
||||
|
||||
_M.decode = function(url)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub("+", " ")
|
||||
url = url:gsub("%%(%x%x)", hex_to_char)
|
||||
return url
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
x
Reference in New Issue
Block a user