import os
import openai
text=("genereaza o propozitie pentru a cauta ce te-ar interesa sa afli pe internet")


#os.remove('raspuns.txt')
#os.remove('audio.mp3')


openai.api_key = "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"

# list engines
engine="text-davinci-002"

# print the first engine's id
#print(engines.data[9].id)

# create a completion
completion = response = openai.Completion.create(
  engine="text-davinci-002",
  prompt=(text),
  temperature=0.7,
  max_tokens=150,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0
)

# print the completion
raspuns = (completion.choices[0].text)
print(completion.choices[0].text)
######################################################

from googlesearch import search


import os
import requests
import shutil

date_time = ("documente")
os.makedirs(date_time, exist_ok=True)
print(f"se cauta {date_time}")

query = (raspuns)
num = 33


log_file_path = f'{date_time}/logs.txt'
log_file = open(log_file_path, 'w')
results: list = search(query, num=int(num), start=0, stop=int(num), pause=5)
results_list = []
for url in results:
    log_file.write(f"{url}\n")
    results_list.append(url)
log_file.flush()
log_file.close()

cwd = os.path.curdir
cwd_log_file = os.path.join(cwd, 'logs.txt')
print(cwd_log_file)
shutil.rmtree(cwd_log_file, ignore_errors=True)
shutil.copy2(log_file_path, cwd_log_file)


def download_url(url_to_download, index, folder):
    print(url_to_download)
    local_filename = f'{index}_'+url_to_download.split('/')[-1]
    local_path = os.path.join(folder, local_filename)
    r = requests.get(url_to_download)
    f = open(local_path, 'wb')
    for chunk in r.iter_content(chunk_size=512 * 1024):
        if chunk:  # filter out keep-alive new chunks
            f.write(chunk)
    f.close()
    return


print("am terminat de cautat, acum incep descarcarea documentelor")
ix = 0
for url in results_list:
    try:
        download_url(url, ix, date_time)
    except Exception as ignored:
        pass
    ix += 1


    
