/
get_info.py
212 lines (172 loc) · 5.57 KB
/
get_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Get book information accroding to isbn
import sys
import requests
import json, yaml
import os
import mimetypes
import pgdb
def get_book_info_from_isbndb(isbn13):
"""
Get book information from isbndb, using canonical isbn13
"""
base_url = 'http://isbndb.com/api/v2/yaml/'
api_key = 'EF2FC19S'
full_url = base_url + api_key + '/book/%d' % isbn13
ret = requests.get(full_url)
data = yaml.load(ret.text)
if data == {}: return None
# Add author info
result = {}
author_list = [i['name'] for i in data['data'][0]['author_data']]
authors = "{%s}" % ','.join(['"'+i+'"' for i in author_list])
result['authors'] = authors
# Add remained fields
map_list = {'isbn10': ["'data'", "0", "'isbn10'"],
'language': ["'data'", "0", "'language'"],
'publishers': ["'data'", "0", "'publisher_name'"],
'title': ["'data'", "0", "'title'"],
'subtitle': ["'data'", "0", "'subtitle'"],
'title_long': ["'data'", "0", "'title_long'"],
'book_id': ["'data'", "0", "'book_id'"],
'subject_id': ["'data'", "0", "'subject_ids'", 0]};
for k in map_list.keys():
try :
index = ''.join(['['+i+']' for i in map_list[k]])
exec """result[k] = data%s""" % index
except (TypeError, KeyError):
pass
return result
#print get_book_info_from_isbndb(9782705656751)
def get_book_info_from_openlib(isbn13):
"""
Get book information from Open Libary, according to canonical isbn13.
"""
base_url = 'http://openlibrary.org/api/books'
query_string = '?bibkeys=ISBN:%d&jscmd=details&format=json' % isbn13
full_url = base_url + query_string
ret = requests.get(full_url)
data = json.loads(ret.text)
if data == {} : return None
data = data['ISBN:%d' % isbn13]
result = {}
# Add author info
author_list = [i['name'] for i in data['details']['authors']]
authors = "{%s}" % ','.join(['"'+i+'"' for i in author_list])
result['authors'] = authors
# Add remined field
map_list = {'isbn10': ["'details'", "'isbn_10'", "0"],
'thumbnail_url': ["'thumbnail_url'"],
'latest_version': ["'details'", "'latest_version'"],
'pages': ["'details'", "'number_of_pages'"],
'publish_date': ["'details'", "'publish_date'"],
'publishers': ["'details'", "'publishers'", "0"],
'version': ["'details'", "'revision'"],
'title': ["'details'", "'title'"],
'subtitle' : ["'details'", "'subtitle'"] }
for k in map_list.keys():
try :
index = ''.join(['['+i+']' for i in map_list[k]])
exec """result[k] = data%s""" % index
except (TypeError, KeyError):
pass
return result
#print get_book_info_from_openlib(9780201142365)
def add_book_info(isbn13, book_info):
"""
Add book information to PostgreSQL server,
isbn13 is canonical form of 13-digit ISBN number;
book_info is got through methods listed above.
"""
if book_info == None: return False
session = pgdb.connect(user='hawk', password='hawk', database='dist')
cursor = session.cursor()
# Add book record if ISBN13(primary key) does not exist.
sql = '''SELECT book_info FROM book_info WHERE isbn13=%d''' % isbn13
cursor.execute(sql)
f = cursor.fetchall()
if f == [] :
cursor.execute('''INSERT INTO book_info(isbn13)
VALUES ('%d')''' % isbn13)
# Add book info
for i in book_info.keys():
sql = u'''UPDATE book_info SET %s='%s'
WHERE isbn13='%s' ''' % (i, book_info[i], isbn13)
cursor.execute(sql)
cursor.close()
session.commit()
session.close()
print "Done."
#isbn13 = 9782705656751
#add_book_info(isbn13, get_book_info_from_isbndb(isbn13))
#isbn13 = 9780201142365
#add_book_info(isbn13, get_book_info_from_openlib(isbn13))
def isbn10to13(isbn10):
"""
Convert isbn10 to isbn13 canonical form.
"""
# string to integer
isbn10 = int(isbn10[0:9])
# calculate checksum
std = isbn10+978*(10**9)
digits = [std/10**i - 10*(std/(10**(i+1))) for i in range(0,12)]
digits.reverse()
weight = [digits[i]*(1 if i%2==0 else 3) for i in range(0,len(digits))]
totals = sum(weight)
reminder = totals - 10*(totals/10)
checked = 0 if reminder==10 else 10-reminder
digits.append(checked)
isbn13 = sum([digits[12-i]*10**i for i in range(0,13)])
return isbn13
def ensure_isbn13(isbn):
"""
Ensure isbn13 canonical form.
"""
try :
if type(isbn) == unicode or type(isbn) == str:
isbn = ''.join(isbn.split('-'))
if len(isbn) == 10 :
isbn = isbn10to13(isbn)
isbn = int(isbn)
except: # parsing failed
return False
if type(isbn) != int: return False
return isbn
def get_book_data(path, isbn):
isbn13 = ensure_isbn13(isbn)
uri = os.path.abspath(os.path.expanduser(path))
if not os.path.isfile(uri):
return False
name = os.path.basename(uri)
size = os.path.getsize(uri)
mime = mimetypes.guess_type(uri)[0]
mime = mime if mimetypes != None else ''
data = {'isbn':isbn13, 'name':name, 'mime':mime, 'size':size, 'uri':uri}
return data
#isbn = '123456789X'
#print get_book_data('~/file/html/www.unicode.org/CodeCharts.pdf', isbn)
def add_book_data(path, isbn):
"""
Add local book data to PostgreSQL server.
"""
data = get_book_data(path=path, isbn=isbn)
if not data : return False
session = pgdb.connect(user='hawk', password='hawk', database='dist')
cursor = session.cursor()
# what if previous record already exists?
sql = '''INSERT INTO book_data(isbn,name,mime,size,uri,data)
VALUES(%(isbn)d, '%(name)s', '%(mime)s',
%(size)d, '%(uri)s', lo_import('%(uri)s') )''' % data
try :
cursor.execute(sql)
except pgdb.DatabaseError:
return False
cursor.close()
session.commit()
session.close()
print "Done"
return True
isbn= sys.argv[1]
print get_book_info_from_openlib(ensure_isbn13(isbn))
#print add_book_data('~/file/html/www.unicode.org/CodeCharts.pdf', isbn)