-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_notify.rb
174 lines (151 loc) · 3.67 KB
/
vk_notify.rb
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
#!/usr/bin/env ruby
# == Synopsis
# simple command line utility for sending
# a message to all application's users
#
# == Usage
# vk_notify.rb [OPTIONS]
#
# --help, -h:
# this help
#
# --users, -u:
# users file
#
# --message, -m:
# message to send
#
# --app, -a:
# app name
#
# == Config
# configuration lives in ~/.vk_apps
# it's a yaml file with following structure
#
# app_name:
# api_id: 123
# api_secret: foo
#
# == Author
# Viktor Kotseruba <[email protected]>
#
# == Copyright
# Copyright (c) 2010 Beenza Games
require "yaml"
require "getoptlong"
require "rdoc/usage"
require "md5"
require "net/http"
require "uri"
require "json"
API_URL = "http://api.vkontakte.ru/api.php"
API_VERSION = "3.0"
METHOD = "secure.sendNotification"
CHUNK_SIZE = 100
API_URI = URI.parse(API_URL)
class VkNotify
def run
configure parse_arguments
load_vk_apps
load_app_config
parse_users_file
send_message
end
private
def load_app_config
@app_config = @vk_apps[@config[:app]]
throw "app not found" unless @app_config
throw "api_id not specified" unless @app_config["api_id"]
throw "api_secret not specified" unless @app_config["api_secret"]
end
def load_vk_apps
@vk_apps = YAML.load_file File.expand_path("~/.vk_apps")
end
def parse_users_file
@users = []
file = File.new @config[:users_file]
while line = file.gets
@users << line.to_i if line.to_i > 0
end
file.close
@users.uniq!
end
def parse_arguments
opts = GetoptLong.new(
["--help", "-h", GetoptLong::NO_ARGUMENT],
["--users", "-u", GetoptLong::REQUIRED_ARGUMENT],
["--message", "-m", GetoptLong::REQUIRED_ARGUMENT],
["--app", "-a", GetoptLong::REQUIRED_ARGUMENT]
)
options = {}
opts.each { |key, value| options[key] = value }
options
end
def configure(options)
RDoc::usage if options["--help"]
@config = {
:users_file => options["--users"],
:message => options["--message"],
:app => options["--app"]
}
RDoc::usage unless @config[:users_file] && @config[:message] && @config[:app]
end
def send_message
@http = Net::HTTP.new(API_URI.host, API_URI.port)
total = @users.size
complete = 0
until @users.empty?
uids = @users.slice!(0, CHUNK_SIZE)
send_message_to uids
if total > complete + CHUNK_SIZE
complete += CHUNK_SIZE
else
complete = total
end
display_progress complete, total
end
end
def display_progress(complete, total)
puts "complete #{(100.0 * complete / total).ceil}% (#{complete} of #{total})"
end
def send_message_to(uids)
params = get_params_for uids
query = params.map{ |key, value|
"#{CGI::escape(key)}=#{CGI::escape(value.to_s)}"
} * "&"
http_response = @http.get("#{API_URI.path}?#{query}")
response = JSON.parse(http_response.body)
if response.include? "error"
if response["error"]["error_code"] == 6
sleep 0.5
send_message_to uids
else
throw response["error"]["error_msg"]
end
end
end
def get_params_for(uids)
params = {
"method" => METHOD,
"api_id" => @app_config["api_id"],
"v" => API_VERSION,
"format" => "JSON",
"timestamp" => Time.now.to_i,
"random" => ((1 << 32) * rand).ceil,
"uids" => uids * ",",
"message" => @config[:message]
}
sign_params! params
params
end
def sign_params!(params)
sig_string = ""
params.sort.each do |key, val|
sig_string += "#{key}=#{val}"
end
sig_string += @app_config["api_secret"]
params["sig"] = MD5.new(sig_string).hexdigest
end
end
app = VkNotify.new
app.run