Skip to content

Instantly share code, notes, and snippets.

@0xded093
Created February 24, 2016 13:55
Show Gist options
  • Save 0xded093/583cd7bdd567f819d2ba to your computer and use it in GitHub Desktop.
Save 0xded093/583cd7bdd567f819d2ba to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
##################################################################################################################################################################################
# versione con phantomjs - phantomjs --webdriver=4444 --ssl-protocol=any --ssl-ciphers=any --ignore-ssl-errors=true --webdriver-logfile=./ghost.log --webdriver-loglevel=INFO #
# http://txt2re.com/index-python.php3?s=40;XLBMAACJ/0sxNjAwNDIAAIeAOAEAAIkEMTQ1OQ==9;5679664040;&-72&-74&58&53&13&8&-84&6&-68&10&2&-87&-88&12&-85&5&-86 #
# #
# #
##################################################################################################################################################################################
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 smtplib
# avvio PhantomJS
sys = platform.system()
FNULL = open(os.devnull, 'w')
#subprocess.call('./phantomjs --webdriver=4444 --ssl-protocol=any --ssl-ciphers=any --ignore-ssl-errors=true', shell=True)
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)
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)
pass
username = raw_input("Username:");
passphrase = getpass.getpass("Password:");
anno = input("Inserisci l'anno:")
mese = input("Inserisci il mese:")
giorno = input("Inserisci i giorni (separati da virgola):")
presidio_fas = raw_input("Inserisci la fase:")
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
def Newlines():
time.sleep(2)
newline = driver.find_element_by_id("listaRigheOdT_btnNew")
newline.click()
def Commessa():
time.sleep(2)
findcom = driver.find_element_by_id("btnRicercaCommessa")
findcom.click()
time.sleep(2)
searchcom = driver.find_element_by_id("searchCodCommessa")
searchcom.send_keys("K160042")
searchfase = driver.find_element_by_id("searchFase")
searchfase.send_keys(presidio_fas)
filtro = driver.find_element_by_id("listaCommesse_btnFiltra")
filtro.click()
time.sleep(2)
# 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(2)
def Datafunc():
time.sleep(2)
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(2)
#driver.save_screenshot('out.png');
def WeekDay():
time.sleep(2)
Newlines()
print "Aggiungo riga 1 per il giorno "+dataita+" - feriale"
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()
print "OK"
Newlines()
print "Aggiungo riga 2 per il giorno "+dataita+" - feriale"
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()
print "OK"
Newlines()
print "Aggiungo riga 3 per il giorno "+dataita+" - feriale"
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()
print "OK"
def WeekendDay():
time.sleep(2)
Newlines()
print "Aggiungo unica riga per il giorno "+dataita+" - festivo"
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()
print "OK"
time.sleep(2)
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 "Apro Navision - "+driver.current_url
print "------------------------------------------------"
#WebDriverWait(driver, 10).until(EC.title_contains("Log On"))
# 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(2)
newrap = driver.find_element_by_id("listaOdT_btnNew")
newrap.click()
for i in giorno:
datatoday = date(anno, mese, 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 "------------------------------------------------"
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(2)
select_rap = driver.find_element_by_id(rapportino)
select_rap.click()
approve = driver.find_element_by_id("btn_CambiaStato")
approve.click()
print "------------------------------------------------"
print "Tutte le ore sono state inserite nel "+rapportino+" e sono state mandate in approvazione"
print "------------------------------------------------"
pass
elif choice in no:
print "------------------------------------------------"
print "Tutte le ore sono state inserite nel "+rapportino
print "------------------------------------------------"
pass
else:
print "Rispondi con 'yes' or 'no'"
#choice2 = raw_input("Fatto, vuoi mandare una mail al tuo responsabile? [yes/no]").lower()
#
#if choice2 in yes:
# # mail al responsabile
# src_resp = driver.page_source
#
# # find name
# regex_resp = re.compile(ur'(?<= </span>\n </td>\n <td>\n <span class="editor-field">\n )(.*)(?=\n)', re.DOTALL)
# name_resp = re.search(regex_resp, src_resp).group()
#
# # parse name
# fl = name_resp.split()
# first_name = fl[1]
# surname = fl[0]
# first_initial = first_name[0]+"."
#
# mail_resp = first_initial+surname+"@lutech.it"
# username_outlook = raw_input("Username Outlook:");
# password_outlook = getpass.getpass("Password Outlook:");
#
# fromaddr = username_outlook
# toaddrs = mail_resp
# subject = "Consuntivazione fatta"
# msg = ("From: %s\r\nTo: %s\r\nMIME-Version: 1.0\r\nContent-type: text/html\r\nSubject: %s\r\n\r\n"
# % (fromaddr, toaddrs, subject))
#
# text = 'Ciao,<br>ho fatto la consuntivazione per la reperibilita, il rapportino '+rapportino+' e in approvazione<br>Grazie,<br>Ciao'
#
# msg = msg + text
#
# # Send mail
# server = smtplib.SMTP('smtp.office365.com:587')
# server.starttls()
# server.login(username_outlook,password_outlook)
# server.sendmail(fromaddr, toaddrs, msg)
# server.quit()
# print "Mail inviata a "+name_resp
# pass
#elif choice in no:
# print OK
#else:
# print "Rispondi con 'yes' or 'no'"
#############################################################################
## kill phantom
#if sys == 'Windows':
# subprocess.Popen('Taskkill /F /IM phantomjs.exe', stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
# pass
#elif sys == 'Darwin':
# subprocess.Popen('killall phantomjs', stdout=FNULL, stderr=subprocess.STDOUT, shell=True)
# pass
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment