Created
February 19, 2024 16:32
-
-
Save olegykz/6b2436c6fecea9ec3f8cd008e89fe936 to your computer and use it in GitHub Desktop.
Super dirty script which does its job: grabs all the comments & posts from specified user in particular group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'vkontakte_client' | |
require 'cgi' | |
require 'watir' | |
CLIENT_ID = 1234567 | |
l = Logger.new("#{Time.now.to_i}.log") | |
redirect_uri = CGI.escape('https://oauth.vk.com/blank.html') | |
scope = 8192 | |
token = nil # In order to avoid browser step you might put real token here | |
Timeout::timeout(300) do | |
break if token | |
url = "https://oauth.vk.com/authorize?client_id=#{CLIENT_ID}&redirect_uri=#{redirect_uri}&display=page&response_type=token&scope=#{scope}" | |
b = Watir::Browser.new | |
b.goto(url) | |
loop do | |
l.debug 'Waiting for token...' | |
if b.url.include?('access_token') | |
token = CGI.parse(b.url[/#.+/])['#access_token'].first | |
l.info "Got: #{token[0..5]}" | |
break | |
end | |
sleep(1) | |
end | |
rescue Timeout::Error | |
l.warn 'Timeout' | |
exit 1 | |
end | |
api = Vkontakte::API.new(token) | |
group_id = -123 | |
user_id = 123 | |
posts_offset = 0 | |
get_link = ->(post_id, thread_id, comment_id ) { "https://vk.com/wall#{group_id}_#{post_id}?reply=#{comment_id}&thread=#{thread_id}" } | |
loop do | |
posts = api.wall_get(owner_id: group_id, count: 100, offset: posts_offset) | |
l.info "Got #{posts['count']} posts (offset #{posts_offset})" | |
posts['items'].each do |item| | |
l.info "#{Time.at(item['date'])}: P / #{item['text']} (#{get_link.(item['id'], '', '')})" if item['from_id'] == user_id | |
comments_offset = 0 | |
comments = sleep(0.2) && api.wall_getComments(owner_id: group_id, post_id: item['id'], count: 100, offset: comments_offset) | |
comments['items'].each do |comment| | |
l.info "#{Time.at(comment['date'])}: C / #{comment['text']} (#{get_link.(item['id'], comment['id'], '')})" if comment['from_id'] == user_id | |
thread_count = comment.dig('thread', 'count') | |
next if thread_count.zero? | |
comment_thread = sleep(0.2) && api.wall_getComments(owner_id: group_id, comment_id: comment['id'], count: 100) | |
comment_thread['items'].each do |c_t| | |
if c_t['from_id'] == user_id | |
l.info "#{Time.at(c_t['date'])}: CT / #{c_t['text']} (#{get_link.(item['id'], comment['id'], c_t['id'])})" | |
end | |
end | |
end | |
end | |
posts_offset += 100 | |
break if posts_offset >= posts['count'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment