dofile( "utils.lua" ) 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 = {} object = json.decode( jsonText ) if( object["response"] == nil ) then return nil end for k, item in ipairs( object["response"]["items"] ) do if (item["is_pinned"] ~= 1) then local link = VK_BASE_DOMAIN .. "/wall" .. item["owner_id"] .. "_" .. item["id"] 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 = {} if( item["attachments"] ~= nil ) then for k, attach in ipairs( item["attachments"] ) do if( attach["type"] == "photo" ) then local attachInfo = attach["photo"] local photoSizes = getSortPhotos( attachInfo ) table.insert( photos, { small = photoSizes[3], large = photoSizes[ table.maxn( photoSizes ) ] } ) elseif( attach["type"] == "link" ) then local attachInfo = attach["link"] local photo = nil if ( attachInfo["photo"] ~= nil ) then local photoSizes = getSortPhotos(attachInfo["photo"]) photo = photoSizes[1] end table.insert( links, { photo = photo, url = attachInfo["url"], title = attachInfo["title"] } ) elseif( attach["type"] == "video" ) then local attachInfo = attach["video"] local photoSizes = getSortPhotos( attachInfo ) local url = VK_BASE_DOMAIN .. "/video" .. attachInfo["owner_id"] .. "_" .. attachInfo["id"] table.insert( videos, { photo = photoSizes[2], url = url, title = attachInfo["title"] } ) end end end table.insert( items, createItem( date, title, description, link, videos, photos, links ) ) end end return items end function getFeed( domain, count, offset, access_token ) local url = "" if( tonumber( domain ) ~= nil ) then url = API_VK_SERVER .. "/method/wall.get?owner_id=-" .. domain .. "&offset=" .. offset .. "&count=" .. count .. "&v=5.44" else 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 function getFeedInfo( domain, access_token ) local url = API_VK_SERVER .. "/method/groups.getById?group_id=" .. domain .. "&fields=city,country,place,description,wiki_page,members_count,counters,start_date,finish_date,can_post,can_see_all_posts,activity,status,contacts,links,fixed_post,verified,site,ban_info&v=5.44" if( access_token ~= nil ) then url = url .. "&access_token=" .. access_token end return os.download( url ) end function parseFeedInfo( jsonText ) local object = json.decode( jsonText ) if( object["response"] == nil ) then return nil end local feed = object["response"][1] local info = {} info["name"] = feed["name"] info["description"] = feed["description"] if feed["screen_name"] ~= nil then info["url"] = VK_BASE_DOMAIN .. "/" .. feed["screen_name"] elseif feed["type"] == "page" then info["url"] = VK_BASE_DOMAIN .. "/public" .. feed["id"] elseif feed["type"] == "group" then info["url"] = VK_BASE_DOMAIN .. "/group" .. feed["id"] end return info end function createItem( date, title, description, link, videos, photos, links ) local photoMaxHeight = 100 if( table.maxn(photos) == 1 ) then photoMaxHeight = nil end photosHtml = "

" for k, photo in ipairs(photos) do if photoMaxHeight ~= nil then photosHtml = photosHtml .. "" else photosHtml = photosHtml .. "" end end photosHtml = photosHtml .. "

" linksHtml = "

" for k, link in ipairs(links) do if link["photo"] ~= nil then linksHtml = linksHtml .. "" .. link["title"] .."" else linksHtml = linksHtml .. "" .. link["title"] .."" end end linksHtml = linksHtml .. "

" videosHtml = "

" for k, video in ipairs(videos) do videosHtml = videosHtml .. "

" .. video["title"] .."

" end videosHtml = videosHtml .. "

" return { title = title, pubDate = date, link = link, description = description:nl2br() .. photosHtml .. videosHtml .. linksHtml } end function getSortPhotos(photo) local sizes = {} for key in pairs(photo) do if( key:starts( "photo_" ) ) then local size = tonumber( key:sub( string.len( "photo_" ) + 1 ) ) table.insert( sizes, size ) end end table.sort( sizes ) local photos = {} for k, size in ipairs(sizes) do table.insert( photos, photo[ "photo_" .. size ] ) end return photos end