add compat with anki alpha

This commit is contained in:
Alex Yatskov 2017-01-28 17:31:33 -08:00
parent 03f58f82c0
commit 3d12f301dd

View File

@ -15,14 +15,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import PyQt4
import anki import anki
import aqt import aqt
import hashlib import hashlib
import json import json
import select import select
import socket import socket
import urllib2
# #
@ -33,6 +31,28 @@ API_VERSION = 1
URL_TIMEOUT = 10 URL_TIMEOUT = 10
#
# General helpers
#
try:
import urllib2
urlQuote = urllib2.quote
urlOpen = urllib2.urlopen
except ImportError:
from urllib import request
urlQuote = request.quote
urlOpen = request.urlopen
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')
# #
# Audio helpers # Audio helpers
# #
@ -46,13 +66,13 @@ def audioBuildFilename(kana, kanji):
def audioDownload(kana, kanji): def audioDownload(kana, kanji):
url = 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji={}'.format(urllib2.quote(kanji.encode('utf-8'))) url = 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji={}'.format(urlQuote(kanji.encode('utf-8')))
if kana: if kana:
url += '&kana={}'.format(urllib2.quote(kana.encode('utf-8'))) url += '&kana={}'.format(urlQuote(kana.encode('utf-8')))
try: try:
resp = urllib2.urlopen(url, timeout=URL_TIMEOUT) resp = urlOpen(url, timeout=URL_TIMEOUT)
except urllib2.URLError: except:
return None return None
if resp.code != 200: if resp.code != 200:
@ -91,8 +111,8 @@ class AjaxClient:
def __init__(self, sock, handler): def __init__(self, sock, handler):
self.sock = sock self.sock = sock
self.handler = handler self.handler = handler
self.readBuff = '' self.readBuff = bytes()
self.writeBuff = '' self.writeBuff = bytes()
def advance(self, recvSize=1024): def advance(self, recvSize=1024):
@ -129,22 +149,22 @@ class AjaxClient:
self.sock.close() self.sock.close()
self.sock = None self.sock = None
self.readBuff = '' self.readBuff = bytes()
self.writeBuff = '' self.writeBuff = bytes()
def parseRequest(self, data): def parseRequest(self, data):
parts = data.split('\r\n\r\n', 1) parts = data.split(makeBytes('\r\n\r\n'), 1)
if len(parts) == 1: if len(parts) == 1:
return None, 0 return None, 0
headers = {} headers = {}
for line in parts[0].split('\r\n'): for line in parts[0].split(makeBytes('\r\n')):
pair = line.split(': ') pair = line.split(makeBytes(': '))
headers[pair[0]] = pair[1] if len(pair) > 1 else None headers[pair[0]] = pair[1] if len(pair) > 1 else None
headerLength = len(parts[0]) + 4 headerLength = len(parts[0]) + 4
bodyLength = int(headers['Content-Length']) bodyLength = int(headers[makeBytes('Content-Length')])
totalLength = headerLength + bodyLength totalLength = headerLength + bodyLength
if totalLength > len(data): if totalLength > len(data):
@ -183,7 +203,7 @@ class AjaxServer:
def advanceClients(self): def advanceClients(self):
self.clients = filter(lambda c: c.advance(), self.clients) self.clients = list(filter(lambda c: c.advance(), self.clients))
def listen(self, address='127.0.0.1', port=8765, backlog=5): def listen(self, address='127.0.0.1', port=8765, backlog=5):
@ -197,22 +217,22 @@ class AjaxServer:
def handlerWrapper(self, req): def handlerWrapper(self, req):
body = json.dumps(self.handler(json.loads(req.body))) body = makeBytes(json.dumps(self.handler(json.loads(makeStr(req.body)))))
resp = '' resp = bytes()
headers = { headers = [
'HTTP/1.1 200 OK': None, ['HTTP/1.1 200 OK', None],
'Content-Type': 'text/json', ['Content-Type', 'text/json'],
'Content-Length': str(len(body)) ['Content-Length', str(len(body))]
} ]
for key, value in headers.items(): for [key, value] in headers:
if value is None: if value is None:
resp += '{}\r\n'.format(key) resp += makeBytes('{}\r\n'.format(key))
else: else:
resp += '{}: {}\r\n'.format(key, value) resp += makeBytes('{}: {}\r\n'.format(key, value))
resp += '\r\n' resp += makeBytes('\r\n')
resp += body resp += body
return resp return resp
@ -358,7 +378,7 @@ class AnkiConnect:
self.server = AjaxServer(self.handler) self.server = AjaxServer(self.handler)
self.server.listen() self.server.listen()
self.timer = PyQt4.QtCore.QTimer() self.timer = PyQt.QtCore.QTimer()
self.timer.timeout.connect(self.advance) self.timer.timeout.connect(self.advance)
self.timer.start(interval) self.timer.start(interval)