-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-students.ruby
More file actions
79 lines (67 loc) · 1.86 KB
/
fetch-students.ruby
File metadata and controls
79 lines (67 loc) · 1.86 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
require 'csv'
require 'json'
require 'net/http'
$airTableToken = ARGV[0]
$requestsPerSecondLimit = 4
def requestStudentsPage(offset)
requestParams = { }
if (offset != nil)
requestParams["offset"] = offset
end
uri = URI('https://api.airtable.com/v0/appFDFb3CkxWHXrd5/Users?view=All%20participants')
uri.query = URI.encode_www_form(requestParams)
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{$airTableToken}"
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) {|http|
http.use_ssl = true
http.request(req)
}
return res
end
def getStudentsPage(offset)
response = requestStudentsPage(offset)
return JSON.parse(response.body)
end
def writeStudentsToCsv(csv, studentsPage)
studentsPage["records"].each do |record|
fields = record["fields"]
if (fields != nil)
telegramId = fields["Telegram Nickname"]
firstName = fields["First Name"]
lastName = fields["Last Name"]
if (telegramId != nil && firstName != nil && lastName != nil)
csv << ["@" + telegramId, firstName + " " + lastName]
end
end
end
end
class LimiterPerSecond
def initialize(limit)
@limit = limit
@counter = 0
end
def execute()
if (@counter == @limit)
sleep(1)
@counter = 0
end
@counter = @counter + 1
result = yield
return result
end
end
csv_string = CSV.generate do |csv|
csv << ["telegramId", "name"]
offset = nil
requestCounter = 1
limiter = LimiterPerSecond.new(3)
loop do
studentsPage = limiter.execute { getStudentsPage(offset) }
writeStudentsToCsv(csv, studentsPage)
offset = studentsPage['offset']
if offset == nil
break
end
end
end
puts csv_string