deploy.py 3.43 KB
#-*- coding: utf-8 -*-
from flask import Flask, render_template, request, url_for
from iselenium.py import SeleniumInterface as SI
import random

app = Flask(__name__)

data = {
	"header" : {
		"dominio" : "dom",
		"fecha" : "15-01-2019",
	},
	"alineador" : {
		"eje_delan" : "0.000"
	},
	"suspension" : [
		{
			"ren_izq" : "0.000",
			"peso" : "0.000",
			"ren_der" : "0.000",
		},
		{
			"ren_izq" : "0.000",
			"peso" : "0.000",
			"ren_der" : "0.000",
		}
	],
	"frenos" : [
		{
			"f_izq" : "0.000",
			"res_izq" : "0.000",
			"ov_izq" : "0.000",
			"peso" : "0.000",
			"f_der" : "0.000",
			"res_der" : "0.000",
			"ov_der" : "0.000",	
		},
		{
			"f_izq" : "0.000",
			"res_izq" : "0.000",
			"ov_izq" : "0.000",
			"peso" : "0.000",
			"f_der" : "0.000",
			"res_der" : "0.000",
			"ov_der" : "0.000",	
		},
		{
			"f_izq" : "0.000",
			"res_izq" : "0.000",
			"ov_izq" : "0.000",
			"peso" : "0.000",
			"f_der" : "0.000",
			"res_der" : "0.000",
			"ov_der" : "0.000",	
		},
		{
			"f_izq" : "0.000",
			"res_izq" : "0.000",
			"ov_izq" : "0.000",
			"peso" : "0.000",
			"f_der" : "0.000",
			"res_der" : "0.000",
			"ov_der" : "0.000",	
		}
	],
	"trasero" : {
		"f_izq" : "0.000",
		"f_der" : "0.000"
	},
	"gaseshumos" : {
		"co" : "0.000",
		"hc" : "0.000",
		"med" : "0.000"	
	}
}

@app.route('/')
def main():
	plate = "SLV350"
	s = SI(SI.Chrome)

	login(s)
	gototec(s)
	return render_template(
		"resultados.html",
		data = readdata(s)
	)

def login(s):
	s.get("https://rto.cent.gov.ar/rto")

	login = s.find(SI.By.NAME, "j_username")
	s.write(login, "salvatellih")

	psw = s.find(SI.By.NAME, "j_password")
	s.write(psw, "3")

	button = s.find(SI.By.ID, "submit")
	button.click()

def gototec(s):
	s.get("https://rto.cent.gov.ar/rto/RTO/listaDePlanillas")

	# children of parent of td with innerText = plate
	columns = s.children(s.parent( s.find(SI.By.XPATH, f"//tr//td[text()='{plate}']") ))
	# fecha

	# get all a tags and click the last one
	options = s.findFromElement(columns[-1], SI.By.TAG_NAME, "a", "1-")
	options[-1].click()

	# if last clickable is 'Datos Técnicos', click, else you are already there
	tec = s.find(SI.By.XPATH, "//a/span[@class='externo']/parent::*", "1-")[-1]

	if tec.get_attribute("innerText") == "ir a Datos Técnicos":
		tec.click()

def readdata(s):
	answer = {
		"header" : {},
		"alineador" : {},
		"suspension" : [],
		"frenos" : [],
		"trasero" : {},
		"gaseshumos" : {},
	}
	
	# Tables
	frenos = s.find(SI.By.XPATH, "//table[@class='tabla_ensayo']")
	estacionamiento = s.find(SI.By.ID, "tablaFrenoEstacionario")
	medidas = s.find(SI.By.TAG_NAME, "fieldset")

	ejes = s.findFromElement(frenos, SI.By.XPATH, "/tbody//tr[@class='even' or @class='odd']", "1-4")

	for e in ejes:
		c = s.children(e); a = []

		# make a map from numbers to string indices? 1 => PesoEstatico, ...
		# useful for random generated values too

		# for the 2nd, 3rd, 5th and 6th column, attempt to read the value
		for column in [c[1], c[2], c[4], c[5]]:
			# reach: gets the first input child and reads it
			reach = lambda c, s: s.read(s.findFromElement(c,SI.By.TAG_NAME, "input"))
			a.append(attempt_do(reach, column, s))

		answer.append(a)

	# eje delantero

	
	# fuerza freno estacionamiento
	# co
	# hc
	# opacidad log

	return answer

# Executes the lambda with the arguments, with try except
def attempt_do(f, *args, **kwargs, default = ""):
	try:
		return f(*args, **kwargs)
	except:
		return default

# Inicio del servicio
if __name__ == "__main__":
        app.run("0.0.0.0")