WIP
This commit is contained in:
parent
dd6168f5e6
commit
a313843b8e
@ -19,6 +19,7 @@ import anki
|
|||||||
import aqt
|
import aqt
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
import os.path
|
||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
@ -37,12 +38,10 @@ URL_TIMEOUT = 10
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib2
|
import urllib2
|
||||||
urlQuote = urllib2.quote
|
web = urllib2
|
||||||
urlOpen = urllib2.urlopen
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib import request
|
from urllib import request
|
||||||
urlQuote = request.quote
|
web = request
|
||||||
urlOpen = request.urlopen
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import PyQt4 as PyQt
|
import PyQt4 as PyQt
|
||||||
@ -57,22 +56,10 @@ makeStr = lambda data: data.decode('utf-8')
|
|||||||
# Audio helpers
|
# Audio helpers
|
||||||
#
|
#
|
||||||
|
|
||||||
def audioBuildFilename(kana, kanji):
|
def audioDownload(url):
|
||||||
filename = u'yomichan_{}'.format(kana)
|
|
||||||
if kanji:
|
|
||||||
filename += u'_{}'.format(kanji)
|
|
||||||
filename += u'.mp3'
|
|
||||||
return filename
|
|
||||||
|
|
||||||
|
|
||||||
def audioDownload(kana, kanji):
|
|
||||||
url = 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji={}'.format(urlQuote(kanji.encode('utf-8')))
|
|
||||||
if kana:
|
|
||||||
url += '&kana={}'.format(urlQuote(kana.encode('utf-8')))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = urlOpen(url, timeout=URL_TIMEOUT)
|
resp = web.urlopen(url, timeout=URL_TIMEOUT)
|
||||||
except:
|
except web.URLError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if resp.code != 200:
|
if resp.code != 200:
|
||||||
@ -81,18 +68,33 @@ def audioDownload(kana, kanji):
|
|||||||
return resp.read()
|
return resp.read()
|
||||||
|
|
||||||
|
|
||||||
def audioIsPlaceholder(data):
|
|
||||||
m = hashlib.md5()
|
|
||||||
m.update(data)
|
|
||||||
return m.hexdigest() == '7e2c2f954ef6051373ba916f000168dc'
|
|
||||||
|
|
||||||
|
|
||||||
def audioInject(note, fields, filename):
|
def audioInject(note, fields, filename):
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if field in note:
|
if field in note:
|
||||||
note[field] += u'[sound:{}]'.format(filename)
|
note[field] += u'[sound:{}]'.format(filename)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# AjaxRequest
|
# AjaxRequest
|
||||||
#
|
#
|
||||||
@ -264,11 +266,27 @@ class AnkiBridge:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if audio is not None and len(audio['fields']) > 0:
|
if audio is not None and len(audio['fields']) > 0:
|
||||||
data = audioDownload(audio['kana'], audio['kanji'])
|
url = audio.get('url')
|
||||||
if data is not None and not audioIsPlaceholder(data):
|
if url is None:
|
||||||
filename = audioBuildFilename(audio['kana'], audio['kanji'])
|
data = jpodDownload(audio['kana'], audio['kanji'])
|
||||||
audioInject(note, audio['fields'], filename)
|
filename = jpodBuildFilename(audio['kana'], audio['kanji'])
|
||||||
self.media().writeData(filename, data)
|
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)
|
||||||
|
|
||||||
self.startEditing()
|
self.startEditing()
|
||||||
collection.addNote(note)
|
collection.addNote(note)
|
||||||
|
Loading…
Reference in New Issue
Block a user