-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegraph.lua
356 lines (322 loc) · 10.8 KB
/
telegraph.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
local remove, insert, concat = table.remove, table.insert, table.concat
local format, lower = string.format, string.lower
local unpack = table.unpack or unpack
local ceil = math.ceil
local _fail = fail -- luacheck: ignore
local request = require("lapis.http").request
local util = require("lapis.util")
local encode_query_string = util.encode_query_string
local to_json = util.to_json
local from_json = util.from_json
-- luarocks install web_sanitize
local whitelist = require("web_sanitize.whitelist"):clone()
local scanner = require("web_sanitize.query.scan_html")
local sanitizer = require("web_sanitize.html").Sanitizer
-- https://telegra.ph/api#NodeElement
whitelist.tags = {a = {href = whitelist.tags.a.href}, aside = true, b = true, blockquote = true, br = true, code = true, em = true, figcaption = true, figure = true, h3 = true, h4 = true, hr = true, i = true, iframe = {src = whitelist.tags.img.src}, img = {src = whitelist.tags.img.src}, li = true, ol = true, p = true, pre = true, s = true, strong = true, u = true, ul = true, video = {src = whitelist.tags.img.src}}
whitelist.self_closing = {br = true, hr = true, img = true}
whitelist.add_attributes = {}
local API = "https://api.telegra.ph/%s%s"
local function assert_path(method, path, position)
assert(type(path) == "string", format("bad argument #%i to '%s' (string expected, got %s)", position or 1, method, type(path)))
end
local function assert_params(method, params, position)
params = params or {}
assert(type(params) == "table", format("bad argument #%i to '%s' (table expected, got %s)", position or 1, method, type(params)))
end
local function assert_data(method, data, position)
assert(type(data) == "table", format("bad argument #%i to '%s' (table expected, got %s)", position or 1, method, type(data)))
end
local telegraph = {}
telegraph.__index = telegraph
-- STATIC METHODS
telegraph.safe = function()
for key, value in pairs(telegraph) do
if type(value) == "function" then
telegraph[key] = function(...)
local result = {pcall(value, ...)}
if remove(result, 1) then
return unpack(result)
else
return _fail, unpack(result)
end
end
end
end
return telegraph
end
function telegraph.new(access_token)
local self = setmetatable({}, telegraph)
self.access_token = access_token
return self
end
setmetatable(telegraph, {
__call = function(self, ...)
return self.new(...)
end
})
-- METHODS
-- https://telegra.ph/api#createAccount
function telegraph:createAccount(params, no_define_access_token)
assert_params("createAccount", params)
local Account = self:Account(assert(self:_request("createAccount", nil, false, params)))
self.access_token = nil
if not no_define_access_token then
self.access_token = Account.access_token
end
return Account
end
-- https://telegra.ph/api#editAccountInfo
function telegraph:editAccountInfo(params)
assert_params("editAccountInfo", params)
local Account = self:Account(assert(self:_request("editAccountInfo", nil, true, params)))
return Account
end
-- https://telegra.ph/api#getAccountInfo
function telegraph:getAccountInfo(params, all_fields)
assert_params("getAccountInfo", params)
if all_fields then
params.fields = {"short_name", "author_name", "author_url", "auth_url", "page_count"}
end
local Account = self:Account(assert(self:_request("getAccountInfo", nil, true, params)))
return Account
end
-- https://telegra.ph/api#revokeAccessToken
function telegraph:revokeAccessToken(params, no_define_access_token)
assert_params("revokeAccessToken", params)
local Account = self:Account(assert(self:_request("revokeAccessToken", nil, true, params)))
self.access_token = nil
if not no_define_access_token then
self.access_token = Account.access_token
end
return Account
end
-- https://telegra.ph/api#createPage
function telegraph:createPage(params, return_content)
assert_params("createPage", params)
if return_content then
params.return_content = true
end
local Page = self:Page(assert(self:_request("createPage", nil, true, params)))
return Page
end
-- https://telegra.ph/api#editPage
function telegraph:editPage(path, params, return_content)
assert_path("editPage", path)
assert_params("editPage", params, 2)
if return_content then
params.return_content = true
end
local Page = self:Page(assert(self:_request("editPage", path, true, params)))
return Page
end
-- https://telegra.ph/api#getPage
function telegraph:getPage(path, params, return_content)
assert_path("getPage", path)
assert_params("getPage", params, 2)
if return_content then
params.return_content = true
end
local Page = self:Page(assert(self:_request("getPage", path, true, params)))
return Page
end
-- https://telegra.ph/api#getPageList
function telegraph:getPageList(params, get_all)
assert_params("getPageList", params)
if get_all then
local page_count = tonumber(self:getAccountInfo({}, true).page_count)
if page_count and page_count > 0 then
local limit = 200
local total = ceil(limit / page_count)
local Pages
for current = 1, total do
local offset = (current * limit) - limit
local PageList = self:PageList(self:getPageList({offset = offset, limit = limit}, false))
if Pages then
for page = 1, #PageList.pages do
insert(Pages, PageList.pages[page])
end
else
Pages = PageList.pages
end
end
return self:PageList({total_count = page_count, pages = Pages})
end
end
local PageList = self:PageList(assert(self:_request("getPageList", nil, true, params)))
return PageList
end
-- https://telegra.ph/api#getViews
function telegraph:getViews(path, params)
assert_path("getViews", path)
assert_params("getViews", params, 2)
local PageViews = self:PageViews(assert(self:_request("getViews", path, true, params)))
return PageViews
end
-- TYPES
-- https://telegra.ph/api#Account
function telegraph:Account(data)
assert_data("Account", data)
return setmetatable(data, {type = "Account"})
end
-- https://telegra.ph/api#PageList
function telegraph:PageList(data)
assert_data("PageList", data)
for index = 1, #data.pages do
if self:type(data.pages[index]) ~= "Page" then
data.pages[index] = self:Page(data.pages[index])
end
end
return setmetatable(data, {type = "PageList"})
end
-- https://telegra.ph/api#Page
function telegraph:Page(data)
assert_data("Page", data)
if data.content then
data.content = self:Node(data.content)
end
return setmetatable(data, {type = "Page"})
end
-- https://telegra.ph/api#PageViews
function telegraph:PageViews(data)
assert_data("PageViews", data)
return setmetatable(data, {type = "PageViews"})
end
-- https://telegra.ph/api#Node
function telegraph:Node(data)
assert_data("Node", data)
for index = 1, #data do
if type(data[index]) == "table" then
data[index] = self:NodeElement(data[index])
end
end
return setmetatable(data, {type = "Node", __tostring = function(node) return telegraph:toContent(node) end})
end
-- https://telegra.ph/api#NodeElement
function telegraph:NodeElement(data)
assert_data("NodeElement", data)
if data.children then
data.children = self:Node(data.children)
end
return setmetatable(data, {type = "NodeElement"})
end
-- https://telegra.ph/api#Content-format
function telegraph:toNode(content, strip_tags)
content = sanitizer({whitelist = whitelist, strip_comments = true, strip_tags = strip_tags})(content)
-- https://github.com/olueiro/lapis_layout/blob/f43a52cfc5cbf631b6d3595d7748a85074b3286f/lapis_layout.lua#L28
local tree = {}
scanner.scan_html(content, function(stack)
local current = tree
for _, node in pairs(stack) do
local num = node.num
if current[num] then
current = current[num]
else
if node.type == "text_node" then
insert(current, node:inner_text())
else
current[num] = {tag = node.tag, attrs = node.attr}
current = current[num]
end
end
end
end, {text_nodes = true})
local function children(node)
local nodes = {}
for index = 1, #node do
local value = node[index]
if type(value) == "string" then
local text = {}
for subindex = index, #node do
if type(node[subindex]) == "string" then
insert(text, node[subindex])
node[subindex] = true
else
break
end
end
insert(nodes, concat(text))
elseif type(value) == "table" then
local attrs = value.attrs
local params = {}
if attrs then
for _, key in ipairs(attrs) do
params[key] = attrs[key]
end
end
insert(nodes, {tag = lower(value.tag), attrs = params, children = children(value)})
end
end
return #nodes == 0 and nil or nodes
end
return self:Node(children(tree) or {})
end
-- https://telegra.ph/api#Content-format
function telegraph:toContent(Node)
local function node(Node)
local content = {}
if self:type(Node) == "Node" then
for index = 1, #Node do
local NodeElement = Node[index]
if type(NodeElement) == "string" then
insert(content, NodeElement)
elseif self:type(NodeElement) == "NodeElement" then
local tag = NodeElement.tag
local attrs = NodeElement.attrs
local params = {}
if attrs then
for key, value in pairs(attrs) do
insert(params, format(" %s=%q", key, value))
end
end
local children = NodeElement.children and node(NodeElement.children)
insert(content, format("<%s%s>%s</%s>", tag, concat(params), children or "", tag))
end
end
end
return concat(content)
end
return sanitizer({whitelist = whitelist, strip_comments = true})(node(Node))
end
function telegraph:type(object)
if type(object) ~= "table" then
return false
end
local mt = getmetatable(object)
if not mt then
return false
end
return mt.type or false
end
function telegraph:_request(method, path, access_token_required, params)
params = params or {}
if access_token_required then
if not self.access_token then
return _fail, "access token not found"
end
params.access_token = self.access_token
end
for key, value in pairs(params) do
if type(value) == "table" then
local ok, json = pcall(to_json, value)
if not ok then
return _fail, "unable to convert to json"
end
params[key] = json
end
end
local url = format(API, method, path and "/" .. path or "")
local response, status = request(url, encode_query_string(params))
if not response then
return _fail, tostring(status)
end
local ok, json = pcall(from_json, response)
if not ok then
return _fail, "response is not a json"
end
if not json.ok then
return _fail, tostring(json.error)
end
return json.result
end
return telegraph