'WUnderground, Extraction of Extremes Today

As contributor to WUnderground not a problem to read via API-call the JSON-outputfile with Today's values for my station.

That JSON-file has a series of numbered 'bins', with the series growing with time from 00:00. In each numbered 'bin' an equivalent dataset reporting values. At the end of the day a few hundred 'bins' in the JSON-file.

Avoiding setup of a local database, to find an actual survey of Extremes_Today, it is required to periodically scan the latest JSON-file from bin0 till the latest added bin in a recursive way.

It means in some way to read each numbered bin, extract&evaluate values, jump to next bin, till last bin reached & processed.

Trying the 2 approaches below in a Python-script: these 2 script-segments just should check & report that a bin exists. The scriptlines till 442 do other jobs (incl. complete read-out of bin=0 for references), already running without error.

    # Line 442 = In WU JSON-Output Today's Data find & process next Bin upto/incl. last Bin
    # Example call-string for ToDay-info = https://api.weather.com/v2/pws/observations/all/1day?stationId=KMAHANOV10&format=json&units=m&apiKey=yourApiKey
    # Extracting contents of the JSON-file by the scriptlines below
    #    page = urllib.urlopen('https://api.weather.com/v2/pws/observations/all/1day?stationId=KMAHANOV10&format=json&units=m&apiKey=yourApiKey')
    #    content_test = page.read()
    #    obj_test2 = json.loads(content_test)
    # Extraction of a value is like 
    # Epochcheck = obj_test2['observations'][Bin]['epoch']
    # 'epoch' is present as element in all bins of the JSON-file (with trend related to the number of the bin) and therefore choosen as key for scan & search. If not found, then that bin not existing = passed last present bin 
    
    # Bin [0] earlier separately has been processed => initial contents at 00:00 = references for Extremes-search
    # GENERAL setup of the scanning function:
    # Bin = 0
    # while 'epoch' exists
    #     Read 'epoch' & translate to CET/LocalTime
    #     Compare values of Extremes in that bin with earlier Extremes
    #         if hi_value higher than hiExtreme => new hiExtreme & adapt HiTime (= translated 'epoch')
    #         if low_value lower than LowExtreme => new lowExtreme & adapt LowTime (= translated 'epoch')
    #     Bin = Bin + 1

    # Approach1
        Bin = 0
        Epochcheck =  obj_test2['observations'][0]['epoch']
        try:
            Epochcheck = obj_test2['observations'][Bin]['epoch']
            print(Bin)
            Bin += 1
        except NameError:
            Epochcheck = None  
    
    # Approach2
        Bin = 0
        Epochcheck = obj_test2['observations'][0]['epoch']
        While Epochcheck is not None:
            Epochcheck = obj_test2['observations'][Bin]['epoch']
            Print(Bin)
            Bin += 1

Approach1 does not throw an error, but it steps out at Bin = 1.

Approach2 reports a syntax error.

    File "/home/pi/domoticz/scripts/python/URL_JSON_WU_to_HWA_Start01a_0186.py", line 476
        While Epochcheck is not None:
                       ^
    SyntaxError: invalid syntax

Apparently the checkline with dynamically variable contents for Bin cannot be set up in this way: the dynamic setting of variable Bin must be inserted/described in a different way.

    Epochcheck = obj_test2['observations'][Bin]['epoch']

What is in Python the appropriate way to perform such JSON-scanning using a dynamic variable [Bin]? Or simpler way of scan&extract a series of Bins in a JSON-file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source