aboutsummaryrefslogtreecommitdiff
path: root/vkfeed.lua
blob: 8bf6caec14d5d18ebf732eb4a622a7b0209e99f3 (plain)
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
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 = "<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