本文整理汇总了Python中openlibrary.api.OpenLibrary.save方法的典型用法代码示例。如果您正苦于以下问题:Python OpenLibrary.save方法的具体用法?Python OpenLibrary.save怎么用?Python OpenLibrary.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类openlibrary.api.OpenLibrary
的用法示例。
在下文中一共展示了OpenLibrary.save方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_cover_to_work
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
def add_cover_to_work(w):
if 'cover_edition' in w:
return
q = {'type':'/type/edition', 'works':w['key'], 'publish_date': None, 'languages': '/l/eng'}
cover_edition = pick_cover(query_iter(q))
if not cover_edition:
q = {'type':'/type/edition', 'works':w['key'], 'publish_date': None}
cover_edition = pick_cover(query_iter(q))
if not cover_edition:
return
w['cover_edition'] = Reference(cover_edition)
if ol is None:
rc = read_rc()
ol = OpenLibrary("http://openlibrary.org")
ol.login('WorkBot', rc['WorkBot'])
print ol.save(w['key'], w, 'added cover to work')
示例2: write_to_ol
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
def write_to_ol(olkey, oljson):
ol = OpenLibrary("http://openlibrary.org")
# Log in [daniel, sam]
logged_in = False
for attempt in range(5):
try:
ol.autologin()
logged_in = True
break
except:
print 'ol.autologin() error; retrying'
if not logged_in:
sys.exit('Failed to log in.')
ol.save(olkey, oljson, 'Adding Table of Contents')
示例3: range
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
ltid = row[0]
olid = row[1]
key = '/books' + olid[olid.rindex('/'):len(olid)]
c.execute('select * from ids where key = ?', (key,))
x = c.fetchone()
if x != None:
continue
print 'Trying to get key: %r' % key
for attempt in range(5):
try:
data = ol.get(key)
break
except:
print 'ol.get() error; retrying'
if data.has_key('identifiers'):
if data['identifiers'].has_key('librarything'):
if data['identifiers']['librarything'].count(ltid) == 0:
data['identifiers']['librarything'].append(ltid)
else:
data['identifiers']['librarything'] = [ltid]
else:
data['identifiers'] = {'librarything': [ltid]}
for attempt in range(5):
try:
print ol.save(key, data, 'added LibraryThing ID')
break
except:
print 'ol.save() error; retrying'
c.execute('insert into ids values (?, ?)', (key, ltid))
conn.commit()
示例4: OpenLibrary
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
from openlibrary.api import OpenLibrary
ol = OpenLibrary('http://openlibrary.org/')
data = eval(open('update').read())
done = []
for author in data:
print (author['key'], author['name'])
akey = author['key']
a = ol.get(akey)
if not a.get('bio') and author['bio']:
a['bio'] = author['bio']
ol.save(akey, a, 'Add author bio from Smashwords.')
for edition in author['editions']:
# wkey = ol.new({
# 'type': '/type/work',
# 'title': edition['title'],
# 'authors': [{'author': {'key': akey}}],
# 'description': edition['description'],
# 'subjects': ['Lending library'],
# })
#
wkey = edition['work']
w = ol.get(wkey)
assert edition['description']
if not w.get('description'):
w['description'] = edition['description']
if 'Lending library' not in w.get('subjects', []):
示例5: len
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
if do_updates:
ol.save_many(update, "add editions to new work")
work_keys.append(wkey)
elif len(existing) == 1:
key = list(existing)[0]
work_keys.append(key)
if work_title[key] == w['title']:
print 'no change:', key, `w['title']`
else:
print 'new title:', key, `work_title[key]`, '->', `w['title']`
print sorted(work_to_edition[key])
print w['editions']
existing_work = ol.get(key)
existing_work['title'] = w['title']
if do_updates:
ol.save(key, existing_work, 'update work title')
else:
use_key = min(existing, key=lambda i: int(re_work_key.match(i).group(1)))
work_keys.append(use_key)
print 'merge work:', key, `w['title']`, 'to', use_key
update = [{'type': '/type/redirect', 'location': use_key, 'key': wkey} for wkey in existing if wkey != use_key]
for wkey in existing:
for ekey in work_to_edition[wkey]:
e = ol.get(ekey)
e['works'] = [Reference(use_key)]
update.append(e)
if work_title[use_key] != w['title']:
print 'update work title', `work_title[use_key]`, '->', `w['title']`
existing_work = ol.get(use_key)
existing_work['title'] = w['title']
update.append(existing_work)
示例6: has
# 需要导入模块: from openlibrary.api import OpenLibrary [as 别名]
# 或者: from openlibrary.api.OpenLibrary import save [as 别名]
class VacuumBot:
"""VacuumBot can help clean up Open Library, just tell it what to do!
The VacuumBot has (its) methods to do specific cleanup tasks.
It needs the credentials of a bot account on Open Library and some instructions.
Naturally, it shows no mercy.
"""
def __init__(self, username, password):
"""Takes a username and password of a bot account to establish a connection to OL.
"""
self.ol = OpenLibrary()
self.ol.login(username, password)
self.pagreg = re.compile(r"[^\s]\s+[:;]$")
self.emptypagreg = re.compile(r"[,.:;]+$")
self.formatdict = simplejson.load(codecs.open("formatdict.json", "rb", "utf-8"))
self.enc2 = codecs.getencoder("ascii")
self.savebuffer = {}
self.badrecords = []
self.aucache = {}
self.wocache = {}
#self.formatcache = NKCache("ol_books_formats", api_key = "cfdeaeda-4a22-4ae7-a2bf-1634da98fa1b")
self.logfile = codecs.EncodedFile(open("vacuumbot-log.tsv", "ab"), "unicode_internal", "utf-8", "replace")
def enc(self, str):
return self.enc2(str, "backslashreplace")[0]
def flog(self, key, operation, message):
"""Log to file 'vacuumbot-log.tsv'. Lines are time, key, operation and message, tab-separated.
"""
self.logfile.write(unicode(strftime("%Y-%m-%d_%H:%M:%S", localtime()) + "\t" + key + "\t" + operation + "\t" + message + "\n"))
def save_error(self, key, message):
errorfile = codecs.EncodedFile(open("vacuumbot-errors.txt", "ab"), "unicode_internal", "utf-8", "replace")
errorfile.write(unicode("[" + strftime("%Y-%m-%d_%H:%M:%S", localtime()) + "] Could not save record for: " + key + ", error was: " + message + "\n"))
errorfile.close()
def query(self, query):
return self.ol.query(query)
def ol_save(self, key, record, message):
try:
self.ol.save(key, record, self.enc(message))
self.flog(key, "direct save", message)
print_log("Saved "+key+": "+message)
except OLError as e:
self.save_error(key, str(e))
print_log("Save failed: "+str(e))
def ol_save2(self, key, record, message):
if message != None:
record = marshal(record)
if message in self.savebuffer.keys():
self.savebuffer[message][key] = record
if len(self.savebuffer[message]) >= 100:
self.flush(message)
else:
self.savebuffer[message] = {}
self.savebuffer[message][key] = record
self.flog(key, "buffer save", message)
else:
raise Exception("Message for saving is missing!")
def flush(self, buffer_name):
try:
if len(self.savebuffer[buffer_name]) > 0:
self.ol.save_many(self.savebuffer[buffer_name].values(), self.enc(buffer_name))
for key in self.savebuffer[buffer_name].keys():
self.flog(key, "buffer flush", buffer_name)
print_log("Flushed buffer ("+str(len(self.savebuffer[buffer_name]))+" records): "+buffer_name)
self.savebuffer[buffer_name] = {}
sleep(1)
except OLError as e:
# Try to remove rejected record from buffer
err_mess = simplejson.loads(re.sub(r'^[^{]*', "", str(e)))
if err_mess["error"] == "bad_data":
k = err_mess["at"]["key"]
del self.savebuffer[buffer_name][k]
self.save_error(k, "Multisave failed: "+str(e)+"; removed record from buffer")
else:
k = self.savebuffer[buffer_name].keys()[0]
self.save_error(k, "Multisave failed: "+str(e))
def flush_all(self):
for m in self.savebuffer.keys():
self.flush(m)
def ol_get(self, key, v=None):
"""Gets a record from OL and catches OLErrors.
Make sure you check for None when you process this function's result.
"""
try:
return self.ol.get(key, v)
except OLError as e:
self.save_error(key, str(e))
print_log("Get failed: "+str(e))
def clean_author_dates(self):
#.........这里部分代码省略.........