|
$ -> |
|
API_ID = <%= @config['vk_api_id'] %> |
|
ANONYMOUS_NAME = '<%= @config['anonymous_name'] %>' |
|
offset = 0 |
|
limit = 200 # Maximum |
|
timeout_limit = <%= @config['vk_timeout'] %> |
|
avatar_field = 'photo_<%= @config['vk_avatar_size'] %>' |
|
page_index = 0 |
|
rows_count = 0 |
|
max_rows_count = 50 |
|
last_index = <%= @config['last_index'] %> |
|
|
|
timer_id = null |
|
|
|
module_ids = { |
|
blog: 1 |
|
news: 2 |
|
publ: 3 |
|
photo: 4 |
|
load: 5 |
|
dir: 6 |
|
board: 7 |
|
} |
|
|
|
parse_url = (url) -> |
|
a = url.split '/' |
|
{ |
|
module_id: module_ids[a[3]] |
|
material_id: a[a.length - 1].split('-').pop() |
|
} |
|
|
|
pages = [] |
|
$target = $('#target') |
|
$progressbar = $('#progressbar') |
|
|
|
comment_template = (comment, options = {}) -> |
|
if comment.user |
|
username = "#{comment.user.first_name} #{comment.user.last_name}".replace(/\|/g, "|") |
|
else |
|
username = ANONYMOUS_NAME |
|
comment.user = {} |
|
comment_text = comment.text.replace(/\|/g, "|") |
|
vk_avatar_url = comment.user[avatar_field] || '\\N' |
|
vk_profile_url = comment.user.uid && "http://vk.com/id#{comment.user.uid}" || '' |
|
[ |
|
comment.id # id |
|
options.module_id # module_id |
|
options.material_id # material_id |
|
0 # premoderation_flag (0 - active, 1 - disabled) |
|
comment.date # created_at timestamp |
|
'' # nickname |
|
username # username |
|
'' # email |
|
comment.user.uid || '' # site_url(?) || vk_uid |
|
'0.0.0.0' # ip |
|
comment_text # text |
|
'' # answer text |
|
0 # user_id |
|
options.parent_id # parent_id |
|
0 # rate |
|
'' # rate_user_ids |
|
0 # unknown_id |
|
101 # unknown_constant (101 (?) - vk, 0 - for ucoz, N - for old) |
|
comment.user.uid || '\\N' # vk_uid |
|
vk_profile_url # vk_profile_url |
|
vk_avatar_url # vk_avatar_url |
|
0 # anonimous? (0 - no, 1 - yes) |
|
0 # unknown_field |
|
].join('|') + "|\n" |
|
|
|
VK.init( |
|
apiId: API_ID |
|
onlyWidgets: true |
|
) |
|
|
|
get_pages = () -> |
|
console.log "get_pages #{limit}, #{offset}" |
|
VK.Api.call('widgets.getPages', |
|
widget_api_id: API_ID |
|
period: 'alltime' |
|
count: limit |
|
offset: offset |
|
, (r) -> |
|
console.log 'get_pages response' |
|
unless r.response |
|
console.log "ERROR: call 'widgets.getPages'" |
|
console.log r |
|
return |
|
for page in r.response.pages |
|
if page.comments.count |
|
pages.push page |
|
if r.response.pages.length < limit || (offset + limit) >= parseInt(r.response.count) |
|
console.log 'Clear interval...' |
|
clearInterval timer_id |
|
start_comment_cycle() |
|
return |
|
offset += limit |
|
console.log "Limit: #{limit}, offset: #{offset}, count: #{r.response.count}" |
|
return |
|
) |
|
return |
|
get_pages() # VK login |
|
|
|
$('#get_pages').click -> |
|
$(this).prop('disabled', true) |
|
console.log 'click!' |
|
old_offset = offset |
|
get_pages() |
|
timer_id = setInterval(() -> |
|
console.log "cycle... #{old_offset} #{offset}" |
|
if old_offset != offset |
|
old_offset = offset |
|
get_pages() |
|
return |
|
, timeout_limit) |
|
return |
|
|
|
start_comment_cycle = () -> |
|
console.log 'start_comment_cycle!' |
|
old_index = page_index = 0 |
|
get_comments_for(pages[page_index]) |
|
timer_id = setInterval(() -> |
|
console.log "get_comments cycle... i#{old_index} i#{page_index} #{pages[page_index].url} (#{pages[page_index].page_id})" |
|
if old_index != page_index |
|
old_index = page_index |
|
get_comments_for(pages[page_index]) |
|
return |
|
, timeout_limit) |
|
return |
|
|
|
get_comments_for = (page) -> |
|
old_offset = offset = 0 |
|
local_timer_id = null |
|
result = '' |
|
vk_get_comments = () -> |
|
options = |
|
widget_api_id: API_ID |
|
url: page.url |
|
count: limit |
|
offset: offset |
|
fields: ['from_id', 'date', 'text', 'replies', avatar_field] |
|
options['page_id'] = page.page_id if page.page_id |
|
VK.Api.call("widgets.getComments", options, (r) -> |
|
unless r.response |
|
console.log "ERROR: call 'widgets.getComments'" |
|
console.log r |
|
return |
|
add_to_result = (com, parent_id = null) -> |
|
options = parse_url page.url |
|
options.parent_id = com.parent_id || parent_id || 0 |
|
result += comment_template(com, options) |
|
rows_count += 1 |
|
return |
|
result_arr = [] |
|
user_ids = [] |
|
for comment in r.response.posts |
|
last_index += 1 |
|
comment.id = last_index |
|
result_arr.push comment |
|
user_ids.push(comment.from_id) unless comment.user |
|
# Если на этот комментарий отвечали... |
|
if comment.comments.replies.length |
|
# Добавляем каждый ответ к себе |
|
for reply in comment.comments.replies |
|
last_index += 1 |
|
reply.id = last_index |
|
# Для ответов иногда не подгружается информация о пользователе. |
|
# Для комментариев первого уровня такого поведения не замечено |
|
user_ids.push(reply.uid) unless reply.user |
|
reply.parent_id = comment.id |
|
result_arr.push reply |
|
|
|
final_callback = -> |
|
if r.response.count < limit || (offset + limit) >= parseInt(r.response.count) |
|
$target.append result |
|
add_to_file = (need_clear = true) -> |
|
$.post('/add_to_file', |
|
comments: $target.html(), |
|
(data) -> |
|
$progressbar.hide() |
|
$target.html('') |
|
rows_count = 0 |
|
if need_clear |
|
clear_interval() |
|
else # all is done, refresh page |
|
window.location.reload() |
|
return |
|
) |
|
return |
|
clear_interval = () -> |
|
console.log "Clear getComments #{page_index} interval..." |
|
clearInterval(local_timer_id) |
|
if (page_index + 1) >= pages.length |
|
console.log "Clear main pages interval..." |
|
clearInterval timer_id |
|
add_to_file(false) |
|
return |
|
page_index += 1 |
|
return |
|
if rows_count >= max_rows_count |
|
$progressbar.show() |
|
add_to_file() |
|
else |
|
clear_interval() |
|
return |
|
offset += limit |
|
console.log "Limit: #{limit}, offset: #{offset}, count: #{r.response.count}" |
|
return |
|
|
|
if user_ids.length |
|
VK.Api.call('users.get', |
|
user_ids: user_ids |
|
fields: [avatar_field] |
|
, (ur)-> |
|
console.log "users.get" |
|
console.log user_ids |
|
console.log ur |
|
users = {} |
|
for user in ur.response |
|
users[user.uid] = user |
|
console.log users |
|
for comment in result_arr |
|
comment.user ||= users[comment.from_id || comment.uid] |
|
add_to_result comment |
|
final_callback() |
|
return |
|
) |
|
else |
|
for r_comment in result_arr |
|
add_to_result r_comment |
|
final_callback() |
|
return |
|
) |
|
return |
|
vk_get_comments() |
|
local_timer_id = setInterval(() -> |
|
if old_offset != offset |
|
old_offset = offset |
|
vk_get_comments() |
|
return |
|
, timeout_limit) |
|
return |
|
|
|
return |