Skip to content

Commit 420d0d9

Browse files
author
cclauss
authored
Support both Python 2 and Python 3
1 parent 13ad0ec commit 420d0d9

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

zh-cn.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
#!/usr/bin/env python
12
# -*- coding:utf8 -*-
23
"""
34
读取game-programmer.dot将其转换为中文版的game-programmer-zh-cn.dot
45
"""
5-
import json
6-
import re
7-
import urllib2
6+
from __future__ import print_function
87
import csv
8+
import json
99
import os
10+
import re
11+
try:
12+
from urllib.request import urlopen # Python 3
13+
except ImportError:
14+
from urllib2 import urlopen # Python 2
15+
1016

1117
IMAGE_PATH = './images-zh-cn/{book_index}.jpg'
1218

@@ -16,12 +22,12 @@ def get_image(isbn, image_filename):
1622
:rtype: str
1723
"""
1824
print("get_image(" + isbn + "," + isbn + ")")
19-
response = urllib2.urlopen(book_url)
25+
response = urlopen(book_url)
2026
html = response.read()
2127
re_image_url = r"https://img\d\.doubanio\.com/lpic/s\d*\.jpg"
2228
image_url = re.search(re_image_url, html).group()
2329
with open(image_filename, 'w') as ft:
24-
response = urllib2.urlopen(image_url)
30+
response = urlopen(image_url)
2531
image = response.read()
2632
ft.write(image)
2733

@@ -30,12 +36,12 @@ def get_book_url_year(isbn):
3036
url = "https://api.douban.com/v2/book/isbn/" + isbn
3137
result = "https://book.douban.com/", ""
3238
try:
33-
response = urllib2.urlopen(url)
39+
response = urlopen(url)
3440
detail = response.read()
3541
return json.loads(detail)["alt"], json.loads(detail)["pubdate"][:4]
3642
except Exception as e:
37-
print isbn
38-
print e
43+
print(isbn)
44+
print(e)
3945
return result
4046

4147
def get_book_info(book_index):
@@ -161,25 +167,25 @@ def get_book_info(book_index):
161167
label_line_match = RE_LABEL_LINE.match(line_without_space)
162168
content_line_match = RE_CONTENT_LINE.match(line_without_space)
163169

164-
if book_line_match != None:
170+
if book_line_match is not None:
165171
book_index = line_without_space.split(" ")[0]
166172
book_title, book_url, book_year = get_book_info(book_index.strip('"'))
167-
if book_title == None:
173+
if book_title is None:
168174
zh_f.write(line)
169175
else:
170176
image_path = IMAGE_PATH.format(book_index=book_index.strip('"'))
171177
writeline = space_front+ BOOK_LINE.format(book_index=book_index, image_path=image_path, book_title=book_title, book_year=book_year, url=book_url)
172178
zh_f.write(writeline)
173-
elif section_line_match != None:
179+
elif section_line_match is not None:
174180
sectionID = re.search(r'\d+\.', line_without_space).group()
175181
writeline = space_front + SECTION_LINE.format(section_title=SECTION_TITLE_DICT[sectionID])
176182
zh_f.write(writeline)
177-
elif label_line_match != None:
183+
elif label_line_match is not None:
178184
label_index = label_line_match.group().split(' ')[0]
179185
en_label_content = re.search(r'label="[\w -=\./\\]*"', line_without_space).group()[7:-1]
180186
writeline = space_front + LABEL_LINE.format(label_index=label_index, label=LABEL_DICT[en_label_content])
181187
zh_f.write(writeline)
182-
elif content_line_match != None:
188+
elif content_line_match is not None:
183189
sectionID = line_without_space.split('.')[0][37:] + '.'
184190
section_title =SECTION_TITLE_DICT[sectionID]
185191
if '<' in line_without_space:

0 commit comments

Comments
 (0)