Instantly share code, notes, and snippets.
Created
January 30, 2017 14:18
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save 0xded093/5e0357333c3a0d796f0974ebbd1f21aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: latin-1 -*- | |
import os | |
import platform | |
import subprocess | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
import time | |
from datetime import date | |
import getpass | |
import re | |
import urllib2 | |
import argparse | |
# avvio PhantomJS | |
sys = platform.system() | |
FNULL = open(os.devnull, 'w') | |
if sys == 'Windows': | |
subprocess.Popen('c:\\phantomjs.exe --webdriver=4444 --ssl-protocol=any --ssl-ciphers=any --ignore-ssl-errors=true', stdout=FNULL, stderr=subprocess.STDOUT, shell=True) | |
clearing = 'cls' | |
pass | |
elif sys == 'Darwin': | |
subprocess.Popen('phantomjs --webdriver=4444 --ssl-protocol=any --ssl-ciphers=any --ignore-ssl-errors=true', stdout=FNULL, stderr=subprocess.STDOUT, shell=True) | |
clearing = 'clear' | |
pass | |
# argparse | |
parser = argparse.ArgumentParser(description='Navision Tool') | |
parser.add_argument('-c','--commessa', help='Commesssa-Fase', required=True) | |
parser.add_argument('-a','--anno', help='Anno', required=True) | |
parser.add_argument('-m','--mese', help='Mese', required=True) | |
parser.add_argument('-g','--giorni', help='Giorni', required=True) | |
parser.add_argument('-t','--timeout', help='Connection Timeout', required=False) | |
args = vars(parser.parse_args()) | |
username = raw_input("Username:"); | |
passphrase = getpass.getpass("Password:"); | |
pagamento = [] | |
user_agent = ( | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + | |
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36" | |
) | |
dcap = dict(DesiredCapabilities.PHANTOMJS) | |
dcap["phantomjs.page.settings.userAgent"] = user_agent | |
if args['timeout']: | |
timeout = int(args['timeout']) | |
else: | |
timeout = 2 | |
def Newlines(): | |
time.sleep(timeout) | |
newline = driver.find_element_by_id("listaRigheOdT_btnNew") | |
newline.click() | |
def Commessa(): | |
time.sleep(timeout) | |
findcom = driver.find_element_by_id("btnRicercaCommessa") | |
findcom.click() | |
time.sleep(timeout) | |
searchcom = driver.find_element_by_id("searchCodCommessa") | |
searchcom.send_keys(args['commessa'].split('-')[0]) | |
searchfase = driver.find_element_by_id("searchFase") | |
searchfase.send_keys(args['commessa'].split('-')[1]) | |
filtro = driver.find_element_by_id("listaCommesse_btnFiltra") | |
filtro.click() | |
time.sleep(timeout) | |
# trova id con regex | |
src = driver.page_source | |
p = re.compile(ur'(?s)(?<="listaCommesse_Key">).*?(?=</td><td role="gridcell")') | |
id_commessa = re.search(p, src).group() | |
selectcom = driver.find_element_by_id(id_commessa) | |
selectcom.click() | |
time.sleep(timeout) | |
def Datafunc(): | |
time.sleep(timeout) | |
selectdata = driver.find_element_by_id("Data") | |
selectdata.clear() | |
selectdata.send_keys(dataita) | |
km = driver.find_element_by_id("KmPercorsi") | |
km.click() | |
def Savefunc(): | |
# fix calcolo ore ####################################### | |
calc_ore = driver.find_element_by_id("LuogoDestinazine") | |
calc_ore.click() | |
######################################################### | |
saveriga = driver.find_element_by_id("btnSalva") | |
saveriga.click() | |
time.sleep(timeout) | |
#driver.save_screenshot('out.png'); | |
def WeekDay(): | |
time.sleep(timeout) | |
Newlines() | |
pagamento.append(19.80) | |
print "[+] "+dataita+" - Feriale - 00:00-09:00 - $$$ => ",sum(pagamento) | |
Datafunc() | |
Commessa() | |
inizio = driver.find_element_by_id("tini") | |
inizio.send_keys("00:00") | |
fine = driver.find_element_by_id("tfin") | |
fine.send_keys("09:00") | |
Savefunc() | |
Newlines() | |
pagamento.append(2.20) | |
print "[+] "+dataita+" - Feriale - 13:00-14:00 - $$$ => ",sum(pagamento) | |
Datafunc() | |
Commessa() | |
inizio = driver.find_element_by_id("tini") | |
inizio.send_keys("13:00") | |
fine = driver.find_element_by_id("tfin") | |
fine.send_keys("14:00") | |
Savefunc() | |
Newlines() | |
pagamento.append(13.20) | |
print "[+] "+dataita+" - Feriale - 18:00-24:00 - $$$ => ",sum(pagamento) | |
Datafunc() | |
Commessa() | |
inizio = driver.find_element_by_id("tini") | |
inizio.send_keys("18:00") | |
fine = driver.find_element_by_id("tfin") | |
fine.send_keys("00:00") | |
Savefunc() | |
def WeekendDay(): | |
time.sleep(timeout) | |
Newlines() | |
pagamento.append(52.80) | |
print "[+] "+dataita+" - Festivo - 00:00-24:00 - $$$ => ",sum(pagamento) | |
Datafunc() | |
Commessa() | |
inizio = driver.find_element_by_id("tini") | |
inizio.send_keys("00:00") | |
fine = driver.find_element_by_id("tfin") | |
fine.send_keys("00:00") | |
Savefunc() | |
time.sleep(timeout) | |
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.PHANTOMJS.copy()) | |
driver.set_window_size(1600, 900) | |
print "------------------------------------------------------" | |
#driver.get("http://repubblica.it") | |
driver.get("https://navisionweb.lutech.it") | |
print "Opening "+driver.current_url | |
print "------------------------------------------------------" | |
time.sleep(1) | |
#WebDriverWait(driver, 10).until(EC.title_contains("Log On")) | |
os.system(clearing) | |
# login | |
login = driver.find_element_by_id("UserName") | |
login.send_keys(username) | |
password = driver.find_element_by_id("Password") | |
password.send_keys(passphrase) | |
password.send_keys(Keys.RETURN) | |
# Gestione consuntivazione | |
#WebDriverWait(driver, 10).until(EC.title_contains("Home Page")) | |
menu1 = driver.find_element_by_id("menu1") | |
menu1.click() | |
# nuovo rapportino | |
time.sleep(timeout) | |
newrap = driver.find_element_by_id("listaOdT_btnNew") | |
newrap.click() | |
for i in args['giorni'].split(','): | |
datatoday = date(int(args['anno']), int(args['mese']), int(i)) | |
dataita = datatoday.strftime("%d/%m/%y"); | |
if (datatoday.weekday() !=5 and datatoday.weekday() !=6): | |
WeekDay() | |
else: | |
WeekendDay() | |
## Cambia stato | |
rapportino = driver.find_element_by_id("OdTNo_h").get_attribute('value') | |
## choose | |
yes = set(['yes','y', 'ye', '']) | |
no = set(['no','n']) | |
print "\n" | |
print "Rimborso : $",sum(pagamento) | |
print "\n" | |
choice = raw_input("Fatto, vuoi mandare in approvazione? [yes/no]").lower() | |
if choice in yes: | |
goback = driver.find_element_by_id("btnBack") | |
goback.click() | |
time.sleep(timeout) | |
select_rap = driver.find_element_by_id(rapportino) | |
select_rap.click() | |
approve = driver.find_element_by_id("btn_CambiaStato") | |
approve.click() | |
print "------------------------------------------------" | |
print "Ore inserite nel "+rapportino+" e mandate in approvazione" | |
print "------------------------------------------------" | |
pass | |
elif choice in no: | |
print "------------------------------------------------" | |
print "Ore inserite nel "+rapportino | |
print "------------------------------------------------" | |
pass | |
else: | |
print "Rispondi con 'yes' or 'no'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment