2018-08-17 18:31:23 +00:00
|
|
|
from tkinter import *
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import unicodedata
|
2018-08-18 13:31:37 +00:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
class Labbelink (Label):
|
|
|
|
|
|
|
|
def __init__(self,parent,text,link):
|
|
|
|
Label.__init__(self,parent,text=text,fg="blue",cursor="hand2")
|
|
|
|
self.link=link
|
|
|
|
self.bind("<Button-1>",self._openlink)
|
|
|
|
|
|
|
|
def _openlink(self,event):
|
|
|
|
webbrowser.open(self.link)
|
2018-08-17 18:31:23 +00:00
|
|
|
|
|
|
|
class Interface:
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.datafilepath=os.path.dirname(os.path.realpath(__file__))+"/chaine.json"
|
|
|
|
self.data = load_jsonfile(self.datafilepath)
|
|
|
|
self.fenetre = Tk()
|
|
|
|
self.fenetre.title("recherche de chaine")
|
|
|
|
self.value = StringVar()
|
|
|
|
self.label = Label(self.fenetre, text="entrer numero de chaine")
|
|
|
|
self.entree = Entry(self.fenetre, textvariable=self.value, width=30)
|
|
|
|
self.frame = Frame(self.fenetre)
|
2018-08-18 13:31:37 +00:00
|
|
|
self.resultframe = Frame(self.fenetre)
|
2018-08-17 18:31:23 +00:00
|
|
|
self.bouton_update_base = Button(
|
|
|
|
self.fenetre, text="update la base de chaine", command=self.click_update)
|
|
|
|
self.bouton = Button(self.frame, text="OK", command=self.click)
|
|
|
|
self.reset = Button(self.frame, text="reset", command=self.click_reset)
|
|
|
|
self.label.pack()
|
|
|
|
self.entree.pack()
|
|
|
|
self.entree.focus_set()
|
2018-08-18 13:31:37 +00:00
|
|
|
self.resultframe.pack()
|
2018-08-17 18:31:23 +00:00
|
|
|
self.frame.pack()
|
|
|
|
self.bouton.pack(side=LEFT)
|
|
|
|
self.reset.pack(side=RIGHT)
|
|
|
|
self.bouton_update_base.pack()
|
|
|
|
self.fenetre.bind("<Key-Return>", self.enter)
|
|
|
|
self.fenetre.bind("<Key-Escape>", self.eventreset)
|
|
|
|
|
|
|
|
|
|
|
|
def enter(self,evt):
|
|
|
|
self.click()
|
|
|
|
|
|
|
|
def eventreset(self,evt):
|
|
|
|
self.click_reset()
|
|
|
|
|
|
|
|
def mainloop(self):
|
|
|
|
self.fenetre.mainloop()
|
|
|
|
|
|
|
|
def click(self):
|
|
|
|
print(self.value.get())
|
2018-08-18 13:31:37 +00:00
|
|
|
labellink = Label(self.resultframe, text="")
|
|
|
|
|
2018-08-17 18:31:23 +00:00
|
|
|
try:
|
2018-08-18 13:31:37 +00:00
|
|
|
link= Labbelink(self.resultframe,self.data[self.value.get()],geturlprogrammetv(self.data[self.value.get()]))
|
|
|
|
link.pack()
|
2018-08-17 18:31:23 +00:00
|
|
|
print(self.data[self.value.get()])
|
|
|
|
except KeyError:
|
|
|
|
print("numero de chaine inconnue")
|
2018-08-18 13:31:37 +00:00
|
|
|
unknow=Label(self.resultframe, text="numero de chaine inconnue")
|
|
|
|
unknow.pack()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#self.label2["text"] += geturlprogrammetv(self.data[self.value.get()])
|
|
|
|
|
2018-08-17 18:31:23 +00:00
|
|
|
self.value.set("")
|
|
|
|
|
|
|
|
def click_reset(self):
|
|
|
|
print("reset")
|
2018-08-18 13:31:37 +00:00
|
|
|
for child in self.resultframe.winfo_children():
|
|
|
|
child.destroy()
|
|
|
|
|
2018-08-17 18:31:23 +00:00
|
|
|
|
|
|
|
def click_update(self):
|
|
|
|
parsechaine(self.datafilepath)
|
|
|
|
self.data = load_jsonfile(self.datafilepath)
|
2018-08-18 13:31:37 +00:00
|
|
|
labelupdate = Label(self.resultframe, text="update chaine done"+"\r")
|
|
|
|
labelupdate.pack()
|
|
|
|
|
|
|
|
def _openlink(self,link):
|
|
|
|
webbrowser.open_new(link)
|
|
|
|
|
2018-08-17 18:31:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_jsonfile(file):
|
|
|
|
try:
|
|
|
|
with open(file, 'r', encoding='utf-8') as f:
|
|
|
|
return json.load(f)
|
|
|
|
except FileNotFoundError:
|
|
|
|
parsechaine(file)
|
|
|
|
with open(file, 'r', encoding='utf-8') as f:
|
|
|
|
return json.load(f)
|
|
|
|
|
|
|
|
def RepresentsInt(s):
|
|
|
|
try:
|
|
|
|
int(s)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def parsechaine(file):
|
|
|
|
import urllib.request
|
|
|
|
URL = 'https://fr.wikipedia.org/wiki/Liste_des_cha%C3%AEnes_de_Canal'
|
|
|
|
liste_chaine = {}
|
|
|
|
response = urllib.request.urlopen(URL)
|
|
|
|
html = response.read()
|
|
|
|
parse = BeautifulSoup(html,"html.parser")
|
|
|
|
for item in parse.find_all('table'):
|
|
|
|
if (item.get("class") == ['wikitable'] or item.get("class") == ['wikitable', 'sortable']):
|
|
|
|
for tr in item.find_all('tr'):
|
|
|
|
|
|
|
|
firstTD = tr.find()
|
|
|
|
num = firstTD.text
|
|
|
|
#print(num)
|
|
|
|
if RepresentsInt(num):
|
|
|
|
|
|
|
|
if RepresentsInt(firstTD.find_next().string):
|
|
|
|
#print(firstTD.find_next().find_next().text)
|
|
|
|
liste_chaine[int(num)] = firstTD.find_next().find_next().text
|
|
|
|
else:
|
|
|
|
#print(firstTD.find_next().string)
|
|
|
|
liste_chaine[int(num)] = firstTD.find_next().text
|
|
|
|
print(json.dumps(liste_chaine, indent=4))
|
|
|
|
with open(file, 'w', encoding='utf-8') as f:
|
|
|
|
json.dump(liste_chaine, f, indent=4)
|
|
|
|
|
|
|
|
def geturlprogrammetv(strsearch):
|
|
|
|
strsearch=unicodedata.normalize('NFD', strsearch).encode('ascii', 'ignore')
|
|
|
|
strsearch=strsearch.decode("utf-8")
|
|
|
|
strsearch=strsearch.replace(" ","+")
|
|
|
|
return "https://www.programme-tv.net/rechercher?q="+strsearch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cli(num):
|
|
|
|
datafilepath=os.path.dirname(os.path.realpath(__file__))+"/chaine.json"
|
|
|
|
data = load_jsonfile(datafilepath)
|
|
|
|
print(num)
|
|
|
|
try:
|
|
|
|
print(data[num])
|
|
|
|
except KeyError:
|
|
|
|
print("numero de chaine inconnue")
|
|
|
|
return
|
|
|
|
print (geturlprogrammetv(data[num]))
|
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
for i in sys.argv[1:]:
|
|
|
|
if i =="update":
|
|
|
|
parsechaine(os.path.dirname(os.path.realpath(__file__))+"/chaine.json")
|
|
|
|
else:
|
|
|
|
cli(i)
|
|
|
|
else:
|
|
|
|
interface = Interface()
|
|
|
|
interface.value.set("")
|
|
|
|
interface.mainloop()
|