Browse Source

feat: independent language options

dev
Dnomd343 1 year ago
parent
commit
e0f520badf
  1. 4
      gvlk/config.yml
  2. 11
      gvlk/fetch.py

4
gvlk/config.yml

@ -0,0 +1,4 @@
lang:
- en-us
- zh-cn
- zh-tw

11
gvlk/fetch.py

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import yaml
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
TIMEOUT = 10 LANG = yaml.full_load(open('config.yml').read())['lang']
LANG = ['en-us', 'zh-cn', 'zh-tw']
URL = 'https://learn.microsoft.com/%s/windows-server/get-started/kms-client-activation-keys' URL = 'https://learn.microsoft.com/%s/windows-server/get-started/kms-client-activation-keys'
@ -33,7 +33,7 @@ def extractKeys(items: list) -> dict: # detached from original html elements
def fetchGvlk(lang: str) -> dict: # fetch GVLKs of the specified language def fetchGvlk(lang: str) -> dict: # fetch GVLKs of the specified language
request = requests.get(URL % lang, timeout = TIMEOUT) request = requests.get(URL % lang, timeout = 15)
request.raise_for_status() # only http-code 2xx request.raise_for_status() # only http-code 2xx
request.encoding = 'utf-8' request.encoding = 'utf-8'
content = BeautifulSoup(request.text, 'lxml').select('.content')[0] # html parsing content = BeautifulSoup(request.text, 'lxml').select('.content')[0] # html parsing
@ -72,4 +72,7 @@ def combineGvlk(rawData: dict) -> dict: # merge multiple languages
gvlkData = combineGvlk({x: fetchGvlk(x) for x in LANG}) gvlkData = combineGvlk({x: fetchGvlk(x) for x in LANG})
print(json.dumps(gvlkData)) # output as json format
with open('raw.json', 'w') as fp: # output as `raw.json`
fp.write(json.dumps(gvlkData, indent = 2, ensure_ascii = False))
fp.write('\n')

Loading…
Cancel
Save