import os
import requests
import time
from random import randint

def save2File(minV,maxV):
    #The base url of the nuclear data
    baseURL="http://nucleardata.nuclear.lu.se/toi/"
    #This is the query part
    qInfo="Gamma.asp?sql=&Min="+str(minV)+"&Max="+str(maxV)
    #Pasting everything together
    totURL=baseURL+qInfo

    print(totURL)
    #The filename of the corresponding page
    filename="minV"+str(minV)+"maxV"+str(maxV)+".html"
    print("This will be downloaded to %s" %(filename))

    #For storing everything in a directory
    myDir="myHTMLFiles"
    if not os.path.isdir(myDir):
        os.makedirs(myDir)

    #We know the maximun value in the database (inspected by hand) is
    #11258.9 so no point in doing a query beyond this point
    totMax=11259
    if maxV >= totMax:
        #There should also be a check before this function is called.
        print("Maximum value is too high, no point in doing the query")
        return False

    #Getting the uirl info
    r=requests.get(totURL)
    #Converting the content into a string
    sContent=str(r.content)

    #Checking if there was something found.
    relaxResStr="No gammas found."
    if sContent.find(relaxResStr) != -1:
        print("For %s no gammas where found" %(filename))
        #No need tos store anything.
        return False
    # print(sContent)
    
    #Doing the saving
    print("Saving into datafile")
    with open(myDir+'/'+filename, "w") as text_file:
        print(sContent,file=text_file)
        return True

    return False

totMax=11259 #it's really 11258.9

minV=8970
maxV=8971
# save2File(minV,maxV)

# incr=1 #from 0 to 4699 Ctr-C to stop it. And change:
# incr=10 #Used starting from 4700 Ctr-C to stop it. And change:
incr=100 #starting from 7400 till the end

for i in range(4700,totMax+incr+1,incr):
    minV=i
    maxV=i+incr
    print("minV,maxV =", minV,maxV)
    saveBool=save2File(minV,maxV)
    if not saveBool:
        print("Probably check ",minV,maxV)
    rInt=randint(0,2)
    print("Waiting for %d seconds" %(rInt))
    time.sleep(rInt)

print("Finished downloading the database")

