-
Notifications
You must be signed in to change notification settings - Fork 4
/
sectionEstatsPresentation.rb
145 lines (124 loc) · 3.33 KB
/
sectionEstatsPresentation.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
require "net/http"
require "uri"
require 'pry'
require 'json'
require 'CSV'
require_relative 'classes'
@lastmessage = 144752542699067213
@allmessages = []
@messagecounts = []
@groupmembers = []
def stats(id)
# var = "https://v2.groupme.com/groups/" + INSERT YOUR GROUP NUMBER HERE + "/messages?before_id=" + id.to_s + "&token=" + INSERT YOUR TOKEN HERE
var = "https://v2.groupme.com/groups/" + INSERT YOUR GROUP NUMBER HERE + "/messages?token=" + INSERT YOUR TOKEN HERE
uri = URI.parse(var)
response = Net::HTTP.get_response(uri)
test = JSON.parse(response.body)
binding.pry
if test["response"]["messages"][-1]["id"].to_i == 144045784692047258
@allmessages.flatten
saveMessagesToCSV
else
@lastmessage = test["response"]["messages"][-1]["id"]
@allmessages << test["response"]["messages"]
puts @lastmessage
stats(@lastmessage)
end
end
def saveMessagesToCSV
CSV.open("data.csv", "wb") do |csv|
csv << @allmessages.flatten.first.keys # adds the attributes name on the first line
@allmessages.flatten.each do |hash|
csv << hash.values
end
end
end
def getMembers
var = "https://v2.groupme.com/groups/" + INSERT YOUR GROUP NUMBER HERE + "/messages?token=" + INSERT YOUR TOKEN HERE
uri = URI.parse(var)
response = Net::HTTP.get_response(uri)
test = JSON.parse(response.body)
members = test["response"]["group"]["memberships"]
members.each do |member|
@groupmembers << {:id => member["id"], :name => member["nickname"]}
end
saveMembersToCSV
end
def saveMembersToCSV
CSV.open("members.csv", "wb") do |csv|
csv << @groupmembers.first.keys # adds the attributes name on the first line
@groupmembers.each do |hash|
csv << hash.values
end
end
end
def openCSV
CSV.read('data.csv', :headers => true, :header_converters => :symbol).map {|row| @allmessages << Message.new(row.to_hash)}
CSV.read('members.csv', :headers => true, :header_converters => :symbol).map {|row| @groupmembers << Member.new(row.to_hash)}
end
def favsfix
@allmessages.map do |x|
favs = []
x.favorited_by.split(/,/).map! do |y|
favs << y.slice!(2,8).to_i
end
x.favorited_by = favs
end
end
def messagecounts
count = []
used = []
@allmessages.each do |x|
if used.include? x.name
count.map do |y|
if y[0] == x.name
y[1] += 1
end
end
else
count << [x.name, 1]
used << x.name
end
end
count.sort! {|x,y| y[1] <=> x[1]}
i = 1
count.each do |x|
p i.to_s + ") " + x[0] + " - " + x[1].to_s + " posts"
i += 1
end
end
def mostpopular
favcount = []
favmessages = []
@allmessages.each do |x|
favcount << [x.id, x.favorited_by.count]
end
favcount.sort! {|x,y| y[1] <=> x[1]}
test = favcount.slice!(0,10)
test.each do |x|
favmessages << @allmessages.find {|y| y.id == x[0] }
end
i = 0
n = 1
puts "The most popular posts were:"
favmessages.each do |x|
if x.text == nil
puts n.to_s + ") A picture by: " + x.name + " with " + test[i][1].to_s + " votes and the link of: " + x.picture_url
else
puts n.to_s + ") " + x.text + " by: " + x.name + " with " + test[i][1].to_s + " votes"
end
puts
puts
i += 1
n += 1
end
end
def run_it
openCSV
favsfix
messagecounts
mostpopular
end
# getMembers
# stats(145503304223160963)
run_it