Skip to content

Instantly share code, notes, and snippets.

@AMD-NICK
Last active August 29, 2017 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AMD-NICK/c942e08f1e457ebd37188409fc18d503 to your computer and use it in GitHub Desktop.
Save AMD-NICK/c942e08f1e457ebd37188409fc18d503 to your computer and use it in GitHub Desktop.
Скачиватель фото со стены группы VK, созданный на Gmod Lua
local VKBOT = VK.New("")
local function log(s)
file.Append("vk_img_downloader.txt",os.date("%H:%M") .. " > " .. s .. "\n")
print(s)
end
VK_IMG_QUEUE = VK_IMG_QUEUE or {}
local queue_in_process
local function queue(i) -- i декоративный
local IMG = VK_IMG_QUEUE[1]
if !IMG then
queue_in_process = false
return
end
queue_in_process = true
local img_url = IMG.url
local texture_uid = img_url:match("^[https://|http://].+userapi.com/(.+).jpg")
IMG.TEX = texture.Create(texture_uid)
IMG.TEX.UID = IMG.name
IMG.TEX:SetFormat("jpeg")
local function cb()
log("DOWNLOADED" .. (" (%s/%s): "):format(i,#VK_IMG_QUEUE) .. img_url)
table.remove(VK_IMG_QUEUE,1)
queue(i + 1)
end
IMG.TEX:Download(img_url, cb,cb)
end
local function addImageToQueue(url,file_name)
table.insert(VK_IMG_QUEUE,{url = url,name = file_name})
log(#VK_IMG_QUEUE .. " items in queue")
if !queue_in_process then
queue(1)
end
end
VK_IMG_QUEUE = {}
local PHOTOS_QUALITY_PRIORITIES = {
"photo_2560", "photo_1280",
"photo_807", "photo_604",
"photo_130", "photo_75",
}
-- local PHOTOS_TYPES = map({"posted_photo","photo"})
local function processPost(post)
if !post.attachments then return end
for _,att in ipairs(post.attachments) do
-- if !PHOTOS_TYPES[att.type] then continue end
local photo = att.photo
if !photo then prt({пост_без_фото = post}) continue end
for _,key in ipairs(PHOTOS_QUALITY_PRIORITIES) do
if photo[key] then
addImageToQueue(photo[key],"vk;" .. post.id .. ";_" .. photo.owner_id .. "_" .. photo.id)
break -- прекращаем поиск фото другого качества
end
end
end
end
--[[-------------------------------------------------------------------------
Режим #1
Скачивание фото с N постов
startAmount(owner_id,n_items)
---------------------------------------------------------------------------]]
local function processNItems(owner_id,ITEMS_AMOUNT,MAX_ITEMS_PER_REQUEST)
local last_request_items = ITEMS_AMOUNT % MAX_ITEMS_PER_REQUEST
local need_requests = math.ceil(ITEMS_AMOUNT / MAX_ITEMS_PER_REQUEST)
for i = 1,need_requests do
local offset = (i - 1) * MAX_ITEMS_PER_REQUEST
local request_items = MAX_ITEMS_PER_REQUEST
-- Последний запрос
if i == need_requests and last_request_items ~= 0 then
request_items = last_request_items -- 0
end
VKBOT:GetWall(owner_id,request_items,offset,function(data)
for _,post in ipairs(data.items) do
processPost(post)
end
end)
end
end
local function startAmount(owner_id,n_items)
log("Начинаем скачивать фотки с " .. n_items .. " постов\n\n\n\n\n\n")
processNItems(owner_id,n_items,100)
end
--[[-------------------------------------------------------------------------
Режим #2
Скачивание фото по пост с указанным ID
startUntil(owner_id,post_id)
---------------------------------------------------------------------------]]
local POSTS_PER_STEP = 2
local MAX_STEPS_BEFORE_DIE = 50
local _step = 0 -- счетчик
local function processPostsUntil(owner_id,post_id, _offset)
VKBOT:GetWall(owner_id,POSTS_PER_STEP,_offset,function(data)
_step = _step + 1
if _step >= MAX_STEPS_BEFORE_DIE then
log("Сделали уже " .. MAX_STEPS_BEFORE_DIE .. " запросов, но не смогли найти нужный пост. Сворачиваемся")
return
end
for _,post in ipairs(data.items) do
processPost(post)
if post_id == post.id then -- дошли до нужного поста
log("Нашли нужный пост за " .. _step .. " шагов")
_step = 0
return
end
end
processPostsUntil(owner_id,post_id, (_offset or 0) + POSTS_PER_STEP)
end)
end
local function startUntil(owner_id,post_id)
log("Начинаем собирать фотки по пост #" .. post_id .. "\n\n\n\n\n\n")
processPostsUntil(owner_id,post_id)
end
--[[-------------------------------------------------------------------------
Тестовые запуски
---------------------------------------------------------------------------]]
-- startAmount(-102092477,10000)
startUntil(-102092477,17265)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment