32 lines
948 B
Python
32 lines
948 B
Python
|
# Taken from https://github.com/FooSoft/anki-connect
|
||
|
|
||
|
import json
|
||
|
import urllib.request
|
||
|
|
||
|
|
||
|
def request(action, **params):
|
||
|
return {'action': action, 'params': params, 'version': 6}
|
||
|
|
||
|
|
||
|
def invoke(action, **params):
|
||
|
request_json = json.dumps(request(action, **params)).encode('utf-8')
|
||
|
response = json.load(urllib.request.urlopen(urllib.request.Request('http://localhost:8765', request_json)))
|
||
|
if len(response) != 2:
|
||
|
raise Exception('response has an unexpected number of fields')
|
||
|
if 'error' not in response:
|
||
|
raise Exception('response is missing required error field')
|
||
|
if 'result' not in response:
|
||
|
raise Exception('response is missing required result field')
|
||
|
if response['error'] is not None:
|
||
|
raise Exception(response['error'])
|
||
|
return response['result']
|
||
|
|
||
|
|
||
|
def main():
|
||
|
result = invoke('deckNames')
|
||
|
print('got list of decks: {}'.format(result))
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|