import time
import requests
from PIL import Image

GET = "https://pydefis.callicode.fr/defis/MachineLearning/intern/C0MfHAoFXBILF1IiEgUCC0BVX14VE1FfTU5RVV9DAgQAUVdMaWtqGkJRXUBaWR4QHABGT0BEWDEeEABqXEEoKEBVBx4ABUVNU0ZdQgAFbBtQUy5fTTdpHQUGXlJERkFMBwQfD0dULl9NX28cEWwrTEJHQ10ABR0XXVZCXV5bB20dF0Q8X1M2NwMBABlGU0ZfTUEGBh8PR1QuX01ZbxwRbCtMQkBDXwUFHRdIVl1BWDEeEANqXEEoKEBaHAUdF0NUXUVfWW8cEQQtPA4%3D/getpoint"
GET2 = "https://pydefis.callicode.fr/defis/MachineLearning/intern/C0MfHAoFXBILF1IiEgUCC0BVX14VE1FfTU5RVV9DAgQAUVdMaWtqGkJRXUBaWR4QHABGT0BEWDEeEABqXEEoKEBVBx4ABUVNU0ZdQgAFbBtQUy5fTTdpHQUGXlJERkFMBwQfD0dULl9NX28cEWwrTEJHQ10ABR0XXVZCXV5bB20dF0Q8X1M2NwMBABlGU0ZfTUEGBh8PR1QuX01ZbxwRbCtMQkBDXwUFHRdIVl1BWDEeEANqXEEoKEBaHAUdF0NUXUVfWW8cEQQtPA4%3D/reponse/"

#Collect data
while True:
    res = requests.get(GET)
    x,y = [float(v) for v in res.text.replace("[", "").replace("]", "").split(",")]
    res = requests.get(GET2 + str(1))
    color = res.text
    datafile = open("MachineLearning_data.txt", "a")
    datafile.write("{},{},{}".format(x,y,color))
    datafile.close
    time.sleep(1)
