empyrean 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,192 @@
1
+ # templaterenderer.rb - renders a ERB template
2
+ #
3
+ # This file is part of Empyrean
4
+ # Copyright (C) 2015 nilsding, pixeldesu
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'erb'
20
+ require 'empyrean/defaults'
21
+
22
+ module Empyrean
23
+ class TemplateRenderer
24
+
25
+ ##
26
+ # Initializes a new TemplateRenderer.
27
+ #
28
+ # template: The template to use (i.e. not the file name)
29
+ # parsed_tweets: The dict that gets returned by TweetParser::merge_parsed
30
+ def initialize(config, template, parsed_tweets)
31
+ @config = config
32
+ @template = template
33
+ @parsed = parsed_tweets
34
+ end
35
+
36
+ ##
37
+ # Renders @template.
38
+ def render
39
+ mentions = mentions_erb
40
+ hashtags = hashtags_erb
41
+ smileys = smileys_erb
42
+ clients = clients_erb
43
+ counters = {
44
+ tweets: @parsed[:tweet_count],
45
+ retweets: @parsed[:retweet_count],
46
+ retweets_percentage: (@parsed[:retweet_count] * 100 / @parsed[:tweet_count].to_f).round(2),
47
+ selftweets: @parsed[:selftweet_count],
48
+ selftweets_percentage: (@parsed[:selftweet_count] * 100 / @parsed[:tweet_count].to_f).round(2)
49
+ }
50
+ times_of_day = times_erb
51
+ erb = ERB.new @template
52
+ erb.result binding
53
+ end
54
+
55
+ private
56
+ ##
57
+ # Returns an array with the mentions which can be easily used within ERB.
58
+ def mentions_erb
59
+ retdict = {
60
+ enabled: @config[:mentions][:enabled],
61
+ top: [],
62
+ nottop: []
63
+ }
64
+
65
+ if @config[:mentions][:enabled]
66
+ top = @parsed[:mentions].slice(0, @config[:mentions][:top]) # top X mentions
67
+ top.each do |mention|
68
+ retdict[:top] << mention[1]
69
+ end
70
+
71
+ nottop = @parsed[:mentions].slice(@config[:mentions][:top], @config[:mentions][:notop]) # not in the top X
72
+ unless nottop.nil?
73
+ nottop.each do |mention|
74
+ mention[1].delete(:example)
75
+ retdict[:nottop] << mention[1]
76
+ end
77
+ end
78
+ end
79
+
80
+ retdict
81
+ end
82
+
83
+ ##
84
+ # Returns an array with the hashtags which can be easily used within ERB.
85
+ def hashtags_erb
86
+ retdict = {
87
+ enabled: @config[:hashtags][:enabled],
88
+ top: [],
89
+ nottop: []
90
+ }
91
+
92
+ if @config[:hashtags][:enabled]
93
+ top = @parsed[:hashtags].slice(0, @config[:hashtags][:top]) # top X hashtags
94
+ top.each do |hashtag|
95
+ retdict[:top] << hashtag[1]
96
+ end
97
+
98
+ nottop = @parsed[:hashtags].slice(@config[:hashtags][:top], @config[:hashtags][:notop]) # not in the top X
99
+ unless nottop.nil?
100
+ nottop.each do |hashtag|
101
+ hashtag[1].delete(:example)
102
+ retdict[:nottop] << hashtag[1]
103
+ end
104
+ end
105
+ end
106
+
107
+ retdict
108
+ end
109
+
110
+ ##
111
+ # Returns an array with the smileys which can be easily used within ERB.
112
+ def smileys_erb
113
+ retdict = {
114
+ enabled: @config[:smileys][:enabled],
115
+ top: [],
116
+ nottop: []
117
+ }
118
+
119
+ if @config[:smileys][:enabled]
120
+ top = @parsed[:smileys].slice(0, @config[:smileys][:top]) # top X smileys
121
+ top.each do |smiley|
122
+ retdict[:top] << smiley[1]
123
+ end
124
+
125
+ nottop = @parsed[:smileys].slice(@config[:smileys][:top], @config[:smileys][:notop]) # not in the top X
126
+ unless nottop.nil?
127
+ nottop.each do |smiley|
128
+ smiley[1].delete(:example)
129
+ retdict[:nottop] << smiley[1]
130
+ end
131
+ end
132
+ end
133
+
134
+ retdict
135
+ end
136
+
137
+ ##
138
+ # Returns an array with the clients which can be easily used within ERB.
139
+ def clients_erb
140
+ retdict = {
141
+ enabled: @config[:clients][:enabled],
142
+ top: [],
143
+ nottop: []
144
+ }
145
+
146
+ if @config[:clients][:enabled]
147
+ top = @parsed[:clients].slice(0, @config[:clients][:top]) # top X clients
148
+ top.each do |client|
149
+ retdict[:top] << {
150
+ name: client[1][:name],
151
+ url: client[1][:url],
152
+ count: client[1][:count],
153
+ percentage: (client[1][:count] * 100 / @parsed[:tweet_count].to_f).round(2)
154
+ }
155
+ end
156
+
157
+ nottop = @parsed[:clients].slice(@config[:clients][:top], @config[:clients][:notop]) # not in the top X
158
+ unless nottop.nil?
159
+ nottop.each do |client|
160
+ client[1].delete(:example)
161
+ retdict[:nottop] << {
162
+ name: client[1][:name],
163
+ url: client[1][:url],
164
+ count: client[1][:count],
165
+ percentage: (client[1][:count] * 100 / @parsed[:tweet_count].to_f).round(2)
166
+ }
167
+ end
168
+ end
169
+ end
170
+
171
+ retdict
172
+ end
173
+
174
+ def times_erb
175
+ retarr = []
176
+ max_count = @parsed[:times_of_day].max
177
+
178
+ @parsed[:times_of_day].each do |count|
179
+ retarr << {
180
+ count: count,
181
+ percentage: (count * 100 / @parsed[:tweet_count].to_f).round(1),
182
+ size: ((count / max_count.to_f) * 100).to_i,
183
+ max: count == max_count
184
+ }
185
+ end
186
+
187
+ retarr
188
+ end
189
+ end
190
+ end
191
+
192
+ # kate: indent-width 2
@@ -0,0 +1,237 @@
1
+ <%#-*- coding: utf-8 -*-%>
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <title>Twitter Stats</title>
9
+ <link rel="stylesheet" href="http://static.leafc.at/css/bootstrap.css">
10
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
11
+ <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
12
+ <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
13
+ <!--[if lt IE 9]>
14
+ <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
15
+ <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
16
+ <![endif]-->
17
+ </head>
18
+ <style>
19
+ thead {
20
+ font-weight: bold;
21
+ }
22
+
23
+ .badge {
24
+ float: right;
25
+ }
26
+
27
+ .float-right {
28
+ float: right;
29
+ }
30
+ .progress.vertical {
31
+ position: relative;
32
+ width: 20px;
33
+ margin: 0 auto;
34
+ background: none;
35
+ box-shadow: none;
36
+ border: none;
37
+ }
38
+ td.bottom-center {
39
+ vertical-align: bottom;
40
+ text-align: center;
41
+ }
42
+ .small {
43
+ font-size: 80%;
44
+ }
45
+ .bold {
46
+ font-weight: bold;
47
+ }
48
+ </style>
49
+ <body>
50
+ <div class="container">
51
+ <div class="page-header">
52
+ <h2>Twitter Stats</h2>
53
+ </div>
54
+
55
+ <p>Statistics generated on <%= Time.now.strftime("%A %d %B %Y - %T") %></p>
56
+ <p>Total number of tweets: <%= counters[:tweets] %> (<%= counters[:retweets_percentage] %>% retweets)</p>
57
+
58
+ <h4>Most active times</h4>
59
+ <p>What time do you tweet the most?</p>
60
+ <table class="table">
61
+ <tr>
62
+ <% times_of_day.each_with_index do |time, index| %>
63
+ <td class="small bottom-center">
64
+ <%= time[:percentage] %>%
65
+ <% if time[:size] > 0 %>
66
+ <br />
67
+ <div class="progress vertical" style="height: <%= time[:size] %>px;" alt="<%= time[:count] %>" title="<%= time[:count] %>"><div class="progress-bar" style="width: 100%"></div>
68
+ <% end %>
69
+ </td>
70
+ <% end %>
71
+ </tr>
72
+ <tr>
73
+ <% times_of_day.each_with_index do |time, index| %>
74
+ <td class="<% if time[:max] %>bold <% end %>small" align="center"><%= index %></td>
75
+ <% end %>
76
+ </tr>
77
+ </table>
78
+
79
+ <% if mentions[:enabled] %>
80
+ <h4>Most mentioned users</h4>
81
+ <p>With this, you can see the people you mentioned most in your Twitter time!</p>
82
+ <table class="table table-responsive table-bordered table-condensed">
83
+ <thead>
84
+ <tr>
85
+ <td>&nbsp;</td>
86
+ <td>Username</td>
87
+ <td>Times Mentioned</td>
88
+ <td>Example</td>
89
+ </tr>
90
+ </thead>
91
+ <tbody>
92
+ <% mentions[:top].each_with_index do |user, index| %>
93
+ <tr>
94
+ <td><%= index + 1 %>.</td>
95
+ <td><b><a href="https://twitter.com/<%= user[:name] %>">@<%= user[:name] %></a></b></td>
96
+ <td><%= user[:count] %></td>
97
+ <td><%= user[:example][:text] %> <a class="float-right" href="https://twitter.com/statuses/<%= user[:example][:id] %>"><i class="fa fa-arrow-right"></i></a></td>
98
+ </tr>
99
+ <% end %>
100
+ </tbody>
101
+ </table>
102
+ <% if mentions[:nottop].length > 0 %>
103
+ <h5>These didn't make it to the top…</h5>
104
+ <table class="table table-responsive table-bordered table-condensed">
105
+ <tr>
106
+ <% mentions[:nottop].each_with_index do |user, index| %>
107
+ <td><b><a href="https://twitter.com/<%= user[:name] %>">@<%= user[:name] %></a></b> <span class="badge"><%= user[:count] %></span></td>
108
+ <% if index % 5 == 4 %>
109
+ </tr>
110
+ <tr>
111
+ <% end %>
112
+ <% end %>
113
+ </tr>
114
+ </table>
115
+ <% end %>
116
+ <% end %>
117
+
118
+ <% if clients[:enabled] %>
119
+ <h4>Most used clients</h4>
120
+ <p>What is/was your favorite and most used client? Find it out right here!</p>
121
+ <table class="table table-responsive table-bordered table-condensed">
122
+ <thead>
123
+ <tr>
124
+ <td>&nbsp;</td>
125
+ <td>Client</td>
126
+ <td>Times Used</td>
127
+ <td>Percentage (% of total)</td>
128
+ </tr>
129
+ </thead>
130
+ <tbody>
131
+ <% clients[:top].each_with_index do |client, index| %>
132
+ <tr>
133
+ <td><%= index + 1 %>.</td>
134
+ <td><b><a href="<%= client[:url] %>"><%= client[:name] %></a></b></td>
135
+ <td><%= client[:count] %></td>
136
+ <td><%= client[:percentage] %>%</td>
137
+ </tr>
138
+ <% end %>
139
+ </tbody>
140
+ </table>
141
+ <% if clients[:nottop].length > 0 %>
142
+ <h5>These didn't make it to the top…</h5>
143
+ <table class="table table-responsive table-bordered table-condensed">
144
+ <tr>
145
+ <% clients[:nottop].each_with_index do |client, index| %>
146
+ <td><b><a href="<%= client[:url] %>"><%= client[:name] %></a></b> <span class="badge"><%= client[:count] %></span></td>
147
+ <% if index % 5 == 4 %>
148
+ </tr>
149
+ <tr>
150
+ <% end %>
151
+ <% end %>
152
+ </tr>
153
+ </table>
154
+ <% end %>
155
+ <% end %>
156
+
157
+ <% if smileys[:enabled] %>
158
+ <h4>Most used smileys :^)</h4>
159
+ <p>;-)</p>
160
+ <table class="table table-responsive table-bordered table-condensed">
161
+ <thead>
162
+ <tr>
163
+ <td>&nbsp;</td>
164
+ <td>Smiley</td>
165
+ <td>Times Used</td>
166
+ <td>Example</td>
167
+ </tr>
168
+ </thead>
169
+ <tbody>
170
+ <% smileys[:top].each_with_index do |smiley, index| %>
171
+ <tr>
172
+ <td><%= index + 1 %>.</td>
173
+ <td><%= smiley[:smiley] %></td>
174
+ <td><%= smiley[:count] %></td>
175
+ <td><%= smiley[:example][:text] %> <a class="float-right" href="https://twitter.com/statuses/<%= smiley[:example][:id] %>"><i class="fa fa-arrow-right"></i></a></td>
176
+ </tr>
177
+ <% end %>
178
+ </tbody>
179
+ </table>
180
+ <% if smileys[:nottop].length > 0 %>
181
+ <h5>These didn't make it to the top… ;_;</h5>
182
+ <table class="table table-responsive table-bordered table-condensed">
183
+ <tr>
184
+ <% smileys[:nottop].each_with_index do |smiley, index| %>
185
+ <td><%= smiley[:smiley] %> <span class="badge"><%= smiley[:count] %></span></td>
186
+ <% if index % 5 == 4 %>
187
+ </tr>
188
+ <tr>
189
+ <% end %>
190
+ <% end %>
191
+ </tr>
192
+ </table>
193
+ <% end %>
194
+ <% end %>
195
+
196
+ <% if hashtags[:enabled] %>
197
+ <h4>Most used hashtags</h4>
198
+ <p>What were you talking about on Twitter most? <span class="text-muted">(Or you simply spammed/overused those hashtags)</span></p>
199
+ <table class="table table-responsive table-bordered table-condensed">
200
+ <thead>
201
+ <tr>
202
+ <td>&nbsp;</td>
203
+ <td>Hashtag</td>
204
+ <td>Times Used</td>
205
+ <td>Example</td>
206
+ </tr>
207
+ </thead>
208
+ <tbody>
209
+ <% hashtags[:top].each_with_index do |hashtag, index| %>
210
+ <tr>
211
+ <td><%= index + 1 %>.</td>
212
+ <td><b><a href="https://twitter.com/search?q=%23<%= hashtag[:hashtag] %>">#<%= hashtag[:hashtag] %></a></b></td>
213
+ <td><%= hashtag[:count] %></td>
214
+ <td><%= hashtag[:example][:text] %> <a class="float-right" href="https://twitter.com/statuses/<%= hashtag[:example][:id] %>"><i class="fa fa-arrow-right"></i></a></td>
215
+ </tr>
216
+ <% end %>
217
+ </tbody>
218
+ </table>
219
+ <% if hashtags[:nottop].length > 0 %>
220
+ <h5>These didn't make it to the top…</h5>
221
+ <table class="table table-responsive table-bordered table-condensed">
222
+ <tr>
223
+ <% hashtags[:nottop].each_with_index do |hashtag, index| %>
224
+ <td><b><a href="https://twitter.com/search?q=%23<%= hashtag[:hashtag] %>">#<%= hashtag[:hashtag] %></a></b> <span class="badge"><%= hashtag[:count] %></span></td>
225
+ <% if index % 5 == 4 %>
226
+ </tr>
227
+ <tr>
228
+ <% end %>
229
+ <% end %>
230
+ </tr>
231
+ </table>
232
+ <% end %>
233
+ <% end %>
234
+
235
+ </div>
236
+ </body>
237
+ </html>