aboutsummaryrefslogtreecommitdiff
path: root/vkfeed.lua
diff options
context:
space:
mode:
authorSergey Nazaryev <sergey@nazaryev.ru>2016-01-25 19:36:50 +0300
committerSergey Nazaryev <sergey@nazaryev.ru>2017-04-02 22:04:57 +0300
commitbd00cf94dabd0330e3ef758a440d68541bf312d6 (patch)
treee87d900a0c15b4a24b51996c36b35715d0317c0c /vkfeed.lua
downloadvk2rss-bd00cf94dabd0330e3ef758a440d68541bf312d6.zip
vk2rss-bd00cf94dabd0330e3ef758a440d68541bf312d6.tar.gz
vk2rss-bd00cf94dabd0330e3ef758a440d68541bf312d6.tar.bz2
Initial release.
Diffstat (limited to 'vkfeed.lua')
-rw-r--r--vkfeed.lua165
1 files changed, 165 insertions, 0 deletions
diff --git a/vkfeed.lua b/vkfeed.lua
new file mode 100644
index 0000000..767e544
--- /dev/null
+++ b/vkfeed.lua
@@ -0,0 +1,165 @@
+dofile( "utils.lua" )
+
+local json = require "cjson"
+
+local API_VK_SERVER = "https://api.vk.com/"
+local VK_BASE_DOMAIN = "https://vk.com/"
+
+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 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
+ 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 = "<p>"
+ for k, photo in ipairs(photos) do
+ if photoMaxHeight ~= nil then
+ photosHtml = photosHtml .. "<a href='" .. photo["large"] .. "'><img hspace='3' height=".. photoMaxHeight .." src='" .. photo["small"] .. "'/></a>"
+ else
+ photosHtml = photosHtml .. "<a href='" .. photo["large"] .. "'><img hspace='3' src='" .. photo["small"] .. "'/></a>"
+ end
+ end
+ photosHtml = photosHtml .. "</p>"
+
+ linksHtml = "<p>"
+ for k, link in ipairs(links) do
+ if link["photo"] ~= nil then
+ linksHtml = linksHtml .. "<a href='" .. link["url"] .. "'><img hspace='6' src='" .. link["photo"] .. "'/>" .. link["title"] .."</a>"
+ else
+ linksHtml = linksHtml .. "<a href='" .. link["url"] .. "'>" .. link["title"] .."</a>"
+ end
+ end
+ linksHtml = linksHtml .. "</p>"
+
+ videosHtml = "<p>"
+ for k, video in ipairs(videos) do
+ videosHtml = videosHtml .. "<a href='" .. video["url"] .. "'><img hspace='3' src='" .. video["photo"] .. "'/><p>" .. video["title"] .."</p></a>"
+ end
+ videosHtml = videosHtml .. "</p>"
+
+ 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