class TiaCrawler::TiaService

Public Class Methods

authenticate(credentials, token, cookie) click to toggle source

Atentica e retorna o cookie da autenticação

# File lib/tia/services/tia-service.rb, line 46
def self.authenticate(credentials, token, cookie)
  # Cabeçalho da requisição HTTP para autenticação
  headers =
  {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Cookie' => "#{cookie}",
    'Referer' => TiaURL::Login
  }

  # Corpo da requisição HTTP para autenticação
  body = {
    token: token,
    alumat: credentials.tia,
    pass: credentials.password,
    unidade: credentials.unit
  }

  # Pega a resposta do POST request do formulário de autenticação
  res = Networking.post TiaURL::Auth, headers, body, true

  # Pega o novo cookie no header da resposta HTTP
  # O novo cookie é o que precisa ser usado para fazer os próximos requests
  newCookie = res['set-cookie'].split('; ')[0]

  return newCookie
end
getAuthCookie(credentials) click to toggle source
# File lib/tia/services/tia-service.rb, line 13
def self.getAuthCookie(credentials)
  token, cookie = TiaService.get_login_token_and_cookie
  newCookie = TiaService.authenticate credentials, token, cookie

  # ???
  theCookie = "tiaMackenzieWeb=a%3A2%3A%7Bs%3A4%3A%22user%22%3BN%3Bs%3A6%3A%22passwd%22%3BN%3B%7D; #{newCookie}"

  return theCookie
end
home(credentials) click to toggle source
# File lib/tia/services/tia-service.rb, line 73
def self.home(credentials)
  cookie = TiaService.getAuthCookie credentials
  # Cabeçalho da requisição HTTP para autenticação
  headers =
  {
    'Cookie' => cookie,
    'Referer' => TiaURL::Auth
  }

  res = Networking.post TiaURL::Home, headers, nil, true
  nokogiriHTML = Nokogiri::HTML res.body

  tia, name = nokogiriHTML.css('center h2').inner_html.split(' - ')
  subject = nokogiriHTML.css('center h3')
  puts "#{tia} - #{name}"
  puts "#{subject}"

  return nokogiriHTML
end
horarios(credentials) click to toggle source
# File lib/tia/services/tia-service.rb, line 134
def self.horarios(credentials)
  cookie = TiaService.getAuthCookie credentials
  # Cabeçalho da requisição HTTP para autenticação
  headers =
  {
    'Cookie' => cookie,
    'Referer' => TiaURL::Home
  }

  res = Networking.post TiaURL::Horarios, headers, nil, true
  nokogiriHTML = Nokogiri::HTML res.body

  return TiaService.jsonSubjects nokogiriHTML
end
jsonSubjects(html) click to toggle source
# File lib/tia/services/tia-service.rb, line 93
def self.jsonSubjects(html)
  tables = html.css('.table-responsive')

  theSubjects = {}

  dias = []

  header = tables[0].css('tr')[0].css('strong')
  header[1...header.size].each do |d|
    dias << d.inner_html
  end

  tables.each_with_index do |table, i|
    rows = table.css('tr')
    rows[1...rows.size].each_with_index do |row, j|
      cols = row.css('td div')
      cols[1...cols.size].each do |c|
        items = c.inner_html.split('<br>')
        name = items[0].strip
        classid = items[1]
        building = items[2]
        classroom = items[3]

        if name != "--"
          dia = dias[j]

          theSubjects[name] = Subject.new name if theSubjects[name].nil?
          theSubjects[name].timetable.append dia, cols[0].inner_html
        end
      end
    end
  end

  json = []
  theSubjects.each do |key, val|
    json << val.as_json
  end

  return json
end