diff options
Diffstat (limited to 'vkfeed.lua')
-rw-r--r-- | vkfeed.lua | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -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 |