anki-connect/AnkiConnect.py

441 lines
10 KiB
Python
Raw Normal View History

2016-05-21 22:10:12 +00:00
# Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
# Author: Alex Yatskov <alex@foosoft.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import anki
import aqt
2016-07-17 05:01:23 +00:00
import hashlib
2016-05-21 22:10:12 +00:00
import json
2017-01-30 01:34:18 +00:00
import os.path
2016-05-21 22:10:12 +00:00
import select
import socket
2016-05-29 22:49:38 +00:00
#
# Constants
#
API_VERSION = 1
2016-07-26 05:54:40 +00:00
URL_TIMEOUT = 10
2016-05-29 22:49:38 +00:00
2017-01-29 01:31:33 +00:00
#
# General helpers
#
try:
import urllib2
2017-01-30 01:34:18 +00:00
web = urllib2
2017-01-29 01:31:33 +00:00
except ImportError:
from urllib import request
2017-01-30 01:34:18 +00:00
web = request
2017-01-29 01:31:33 +00:00
try:
import PyQt4 as PyQt
except ImportError:
import PyQt5 as PyQt
makeBytes = lambda data: data.encode('utf-8')
makeStr = lambda data: data.decode('utf-8')
2016-07-17 05:01:23 +00:00
#
2016-07-17 05:56:02 +00:00
# Audio helpers
2016-07-17 05:01:23 +00:00
#
2017-01-30 01:34:18 +00:00
def audioDownload(url):
2016-07-17 05:01:23 +00:00
try:
2017-01-30 01:34:18 +00:00
resp = web.urlopen(url, timeout=URL_TIMEOUT)
except web.URLError:
2016-07-17 05:01:23 +00:00
return None
if resp.code != 200:
return None
2016-07-17 16:38:33 +00:00
return resp.read()
2016-07-17 05:01:23 +00:00
2016-07-17 16:38:33 +00:00
2016-07-17 17:34:07 +00:00
def audioInject(note, fields, filename):
2016-07-17 16:38:33 +00:00
for field in fields:
if field in note:
2016-07-17 17:34:07 +00:00
note[field] += u'[sound:{}]'.format(filename)
2016-07-17 05:01:23 +00:00
2017-01-30 01:34:18 +00:00
#
# Legacy JPOD101 handlers
#
def jpodBuildFilename(kana, kanji):
filename = u'yomichan_{}'.format(kana)
if kanji:
filename += u'_{}'.format(kanji)
filename += u'.mp3'
return filename
def jpodDownload(kana, kanji):
url = 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji={}'.format(web.quote(kanji.encode('utf-8')))
if kana:
url += '&kana={}'.format(web.quote(kana.encode('utf-8')))
return audioDownload(url)
2016-05-21 22:10:12 +00:00
#
# AjaxRequest
#
class AjaxRequest:
def __init__(self, headers, body):
self.headers = headers
self.body = body
#
# AjaxClient
#
class AjaxClient:
def __init__(self, sock, handler):
self.sock = sock
self.handler = handler
2017-01-29 01:31:33 +00:00
self.readBuff = bytes()
self.writeBuff = bytes()
2016-05-21 22:10:12 +00:00
def advance(self, recvSize=1024):
if self.sock is None:
return False
rlist, wlist = select.select([self.sock], [self.sock], [], 0)[:2]
if rlist:
msg = self.sock.recv(recvSize)
if not msg:
self.close()
return False
self.readBuff += msg
req, length = self.parseRequest(self.readBuff)
if req is not None:
self.readBuff = self.readBuff[length:]
self.writeBuff += self.handler(req)
if wlist and self.writeBuff:
length = self.sock.send(self.writeBuff)
self.writeBuff = self.writeBuff[length:]
if not self.writeBuff:
self.close()
return False
return True
def close(self):
if self.sock is not None:
self.sock.close()
self.sock = None
2017-01-29 01:31:33 +00:00
self.readBuff = bytes()
self.writeBuff = bytes()
2016-05-21 22:10:12 +00:00
def parseRequest(self, data):
2017-01-29 01:31:33 +00:00
parts = data.split(makeBytes('\r\n\r\n'), 1)
2016-05-21 22:10:12 +00:00
if len(parts) == 1:
return None, 0
headers = {}
2017-01-29 01:31:33 +00:00
for line in parts[0].split(makeBytes('\r\n')):
pair = line.split(makeBytes(': '))
2016-05-21 22:10:12 +00:00
headers[pair[0]] = pair[1] if len(pair) > 1 else None
headerLength = len(parts[0]) + 4
2017-01-29 01:31:33 +00:00
bodyLength = int(headers[makeBytes('Content-Length')])
2016-05-21 22:10:12 +00:00
totalLength = headerLength + bodyLength
if totalLength > len(data):
return None, 0
body = data[headerLength : totalLength]
return AjaxRequest(headers, body), totalLength
#
# AjaxServer
#
class AjaxServer:
def __init__(self, handler):
self.handler = handler
self.clients = []
self.sock = None
def advance(self):
if self.sock is not None:
self.acceptClients()
self.advanceClients()
def acceptClients(self):
rlist = select.select([self.sock], [], [], 0)[0]
if not rlist:
return
clientSock = self.sock.accept()[0]
if clientSock is not None:
clientSock.setblocking(False)
self.clients.append(AjaxClient(clientSock, self.handlerWrapper))
def advanceClients(self):
2017-01-29 01:31:33 +00:00
self.clients = list(filter(lambda c: c.advance(), self.clients))
2016-05-21 22:10:12 +00:00
def listen(self, address='127.0.0.1', port=8765, backlog=5):
self.close()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setblocking(False)
self.sock.bind((address, port))
self.sock.listen(backlog)
def handlerWrapper(self, req):
2017-01-29 01:31:33 +00:00
body = makeBytes(json.dumps(self.handler(json.loads(makeStr(req.body)))))
resp = bytes()
2016-05-21 22:10:12 +00:00
2017-01-29 01:31:33 +00:00
headers = [
['HTTP/1.1 200 OK', None],
['Content-Type', 'text/json'],
['Content-Length', str(len(body))]
]
2016-05-21 22:10:12 +00:00
2017-01-29 01:31:33 +00:00
for [key, value] in headers:
2016-05-21 22:10:12 +00:00
if value is None:
2017-01-29 01:31:33 +00:00
resp += makeBytes('{}\r\n'.format(key))
2016-05-21 22:10:12 +00:00
else:
2017-01-29 01:31:33 +00:00
resp += makeBytes('{}: {}\r\n'.format(key, value))
2016-05-21 22:10:12 +00:00
2017-01-29 01:31:33 +00:00
resp += makeBytes('\r\n')
2016-05-21 22:10:12 +00:00
resp += body
return resp
def close(self):
if self.sock is not None:
self.sock.close()
self.sock = None
for client in self.clients:
client.close()
self.clients = []
#
# AnkiBridge
#
class AnkiBridge:
2016-07-17 05:08:20 +00:00
def addNote(self, deckName, modelName, fields, tags, audio):
2016-05-29 17:31:24 +00:00
collection = self.collection()
if collection is None:
return
2016-07-17 16:38:33 +00:00
note = self.createNote(deckName, modelName, fields, tags)
2016-05-21 22:34:09 +00:00
if note is None:
return
2016-07-17 16:40:28 +00:00
if audio is not None and len(audio['fields']) > 0:
2017-01-30 01:34:18 +00:00
url = audio.get('url')
if url is None:
data = jpodDownload(audio['kana'], audio['kanji'])
filename = jpodBuildFilename(audio['kana'], audio['kanji'])
skipHash = '7e2c2f954ef6051373ba916f000168dc'
else:
data = audioDownload(url)
filename = os.path.basename(audio['filename'])
skipHash = audio.get('skipHash')
if data is not None:
if skipHash is None:
skip = False
else:
m = hashlib.md5()
m.update(data)
skip = skipHash == m.hexdigest()
if not skip:
audioInject(note, audio['fields'], filename)
self.media().writeData(filename, data)
2016-05-21 22:34:09 +00:00
2016-07-17 16:38:33 +00:00
self.startEditing()
2016-05-21 22:34:09 +00:00
collection.addNote(note)
collection.autosave()
2016-07-17 16:38:33 +00:00
self.stopEditing()
2016-05-21 22:34:09 +00:00
return note.id
2016-05-21 22:10:12 +00:00
def canAddNote(self, deckName, modelName, fields):
return bool(self.createNote(deckName, modelName, fields))
2016-07-17 16:38:33 +00:00
def createNote(self, deckName, modelName, fields, tags=[]):
2016-05-29 17:31:24 +00:00
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2016-05-21 22:10:12 +00:00
if model is None:
2016-05-21 22:34:09 +00:00
return
2016-05-21 22:10:12 +00:00
2016-05-29 17:31:24 +00:00
deck = collection.decks.byName(deckName)
2016-05-21 22:10:12 +00:00
if deck is None:
2016-05-21 22:34:09 +00:00
return
2016-05-21 22:10:12 +00:00
2016-05-29 17:31:24 +00:00
note = anki.notes.Note(collection, model)
2016-05-21 22:10:12 +00:00
note.model()['did'] = deck['id']
note.tags = tags
for name, value in fields.items():
if name in note:
note[name] = value
if not note.dupeOrEmpty():
return note
def startEditing(self):
self.window().requireReset()
def stopEditing(self):
2016-05-29 17:31:24 +00:00
if self.collection() is not None:
2016-05-21 22:10:12 +00:00
self.window().maybeReset()
def window(self):
return aqt.mw
def collection(self):
return self.window().col
2016-07-17 16:38:33 +00:00
def media(self):
collection = self.collection()
if collection is not None:
return collection.media
2016-05-21 22:10:12 +00:00
def modelNames(self):
2016-05-29 17:31:24 +00:00
collection = self.collection()
if collection is not None:
return collection.models.allNames()
2016-05-21 22:10:12 +00:00
def modelFieldNames(self, modelName):
2016-05-29 17:31:24 +00:00
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2016-05-21 22:10:12 +00:00
if model is not None:
return [field['name'] for field in model['flds']]
def deckNames(self):
2016-05-29 17:31:24 +00:00
collection = self.collection()
if collection is not None:
return collection.decks.allNames()
2016-05-21 22:10:12 +00:00
#
# AnkiConnect
#
class AnkiConnect:
def __init__(self, interval=25):
self.anki = AnkiBridge()
self.server = AjaxServer(self.handler)
self.server.listen()
2017-01-29 01:31:33 +00:00
self.timer = PyQt.QtCore.QTimer()
2016-05-21 22:10:12 +00:00
self.timer.timeout.connect(self.advance)
self.timer.start(interval)
def advance(self):
self.server.advance()
def handler(self, request):
2016-05-22 00:11:00 +00:00
action = 'api_' + (request.get('action') or '')
2016-05-21 22:10:12 +00:00
if hasattr(self, action):
2016-05-22 00:11:00 +00:00
return getattr(self, action)(**(request.get('params') or {}))
2016-05-21 22:10:12 +00:00
2016-05-21 22:34:09 +00:00
def api_deckNames(self):
return self.anki.deckNames()
def api_modelNames(self):
return self.anki.modelNames()
def api_modelFieldNames(self, modelName):
return self.anki.modelFieldNames(modelName)
2016-05-29 06:23:14 +00:00
def api_addNote(self, note):
return self.anki.addNote(
note['deckName'],
note['modelName'],
note['fields'],
2016-07-17 05:01:23 +00:00
note['tags'],
2016-07-17 05:08:20 +00:00
note.get('audio')
2016-05-29 06:23:14 +00:00
)
def api_canAddNotes(self, notes):
results = []
for note in notes:
results.append(self.anki.canAddNote(
note['deckName'],
note['modelName'],
2016-07-17 05:56:02 +00:00
note['fields']
2016-05-29 06:23:14 +00:00
))
return results
2016-05-29 22:49:38 +00:00
def api_version(self):
return API_VERSION
2016-05-21 22:10:12 +00:00
#
# Entry
#
ac = AnkiConnect()