#!/usr/bin/env python3 import os import requests import sys import time SEPARATOR = ';' def load_temps() -> str: base_url = 'https://webb.nasa.gov/content/webbLaunch/flightCurrentState2.0.json' timestamp = int(time.time()) url = f'{base_url}?unique={timestamp}' result = requests.get(url) if result.status_code != 200: print(f'Could not load data. Status: {result.status_code}', file=sys.stderr) sys.exit(1) data = result.json()['currentState'] cols = [ timestamp, data['tempWarmSide1C'], data['tempWarmSide2C'], data['tempCoolSide1C'], data['tempCoolSide2C'] ] return SEPARATOR.join((str(x) for x in cols)) def write_csv(line: str, file: os.PathLike = 'data.csv'): lines = [] if not os.path.isfile(file): header = ( 'timestamp' + SEPARATOR + 'tempWarmSide1C' + SEPARATOR + 'tempWarmSide2C' + SEPARATOR + 'tempCoolSide1C' + SEPARATOR + 'tempCoolSide2C' ) lines.append(header) lines.append(line) with open(file, mode='a') as f: f.write('\n'.join(lines) + '\n') if __name__ == '__main__': data = load_temps() write_csv(data)