tia-crawler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd31504cbd760b69af3b4f391fb2db9ca2cd74c3
4
+ data.tar.gz: 29de4f16599b89eaf1afda7f553a2dc83c0d141c
5
+ SHA512:
6
+ metadata.gz: bb3fc8f12106af50de8aa17b0a7dc9ece6f96bf2ff1407418d6aab9f15900829b55acf696cc3251b07e537eb86a543c564d349b4a28d4992e8271665fcbf11b2
7
+ data.tar.gz: 1f4c7fae45f0ca6408997c96b827159446270deb05a06d4ee69110ede4a2ef8e856ce2e766a79c6d8eb2748c47b4d9c43b83afd3eebebf495f0d6224d7714b63
data/bin/tia-crawler ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ if RUBY_VERSION < '2.0.0'
4
+ abort "tia-crawler requires Ruby 2.0.0 or higher"
5
+ end
6
+
7
+ require_relative '../lib/tia-crawler'
8
+
9
+ TiaCrawler::CLI.start ARGV
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require_relative 'tia/services/tia-service'
4
+ require_relative 'tia/models/credentials'
5
+
6
+ module TiaCrawler
7
+ class CLI < Thor
8
+ desc 'horarios', 'Retorna a lista de horarios em formato JSON'
9
+ def horarios
10
+ print "Tia: "
11
+ alumat = STDIN.gets.chomp
12
+
13
+ print "Senha: "
14
+ password = STDIN.noecho(&:gets)
15
+
16
+ puts "\nCrawling horarios..."
17
+
18
+ credentials = Credentials.new alumat, password, '001'
19
+ puts TiaService.horarios credentials
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module TiaCrawler
2
+ class Credentials
3
+ attr_accessor :tia, :password, :unit
4
+ def initialize(tia, pass, unit)
5
+ @tia = tia
6
+ @password = pass
7
+ @unit = unit
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module TiaCrawler
2
+ class TiaURL
3
+ Login = 'https://www3.mackenzie.br/tia/index.php'
4
+ Auth = 'https://www3.mackenzie.br/tia/verifica.php'
5
+ Home = 'https://www3.mackenzie.br/tia/index2.php'
6
+ Horarios = 'https://www3.mackenzie.br/tia/horarChamada.php'
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+ module TiaCrawler
2
+ class Timetable
3
+ attr_accessor :seg, :ter, :qua, :qui, :sex, :sab
4
+
5
+ def initialize
6
+ @seg = []
7
+ @ter = []
8
+ @qua = []
9
+ @qui = []
10
+ @sex = []
11
+ @sab = []
12
+ end
13
+
14
+ def append(dayString, val)
15
+ if dayString == "Segunda"
16
+ @seg << val
17
+ elsif dayString == "Terça"
18
+ @ter << val
19
+ elsif dayString == "Quarta"
20
+ @qua << val
21
+ elsif dayString == "Quinta"
22
+ @qui << val
23
+ elsif dayString == "Sexta"
24
+ @sex << val
25
+ elsif dayString == "Sábado"
26
+ @sab << val
27
+ else
28
+ puts "Failed to append"
29
+ end
30
+ end
31
+
32
+ def as_json
33
+ return {
34
+ "seg" => @seg,
35
+ "ter" => @ter,
36
+ "qua" => @qua,
37
+ "qui" => @qui,
38
+ "sex" => @sex,
39
+ "sab" => @sab
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'nokogiri'
4
+
5
+ module TiaCrawler
6
+ class Networking
7
+ def self.post(url, headers, body, ssl = true)
8
+ uri = URI.parse url
9
+ https = Net::HTTP.new uri.host, uri.port
10
+ https.use_ssl = ssl
11
+ req = Net::HTTP::Post.new uri.path, headers
12
+ req.set_form_data body unless body.nil?
13
+ res = https.request req
14
+ return res
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,147 @@
1
+ require 'io/console'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'nokogiri'
5
+
6
+ require_relative '../models/tia-url'
7
+ require_relative '../models/timetable'
8
+ require_relative 'networking'
9
+
10
+ module TiaCrawler
11
+ class TiaService
12
+ def self.getAuthCookie(credentials)
13
+ token, cookie = TiaService.get_login_token_and_cookie
14
+ newCookie = TiaService.authenticate credentials, token, cookie
15
+
16
+ # ???
17
+ theCookie = "tiaMackenzieWeb=a%3A2%3A%7Bs%3A4%3A%22user%22%3BN%3Bs%3A6%3A%22passwd%22%3BN%3B%7D; #{newCookie}"
18
+
19
+ return theCookie
20
+ end
21
+
22
+ # Entra na página de login do tia para pegar
23
+ # o token e o cookie necessários para o login
24
+ def self.get_login_token_and_cookie
25
+ begin
26
+ # Entra na página de login do TIA
27
+ uri = URI.parse TiaURL::Login
28
+ response = Net::HTTP.get_response uri
29
+
30
+ # Guarda o cookie recebido na resposta HTTP (PHPSESSID)
31
+ cookie = response['set-cookie'].split('; ')[0]
32
+
33
+ # Guarda o token que está no formulário de login, dentro da página
34
+ nokogiriHTML = Nokogiri::HTML response.body
35
+ token = nokogiriHTML.css('[name="token"]')[0]['value']
36
+
37
+ return {token: token, cookie: cookie}
38
+ rescue Exception => e
39
+ puts "❗️ Failed!"
40
+ puts e
41
+ end
42
+ end
43
+
44
+ # Atentica e retorna o cookie da autenticação
45
+ def self.authenticate(credentials, token, cookie)
46
+ # Cabeçalho da requisição HTTP para autenticação
47
+ headers =
48
+ {
49
+ 'Content-Type' => 'application/x-www-form-urlencoded',
50
+ 'Cookie' => "#{cookie}",
51
+ 'Referer' => TiaURL::Login
52
+ }
53
+
54
+ # Corpo da requisição HTTP para autenticação
55
+ body = {
56
+ token: token,
57
+ alumat: credentials.tia,
58
+ pass: credentials.password,
59
+ unidade: credentials.unit
60
+ }
61
+
62
+ # Pega a resposta do POST request do formulário de autenticação
63
+ res = Networking.post TiaURL::Auth, headers, body, true
64
+
65
+ # Pega o novo cookie no header da resposta HTTP
66
+ # O novo cookie é o que precisa ser usado para fazer os próximos requests
67
+ newCookie = res['set-cookie'].split('; ')[0]
68
+
69
+ return newCookie
70
+ end
71
+
72
+ def self.home(credentials)
73
+ cookie = TiaService.getAuthCookie credentials
74
+ # Cabeçalho da requisição HTTP para autenticação
75
+ headers =
76
+ {
77
+ 'Cookie' => cookie,
78
+ 'Referer' => TiaURL::Auth
79
+ }
80
+
81
+ res = Networking.post TiaURL::Home, headers, nil, true
82
+ nokogiriHTML = Nokogiri::HTML res.body
83
+
84
+ tia, name = nokogiriHTML.css('center h2').inner_html.split(' - ')
85
+ subject = nokogiriHTML.css('center h3')
86
+ puts "#{tia} - #{name}"
87
+ puts "#{subject}"
88
+
89
+ return nokogiriHTML
90
+ end
91
+
92
+ def self.jsonSubjects(html)
93
+ tables = html.css('.table-responsive')
94
+
95
+ theSubjects = {}
96
+
97
+ dias = []
98
+
99
+ header = tables[0].css('tr')[0].css('strong')
100
+ header[1...header.size].each do |d|
101
+ dias << d.inner_html
102
+ end
103
+
104
+ tables.each_with_index do |table, i|
105
+ rows = table.css('tr')
106
+ rows[1...rows.size].each_with_index do |row, j|
107
+ cols = row.css('td div')
108
+ cols[1...cols.size].each do |c|
109
+ items = c.inner_html.split('<br>')
110
+ name = items[0].strip
111
+ classid = items[1]
112
+ building = items[2]
113
+ classroom = items[3]
114
+
115
+ if name != "--"
116
+ dia = dias[j]
117
+ theSubjects[name] = Timetable.new if theSubjects[name].nil?
118
+ theSubjects[name].append dia, cols[0].inner_html
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ json = {}
125
+ theSubjects.each do |key, val|
126
+ json[key] = val.as_json
127
+ end
128
+
129
+ return json
130
+ end
131
+
132
+ def self.horarios(credentials)
133
+ cookie = TiaService.getAuthCookie credentials
134
+ # Cabeçalho da requisição HTTP para autenticação
135
+ headers =
136
+ {
137
+ 'Cookie' => cookie,
138
+ 'Referer' => TiaURL::Home
139
+ }
140
+
141
+ res = Networking.post TiaURL::Horarios, headers, nil, true
142
+ nokogiriHTML = Nokogiri::HTML res.body
143
+
144
+ return TiaService.jsonSubjects nokogiriHTML
145
+ end
146
+ end
147
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tia-crawler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aleph Retamal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem para acessar os dados do TIA Mackenzista
14
+ email: retamal.ph@gmail.com
15
+ executables:
16
+ - tia-crawler
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/tia-crawler
21
+ - lib/tia-crawler.rb
22
+ - lib/tia/models/credentials.rb
23
+ - lib/tia/models/tia-url.rb
24
+ - lib/tia/models/timetable.rb
25
+ - lib/tia/services/networking.rb
26
+ - lib/tia/services/tia-service.rb
27
+ homepage: https://github.com/alaphao/tia-crawler
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.5.1
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Tia Crawler
51
+ test_files: []