#!/usr/bin/python3

import json
import os
import collections
from urllib.request import urlopen
from shutil import copyfile


# aliases
OrderedDict = collections.OrderedDict

os.chdir("/var/www/html/covid-portal/scripts/wastewater")

os.remove("wastewater_old.json")
os.rename("wastewater.json", "wastewater_old.json")
dst= 'wastewater.json'

greek_data=[]
url='https://www.ebi.ac.uk/ena/portal/api/filereport?accession=PRJEB44548&result=read_run&fields=study_accession,sample_accession,experiment_accession,run_accession,tax_id,scientific_name,fastq_ftp,submitted_ftp,sra_ftp&format=json&download=true&limit=0'
data = urlopen(url)
wjson = data.read()
wjdata = json.loads(wjson)


greek_data.extend(wjdata)


greek_data={"wastewater":greek_data}
with open(dst, 'w') as jsonfile:
    json.dump(greek_data, jsonfile, indent=2)


copyfile(dst,'/var/www/html/covid-portal/backup_data/wastewater.json')