-
Notifications
You must be signed in to change notification settings - Fork 6
/
repl.rb
67 lines (55 loc) · 1.57 KB
/
repl.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
module Repl
class Session
require 'mysql2'
require 'httparty'
def initialize(opts)
@logging = opts.delete(:log)
@client = Mysql2::Client.new(opts)
@db = opts[:db] || 'enwiki_p'
@getter = HTTParty
@base_uri = 'https://xtools.wmcloud.org/api/user'
@uri = URI::Parser.new
end
def count_articles_created(username)
@getter.get(
"#{@base_uri}/pages_count/#{@db}/#{@uri.escape(username.score)}"
)['counts']['0']['count'].to_i
end
def count_namespace_edits(username, namespace = 0)
@getter.get(
"#{@base_uri}/namespace_totals/#{@db}/#{@uri.escape(username.score)}"
)['namespace_totals'][namespace.to_s].to_i
end
def count_nonautomated_edits(username)
@getter.get(
"#{@base_uri}/automated_editcount/en.wikipedia.org/#{@uri.escape(username.score)}"
)['nonautomated_editcount'].to_i
end
def count_nonautomated_namespace_edits(username, namespace)
@getter.get(
"#{@base_uri}/automated_editcount/en.wikipedia.org/#{@uri.escape(username.score)}/#{namespace}"
)['nonautomated_editcount'].to_i
end
def count_tool_edits(username, tool)
countAutomatedEdits(username, false, tool)
end
def query(sql)
puts sql if @logging
@client.query(sql)
end
def prepare(sql)
puts sql if @logging
@client.prepare(sql)
end
def escape(string)
@client.escape(string)
end
def getter
@getter
end
private
def count(sql)
query(sql).first.values[0].to_i
end
end
end