Skip to content

Instantly share code, notes, and snippets.

@upidea
Created January 3, 2019 07:39
Show Gist options
  • Select an option

  • Save upidea/7582212affe5343688414a717491d2ae to your computer and use it in GitHub Desktop.

Select an option

Save upidea/7582212affe5343688414a717491d2ae to your computer and use it in GitHub Desktop.
Generate Anki flashcard from web snatch.
import os
import re
import requests
import json
import time
import datetime
import genanki
my_model = genanki.Model(
201901021920,
'每日一句',
fields=[
{'name': 'content'},
{'name': 'translation'},
{'name': 'note'},
{'name': 'picture'},
{'name': 'tts'}
],
css='''
</style>
<style>
body{
margin:0;padding:0;
font:14px/1 'Microsoft Yahei',sans-serif,Arial,Verdana;color:#333;
}
.detail-banner-img {display: block; border:0;}
.detail-content-en{font-size:26px;line-height:34px;margin-top:20px}
.detail-content-zh{font-size:18px;line-height:24px;margin-top:8px}
.detail-content-desc{margin-top:16px}
blockquote{line-height:24px;font-size:14px;color:#666}
blockquote:before{content:'';display:inline-block;background-image:url(_daily.png);background-position:-696px -361px;width:21px;height:14px;top:-6px;}
.icon-sound,.icon-sound:active,.icon-sound:hover{background-image:url(_daily.png);width:17px;height:16px}
.icon-sound{display:inline-block;vertical-align:middle;background-position:-687px -386px;cursor:pointer;margin-left:4px}
.icon-sound:hover{background-position:-670px -386px}.icon-sound:active{background-position:-653px -386px}
</style>
<style>
''',
templates=[
{
'name': '每日一句',
'qfmt': '''
{{#picture}}
{{picture}}
{{/picture}}
<div class="detail-content-en">{{content}}<i class="icon-sound">{{tts}}</i> </div>
''',
'afmt': '''
{{FrontSide}}
<div class="detail-content-zh">{{translation}}</div>
<blockquote class="detail-content-desc">{{note}}</blockquote>
'''
},
])
my_deck = genanki.Deck(
201901021952,
'EveryDayEnglish')
media_files = []
btime = 1325347200
while btime<time.mktime(datetime.datetime.now().timetuple()):
dt = time.strftime("%Y-%m-%d",time.localtime(btime))
url = 'http://sentence.iciba.com/index.php?callback=jQuery190020799044250976584_1546069428424&c=dailysentence&m=getdetail&title=%s&_=1546069428428'%(dt,)
r = requests.get(url)
w = re.findall('^jQuery[^(]*\((.*)\)',r.text)
js = json.loads(w[0])
imgname = None
ttsname = None
if js['picture2']:
imgname = os.path.basename(js['picture2'])
media_files.append(imgname)
p = requests.get(js['picture2'])
with open(imgname, 'wb') as f:
f.write(p.content)
if js['tts']:
ttsname = ''.join([os.path.basename(js['tts']), '.mp3'])
media_files.append(ttsname)
p = requests.get(js['tts'])
with open(ttsname, 'wb') as f:
f.write(p.content)
aNote = genanki.Note(model=my_model, fields=[
js['content'],
js['note'],
js['translation'],
"<img src=\"%s\" />"%(imgname,) if imgname else "",
"[sound:%s]"%(ttsname,) if ttsname else ""
])
my_deck.add_note(aNote)
btime = btime+24*3600
genanki.Package(my_deck, media_files=media_files).write_to_file('EveryDayEnglish.apkg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment