def test_urlsafe(): assert h.urlsafe("a b") == "a_b" assert h.urlsafe("a?b") == "a_b" assert h.urlsafe("a?&b") == "a_b" assert h.urlsafe("?a") == "a" assert h.urlsafe("a?") == "a"
def ebook(doc): itemid = doc['ocaid'] availability = get_ia_availability(itemid) d = { "preview_url": "https://archive.org/details/" + itemid, "availability": availability, "formats": {} } prefix = "https://archive.org/download/%s/%s" % (itemid, itemid) if availability == 'full': d["read_url"] = "https://archive.org/stream/%s" % (itemid) d['formats'] = { "pdf": { "url": prefix + ".pdf" }, "epub": { "url": prefix + ".epub" }, "text": { "url": prefix + "_djvu.txt" } } elif availability == "borrow": d['borrow_url'] = u"https://openlibrary.org%s/%s/borrow" % ( doc['key'], h.urlsafe(doc.get("title", "untitled"))) loanstatus = web.ctx.site.store.get('ebooks/' + doc['ocaid'], {'borrowed': 'false'}) d['checkedout'] = (loanstatus['borrowed'] == 'true') return d
def get_readable_path(site, path, patterns, encoding=None): """Returns real_path and readable_path from the given path. The patterns is a list of (path_regex, type, property_name, default_value) tuples. """ def match(path): for pat, type, property, default_title in patterns: m = web.re_compile('^' + pat).match(path) if m: prefix = m.group() extra = web.lstrips(path, prefix) tokens = extra.split("/", 2) # `extra` starts with "/". So first token is always empty. middle = web.listget(tokens, 1, "") suffix = web.listget(tokens, 2, "") if suffix: suffix = "/" + suffix return type, property, default_title, prefix, middle, suffix return None, None, None, None, None, None type, property, default_title, prefix, middle, suffix = match(path) if type is None: path = web.safeunicode(path) return (path, path) if encoding is not None \ or path.endswith(".json") or path.endswith(".yml") or path.endswith(".rdf"): key, ext = os.path.splitext(path) thing = _get_object(site, key) if thing: path = thing.key + ext path = web.safeunicode(path) return (path, path) thing = _get_object(site, prefix) # get_object may handle redirections. if thing: prefix = thing.key if thing and thing.type.key == type: title = thing.get(property) or default_title middle = '/' + h.urlsafe(title.strip()) else: middle = "" if is_exclusion(thing): web.ctx.exclude = True prefix = web.safeunicode(prefix) middle = web.safeunicode(middle) suffix = web.safeunicode(suffix) return (prefix + suffix, prefix + middle + suffix)
def get_readable_path(site, path, patterns, encoding=None): """Returns real_path and readable_path from the given path. The patterns is a list of (path_regex, type, property_name, default_value) tuples. """ def match(path): for pat, type, property, default_title in patterns: m = web.re_compile('^' + pat).match(path) if m: prefix = m.group() extra = web.lstrips(path, prefix) tokens = extra.split("/", 2) # `extra` starts with "/". So first token is always empty. middle = web.listget(tokens, 1, "") suffix = web.listget(tokens, 2, "") if suffix: suffix = "/" + suffix return type, property, default_title, prefix, middle, suffix return None, None, None, None, None, None type, property, default_title, prefix, middle, suffix = match(path) if type is None: path = web.safeunicode(path) return (path, path) if encoding is not None \ or path.endswith(".json") or path.endswith(".yml") or path.endswith(".rdf"): key, ext = os.path.splitext(path) thing = _get_object(site, key) if thing: path = thing.key + ext path = web.safeunicode(path) return (path, path) thing = _get_object(site, prefix) # get_object may handle redirections. if thing: prefix = thing.key if thing and thing.type.key == type: title = thing.get(property) or default_title middle = '/' + h.urlsafe(title.strip()) else: middle = "" prefix = web.safeunicode(prefix) middle = web.safeunicode(middle) suffix = web.safeunicode(suffix) return (prefix + suffix, prefix + middle + suffix)
def _make_url(self, label, suffix, relative=True, **params): """Make url of the form $key/$label$suffix?$params.""" if label is not None: u = self.key + "/" + h.urlsafe(label) + suffix else: u = self.key + suffix if params: u += '?' + urllib.parse.urlencode(params) if not relative: u = _get_ol_base_url() + u return u
def get_readitem(self, iaid, orig_iaid, orig_ekey, wkey, status, publish_date): meta = self.iaid_to_meta.get(iaid) if meta is None: return None collections = meta.get("collection", []) if status == 'missing': return None if (status.startswith('restricted') or status == 'checked out') and not self.options.get('show_all_items'): return None edition = self.iaid_to_ed.get(iaid) ekey = edition.get('key', '') if status == 'full access': itemURL = 'http://www.archive.org/stream/%s' % (iaid) else: # this could be rewrit in terms of iaid... itemURL = u'http://openlibrary.org%s/%s/borrow' % ( ekey, helpers.urlsafe(edition.get('title', 'untitled'))) result = { # XXX add lastUpdate 'enumcron': False, 'match': 'exact' if iaid == orig_iaid else 'similar', 'status': status, 'fromRecord': orig_ekey, 'ol-edition-id': key_to_olid(ekey), 'ol-work-id': key_to_olid(wkey), 'publishDate': publish_date, 'contributor': '', 'itemURL': itemURL, } if edition.get('covers'): cover_id = edition['covers'][0] # can be rewrit in terms of iaid # XXX covers url from yaml? result['cover'] = { "small": "http://covers.openlibrary.org/b/id/%s-S.jpg" % cover_id, "medium": "http://covers.openlibrary.org/b/id/%s-M.jpg" % cover_id, "large": "http://covers.openlibrary.org/b/id/%s-L.jpg" % cover_id, } return result
def get_readitem(self, iaid, orig_iaid, orig_ekey, wkey, status, publish_date): meta = self.iaid_to_meta.get(iaid) if meta is None: return None collections = meta.get("collection", []) if status == 'missing': return None if (status.startswith('restricted') or status == 'checked out') and not self.options.get('show_all_items'): return None edition = self.iaid_to_ed.get(iaid) ekey = edition.get('key', '') if status == 'full access': itemURL = 'http://www.archive.org/stream/%s' % (iaid) else: # this could be rewrit in terms of iaid... itemURL = u'http://openlibrary.org%s/%s/borrow' % (ekey, helpers.urlsafe(edition.get('title', 'untitled'))) result = { # XXX add lastUpdate 'enumcron': False, 'match': 'exact' if iaid == orig_iaid else 'similar', 'status': status, 'fromRecord': orig_ekey, 'ol-edition-id': key_to_olid(ekey), 'ol-work-id': key_to_olid(wkey), 'publishDate': publish_date, 'contributor': '', 'itemURL': itemURL, } if edition.get('covers'): cover_id = edition['covers'][0] # can be rewrit in terms of iaid # XXX covers url from yaml? result['cover'] = { "small": "http://covers.openlibrary.org/b/id/%s-S.jpg" % cover_id, "medium": "http://covers.openlibrary.org/b/id/%s-M.jpg" % cover_id, "large": "http://covers.openlibrary.org/b/id/%s-L.jpg" % cover_id, } return result
def ebook(doc): itemid = doc['ocaid'] availability = get_ia_availability(itemid) d = { "preview_url": "http://www.archive.org/details/" + itemid, "availability": availability } prefix = "http://www.archive.org/download/%s/%s" % (itemid, itemid) if availability == 'full': d["read_url"] = "http://www.archive.org/stream/%s" % (itemid) d['formats'] = { "pdf": { "url": prefix + ".pdf" }, "epub": { "url": prefix + ".epub" }, "text": { "url": prefix + "_djvu.txt" }, "djvu": { "url": prefix + ".djvu", "permission": "open" } } elif availability == "borrow": d['borrow_url'] = u"http://openlibrary.org%s/%s/borrow" % ( doc['key'], h.urlsafe(doc.get("title", "untitled"))) d['formats'] = { "djvu": { "url": prefix + ".djvu", "permission": "restricted" } } else: d['formats'] = { "djvu": { "url": prefix + ".djvu", "permission": "restricted" } } return d
def ebook(doc): itemid = doc['ocaid'] availability = get_ia_availability(itemid) d = { "preview_url": "http://www.archive.org/details/" + itemid, "availability": availability } prefix = "http://www.archive.org/download/%s/%s" % (itemid, itemid) if availability == 'full': d["read_url"] = "http://www.archive.org/stream/%s" % (itemid) d['formats'] = { "pdf": { "url": prefix + ".pdf" }, "epub": { "url": prefix + ".epub" }, "text": { "url": prefix + "_djvu.txt" }, "djvu": { "url": prefix + ".djvu", "permission": "open" } } elif availability == "borrow": d['borrow_url'] = u"http://openlibrary.org%s/%s/borrow" % (doc['key'], h.urlsafe(doc.get("title", "untitled"))) d['formats'] = { "djvu": { "url": prefix + ".djvu", "permission": "restricted" } } else: d['formats'] = { "djvu": { "url": prefix + ".djvu", "permission": "restricted" } } return d
def ebook(doc): itemid = doc['ocaid'] availability = get_ia_availability(itemid) d = { "preview_url": "https://archive.org/details/" + itemid, "availability": availability, "formats": {} } prefix = "https://archive.org/download/%s/%s" % (itemid, itemid) if availability == 'full': d["read_url"] = "https://archive.org/stream/%s" % (itemid) d['formats'] = { "pdf": { "url": prefix + ".pdf" }, "epub": { "url": prefix + ".epub" }, "text": { "url": prefix + "_djvu.txt" } } elif availability == "borrow": d['borrow_url'] = u"https://openlibrary.org%s/%s/borrow" % (doc['key'], h.urlsafe(doc.get("title", "untitled"))) loanstatus = web.ctx.site.store.get('ebooks/' + doc['ocaid'], {'borrowed': 'false'}) d['checkedout'] = (loanstatus['borrowed'] == 'true') return d
def get_readable_edition_item(edition, work, user_inlibrary, initial_edition): ocaid = edition.get('ocaid', False) if not ocaid: return None subjects = work.get_subjects() if not subjects: return None metaxml = ia.get_meta_xml(ocaid) collections = metaxml.get("collection", []) status = '' if 'lendinglibrary' in collections: if not 'Lending library' in subjects: return None status = 'lendable' elif 'inlibrary' in collections: if not 'In library' in subjects: return None if not user_inlibrary: return None status = 'lendable' elif 'printdisabled' in collections: status = 'restricted' return None else: status = 'full access' if status == 'full access': itemURL = "http://www.archive.org/stream/%s" % (ocaid) else: itemURL = u"http://openlibrary.org%s/%s/borrow" % ( edition['key'], helpers.urlsafe(edition.get("title", "untitled"))) if status == 'lendable': loanstatus = web.ctx.site.store.get('ebooks' + edition['key'], {'borrowed': 'false'}) if loanstatus['borrowed'] == 'true': status = 'checked out' if edition['key'] == initial_edition['key']: match = 'exact' else: match = 'similar' result = { 'enumcron': False, # 'orig': 'University of California' # 'htid': '' # 'lastUpdate: "" # XXX from edition.last_modified (datetime) 'match': match, 'status': status, 'fromRecord': initial_edition['key'], 'ol-edition-id': key_to_olid(edition['key']), 'ol-work-id': key_to_olid(work['key']), 'contributor': 'contributor', 'itemURL': itemURL, } if edition.get('covers'): cover_id = edition['covers'][0] # XXX covers url from yaml? result['cover'] = { "small": "http://covers.openlibrary.org/b/id/%s-S.jpg" % cover_id, "medium": "http://covers.openlibrary.org/b/id/%s-M.jpg" % cover_id, "large": "http://covers.openlibrary.org/b/id/%s-L.jpg" % cover_id, } return result
def get_readable_edition_item(edition, work, user_inlibrary, initial_edition): ocaid = edition.get('ocaid', False) if not ocaid: return None subjects = work.get_subjects() if not subjects: return None metaxml = ia.get_meta_xml(ocaid) collections = metaxml.get("collection", []) status = '' if 'lendinglibrary' in collections: if not 'Lending library' in subjects: return None status = 'lendable' elif 'inlibrary' in collections: if not 'In library' in subjects: return None if not user_inlibrary: return None status = 'lendable' elif 'printdisabled' in collections: status = 'restricted' return None else: status = 'full access' if status == 'full access': itemURL = "http://www.archive.org/stream/%s" % (ocaid) else: itemURL = u"http://openlibrary.org%s/%s/borrow" % (edition['key'], helpers.urlsafe(edition.get("title", "untitled"))) if status == 'lendable': loanstatus = web.ctx.site.store.get('ebooks' + edition['key'], {'borrowed': 'false'}) if loanstatus['borrowed'] == 'true': status = 'checked out' if edition['key'] == initial_edition['key']: match = 'exact' else: match = 'similar' result = { 'enumcron': False, # 'orig': 'University of California' # 'htid': '' # 'lastUpdate: "" # XXX from edition.last_modified (datetime) 'match': match, 'status': status, 'fromRecord': initial_edition['key'], 'ol-edition-id': key_to_olid(edition['key']), 'ol-work-id': key_to_olid(work['key']), 'contributor': 'contributor', 'itemURL': itemURL, } if edition.get('covers'): cover_id = edition['covers'][0] # XXX covers url from yaml? result['cover'] = { "small": "http://covers.openlibrary.org/b/id/%s-S.jpg" % cover_id, "medium": "http://covers.openlibrary.org/b/id/%s-M.jpg" % cover_id, "large": "http://covers.openlibrary.org/b/id/%s-L.jpg" % cover_id, } return result
def get_readable_path(site, path, patterns, encoding=None): """Returns real_path and readable_path from the given path. The patterns is a list of (path_regex, type, property_name, default_value) tuples. """ def match(path): for pat, type, property, default_title in patterns: m = web.re_compile('^' + pat).match(path) if m: prefix = m.group() extra = web.lstrips(path, prefix) tokens = extra.split("/", 2) # `extra` starts with "/". So first token is always empty. middle = web.listget(tokens, 1, "") suffix = web.listget(tokens, 2, "") if suffix: suffix = "/" + suffix return type, property, default_title, prefix, middle, suffix return None, None, None, None, None, None type, property, default_title, prefix, middle, suffix = match(path) if type is None: path = web.safeunicode(path) return (path, path) if encoding is not None \ or path.endswith(".json") or path.endswith(".yml") or path.endswith(".rdf"): key, ext = os.path.splitext(path) thing = _get_object(site, key) if thing: path = thing.key + ext path = web.safeunicode(path) return (path, path) thing = _get_object(site, prefix) # XXX For a 1-week trial ~Oct 23 2019 # if ?edition qualifier present on work url, redirect patron to # "best" representative edition if thing.key.startswith('/works/'): i = web.input(edition="") if i.edition: ed = thing.get_representative_edition() if ed: raise web.seeother(ed) # get_object may handle redirections. if thing: prefix = thing.key if thing and thing.type.key == type: title = thing.get(property) or default_title middle = '/' + h.urlsafe(title.strip()) else: middle = "" prefix = web.safeunicode(prefix) middle = web.safeunicode(middle) suffix = web.safeunicode(suffix) return (prefix + suffix, prefix + middle + suffix)