import DuckDuckGoImages as ddg
import os
import shutil
with open('speech.txt','r') as speech:
qq = speech.readlines()
html = ''
html = '''
Booklet
'''
if os.path.isdir('./images/') is True:
shutil.rmtree('./images/')
os.mkdir('./images/')
for q in qq:
if q == '''''':
continue
qDDG = q.replace(' ','+')
qBinded = q.replace(' ','')
print(qDDG)
qBinded = qBinded.replace("\n","")
os.mkdir(f'./images/{qBinded}')
ddg.download(qDDG, folder= f"./images/{qBinded}/", max_urls=10, thumbnails=True)
normalize = os.listdir(f"./images/{qBinded}/")
print(len(os.listdir(f"./images/{qBinded}/")))
if len(os.listdir(f"./images/{qBinded}/")) == 0:
html += f'{q}
'
os.rmdir(f'./images/{qBinded}/')
continue
print('NORMALIZE', normalize)
normalize = normalize[0]
splitExtension = os.path.splitext(normalize)
print('ESTENSIONE', splitExtension)
os.rename(f'./images/{qBinded}/{normalize}', f'./images/{qBinded}/{qBinded}')
os.replace(f'./images/{qBinded}/{qBinded}', f'./images/{qBinded}.jpg')
shutil.rmtree(f'./images/{qBinded}/')
html += f"""{q}"""
html += f""""""
html += '''
'''
html = html.replace(' .','.').replace(" '", "'")
with open('picindex.html','w') as index:
index.write(html)