def fixup(self, os): """ strip marc subfields, add social media hints and facet links """ for e in os.entries: if '$' in e.title: e.title = DublinCore.strip_marc_subfields(e.title) if (os.sort_order == 'release_date' and os.total_results > 0 and os.start_index == 1): cat = BaseSearcher.Cat() cat.title = _('Follow new books on Twitter') cat.subtitle = _("Follow our new books on Twitter.") cat.url = 'https://twitter.com/gutenberg_new' cat.class_ += 'navlink grayed' cat.icon = 'twitter' cat.order = 5 os.entries.insert(0, cat) cat = BaseSearcher.Cat() cat.title = _('Follow new books on Facebook') cat.subtitle = _( "Follow the link and like the page to have us post new books to your wall." ) cat.url = 'https://www.facebook.com/gutenberg.new' cat.class_ += 'navlink grayed' cat.icon = 'facebook' cat.order = 5 os.entries.insert(0, cat) if (len(os.query) and os.start_index == 1): sql2 = BaseSearcher.SQLStatement() sql2.query = "select count (*) from bookshelves" sql2.fulltext('bookshelves.tsvec', os.query) rows = BaseSearcher.SQLSearcher.execute(*sql2.build()) if rows[0][0] > 0: cat = BaseSearcher.Cat() cat.rel = 'related' cat.title = _('Bookshelves') cat.subtitle = __('One bookshelf matches your query.', '{count} bookshelves match your search.', rows[0][0]).format(count=rows[0][0]) cat.url = os.url('bookshelf_search', query=os.query) cat.class_ += 'navlink grayed' cat.icon = 'bookshelf' cat.order = 3 os.entries.insert(0, cat) sql2 = BaseSearcher.SQLStatement() sql2.query = "select count (*) from subjects" sql2.fulltext('subjects.tsvec', os.query) rows = BaseSearcher.SQLSearcher.execute(*sql2.build()) if rows[0][0] > 0: cat = BaseSearcher.Cat() cat.rel = 'related' cat.title = _('Subjects') cat.subtitle = __( 'One subject heading matches your search.', '{count} subject headings match your search.', rows[0][0]).format(count=rows[0][0]) cat.url = os.url('subject_search', query=os.query) cat.class_ += 'navlink grayed' cat.icon = 'subject' cat.order = 3 os.entries.insert(0, cat) sql2 = BaseSearcher.SQLStatement() sql2.query = "select count (*) from authors" sql2.fulltext('authors.tsvec', os.query) rows = BaseSearcher.SQLSearcher.execute(*sql2.build()) if rows[0][0] > 0: cat = BaseSearcher.Cat() cat.rel = 'related' cat.title = _('Authors') cat.subtitle = __('One author name matches your search.', '{count} author names match your search.', rows[0][0]).format(count=rows[0][0]) cat.url = os.url('author_search', query=os.query) cat.class_ += 'navlink grayed' cat.icon = 'author' cat.order = 3 os.entries.insert(0, cat)
def format_downloads(row): """ Format the no. of download for display in results. """ downloads = int(row.get('downloads', 0)) # NOTE: No. of times a book was downloaded return __('1 download', '{0} downloads', downloads).format(downloads)
def format_quantity(row): """ Format the quantity of books for display in results. """ count = int(row.get('quantity', 0)) # NOTE: No. of books by some author, on a subject, etc. return __('1 book', '{0} books', count).format(count)
def __init__(self): self.format = None self.page = None self.template = None self.query = None self.id = None self.sort_order = None self.search_terms = None self.start_index = 1 self.items_per_page = 1 self.total_results = -1 self.page_mode = 'screen' self.user_dialog = ('', '') self.opensearch_support = 0 # 0 = none, 1 = full, 2 = fake(Stanza, Aldiko, ...) self.books_in_archive = babel.numbers.format_number( books_in_archive, locale=str(cherrypy.response.i18n.locale)) self.breadcrumbs = [ (_('Project Gutenberg'), _('Go to the Main page.'), '/'), (__('1 free ebook', '{count} free ebooks', books_in_archive).format(count=self.books_in_archive), _('Start a new search.'), '/ebooks/'), ] # default output formatting functions self.f_format_title = self.format_title self.f_format_subtitle = self.format_author self.f_format_extra = self.format_none # depends on sort order, set in fix_sortorder () self.f_format_url = self.format_bibrec_url self.f_format_thumb_url = self.format_thumb_url self.f_format_icon = self.format_icon # icon class self.user_agent = cherrypy.request.headers.get('User-Agent', '') cherrypy.request.os = self s = cherrypy.session k = cherrypy.request.params host = cherrypy.request.headers.get('X-Forwarded-Host', cherrypy.config['host']) self.host = host.split(',')[-1].strip() # keep only the last hub # turns out X-Forwarded-Protocol (X-Forwarded-Proto is the defacto standaard) # is not a thing and has to be set in HAProxy self.protocol = cherrypy.request.headers.get('X-Forwarded-Protocol', 'https') # sanity check if self.host not in (cherrypy.config['all_hosts']): self.host = cherrypy.config['host'] if self.protocol not in VALID_PROTOCOLS: self.protocol = 'https' self.urlgen = routes.URLGenerator(cherrypy.routes_mapper, {'HTTP_HOST': self.host}) self.set_format(k.get('format')) # query: this param is set when an actual query is requested self.query = '' if 'query' in k: self.query = SQLStatement.preprocess_query(k['query']) # search_terms: this is used to carry the last query # to display in the search input box self.search_terms = self.query or s.get('search_terms', '') self.sort_order = k.get('sort_order') or s.get( 'sort_order') or USER_SORT_ORDERS[0] if self.sort_order not in USER_SORT_ORDERS: raise cherrypy.HTTPError(400, 'Bad Request. Unknown sort order.') s['sort_order'] = self.sort_order try: self.id = int(k.get('id') or '0') self.start_index = int(k.get('start_index') or '1') self.items_per_page = min(100, int(k.get('items_per_page') or '25')) except ValueError as what: raise cherrypy.HTTPError(400, 'Bad Request. ' + str(what)) self.file_host = cherrypy.config['file_host'] self.now = datetime.datetime.utcnow().replace( microsecond=0).isoformat() + 'Z' self.do_animations = 'Kindle/' not in self.user_agent # no animations on e-ink self.ip = cherrypy.request.remote.ip self.type_opds = 'application/atom+xml;profile=opds-catalog' self.base_url = None self.canonical_url = None self.entries = [] # NOTE: For page titles etc. self.pg = self.title = _('Project Gutenberg') # NOTE: The tagline at the top of every page. self.tagline = _( 'Project Gutenberg offers {count} free ebooks to download.' ).format(count=self.books_in_archive) # NOTE: The site's description in the html meta tags. self.description = _( 'Project Gutenberg offers {count} free ebooks for ' 'Kindle, iPad, Nook, Android, and iPhone.').format( count=self.books_in_archive) # NOTE: The placeholder inside an empty search box. self.placeholder = _('Search Project Gutenberg.') # these need to be here because they have to be localized # NOTE: Msg to user indicating the order of the search results. self.sorted_msgs = { 'downloads': _("sorted by popularity"), 'release_date': _("sorted by release date"), 'quantity': _("sorted by quantity of books"), 'title': _("sorted alphabetically"), 'alpha': _("sorted alphabetically by title"), 'author': _("sorted alphabetically by author"), 'nentry': _("sorted by relevance"), 'random': _("in random order"), } self.snippet_image_url = self.url('/pics/logo-144x144.png', host=self.file_host) self.og_type = 'website' self.class_ = ClassAttr() self.title_icon = None self.icon = None self.sort_orders = [] self.alternate_sort_orders = [] lang = self.lang = s.get('_lang_', 'en_US') if len(lang) == 2: lang = self.lang_to_default_locale.get(lang, 'en_US') lang2 = self.lang[:2] self.paypal_lang = lang if lang in PAYPAL_LANGS else 'en_US' self.flattr_lang = lang if lang in FLATTR_LANGS else 'en_US' lang = lang.replace('_', '-') self.google_lang = lang if lang in GOOGLE_LANGS else ( lang2 if lang2 in GOOGLE_LANGS else 'en-US') lang = lang.lower() self.twitter_lang = lang if lang in TWITTER_LANGS else ( lang2 if lang2 in TWITTER_LANGS else 'en') self.viewport = "width=device-width" # , initial-scale=1.0" self.touch_icon = '/gutenberg/apple-icon.png' self.touch_icon_precomposed = None # not yet used if 'user_dialog' in s: self.user_dialog = s['user_dialog'] del s['user_dialog'] msg = k.get('msg') if msg is not None: if msg == 'welcome_stranger': self.user_dialog = (_( "Welcome to Project Gutenberg. " "You'll find here {count} ebooks completely free of charge." ).format(count=self.books_in_archive), _('Welcome'))