Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b7b84b

Browse files
committed
feature:recite with zuohaitao.com/word.html
1 parent 636f2ef commit 1b7b84b

File tree

4 files changed

+86
-12
lines changed

4 files changed

+86
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ utocode
33

44
it is only a storage some demos and documents are in it.
55

6-
6+
* cs - C# demo
7+
+ zlib for .NET
78
* recite - recite English words, python script in windows.
89
* windows - the source can be used only in the windows os.
910
+ crrunner chrome launcher to avoid to be rising hijack.

recite/install.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def copytree_f(s, d):
3131
'r.bat',
3232
'review.bat',
3333
's.bat',
34+
'zuohaitao_com_word.py',
3435
]
3536
for e in excutes:
3637
shutil.copy(e, out)

recite/recite.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
import sound
88
import datetime
99
import phonetic_with_bing as phonetic
10-
10+
import zuohaitao_com_word
11+
book = 'zuohaitao.com/word.html'
1112
def learning(unknowV):
13+
def all():
14+
for ww in unknowV:
15+
print ww['word'],
16+
print ''
1217
print ''
1318
print len(unknowV)
14-
for ww in unknowV:
15-
print ww['word'],
16-
print ''
19+
all()
1720
while True:
1821
i = raw_input('>')
19-
if i == ',quit':
22+
if i == '':
23+
continue
24+
if i == ',quit'or i == ',q':
2025
break
21-
elif i[0] == ',':
26+
if i == ',l':
27+
all()
28+
continue
29+
if i[0] == ',':
2230
for w in unknowV:
2331
if w['word'] == i[1:]:
2432
sound.sound(i[1:])
@@ -28,21 +36,38 @@ def learning(unknowV):
2836
print ''
2937

3038
def words(folder='vocabulary'):
39+
global book
40+
if -1 != book.find('zuohaitao.com/word.html'):
41+
book = zuohaitao_com_word.fetch()
42+
folder = os.path.join(book, folder)
43+
sound.sound_dir = os.path.join(book, 'sound')
3144
vocabulary = []
3245
for f in os.listdir(folder):
3346
if os.path.splitext(f)[1] != '.txt':
3447
continue
3548
with open(os.path.join(folder, f) , 'rb') as f:
3649
for line in f.readlines():
37-
word, t = line.split('\t ')
50+
if line == '\r\n':
51+
continue
52+
word = ''
53+
t = ''
54+
try:
55+
word, t = line.split('\t ')
56+
except:
57+
print f
58+
print line
3859
w = {}
3960
w['word'] = word
4061
w['translation'] = t
4162
sep = t.find('/ ')
4263
if sep != -1:
4364
w['phonetic'] = '/' + t[0:sep].replace('/', '') + '/'
44-
w['translation'] = t[sep+len('/ '):]
45-
vocabulary.append(w)
65+
try:
66+
w['translation'] = t[sep+len('/ '):].decode('utf8')
67+
except:
68+
w['translation'] = t[sep+len('/ '):]
69+
if w['word'] != '':
70+
vocabulary.append(w)
4671
return vocabulary
4772
def exit():
4873
print '\ndo you want to exit? (Y)es/(n)o'
@@ -121,12 +146,11 @@ def loop(v, save=True):
121146
if __name__ == '__main__':
122147
start = 0
123148
save = False
124-
if len(sys.argv) >= 1:
149+
if len(sys.argv) > 1:
125150
try:
126151
start = int(sys.argv[1])
127152
except:
128153
start = 0
129154
save = True
130-
131155
v = words()[start:]
132156
loop(v, save)

recite/zuohaitao_com_word.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import urllib
2+
import os
3+
import datetime
4+
import HTMLParser
5+
book = 'zuohaitao.com_word'
6+
def fetch():
7+
if not os.path.exists(book):
8+
os.mkdir(book)
9+
vocabulary = os.path.join(book, 'vocabulary')
10+
if not os.path.exists(vocabulary):
11+
os.mkdir(vocabulary)
12+
vocabulary_txt = os.path.join(vocabulary, str(datetime.date.today())) + '.txt'
13+
if os.path.isfile(vocabulary_txt):
14+
return book
15+
sound = os.path.join(book, 'sound')
16+
if not os.path.exists(sound):
17+
os.mkdir(sound)
18+
url = 'http://zuohaitao.com/word.html'
19+
word_html = os.path.join(book, str(datetime.date.today())) + '.html'
20+
response = ''
21+
if not os.path.isfile(word_html):
22+
response = urllib.urlopen(url).read()
23+
with open(word_html, 'wb') as f:
24+
f.write(response);
25+
else:
26+
with open(word_html, 'rb') as f:
27+
response = f.read()
28+
class Parser(HTMLParser.HTMLParser):
29+
def __init__(self):
30+
HTMLParser.HTMLParser.__init__(self)
31+
self.flag = False
32+
self.data = ''
33+
def handle_starttag(self, tag, attrs):
34+
if tag == 'pre':
35+
for key, value in attrs:
36+
if key == 'style' and value.find('display:none') != -1:
37+
self.flag = True
38+
break
39+
def handle_endtag(self, tag):
40+
self.flag = False
41+
def handle_data(self, data):
42+
if self.flag:
43+
self.data += data
44+
p = Parser()
45+
p.feed(response)
46+
with open(vocabulary_txt, 'wb') as f:
47+
f.write(p.data.replace(' /', '\t /'))
48+
return book

0 commit comments

Comments
 (0)