create class jsonfile

This commit is contained in:
vincent 2018-08-23 21:12:44 +02:00
parent c12341cd4b
commit 98e07cda07

View File

@ -90,9 +90,8 @@ class emmisionGUI(Frame):
class Interface:
def __init__(self):
self.datafilepath=os.path.dirname(os.path.realpath(__file__))+"/chaine.json"
self.data = load_jsonfile(self.datafilepath)
def __init__(self,data):
self.data = data
self.fenetre = Tk()
self.fenetre.title("recherche de chaine")
self.value = StringVar()
@ -131,11 +130,11 @@ class Interface:
try:
strlink=geturlprogrammetv(self.data[value])
link= Labbelink(self.resultframe.interior,self.data[value],strlink)
strlink=geturlprogrammetv(self.data.get_chaine(value))
link= Labbelink(self.resultframe.interior,self.data.get_chaine(value),strlink)
link.pack()
self.resultframe.update()
print(self.data[value])
print(self.data.get_chaine(value))
emision=parse_emmission(strlink)
if emision:
if emision == "can't find show":
@ -160,8 +159,7 @@ class Interface:
def click_update(self):
parsechaine(self.datafilepath)
self.data = load_jsonfile(self.datafilepath)
self.data.parsechaine()
labelupdate = Label(self.resultframe, text="update chaine done"+"\r")
labelupdate.pack()
@ -169,17 +167,48 @@ class Interface:
webbrowser.open_new(link)
class JSONfile:
def __init__(self,filename):
import os,json
self.datafilepath=os.path.dirname(os.path.realpath(__file__))+"/"+filename
try:
with open(self.datafilepath, 'r', encoding='utf-8') as f:
self.data=json.load(f)
except FileNotFoundError:
self.parsechaine()
def get_chaine(self,number):
return self.data[number]
def parsechaine(self):
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):
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)
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))
self.data=liste_chaine
with open(self.datafilepath, 'w', encoding='utf-8') as f:
json.dump(liste_chaine, f, indent=4)
def __repr__(self):
return str(self.data)
def RepresentsInt(s):
try:
int(s)
@ -188,33 +217,7 @@ def RepresentsInt(s):
return False
except TypeError:
return False
def parsechaine(file):
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")
@ -241,7 +244,7 @@ def parse_emmission(URL):
casting=divcasting.find_all(href=re.compile("biographie"))
count=0
for actor in casting:
casting[i]=actor.text
casting[count]=actor.text
count+=1
divsynopsis=parse.select_one(".episode-synopsis")
img=divsynopsis.find_next('img')['data-src']
@ -259,9 +262,7 @@ def remove_first_space (string):
return string[space_number:]
def cli(num):
datafilepath=os.path.dirname(os.path.realpath(__file__))+"/chaine.json"
data = load_jsonfile(datafilepath)
def cli(num,data):
print(num)
try:
print(data[num])
@ -283,11 +284,13 @@ def cli(num):
else:
print("pas de connection internet impossible de determiner l'émission du soir")
print("")
data=JSONfile("chaine.json")
if len(sys.argv) > 1:
for i in sys.argv[1:]:
if i =="update":
parsechaine(os.path.dirname(os.path.realpath(__file__))+"/chaine.json")
for arg in sys.argv[1:]:
if arg =="update":
data.parsechaine()
else:
cli(i)
cli(arg,data.data)
else:
Interface().mainloop()
Interface(data).mainloop()