diff options
author | Sergey Nazaryev <sergey@nazaryev.ru> | 2020-09-27 16:53:18 +0300 |
---|---|---|
committer | Sergey Nazaryev <sergey@nazaryev.ru> | 2020-09-27 16:53:18 +0300 |
commit | f12b42a254a023ff64bc34ea4e913ae8287fba2a (patch) | |
tree | 36a38e796712a80e74ade86e98afa56aaca2cfcb | |
parent | bd00cf94dabd0330e3ef758a440d68541bf312d6 (diff) | |
download | vk2rss-master.zip vk2rss-master.tar.gz vk2rss-master.tar.bz2 |
-rwxr-xr-x | vk2rss.lua | 4 | ||||
-rw-r--r-- | vkfeed.lua | 42 |
2 files changed, 44 insertions, 2 deletions
@@ -1,4 +1,4 @@ -#!/usr/bin/lua +#!/usr/bin/luajit local xml = require "xml" @@ -43,7 +43,7 @@ function main() for key, item in ipairs( feed ) do table.insert( rss[1], { xml = 'item', { xml = 'title', item["title"] }, - { xml = 'pubDate', item["date"] }, + { xml = 'pubDate', item["pubDate"] }, { xml = 'link', item["link"] }, { xml = 'description', item["description"] }, }) @@ -5,6 +5,38 @@ local json = require "cjson" local API_VK_SERVER = "https://api.vk.com/" local VK_BASE_DOMAIN = "https://vk.com/" +local function chsize(char) + if not char then + return 0 + elseif char > 240 then + return 4 + elseif char > 225 then + return 3 + elseif char > 192 then + return 2 + else + return 1 + end +end + +local function utf8sub(str, startChar, numChars) + local startIndex = 1 + while startChar > 1 do + local char = string.byte(str, startIndex) + startIndex = startIndex + chsize(char) + startChar = startChar - 1 + end + + local currentIndex = startIndex + + while numChars > 0 and currentIndex <= #str do + local char = string.byte(str, currentIndex) + currentIndex = currentIndex + chsize(char) + numChars = numChars -1 + end + return str:sub(startIndex, currentIndex - 1) +end + function parseFeed( feedName, jsonText ) local items = {} @@ -19,6 +51,13 @@ function parseFeed( feedName, jsonText ) local description = item["text"] local date = os.date( "%a, %d %b %Y %X GMT", item["date"] ) + local title = description + local firstLineIdx = title:find("\n") + if( firstLineIdx ~= nil ) then + title = title:sub(0, firstLineIdx-1) + end + title = os.date("[%d.%m.%Y] ", item["date"]) .. utf8sub(title, 0, 30) + local photos = {} local links = {} local videos = {} @@ -70,9 +109,12 @@ function getFeed( domain, count, offset, access_token ) url = API_VK_SERVER .. "/method/wall.get?domain=" .. domain .. "&offset=" .. offset .. "&count=" .. count .. "&v=5.44" end + if( access_token ~= nil ) then url = url .. "&access_token=" .. access_token end + + print(url) return os.download( url ) end |