diceware-generator/dwg.py
2021-09-16 20:33:56 +08:00

64 lines
1.7 KiB
Python

import json
import sys
import pandas as pd
import requests
DW = pd.read_csv('pinyin.wordlist')
SEPARATOR = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '"', '#', '$', '%',
'&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>',
'?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'
]
LENGTH = int(sys.argv[1]) if len(sys.argv) > 1 else 6
URL = "https://api.random.org/json-rpc/2/invoke"
PAYLOAD = {
"jsonrpc": "2.0",
"method": "generateIntegers",
"params": {
"apiKey": "1b426e57-4fdd-4c3e-92d5-eefcba185690",
"n": 5 * LENGTH,
"min": 1,
"max": 6,
"replacement": True,
"base": 10
},
"id": 10110
}
HEADERS = {'content-type': "application/json", 'cache-control': "no-cache"}
CHT = []
WORD = []
RESPONSE = requests.request("POST",
URL,
data=json.dumps(PAYLOAD),
headers=HEADERS)
DATA = json.loads(RESPONSE.text)['result']['random']['data']
for i in range(LENGTH):
ID = int(''.join(str(d) for d in DATA[i * 5:i * 5 + 5]))
SELECT = DW[DW['id'] == ID]
CHT.append(SELECT['cht'].values[0])
WORD.append(SELECT['word'].values[0])
PAYLOAD['params']['n'] = 1
PAYLOAD['params']['min'] = 0
PAYLOAD['params']['max'] = 41
RESPONSE = requests.request("POST",
URL,
data=json.dumps(PAYLOAD),
headers=HEADERS)
DATA = json.loads(RESPONSE.text)
ID = DATA['result']['random']['data'][0]
REQUESTS_LEFT = DATA['result']['requestsLeft']
print(' '.join(CHT))
print(SEPARATOR[ID].join(WORD))
print('Request Left: ' + str(REQUESTS_LEFT))