Jun-21-2019, 09:19 PM
Wanted to edit my previous post but was not able to, so I'm reposting it here
Hi,
New to Python. I am trying to create some code that extracts API JSON from a website and display the results on an LCD screen.
I am trying to display a clock and a message on the LCD when there are no results to show. When results come in, it reverts to display the results, then when no results are available, go back to the clock.
Also, I can't find any better solution than extracting the entire API JSON data, which can take a few seconds pausing the code. IS there a better way to hit the URL than this too? I find this creates a timeout error all the time. I had tried to place a sleep function of 5 secs but found this too creates an error eventually.
Any advice on any of the above on the below code would be appreciated.
NOTE: I've turned off the LCD module to allow for testing. This will all be executed on a Raspberry Pi
Hi,
New to Python. I am trying to create some code that extracts API JSON from a website and display the results on an LCD screen.
I am trying to display a clock and a message on the LCD when there are no results to show. When results come in, it reverts to display the results, then when no results are available, go back to the clock.
Also, I can't find any better solution than extracting the entire API JSON data, which can take a few seconds pausing the code. IS there a better way to hit the URL than this too? I find this creates a timeout error all the time. I had tried to place a sleep function of 5 secs but found this too creates an error eventually.
Any advice on any of the above on the below code would be appreciated.
NOTE: I've turned off the LCD module to allow for testing. This will all be executed on a Raspberry Pi
import requests import pickle from time import sleep import time import datetime import json #import I2C_LCD_Driver # API URL rescue_url = "https://api.somesite.com/rescues" headers = {"Authorization": "Bearer token"} def get_data(url): data_json = [] data = requests.get(url, headers=headers).json() data = data['data'] for dta in data: if dta['attributes']['stat'] == 'open': data_json.append(dta['attributes']) return data_json # function to convert string datetime value when rescue commenced (usually entered by Mecha) def dte_convert(dte): d1 = datetime.datetime.strptime(dte, "%Y-%m-%dT%H:%M:%S.%fZ") + datetime.timedelta(hours=10) d2 = datetime.datetime.now() # set variable to current dateTime dur_s = (d2 - d1).total_seconds() # similar to datediff and calculates to seconds dy = divmod(dur_s, 86400) hrs = divmod(dy[1], 3600) mins = divmod(hrs[1], 60) ret = '%d d %d h %d m' % (dy[0], hrs[0], mins[0]) return ret while True: json_data = get_data(rescue_url) if json_data: for each in json_data: platform = each['platform'] client = each['client'] cde = each['code'] system = each['sys'] res_datetime = dte_convert(each['createdAt']) # mylcd.lcd_display_string("C: %s" %client ,1) # mylcd.lcd_display_string("P: %s" %platform + "C: %s" %str(cde), 2) # mylcd.lcd_display_string("S: %s" %sys, 1) # mylcd.lcd_display_string("A: %s" %str(res_datetime), 2) print("C: %s" % client) print("P: %s" % platform + " CR: %s" % str(cde_red)) print("S: %s" % system) print("A: %s" % str(res_datetime)) else: print("Date: %s" % time.strftime("%d/%m/%Y")) print("Time: %s" % time.strftime("%H:%M")) print('No Active Rescues')