From 5ef2aa674e9c4d84754a973a3e50c1229dfa8901 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 24 Jun 2023 19:41:14 +0800 Subject: [PATCH] feat: GVLK fetch and combine --- assets/{demo.py => gvlk.py} | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) rename assets/{demo.py => gvlk.py} (61%) diff --git a/assets/demo.py b/assets/gvlk.py similarity index 61% rename from assets/demo.py rename to assets/gvlk.py index 535a47e..1ba6704 100755 --- a/assets/demo.py +++ b/assets/gvlk.py @@ -4,6 +4,7 @@ import json import requests from bs4 import BeautifulSoup +LANG = ['en-us', 'zh-cn', 'zh-tw'] URL = 'https://learn.microsoft.com/%s/windows-server/get-started/kms-client-activation-keys' @@ -47,8 +48,28 @@ def fetchKeys(lang: str) -> dict: return analyseKeys(items) -ret = fetchKeys('zh-cn') -print(json.dumps(ret)) +def combineKeys(rawData: dict) -> dict: + firstVal = lambda x: list(x.values())[0] + flipDict = lambda x: {v: k for k, v in x.items()} -ret = fetchKeys('en-us') -print(json.dumps(ret)) + def release(version: str) -> dict: + keys = [x for _, x in firstVal(rawData)[version]['content'].items()] + gvlkItem = { + 'name': {lang: data[version]['name'] for (lang, data) in rawData.items()}, + 'content': [{'name': {}, 'key': x} for x in keys] + } + for index in range(len(keys)): + for (lang, data) in rawData.items(): + data = flipDict(data[version]['content']) + gvlkItem['content'][index]['name'][lang] = data[keys[index]] + return gvlkItem + + result = {} + for gvlkVersion in list(firstVal(rawData)): + result[gvlkVersion] = release(gvlkVersion) + return result + + +print(json.dumps( + combineKeys({x: fetchKeys(x) for x in LANG}) +))