import datetime import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) import csv from urllib.parse import urljoin from urllib.parse import urlencode import json import pytz from requests.auth import HTTPBasicAuth S = requests.Session() S.verify = False eastern = pytz.timezone('US/Eastern') username = "rlx.ops@noaa.gov" password = "rlx25303!" today = datetime.date.today() currentday = datetime.datetime.now() y = today.year m = today.month d = today.day currenttime = currentday.replace(hour=12,minute=0,second=0,microsecond=0) entrytime = currentday.replace(hour=12,minute=30,second=0,microsecond=0) eastern_time = currenttime.astimezone(eastern) obtime = eastern_time.strftime("%Y-%m-%dT%H:%M:%S") eastern_time = entrytime.astimezone(eastern) entrytime = eastern_time.strftime("%Y-%m-%dT%H:%M:%S") def getob(y,m,d): url = "https://mesonet.agron.iastate.edu/request/coop/obs-dl.php?network=WV_COOP&station%5B%5D=RLXW2&year1={year}&month1={month}&day1={day}&year2={year}&month2={month}&day2={day}&what=download&delim=comma".format(year=y,month=m,day=d) print(url) response = S.get(url) csvdecode = response.content.decode('utf-8') csvstuff = csv.reader(csvdecode.splitlines(), delimiter=',') csvlist = list(csvstuff) print(csvlist[1]) print(csvlist[1][5],csvlist[1][6],csvlist[1][7]) return create_dict(csvlist[1][5],csvlist[1][6],csvlist[1][7],csvlist[1][3],csvlist[1][4]) def create_dict(precipitation,snowfall,snowdepth,maxt,mint): data = {} print(precipitation) match precipitation: case "0": precipitation = float(precipitation) gaugeCatchIsTrace=False case "T": precipitation = float(0.00) gaugeCatchIsTrace=True case "0.0001": precipitation = float(0.00) gaugeCatchIsTrace=True case "M": precipitation = "NA" gaugeCatchIsTrace=False case "": precipitation = "NA" gaugeCatchIsTrace=False case _: if float(precipitation) >= 0: precipitation = float(precipitation) gaugeCatchIsTrace=False else: precipitation = "NA" gaugeCatchIsTrace=False match snowfall: case "M": snowfall = "NA" snowfalltrace = False case "T": snowfall = 0.0 snowfalltrace = True case "0.0001": snowfall = 0.0 snowfalltrace = True case _: snowfall = round(float(snowfall),1) snowfalltrace = False match snowdepth: case "M": snowdepth = "NA" snowdepthtrace = False case "T": snowdepth = 0.0 snowdepthtrace = True case "0.0001": snowdepth = 0.0 snowdepthtrace = True case _: snowdepth = round(float(snowdepth),1) snowdepthtrace = False station = "WV-KN-53" entryDateTime=datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") notes="Maximum temperature: " + maxt + "F minimum temperature: " + mint + "F" weatherdata = { "id": 0, "stationNumber": station, "stationName": "South Charleston 0.3 SSW", "obsDateTime": obtime, "gaugeCatch": { "sortValue": 0, "precipValue": precipitation, "formatValue": "0.00", "value": precipitation, "isTrace": gaugeCatchIsTrace, "isSnowDepth": False, "units": "US Units" }, "snowfallDepth": { "sortValue": None, "precipValue": snowfall, "formatValue": 0.0, "value": snowfall, "isTrace": snowfalltrace, "isSnowDepth": True, "units": "US Units" }, "snowfallSwe": { "sortValue": None, "precipValue": 0, "formatValue": "NA", "value": None, "isTrace": False, "isSnowDepth": False, "units": "US Units" }, "snowpackDepth": { "sortValue": None, "precipValue": snowdepth, "formatValue": 0.0, "value": snowdepth, "isTrace": snowdepthtrace, "isSnowDepth": True, "units": "US Units" }, "snowpackSwe": { "sortValue": None, "precipValue": 0, "formatValue": "NA", "value": None, "isTrace": False, "isSnowDepth": False, "units": "US Units" }, "notes": notes, "entryDateTime": entrytime, "dateTimeStamp": "", "units": "US Units" } return weatherdata def coco(data): # Login URL login_url = "https://mobile.cocorahs.org/login?_data=routes%2F_auth.login" # Login Credentials credentials = { "username": "rlx.ops@noaa.gov", "password": "rlx25303!", } # Headers for login request (simplified from the JS fetch headers) headers = { "accept": "*/*", "accept-language": "en-US,en;q=0.9", "content-type": "application/x-www-form-urlencoded;charset=UTF-8", "sec-fetch-mode": "cors", "sec-fetch-site": "same-origin" } # Prepare the payload for POST request payload = urlencode(credentials) # Session for maintaining cookies session = requests.Session() # Perform login login_response = session.post(login_url, headers=headers, data=payload) if login_response.status_code == 204: # Assuming 204 means success for login url = "https://mobile.cocorahs.org/mydata/dailyprecip/add?_data=routes%2Fmydata.dailyprecip.add" response = session.post(url, headers=headers, json=data) if response.status_code == 200 or response.status_code == 204: print("Data submitted successfully!") print(response.text) else: print(f"Failed to submit data. Status code: {response.status_code}") print(response.text) else: print("Login failed.") def main(): data = getob(y, m, d) # This should return the expected data format coco(data) if __name__ == "__main__": main()