petit script d'actualité (si vous la suivez)
Script python pour générer une attestation de promenade en pdf
Première version très perfectible
- nom, adresse dans le script donc il faut dupliquer le script avec plusieurs personnes à la maison (arlette.py, junior.py, ...)
- que la sortie d'une heure
- on passe 2 paramètres optionnels : premier l'heure et en second la date, deplus peut forcer firefox avec
--ff
ps: existe des extensions navigateur (js) beaucoup plus simples d'utilisation mais nous sommes sous linux
Le pdf est enregistré "normalement" dans Téléchargement de notre home
note: installer
python-selenium
#!/usr/bin/env python
# V2 du 03/11/2020
'''
pacman -S python-selenium --asdeps
pacman -S geckodriver --asdeps # si firefox
'''
import sys
import time
from datetime import datetime
from pathlib import Path
from selenium import webdriver # sudo pacman -S python-selenium --asdeps
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
isFF = False
if "--ff" in sys.argv:
print("want Firefox...")
isFF = True
sys.argv = [i for i in sys.argv if i != "--ff"]
mydate = datetime.now().strftime("%d/%m/%Y")
mn = int(datetime.now().strftime("%M"))
mn += 10
if mn > 59:
mn = 59
myhour = f"{datetime.now().strftime('%H')}:{mn}"
if len(sys.argv) > 1:
if sys.argv[1] == "-h":
print("param 1 : heure")
print("param 2 : date")
quit(0)
myhour = sys.argv[1]
if ':' not in myhour:
myhour = f"{myhour}:00"
if len(sys.argv) > 2:
mydate = sys.argv[2]
print("date:", mydate)
print("heure:", myhour)
datas = {
"field-firstname": "prenom",
"field-lastname": "Nom",
"field-birthday": "04/06/2001",
"field-placeofbirth": "Nativité",
"field-address": "10, quai des linux",
"field-city": "Paris",
"field-zipcode": "75001",
"field-datesortie": mydate,
"field-heuresortie": myhour,
}
downloads_path = Path.home() / "Téléchargements"
try:
if isFF:
raise ValueError("not use chromium")
driver = webdriver.Chrome() # run chromium
except:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.preferences.instantApply", True)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, attachment/pdf, application/octet-stream, application/binary")
fp.set_preference("browser.helperApps.showOpenOptionForPdfJS", False)
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.showWhenStarting", False)
#fp.set_preference("browser.download.folderList", 2) # custom rep
#fp.set_preference("browser.download.dir", str(downloads_path)) # ff supporte pas utf8 ?
driver = webdriver.Firefox(firefox_profile=fp) # run firefox
ds = mydate.split('/')
datas["field-datesortie"] = f"{ds[2]}-{ds[1]}-{ds[0]}"
print("Firefox utilise une date \"en\" ! :", datas["field-datesortie"])
driver.get('https://media.interieur.gouv.fr/deplacement-covid-19/')
btnSend = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'generate-btn')))
# remplir formulaire
for key, value in datas.items():
#print(key, '->', value)
element = driver.find_element_by_id(key)
if element:
element.send_keys(value)
# formulaire : la raison
element = driver.find_element_by_id('checkbox-sport_animaux')
element.click()
# on click sur générer ...
btnSend.click()
time.sleep(2)
# pas fermer navigateur si Firefox car bug sauvegarde auto ...
if not isFF:
#pass
driver.quit()
print("\n", downloads_path, ":")
for afile in downloads_path.glob('attestation-*.pdf'):
print(afile)
print("\nls -lt", f"{downloads_path}/attestation-*.pdf")