Adding audio downloader

This commit is contained in:
Alex Yatskov 2016-07-16 22:01:23 -07:00
parent 28b1457a3a
commit 7177650f2f

View File

@ -18,9 +18,11 @@
import PyQt4 import PyQt4
import anki import anki
import aqt import aqt
import hashlib
import json import json
import select import select
import socket import socket
import urllib2
# #
@ -30,6 +32,40 @@ import socket
API_VERSION = 1 API_VERSION = 1
#
# Helpers
#
def downloadAudio(kana, kanji):
url = 'http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana={}'.format(urllib2.quote(kana.encode('utf-8')))
if kanji:
url += '&kanji={}'.format(urllib2.quote(kanji.encode('utf-8')))
try:
resp = urllib2.urlopen(url)
except urllib2.URLError:
return None
if resp.code != 200:
return None
data = resp.read()
m = hashlib.md5()
m.update(data)
digest = m.hexdigest()
if digest == '7e2c2f954ef6051373ba916f000168dc':
return None
filename = u'yomichan_{}'.format(kana)
if kanji:
filename += u'_{}'.format(kanji)
filename += u'.mp3'
return filename, data
# #
# AjaxRequest # AjaxRequest
# #
@ -191,7 +227,7 @@ class AjaxServer:
# #
class AnkiBridge: class AnkiBridge:
def addNote(self, deckName, modelName, fields, tags=[]): def addNote(self, deckName, modelName, fields, tags, audioUrl):
collection = self.collection() collection = self.collection()
if collection is None: if collection is None:
return return
@ -324,7 +360,8 @@ class AnkiConnect:
note['deckName'], note['deckName'],
note['modelName'], note['modelName'],
note['fields'], note['fields'],
note['tags'] note['tags'],
note.get('audioUrl')
) )
@ -334,7 +371,8 @@ class AnkiConnect:
results.append(self.anki.canAddNote( results.append(self.anki.canAddNote(
note['deckName'], note['deckName'],
note['modelName'], note['modelName'],
note['fields'] note['fields'],
note.get('audioUrl')
)) ))
return results return results