susies 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/MIT_LICENSE.txt +21 -0
- data/README.md +35 -0
- data/example/susies.rb +17 -0
- data/lib/constants.rb +57 -0
- data/lib/susies.rb +285 -0
- data/susies.gemspec +14 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0608b19bce844246bc61e260d3f8aa292b932947
|
4
|
+
data.tar.gz: c7d13219a9691883ab1b91939e3a4b0f752bca40
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e38b896a71269ee1eeb55400bdd3c4ea3236f7fef021267e06ec7ad64f77e9af21f638b5019db9b8f79d1df6062f7d3c61adc65ffafdbcf80b99a9f32803f7aa
|
7
|
+
data.tar.gz: 9aef30c9f378ac421db0c86adaa6848e5f5c09b1b843b9b4401de252142a6888b84ccfe587a5293d8d1a2ed1e4724a338ecf9219acfdcfb36c133abb877354dd
|
data/Gemfile
ADDED
data/MIT_LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Simon Ninon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Susies
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Susies is a Ruby Gem intended for Epitech Students.
|
6
|
+
|
7
|
+
It's a simple piece of code allowing students to easily find and register to a Susie Class.
|
8
|
+
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
## Cron Task
|
13
|
+
|
14
|
+
## Enhancements
|
15
|
+
|
16
|
+
## Doc
|
17
|
+
|
18
|
+
You can generate documentation by executing: `sdoc`
|
19
|
+
Don't forget to install the sdoc gem (`gem install sdoc`)
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
1. Fork
|
24
|
+
2. Create your branch (`git checkout -b my-branch`)
|
25
|
+
3. Commit your new features (`git commit -am 'New features'`)
|
26
|
+
4. Push (`git push origin my-branch`)
|
27
|
+
5. Make a `Pull request`
|
28
|
+
|
29
|
+
## License
|
30
|
+
|
31
|
+
[MIT License](MIT_LICENSE.txt)
|
32
|
+
|
33
|
+
## Author
|
34
|
+
|
35
|
+
[Simon Ninon](http://sninon.fr) aka [ninon_s](http://intra.epitech.eu/user/ninon_s) aka [Cylix](http://github.com/Cylix)
|
data/example/susies.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'susies'
|
4
|
+
|
5
|
+
data = {
|
6
|
+
login: %w[login_x],
|
7
|
+
maxStudent: 6,
|
8
|
+
autologinURL: 'https://intra.epitech.eu/my_autologin_url',
|
9
|
+
mailServer: 'smtp.gmail.com',
|
10
|
+
mailPort: 587,
|
11
|
+
mailUname: 'mail@gmail.com',
|
12
|
+
mailPasswd: 'password',
|
13
|
+
mailTargets: %w[mylogin_x@epitech.eu buddy_login@epitech.eu],
|
14
|
+
buddiesAutologins: %w[https://intra.epitech.eu/buddy_autologin_url]
|
15
|
+
}
|
16
|
+
|
17
|
+
Susies.new( data ).check!
|
data/lib/constants.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'active_support/time'
|
2
|
+
|
3
|
+
class Susies
|
4
|
+
# Susie login
|
5
|
+
DEFAULT_LOGIN = "clark_s"
|
6
|
+
# Maximum already registered students
|
7
|
+
DEFAULT_MAX_STUDENT = 9
|
8
|
+
# Minimum start hour
|
9
|
+
DEFAULT_MIN_HOUR = 17
|
10
|
+
# Default Autologin
|
11
|
+
DEFAULT_AUTOLOGIN_URL = ''
|
12
|
+
# Default mail server
|
13
|
+
DEFAULT_MAIL_SERVER = ''
|
14
|
+
# Default mail port
|
15
|
+
DEFAULT_MAIL_PORT = 0
|
16
|
+
# Default mail user name
|
17
|
+
DEFAULT_MAIL_UNAME = ''
|
18
|
+
# Default mail password
|
19
|
+
DEFAULT_MAIL_PASSWD = ''
|
20
|
+
# default start week
|
21
|
+
DEFAULT_START_WEEK = Date.today.beginning_of_week
|
22
|
+
# default end week
|
23
|
+
DEFAULT_END_WEEK = Date.today.end_of_week
|
24
|
+
|
25
|
+
# Susie JSON API
|
26
|
+
SUSIES_URL = "https://intra.epitech.eu/planning/587/events"
|
27
|
+
# Susie Register link
|
28
|
+
REGISTER_URL = "https://intra.epitech.eu/planning/587"
|
29
|
+
|
30
|
+
# Cookie File
|
31
|
+
COOKIE_FILE = "cookies.txt"
|
32
|
+
# Buddies Cookie File
|
33
|
+
BUDDIES_COOKIE_FILE = "buddies_cookie_file.txt"
|
34
|
+
|
35
|
+
# Encapsulation
|
36
|
+
BASE_JSON = "activities"
|
37
|
+
# Register status to a Susie Class
|
38
|
+
EVENT_REGISTERED_JSON = "event_registered"
|
39
|
+
# Nb of people already registered to a Susie Class
|
40
|
+
REGISTERED_JSON = "registered"
|
41
|
+
# Susie data
|
42
|
+
MAKER_JSON = "maker"
|
43
|
+
# Susie login
|
44
|
+
MAKER_LOGIN_JSON = "login"
|
45
|
+
# Susie Class ID
|
46
|
+
ID_JSON = "id"
|
47
|
+
# Susie Class Start
|
48
|
+
START_JSON = "start"
|
49
|
+
# Susie Class End
|
50
|
+
END_JSON = "end"
|
51
|
+
# Susie Class Type
|
52
|
+
TYPE_JSON = "type"
|
53
|
+
# Susie Class Title
|
54
|
+
TITLE_JSON = "title"
|
55
|
+
# Susie Class Desc
|
56
|
+
DESC_JSON = "description"
|
57
|
+
end
|
data/lib/susies.rb
ADDED
@@ -0,0 +1,285 @@
|
|
1
|
+
# Constants
|
2
|
+
require 'constants.rb'
|
3
|
+
# Dependencies
|
4
|
+
require 'active_support/time'
|
5
|
+
require 'net/smtp'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Susies Class
|
10
|
+
##
|
11
|
+
class Susies
|
12
|
+
|
13
|
+
##
|
14
|
+
# Initialize
|
15
|
+
#
|
16
|
+
# @login Asked Susie Login Default: clark_s
|
17
|
+
# @maxStudent Maximum already registered students Default: 9
|
18
|
+
#
|
19
|
+
# @autologinURL Intranet Autologin URL No default
|
20
|
+
# @mailServer Mail Server No default
|
21
|
+
# @mailPort Mail Port No default
|
22
|
+
# @mailUname Mail UserName No default
|
23
|
+
# @mailPasswd Mail Password No default
|
24
|
+
# @mailTargets People to inform by mail about sub. Default: @mailUname
|
25
|
+
#
|
26
|
+
# @buddiesAuto. Buddies autologins No default
|
27
|
+
#
|
28
|
+
# @startWeek Beginning of week Default: beginning of current week
|
29
|
+
# @endWeek End of week Default: end of current week
|
30
|
+
##
|
31
|
+
def initialize(data={})
|
32
|
+
@login = data[:login] || [DEFAULT_LOGIN]
|
33
|
+
@maxStudent = data[:maxStudent] || DEFAULT_MAX_STUDENT
|
34
|
+
@minHour = data[:minHour] || DEFAULT_MIN_HOUR
|
35
|
+
@autologinURL = data[:autologinURL] || DEFAULT_AUTOLOGIN_URL
|
36
|
+
|
37
|
+
@mailServer = data[:mailServer] || DEFAULT_MAIL_SERVER
|
38
|
+
@mailPort = data[:mailPort] || DEFAULT_MAIL_PORT
|
39
|
+
@mailUname = data[:mailUname] || DEFAULT_MAIL_UNAME
|
40
|
+
@mailPasswd = data[:mailPasswd] || DEFAULT_MAIL_PASSWD
|
41
|
+
@mailTargets = data[:mailTargets] || [@mailUname]
|
42
|
+
|
43
|
+
@buddiesAutologins = data[:buddiesAutologins] || []
|
44
|
+
|
45
|
+
@startWeek = DEFAULT_START_WEEK
|
46
|
+
@endWeek = DEFAULT_END_WEEK
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
##
|
51
|
+
# Check!
|
52
|
+
#
|
53
|
+
# Iterate throw weeks, until there is no Susie in a week.
|
54
|
+
# For each week, check if the current user is already registered to a Susie Class.
|
55
|
+
# If user has not already been registered to a Susie Class, try to find an available Susie following criterias (maxStudent and Susie Login).
|
56
|
+
# If a Susie class is found, try to register to this Susie Class and send a mail to user's friends.
|
57
|
+
##
|
58
|
+
def check!
|
59
|
+
log 'Start Checking'
|
60
|
+
setAuthCookie COOKIE_FILE, @autologinURL
|
61
|
+
|
62
|
+
while true
|
63
|
+
susiesData = getSusiesData
|
64
|
+
|
65
|
+
break if noSusie? susiesData
|
66
|
+
|
67
|
+
findSusie susiesData unless registeredThisWeek? susiesData
|
68
|
+
|
69
|
+
nextWeek!
|
70
|
+
end
|
71
|
+
|
72
|
+
log 'End Checking'
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
|
79
|
+
##
|
80
|
+
# findSusie
|
81
|
+
#
|
82
|
+
# Try to find and register to a Susie Class.
|
83
|
+
# Send Mail to user's friends in case of success.
|
84
|
+
##
|
85
|
+
def findSusie(susiesData)
|
86
|
+
log "Trying to register susie class with #{ @login } for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
|
87
|
+
|
88
|
+
susiesData[BASE_JSON].each do |susie|
|
89
|
+
if matchCriterias? susie
|
90
|
+
registerSusie susie, COOKIE_FILE
|
91
|
+
registerBuddies susie
|
92
|
+
informBuddies susie
|
93
|
+
|
94
|
+
return true
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
log "No susie class found following criterias."
|
99
|
+
return false
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
##
|
104
|
+
# formatDate
|
105
|
+
#
|
106
|
+
# Take Date Object as parameter
|
107
|
+
# Return formated Date (YYY-MM-DD)
|
108
|
+
##
|
109
|
+
def formatDate(date)
|
110
|
+
date.strftime '%Y-%m-%d'
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
##
|
115
|
+
# formatTime
|
116
|
+
#
|
117
|
+
# Take Time Object as parameter
|
118
|
+
# Return formated Time (HH:MM YYY-MM-DD)
|
119
|
+
##
|
120
|
+
def formatTime(time)
|
121
|
+
time.strftime '%H:%M %Y-%m-%d'
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
##
|
126
|
+
# getSusiesData
|
127
|
+
#
|
128
|
+
# Return Susie Classes JSON Data
|
129
|
+
#
|
130
|
+
def getSusiesData
|
131
|
+
JSON.parse `curl -s -L -b #{ COOKIE_FILE } '#{ SUSIES_URL }?start=#{ formatDate @startWeek }&end=#{ formatDate @endWeek }&format=json'`
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
##
|
136
|
+
# getRegisterMail
|
137
|
+
#
|
138
|
+
# Generate mail which will be send to user's friends.
|
139
|
+
##
|
140
|
+
def getRegisterMail(susie)
|
141
|
+
<<-MESSAGE
|
142
|
+
Hey,
|
143
|
+
|
144
|
+
I've just registered to a susie class with #{ susie[MAKER_JSON][MAKER_LOGIN_JSON] }.
|
145
|
+
|
146
|
+
Title: #{ susie[TITLE_JSON] }
|
147
|
+
Type: #{ susie[TYPE_JSON] }
|
148
|
+
|
149
|
+
Description:
|
150
|
+
#{ susie[DESC_JSON] }
|
151
|
+
|
152
|
+
Start: #{ susie[START_JSON] }.
|
153
|
+
End: #{ susie[END_JSON] }.
|
154
|
+
Place left: #{ 9 - susie[REGISTERED_JSON] }.
|
155
|
+
|
156
|
+
Register here: #{ REGISTER_URL }/#{ susie[ID_JSON] }.
|
157
|
+
|
158
|
+
MESSAGE
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
##
|
163
|
+
# informBuddies
|
164
|
+
#
|
165
|
+
# send mail to user's friends
|
166
|
+
##
|
167
|
+
def informBuddies(susie)
|
168
|
+
message = getRegisterMail susie
|
169
|
+
|
170
|
+
for email in @mailTargets
|
171
|
+
log "Send mail to #{ email }"
|
172
|
+
sendMail email, message
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
##
|
178
|
+
# log
|
179
|
+
#
|
180
|
+
# Write log info
|
181
|
+
##
|
182
|
+
def log(message)
|
183
|
+
puts "[#{ formatTime Time.now }]> #{ message }"
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
##
|
188
|
+
# matchCriterias
|
189
|
+
#
|
190
|
+
# Does Susie match defined criterias?
|
191
|
+
##
|
192
|
+
def matchCriterias?(susie)
|
193
|
+
@login.each do |login|
|
194
|
+
return true if susie[MAKER_JSON][MAKER_LOGIN_JSON] == login and susie[REGISTERED_JSON] <= @maxStudent and Time.parse(susie[START_JSON]).hour >= @minHour
|
195
|
+
end
|
196
|
+
false
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
##
|
201
|
+
# nextWeek!
|
202
|
+
#
|
203
|
+
# Go to next week
|
204
|
+
##
|
205
|
+
def nextWeek!
|
206
|
+
@startWeek += 1.week
|
207
|
+
@endWeek += 1.week
|
208
|
+
end
|
209
|
+
|
210
|
+
|
211
|
+
##
|
212
|
+
# noSusie?
|
213
|
+
#
|
214
|
+
# Check if there are SusieClasses in the given JSON data
|
215
|
+
##
|
216
|
+
def noSusie?(susiesData)
|
217
|
+
!susiesData[BASE_JSON] or susiesData[BASE_JSON].length == 0
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
##
|
222
|
+
# registerBuddies
|
223
|
+
#
|
224
|
+
# register buddies with given autologins
|
225
|
+
##
|
226
|
+
def registerBuddies(susie)
|
227
|
+
@buddiesAutologins.each do |autologin|
|
228
|
+
setAuthCookie BUDDIES_COOKIE_FILE, autologin
|
229
|
+
registerSusie susie, BUDDIES_COOKIE_FILE
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
##
|
235
|
+
# registerSusie
|
236
|
+
#
|
237
|
+
# register to given SusieClass
|
238
|
+
##
|
239
|
+
def registerSusie(susie, cookie_file)
|
240
|
+
`curl -s -b #{ cookie_file } -L -X POST #{ REGISTER_URL }/#{ susie[ID_JSON] }/subscribe?format=json`
|
241
|
+
log "Succesfully registered to susie class."
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
##
|
246
|
+
# registeredThisWeek?
|
247
|
+
#
|
248
|
+
# Determine if user is already registered to a Susie Class during the given week.
|
249
|
+
##
|
250
|
+
def registeredThisWeek?(weekSusies)
|
251
|
+
weekSusies[BASE_JSON].each do |susie|
|
252
|
+
if susie[EVENT_REGISTERED_JSON]
|
253
|
+
log "Already registered to a susie class with #{ susie[MAKER_JSON][MAKER_LOGIN_JSON] } for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
|
254
|
+
return true
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
log "Not registered to a susie class for week: #{ formatDate @startWeek } #{ formatDate @endWeek }."
|
259
|
+
return false
|
260
|
+
end
|
261
|
+
|
262
|
+
|
263
|
+
##
|
264
|
+
# sendMail
|
265
|
+
#
|
266
|
+
# send mail from user to a buddy.
|
267
|
+
##
|
268
|
+
def sendMail(email, message)
|
269
|
+
smtp = Net::SMTP.new @mailServer, @mailPort
|
270
|
+
smtp.enable_starttls_auto
|
271
|
+
smtp.start @mailServer, @mailUname, @mailPasswd, :plain
|
272
|
+
smtp.send_message message, @mailUname, email
|
273
|
+
end
|
274
|
+
|
275
|
+
|
276
|
+
##
|
277
|
+
# setAuthCookie
|
278
|
+
#
|
279
|
+
# Log on the intra and save user's cookie in COOKIE_FILE
|
280
|
+
##
|
281
|
+
def setAuthCookie(cookie_file, autologin)
|
282
|
+
`curl -s -L -c #{ cookie_file } #{ autologin }`
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
data/susies.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'susies'
|
3
|
+
s.version = '0.0.2'
|
4
|
+
s.date = '2013-04-11'
|
5
|
+
s.summary = 'Epitech Susie Classes Bot'
|
6
|
+
s.description = 'Susies is a Gem to register easily for Epitech Susie Classes'
|
7
|
+
s.has_rdoc = true
|
8
|
+
s.authors = ['Simon Ninon']
|
9
|
+
s.email = 'simon.ninon@gmail.com'
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
12
|
+
s.license = 'MIT'
|
13
|
+
s.homepage = 'https://github.com/Cylix/Susies'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: susies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Ninon
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Susies is a Gem to register easily for Epitech Susie Classes
|
14
|
+
email: simon.ninon@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- Gemfile
|
20
|
+
- MIT_LICENSE.txt
|
21
|
+
- README.md
|
22
|
+
- example/susies.rb
|
23
|
+
- lib/constants.rb
|
24
|
+
- lib/susies.rb
|
25
|
+
- susies.gemspec
|
26
|
+
homepage: https://github.com/Cylix/Susies
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.2.1
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Epitech Susie Classes Bot
|
50
|
+
test_files: []
|