bonjour a tous,
je tenais a vous montrer l'avancé du script en python
attention pour le tester il vous faudra la bibliothèque
BeautifulSoup 4 ainsi que
python 3
normalement les changements sur le site ne devrais pas affecter le bon fonctionnement de ce script (sauf pour la partie 8 heures)
le script actuel retourne des fichiers texte dans le répertoire
repsauv, ses fichiers serviront de base de donnée a un futur meteo.lua
#! /usr/bin/python3
# -*- coding: utf-8 -*-
# Par Didier-T Forum Ubuntu.fr
import urllib.request, os, time, re
from bs4 import BeautifulSoup
adressWeb="http://www.accuweather.com/fr/fr/argenteuil/133593/current-weather/133593"
homedir = os.path.expanduser('~')
repsauv= homedir+"/essais"
def traitement(adress, fichier, pat):
url=urllib.request.Request(adress)
currentCond=urllib.request.urlopen(url)
soup = BeautifulSoup(currentCond)
currentCond.close()
fiche = open(fichier, "w")
#si autres que huit heures
if re.search("huitHeures", fichier) is None:
#récupération date + historique
if re.search("Jour", fichier) is not None or re.search("CC", fichier) is not None:
histo=soup.find(id="feature-history")
#récupération date
date=histo.select("[class~=lt]")
for d in date:
jour=re.compile("^.* - (.*)/(.*)/(.*)").search(d.get_text())
fiche.write("jour: "+jour.group(1)+"\n")
fiche.write("mois: "+jour.group(2)+"\n")
fiche.write("année: "+jour.group(3)+"\n")
#récupération historique
hi=histo.select("[class~=hi]")
for v in hi:
hivalue=v.get_text().split("\n")
fiche.write("maxjour: "+hivalue[2]+"\n")
fiche.write("maxnormal: "+hivalue[3]+"\n")
fiche.write("maxrecord: "+hivalue[4]+"\n")
fiche.write("max1ans: "+hivalue[5]+"\n")
lo=histo.select("[class~=last]")
for v in lo:
lovalue=v.get_text().split("\n")
fiche.write("minjour: "+lovalue[2]+"\n")
fiche.write("minnormal: "+lovalue[3]+"\n")
fiche.write("minrecord: "+lovalue[4]+"\n")
fiche.write("min1ans: "+lovalue[5]+"\n")
#localisation si condition courante
if re.search("CC", fichier) is not None:
ville=re.compile("city: '(.*)',").search(str(soup)).group(1)
longi=re.compile("lon: '(.*)',").search(str(soup)).group(1)
lat=re.compile("lat: '(.*)',").search(str(soup)).group(1)
fiche.write("ville: "+ville+"\n")
fiche.write("lat: "+lat+"\n")
fiche.write("long: "+longi+"\n")
# recherche vent
current_balise=soup.find(id="details")
ventd=re.compile("var d = '(.*)';").search(str(current_balise)).group(1).replace("W", "O")
ventf=re.compile("var s = '(.*)km/h';").search(str(current_balise)).group(1)
fiche.write("direction: "+ventd+"\n")
fiche.write("vitesse: "+ventf+"\n")
#recherche icone
icone=current_balise.select("[class~=icon]")
i=0
for ic in icone:
i=i+1
fiche.write("icone"+str(i)+": "+str(ic).replace('<div class="icon i-', '').replace('"></div>', '')+"\n")
#info conditions générales
cc=current_balise.find_all('li')
for inf in cc:
inf=inf.get_text().replace(" ", "").replace("\n", "")
if re.search(":", inf) is not None:
fiche.write(inf+"\n")
else:
table=soup.find_all('table')
# fiche.write(table[0].prettify())
#recherche icones huit heures
icone=table[0].find_all('div')
i=0
for ic in icone:
i=i+1
fiche.write("icone"+str(i)+": "+str(ic).replace('<div class="icon i-', '').replace('"></div>', '')+"\n")
if i == 8:
break
#donée huit heures
tablevalue=table[0].get_text().split("\n")
i=0
for v in tablevalue:
i=i+1
#pour le débug
# fiche.write(str(i)+" "+v+"\n")
if i >= 18 and i <= 25:
x=x+1
fiche.write("heure"+str(x)+": "+v+"\n")
elif i >= 57 and i <= 64:
x=x+1
fiche.write("temp"+str(x)+": "+v+"\n")
elif i >= 68 and i <= 75:
x=x+1
fiche.write("res"+str(x)+": "+v+"\n")
elif i >= 79 and i <= 86:
x=x+1
fiche.write("humid"+str(x)+": "+v+"\n")
elif i >= 105 and i <= 112:
x=x+1
fiche.write("probpluie"+str(x)+": "+v+"\n")
elif i >= 116 and i <= 123:
x=x+1
fiche.write("probneige"+str(x)+": "+v+"\n")
elif i >= 127 and i <= 134:
x=x+1
fiche.write("probverglas"+str(x)+": "+v+"\n")
elif i >= 153 and i <= 160:
x=x+1
fiche.write("vent"+str(x)+": "+v+"\n")
elif i >= 164 and i <= 171:
x=x+1
fiche.write("uv"+str(x)+": "+v+"\n")
elif i >= 175 and i <= 182:
x=x+1
fiche.write("couvnuage"+str(x)+": "+v+"\n")
elif i >= 186 and i <= 193:
x=x+1
fiche.write("rosée"+str(x)+": "+v+"\n")
else :
x=0
fiche.close()
# Mise en tache de fond
def demonise(entete, id1, id2, huitHeures="non", nbJour=1, interval=5):
while 1 :
debut=time.time()
traitement(ent+nb1+'/current-weather/'+nb2, repsauv+"/CC", '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
if huitHeures=="oui":
traitement(ent+nb1+'/hourly-weather-forecast/'+nb2, repsauv+"/huitHeures", '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
x=0
while x<nbJour:
x=x+1
traitement(ent+nb1+'/daily-weather-forecast/'+nb2+"?day="+str(x), repsauv+"/Jour"+str(x), '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
traitement(ent+nb1+'/morning-weather-forecast/'+nb2+"?day="+str(x), repsauv+"/Matin"+str(x), '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
traitement(ent+nb1+'/afternoon-weather-forecast/'+nb2+"?day="+str(x), repsauv+"/AM"+str(x), '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
traitement(ent+nb1+'/evening-weather-forecast/'+nb2+"?day="+str(x), repsauv+"/Soiree"+str(x), '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
traitement(ent+nb1+'/overnight-weather-forecast/'+nb2+"?day="+str(x), repsauv+"/Nuit"+str(x), '<!-- /#detail-tab-buttons.detail-tab-buttons -->')
fin=time.time()
print("temps de mise a jour",fin-debut)
time.sleep(60*interval)
nb1=re.findall('\d+', adressWeb)[0]
nb2=re.findall('\d+', adressWeb)[1]
ent=adressWeb[:adressWeb.find(nb1)]
demonise(ent, nb1, nb2, "oui", 5, 5)
bon j'ai encore un peut de ménage a faire dedans 😛