-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.rb
More file actions
97 lines (77 loc) · 2.06 KB
/
bot.rb
File metadata and controls
97 lines (77 loc) · 2.06 KB
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
require 'httparty'
require 'json'
require 'uri'
module BSU
PATH = File.dirname(__FILE__)
CFG_PATH = PATH + '/config.json'
class << self
def init
puts "Loading CFG"
@cfg = JSON.parse( File.read( CFG_PATH ) )
@cfg_mutex = Mutex.new
@thread = nil
check_thread
gets
end
def check_thread
@thread = Thread.new {
begin
@cfg[ 'last' ].each do | k, v |
get_data( k )
end
sleep 300
check_thread
rescue => err
puts err
check_thread
end
}
end
def get_data( channel )
resp = JSON.parse(
HTTParty.get(
"https://www.googleapis.com/youtube/v3/search?part=snippet,id&order=date&maxResults=50&channelId=#{ channel }&key=#{ @cfg[ 'youtube' ] }",
:verify => false
).body,
:symbolize_names => true
)
last = nil
to_publish = []
resp[ :items ].each_with_index do | item, index |
id = item[ :id ][ :videoId ]
break if id == @cfg[ 'last' ][ channel ]
if index == 0 then
last = id
end
title = item[ :snippet ][ :title ]
link = "https://www.youtube.com/watch?v=#{ id }"
to_publish.push( [ title, link ] )
end
if !to_publish.empty? then
@cfg[ 'last' ][ channel ] = last
save_cfg
post_VK( to_publish )
end
end
def post_VK( array )
array.each do | post |
l = post[ 1 ]
msg = "#{ post[ 0 ] }\n#{ l }"
resp = JSON.parse(
HTTParty.post(
URI.escape( "https://api.vk.com/method/wall.post?owner_id=#{ @cfg[ 'group' ] }&from_group=1&message=#{ msg }&signed=0&attachments=#{ l }&access_token=#{ @cfg[ 'vk' ] }" ),
:verify => false
).body,
:symbolize_names => true
)
sleep 10
end
end
def save_cfg
@cfg_mutex.synchronize do
File.open( CFG_PATH, 'w+' ) {|f| f.write( JSON.pretty_generate( @cfg ) ) }
end
end
end
end
BSU.init