Пример #1
0
    def test_ajax_categories(self):  # {{{
        'Test /ajax/categories and /ajax/search'
        with self.create_server() as server:
            db = server.handler.router.ctx.library_broker.get(None)
            db.set_pref('virtual_libraries', {'1': 'title:"=Title One"'})
            conn = server.connect()
            request = partial(make_request, conn)

            r, data = request('/categories')
            self.ae(r.status, OK)
            r, xdata = request('/categories/' + db.server_library_id)
            self.ae(r.status, OK)
            self.ae(data, xdata)
            names = {x['name']: x['url'] for x in data}
            for q in ('Newest', 'All books', 'Tags', 'Series', 'Authors',
                      'Enum', 'Composite Tags'):
                self.assertIn(q, names)
            r, data = request(names['Tags'], prefix='')
            self.ae(r.status, OK)
            names = {x['name']: x['url'] for x in data['items']}
            self.ae(set(names), set('Tag One,Tag Two,News'.split(',')))
            r, data = request(names['Tag One'], prefix='')
            self.ae(r.status, OK)
            self.ae(set(data['book_ids']), {1, 2})
            r, data = request('/search?' +
                              urlencode({'query': 'tags:"=Tag One"'}))
            self.ae(r.status, OK)
            self.ae(set(data['book_ids']), {1, 2})
            r, data = request('/search?' + urlencode({
                'query': 'tags:"=Tag One"',
                'vl': '1'
            }))
            self.ae(set(data['book_ids']), {2})
Пример #2
0
    def test_ajax_categories(self):  # {{{
        'Test /ajax/categories and /ajax/search'
        with self.create_server() as server:
            db = server.handler.router.ctx.library_broker.get(None)
            db.set_pref('virtual_libraries', {'1':'title:"=Title One"'})
            conn = server.connect()
            request = partial(make_request, conn)

            r, data = request('/categories')
            self.ae(r.status, OK)
            r, xdata = request('/categories/' + db.server_library_id)
            self.ae(r.status, OK)
            self.ae(data, xdata)
            names = {x['name']:x['url'] for x in data}
            for q in (_('Newest'), _('All books'), _('Tags'), _('Authors'), _('Enum'), _('Composite Tags')):
                self.assertIn(q, names)
            r, data = request(names[_('Tags')], prefix='')
            self.ae(r.status, OK)
            names = {x['name']:x['url'] for x in data['items']}
            self.ae(set(names), set('Tag One,Tag Two,News'.split(',')))
            r, data = request(names['Tag One'], prefix='')
            self.ae(r.status, OK)
            self.ae(set(data['book_ids']), {1, 2})
            r, data = request('/search?' + urlencode({'query': 'tags:"=Tag One"'}))
            self.ae(r.status, OK)
            self.ae(set(data['book_ids']), {1, 2})
            r, data = request('/search?' + urlencode({'query': 'tags:"=Tag One"', 'vl':'1'}))
            self.ae(set(data['book_ids']), {2})
Пример #3
0
def mobile(ctx, rd):
    db, library_id, library_map, default_library = get_library_data(ctx, rd)
    try:
        start = max(1, int(rd.query.get('start', 1)))
    except ValueError:
        raise HTTPBadRequest('start is not an integer')
    try:
        num = max(0, int(rd.query.get('num', 25)))
    except ValueError:
        raise HTTPBadRequest('num is not an integer')
    search = rd.query.get('search') or ''
    with db.safe_read_lock:
        book_ids = ctx.search(rd, db, search)
        total = len(book_ids)
        ascending = rd.query.get('order', '').lower().strip() == 'ascending'
        sort_by = sanitize_sort_field_name(db.field_metadata, rd.query.get('sort') or 'date')
        try:
            book_ids = db.multisort([(sort_by, ascending)], book_ids)
        except Exception:
            sort_by = 'date'
            book_ids = db.multisort([(sort_by, ascending)], book_ids)
        books = [db.get_metadata(book_id) for book_id in book_ids[(start-1):(start-1)+num]]
    rd.outheaders['Last-Modified'] = http_date(timestampfromdt(db.last_modified()))
    order = 'ascending' if ascending else 'descending'
    q = {b'search':search.encode('utf-8'), b'order':bytes(order), b'sort':sort_by.encode('utf-8'), b'num':bytes(num), 'library_id':library_id}
    url_base = ctx.url_for('/mobile') + '?' + urlencode(q)
    lm = {k:v for k, v in iteritems(library_map) if k != library_id}
    return build_index(rd, books, num, search, sort_by, order, start, total, url_base, db.field_metadata, ctx, lm, library_id)
Пример #4
0
def mobile(ctx, rd):
    db, library_id, library_map, default_library = get_library_data(ctx, rd)
    try:
        start = max(1, int(rd.query.get('start', 1)))
    except ValueError:
        raise HTTPBadRequest('start is not an integer')
    try:
        num = max(0, int(rd.query.get('num', 25)))
    except ValueError:
        raise HTTPBadRequest('num is not an integer')
    search = rd.query.get('search') or ''
    with db.safe_read_lock:
        book_ids = ctx.search(rd, db, search)
        total = len(book_ids)
        ascending = rd.query.get('order', '').lower().strip() == 'ascending'
        sort_by = sanitize_sort_field_name(db.field_metadata, rd.query.get('sort') or 'date')
        try:
            book_ids = db.multisort([(sort_by, ascending)], book_ids)
        except Exception:
            sort_by = 'date'
            book_ids = db.multisort([(sort_by, ascending)], book_ids)
        books = [db.get_metadata(book_id) for book_id in book_ids[(start-1):(start-1)+num]]
    rd.outheaders['Last-Modified'] = http_date(timestampfromdt(db.last_modified()))
    order = 'ascending' if ascending else 'descending'
    q = {b'search':search.encode('utf-8'), b'order':order.encode('ascii'), b'sort':sort_by.encode('utf-8'), b'num':as_bytes(num), 'library_id':library_id}
    url_base = ctx.url_for('/mobile') + '?' + urlencode(q)
    lm = {k:v for k, v in iteritems(library_map) if k != library_id}
    return build_index(rd, books, num, search, sort_by, order, start, total, url_base, db.field_metadata, ctx, lm, library_id)
Пример #5
0
 def submitWithQuery(self, url, query={}):
     query.update(self.add_req)
     self.logger("query is:", query)
     if len(query) > 0:
         qs = urlencode(query)
         fullurl = '%s?%s' % (url, qs)
         self.logger("Fullurl is:", fullurl)
     else:
         fullurl = url
     cdata = self.browser.open(fullurl).read()
     return cdata
Пример #6
0
def post(host, url, payload):
    http = HTTPSConnection(host, timeout=10)
    try:
        params = urlencode(payload)
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        http.request("POST", url, params, headers)
        resp = http.getresponse()
    finally:
        http.close()

    return resp
Пример #7
0
 def remote_run(self, name, m, *args):
     from mechanize import HTTPError, Request
     from calibre.utils.serialize import msgpack_loads, msgpack_dumps
     url = self.url + '/cdb/cmd/{}/{}'.format(name, getattr(m, 'version', 0))
     if self.library_id:
         url += '?' + urlencode({'library_id':self.library_id})
     rq = Request(url, data=msgpack_dumps(args),
                  headers={'Accept': MSGPACK_MIME, 'Content-Type': MSGPACK_MIME})
     try:
         res = self.br.open_novisit(rq)
         ans = msgpack_loads(res.read())
     except HTTPError as err:
         self.interpret_http_error(err)
         raise
     if 'err' in ans:
         prints(ans['tb'])
         raise SystemExit(ans['err'])
     return ans['result']
Пример #8
0
 def remote_run(self, name, m, *args):
     from mechanize import HTTPError, Request
     from calibre.utils.serialize import msgpack_loads, msgpack_dumps
     url = self.url + '/cdb/cmd/{}/{}'.format(name, getattr(m, 'version', 0))
     if self.library_id:
         url += '?' + urlencode({'library_id':self.library_id})
     rq = Request(url, data=msgpack_dumps(args),
                  headers={'Accept': MSGPACK_MIME, 'Content-Type': MSGPACK_MIME})
     try:
         res = self.br.open_novisit(rq, timeout=self.timeout)
         ans = msgpack_loads(res.read())
     except HTTPError as err:
         self.interpret_http_error(err)
         raise
     if 'err' in ans:
         if ans['tb']:
             prints(ans['tb'])
         raise SystemExit(ans['err'])
     return ans['result']
Пример #9
0
    def url(self):
        # copy the original query string
        query_string = dict(self.query_string)

        # iterate through macros and set the position in the querystring
        for macro, name in self.macro_map.items():
            if hasattr(self, macro):
                # set the name/value pair
                query_string[name] = [getattr(self, macro)]
            else:
                # remove the name/value pair
                del (query_string[name])

        # copy the url parts and substitute in our new query string
        url_parts = list(self.url_parts)
        url_parts[4] = urlencode(query_string, 1)

        # recompose and return url
        return urlunparse(tuple(url_parts))
Пример #10
0
    def url(self):
        # copy the original query string
        query_string = dict(self.query_string)

        # iterate through macros and set the position in the querystring
        for macro, name in self.macro_map.items():
            if hasattr(self, macro):
                # set the name/value pair
                query_string[name] = [getattr(self, macro)]
            else:
                # remove the name/value pair
                del(query_string[name])

        # copy the url parts and substitute in our new query string
        url_parts = list(self.url_parts)
        url_parts[4] = urlencode(query_string, 1)

        # recompose and return url
        return urlunparse(tuple(url_parts))
Пример #11
0
def download_updates(ver_map={}, server='https://code.calibre-ebook.com'):
    from calibre.utils.https import get_https_resource_securely
    data = {k:unicode_type(v) for k, v in iteritems(ver_map)}
    data['ver'] = '1'
    url = '%s/stores?%s'%(server, urlencode(data))
    # We use a timeout here to ensure the non-daemonic update thread does not
    # cause calibre to hang indefinitely during shutdown
    raw = get_https_resource_securely(url, timeout=90.0)

    while raw:
        name, raw = raw.partition(b'\0')[0::2]
        name = name.decode('utf-8')
        d = decompressobj()
        src = d.decompress(raw)
        src = src.decode('utf-8').lstrip(u'\ufeff')
        # Python complains if there is a coding declaration in a unicode string
        src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src, flags=re.MULTILINE)
        # Translate newlines to \n
        src = io.StringIO(src, newline=None).getvalue()
        yield name, src
        raw = d.unused_data
Пример #12
0
def download_updates(ver_map={}, server='https://code.calibre-ebook.com'):
    from calibre.utils.https import get_https_resource_securely
    data = {k:unicode_type(v) for k, v in iteritems(ver_map)}
    data['ver'] = '1'
    url = '%s/stores?%s'%(server, urlencode(data))
    # We use a timeout here to ensure the non-daemonic update thread does not
    # cause calibre to hang indefinitely during shutdown
    raw = get_https_resource_securely(url, timeout=90.0)

    while raw:
        name, raw = raw.partition(b'\0')[0::2]
        name = name.decode('utf-8')
        d = decompressobj()
        src = d.decompress(raw)
        src = src.decode('utf-8').lstrip(u'\ufeff')
        # Python complains if there is a coding declaration in a unicode string
        src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src, flags=re.MULTILINE)
        # Translate newlines to \n
        src = io.StringIO(src, newline=None).getvalue()
        yield name, src
        raw = d.unused_data
Пример #13
0
def upload_to_fosshub():
    # fosshub has no API to do partial uploads, so we always upload all files.
    print('Sending upload request to fosshub...')
    files = set(installers())
    entries = []
    for fname in files:
        desc = installer_description(fname)
        url = 'https://download.calibre-ebook.com/%s/%s' % (
            __version__, os.path.basename(fname)
        )
        entries.append({
            'url': url,
            'type': desc,
            'version': __version__,
        })
    jq = {
        'software': 'Calibre',
        'apiKey': get_fosshub_data(),
        'upload': entries,
        'delete': [{
            'type': '*',
            'version': '*',
            'name': '*'
        }]
    }
    # print(json.dumps(jq, indent=2))
    rq = urlopen(
        'https://www.fosshub.com/JSTools/uploadJson',
        urlencode({
            'content': json.dumps(jq)
        })
    )
    resp = rq.read()
    if rq.getcode() != 200:
        raise SystemExit(
            'Failed to upload to fosshub, with HTTP error code: %d and response: %s'
            % (rq.getcode(), resp)
        )
Пример #14
0
 def url_for(self, path, **kwargs):
     lid = kwargs.pop('library_id', self.library_id)
     ans = self.ctx.url_for(path, **kwargs)
     q = {'library_id': lid}
     ans += '?' + urlencode(q)
     return ans
Пример #15
0
 def url_for(self, path, **kwargs):
     lid = kwargs.pop('library_id', self.library_id)
     ans = self.ctx.url_for(path, **kwargs)
     q = {'library_id':lid}
     ans += '?' + urlencode(q)
     return ans