diff --git a/lib/python/qmk/cli/kle2json.py b/lib/python/qmk/cli/kle2json.py index 3d11242078..b6b5647612 100755 --- a/lib/python/qmk/cli/kle2json.py +++ b/lib/python/qmk/cli/kle2json.py @@ -4,6 +4,7 @@ import json import os from pathlib import Path +import requests from milc import cli from kle2xy import KLE2xy @@ -14,6 +15,51 @@ from qmk.info import info_json from qmk.info_json_encoder import InfoJSONEncoder +def fetch_json(url): + """Gets the JSON from a url. + """ + response = fetch_url(url) + + if response.status_code == 200: + return response.json() + + print(f'ERROR: {url} returned {response.status_code}: {response.text}') + return {} + + +def fetch_url(url): + """Fetch a URL. + """ + response = requests.get(url, timeout=30) + response.encoding='utf-8-sig' + + return response + + +def fetch_gist(id): + """Retrieve a gist from gist.github.com + """ + url = f'https://api.github.com/gists/{id}' + gist = fetch_json(url) + + for data in gist['files'].values(): + if data['filename'].endswith('kbd.json'): + if data.get('truncated'): + return fetch_url(data['raw_url']).text + else: + return data['content'] + + return None + + +def fetch_kle(id): + """Fetch the kle data from a gist ID. + """ + gist = fetch_gist(id) + + return gist[1:-1] + + @cli.argument('kle', arg_only=True, help='A file or KLE id to convert') @cli.argument('--vid', arg_only=True, default='0x03A8', help='USB VID (Default: 0x03A8)') @cli.argument('--pid', arg_only=True, default='0x0000', help='USB PID (Default: 0x0000)') @@ -39,11 +85,13 @@ def kle2json(cli): cli.log.error('Invalid KLE url: {fg_cyan}%s', cli.args.kle) return False else: - print('FIXME: fetch gist') - return False + raw_code = fetch_kle(kle_path.split('/')[-1]) + else: - cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path) - return False + raw_code = fetch_kle(cli.args.kle) + if not raw_code: + cli.log.error('File {fg_cyan}%s{style_reset_all} was not found.', file_path) + return False # Make sure the user supplied a keyboard if not cli.args.keyboard: diff --git a/requirements.txt b/requirements.txt index 6e907cf8e8..cd392223ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ colorama hjson milc pygments +requests