cave/chaineTV.py

109 lines
2.9 KiB
Python
Raw Normal View History

2018-01-21 20:15:24 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
2018-01-22 18:11:29 +00:00
from bs4 import BeautifulSoup
import urllib.request
import json
2018-01-22 19:33:40 +00:00
import sys
2018-01-21 20:15:24 +00:00
2018-01-22 20:46:53 +00:00
def click():
print(value.get())
2018-01-22 18:11:29 +00:00
try:
2018-01-22 20:46:53 +00:00
label2["text"] += data[value.get()]+"\r"
2018-01-22 19:33:40 +00:00
print(data[value.get()])
2018-01-22 18:11:29 +00:00
except KeyError:
2018-01-22 20:46:53 +00:00
print("numero de chaine inconnue")
label2["text"] += "numero de chaine inconnue"+"\r"
2018-01-22 20:38:57 +00:00
value.set("")
2018-01-21 20:15:24 +00:00
2018-01-22 20:46:53 +00:00
def click_reset():
print("exit")
2018-01-21 20:15:24 +00:00
fenetre.quit
2018-01-22 20:46:53 +00:00
label2["text"] = ""
2018-01-21 20:15:24 +00:00
fenetre.mainloop
2018-01-22 18:11:29 +00:00
def load_jsonfile(file):
with open(file, 'r', encoding='utf-8') as f:
return json.load(f)
2018-01-22 19:33:40 +00:00
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
except TypeError:
return False
def parsechaine():
2018-01-22 20:46:53 +00:00
URL = 'https://fr.wikipedia.org/wiki/Liste_des_cha%C3%AEnes_de_Canal'
liste_chaine = {}
2018-01-22 19:33:40 +00:00
response = urllib.request.urlopen(URL)
html = response.read()
2018-01-22 20:46:53 +00:00
parse = BeautifulSoup(html,"html.parser")
2018-01-22 19:33:40 +00:00
for item in parse.find_all('table'):
2018-01-22 20:46:53 +00:00
if (item.get("class") == ['wikitable'] or item.get("class") == ['wikitable', 'sortable']):
2018-01-22 19:33:40 +00:00
for tr in item.find_all('tr'):
2018-01-22 20:46:53 +00:00
firstTD = tr.find()
num = firstTD.string
2018-01-22 19:33:40 +00:00
if RepresentsInt(num):
2018-01-22 20:46:53 +00:00
#print(num)
2018-01-22 19:33:40 +00:00
if RepresentsInt(firstTD.find_next().string):
2018-01-22 20:46:53 +00:00
#print(firstTD.find_next().find_next().string)
liste_chaine[num] = firstTD.find_next().find_next().string
2018-01-22 19:33:40 +00:00
else:
2018-01-22 20:46:53 +00:00
#print(firstTD.find_next().string)
liste_chaine[num] = firstTD.find_next().string
2018-01-22 19:33:40 +00:00
print(json.dumps(liste_chaine, indent=4))
with open('chaine.json', 'w', encoding='utf-8') as f:
json.dump(liste_chaine, f, indent=4)
2018-01-22 20:46:53 +00:00
data = load_jsonfile('chaine.json')
label2["text"] += "update chaine done"+"\r"
2018-01-22 19:33:40 +00:00
def enter(evt):
click()
2018-01-22 19:56:35 +00:00
def eventreset(evt):
click_reset()
2018-01-22 18:11:29 +00:00
2018-01-22 20:46:53 +00:00
data = load_jsonfile('chaine.json')
2018-01-21 20:15:24 +00:00
fenetre = Tk()
2018-01-22 19:56:35 +00:00
fenetre.title("recherche de chaine")
2018-01-21 20:15:24 +00:00
value = StringVar()
2018-01-22 19:42:03 +00:00
2018-01-22 20:46:53 +00:00
if len(sys.argv) > 1:
arg = True
2018-01-22 19:42:03 +00:00
else:
2018-01-22 19:33:40 +00:00
value.set("")
2018-01-22 20:46:53 +00:00
arg = False
2018-01-22 19:33:40 +00:00
2018-01-22 20:46:53 +00:00
label = Label(fenetre, text="entrer numero de chaine")
2018-01-21 20:15:24 +00:00
entree = Entry(fenetre, textvariable=value, width=30)
2018-01-22 20:46:53 +00:00
frame = Frame(fenetre)
label2 = Label(fenetre, text="")
bouton_update_base = Button(fenetre, text="update la base de chaine", command=parsechaine)
bouton = Button(frame, text="OK", command=click)
reset = Button(frame, text="reset", command=click_reset)
2018-01-21 20:15:24 +00:00
label.pack()
entree.pack()
2018-01-22 19:33:40 +00:00
entree.focus_set()
2018-01-21 20:15:24 +00:00
frame.pack()
2018-01-22 20:46:53 +00:00
bouton.pack(side=LEFT)
reset.pack(side=RIGHT)
2018-01-21 20:15:24 +00:00
label2.pack()
2018-01-22 19:33:40 +00:00
bouton_update_base.pack()
2018-01-22 20:46:53 +00:00
fenetre.bind("<Key-Return>", enter)
fenetre.bind("<Key-Escape>", eventreset)
if arg == True:
2018-01-22 19:42:03 +00:00
for i in sys.argv[1:]:
value.set(i)
click()
value.set("")
2018-01-22 19:33:40 +00:00
2018-01-21 20:15:24 +00:00
fenetre.mainloop()