def index(self, **kwargs):
        """ Output stats. """

        backends = int(
            BaseSearcher.sql_get("SELECT count (*) from pg_stat_activity"))
        active_backends = int(
            BaseSearcher.sql_get(
                "SELECT count (*) - 1 from pg_stat_activity where current_query !~ '^<IDLE>'"
            ))

        ipsessions = list(cherrypy.tools.rate_limiter.cache.values())  # pylint: disable=E1101

        adns = asyncdns.AsyncDNS()

        # blocked IPs
        blocked = sorted([s for s in ipsessions if s.get('blocked', 0) >= 2],
                         key=lambda s: s.ips.sort_key())
        if 'resolve' in kwargs:
            for d in blocked:
                if d.ips.ipinfo is None:
                    d.ips.ipinfo = ipinfo.IPInfo(adns, d.ips.get_ip_to_block())

        # active IPs
        active = sorted([s for s in ipsessions if s.get('active', False)],
                        key=lambda s: s.ips.sort_key())

        # busiest IPs
        busiest = sorted([s for s in active if s.get('blocked', 0) < 2],
                         key=lambda x: -x.get('rhits'))[:10]
        if 'resolve' in kwargs:
            for d in busiest:
                if d.ips.ipinfo is None:
                    d.ips.ipinfo = ipinfo.IPInfo(adns, d.ips.get_ip_to_block())

        # IPs with most sessions
        most_sessions = sorted([
            s for s in active if not s.ips.whitelisted and len(s.sessions) > 1
        ],
                               key=lambda s: -len(s.sessions))[:10]
        if 'resolve' in kwargs:
            for d in most_sessions:
                if d.ips.ipinfo is None:
                    d.ips.ipinfo = ipinfo.IPInfo(adns, d.ips.get_ip_to_block())

        adns.wait()
        adns.cancel()

        return self.output(
            'stats',
            active=active,
            blocked=blocked,
            busiest=busiest,
            most_sessions=most_sessions,
            resolve='resolve' in kwargs,
            rl=cherrypy.tools.rate_limiter,  # pylint: disable=E1101
            backends=backends,
            active_backends=active_backends)
Example #2
0
    def tick(self):
        """ Do things here. """

        try:
            BaseSearcher.books_in_archive = BaseSearcher.sql_get(
                'select count (*) from books')
        except:
            pass
Example #3
0
    def setup(self, os, sql):
        os.sort_orders = ('downloads', 'title', 'author', 'release_date')
        os.title_icon = 'bookshelf'
        os.icon = 'book'
        os.class_ += 'booklink'
        os.f_format_icon = os.format_icon_titles
        os.bookshelf = BaseSearcher.sql_get(
            "select bookshelf from bookshelves where pk = %(pk)s", pk=os.id)
        os.title = _('Books in {bookshelf}').format(bookshelf=os.bookshelf)

        sql.from_.append('mn_books_bookshelves as mn')
        sql.where.append('books.pk = mn.fk_books')
        sql.where.append("mn.fk_bookshelves = %(fk_bookshelves)s")
        sql.params['fk_bookshelves'] = os.id
Example #4
0
    def setup(self, os, sql):
        os.sort_orders = ('downloads', 'title', 'release_date')
        os.title_icon = 'subject'
        os.icon = 'book'
        os.class_ += 'booklink'
        os.f_format_icon = os.format_icon_titles
        os.subject = BaseSearcher.sql_get(
            "select subject from subjects where pk = %(pk)s", pk=os.id)
        os.title = _('Books about {subject}').format(subject=os.subject)

        sql.from_.append('mn_books_subjects as mn')
        sql.where.append('books.pk = mn.fk_books')
        sql.where.append("mn.fk_subjects = %(fk_subjects)s")
        sql.params['fk_subjects'] = os.id
Example #5
0
    def setup(self, os, sql):
        os.sort_orders = ('downloads', 'title', 'release_date')
        os.title_icon = 'author'
        os.icon = 'book'
        os.class_ += 'booklink'
        os.f_format_icon = os.format_icon_titles
        os.author = BaseSearcher.sql_get(
            "select author from authors where pk = %(pk)s", pk=os.id)
        os.title = _('Books by {author}').format(author=os.author)

        sql.from_.append('mn_books_authors as mn')
        sql.where.append('books.pk = mn.fk_books')
        sql.where.append("mn.fk_authors = %(fk_authors)s")
        sql.params['fk_authors'] = os.id
Example #6
0
    def index (self, **dummy_kwargs):
        """ Output sitemap index. """

        sitemaps = []
        now = datetime.datetime.utcnow ().replace (microsecond = 0).isoformat () + 'Z'

        # 99999 is safeguard against bogus ebook numbers
        lastbook = BaseSearcher.sql_get ('select max (pk) as lastbook from books where pk < 99999')

        os = BaseSearcher.OpenSearch ()
        host = cherrypy.config['host']

        for n in range (0, lastbook // SITEMAP_SIZE + 1):
            sitemap = Struct ()
            sitemap.loc = os.url ('sitemap_index', page = n, host = host, format = None)
            sitemap.lastmod = now
            sitemaps.append (sitemap)

        data = Struct ()
        data.sitemaps = sitemaps

        return self.output ('sitemap-index', data = data)
    def index(self, **dummy_kwargs):
        """ A bibrec page. """

        os = BaseSearcher.OpenSearch()

        os.log_request('bibrec')

        dc = BaseSearcher.DC(cherrypy.engine.pool)

        # the bulk of the work is done here
        dc.load_from_database(os.id)
        if not dc.files:
            # NOTE: Error message
            cherrypy.tools.rate_limiter.e404()
            raise cherrypy.HTTPError(404, _('No ebook by that number.'))

        # add these fields so we won't have to test for their existence later
        dc.extra_info = None
        dc.url = None

        dc.translate()
        dc.header = gg.cut_at_newline(dc.title)
        os.title = dc.make_pretty_title()
        dc.extra_info = ''
        dc.class_ = BaseSearcher.ClassAttr()
        dc.order = 10
        dc.icon = 'book'
        if 'Sound' in dc.categories:
            dc.icon = 'audiobook'
        os.title_icon = dc.icon
        os.twit = os.title
        os.qrcode_url = '//%s/cache/epub/%d/pg%d.qrcode.png' % (os.file_host,
                                                                os.id, os.id)

        os.entries.append(dc)

        s = cherrypy.session
        last_visited = s.get('last_visited', [])
        last_visited.append(os.id)
        s['last_visited'] = last_visited

        # can we find some meaningful breadcrumbs ?
        for a in dc.authors:
            if a.marcrel in ('aut', 'cre'):
                book_cnt = BaseSearcher.sql_get(
                    "select count (*) from mn_books_authors where fk_authors = %(aid)s",
                    aid=a.id)
                if book_cnt > 1:
                    os.breadcrumbs.append(
                        (__('One by {author}', '{count} by {author}',
                            book_cnt).format(count=book_cnt,
                                             author=dc.make_pretty_name(
                                                 a.name)),
                         _('Find more ebooks by the same author.'),
                         os.url('author', id=a.id)))

        if os.format in ('html', 'mobile'):
            cat = BaseSearcher.Cat()
            cat.header = _('Similar Books')
            cat.title = _('Readers also downloaded…')
            cat.rel = 'related'
            cat.url = os.url('also', id=os.id)
            cat.class_ += 'navlink grayed noprint'
            cat.icon = 'suggestion'
            cat.order = 30
            os.entries.append(cat)

            for bookshelf in dc.bookshelves:
                cat = BaseSearcher.Cat()
                cat.title = _('In {bookshelf}').format(
                    bookshelf=bookshelf.bookshelf)
                cat.rel = 'related'
                cat.url = os.url('bookshelf', id=bookshelf.id)
                cat.class_ += 'navlink grayed'
                cat.icon = 'bookshelf'
                cat.order = 33
                os.entries.append(cat)

        if os.format in ('mobile', ):
            for author in dc.authors:
                cat = BaseSearcher.Cat()
                cat.title = _('By {author}').format(
                    author=author.name_and_dates)
                cat.rel = 'related'
                cat.url = os.url('author', id=author.id)
                cat.class_ += 'navlink grayed'
                cat.icon = 'author'
                cat.order = 31
                os.entries.append(cat)

            for subject in dc.subjects:
                cat = BaseSearcher.Cat()
                cat.title = _('On {subject}').format(subject=subject.subject)
                cat.rel = 'related'
                cat.url = os.url('subject', id=subject.id)
                cat.class_ += 'navlink grayed'
                cat.icon = 'subject'
                cat.order = 32
                os.entries.append(cat)

        os.total_results = 1

        os.template = 'results' if os.format == 'mobile' else 'bibrec'
        os.page = 'bibrec'
        os.og_type = 'book'
        os.finalize()

        return self.format(os)