예제 #1
0
    def get_covers(self, offset=0, limit=20):
        editions = self.get_editions(offset, limit)
        olids = [e['key'].split('/')[-1] for e in editions]

        try:
            url = '%s/b/query?cmd=ids&olid=%s' % (get_coverstore_url(),
                                                  ",".join(olids))
            data = urllib.request.urlopen(url).read()
            cover_ids = simplejson.loads(data)
        except IOError as e:
            print('ERROR in getting cover_ids', str(e), file=web.debug)
            cover_ids = {}

        def make_cover(edition):
            edition = dict(edition)
            edition.pop('type', None)
            edition.pop('subjects', None)
            edition.pop('languages', None)

            olid = edition['key'].split('/')[-1]
            if olid in cover_ids:
                edition['cover_id'] = cover_ids[olid]

            return edition

        return [make_cover(e) for e in editions]
예제 #2
0
    def upload(self, key, i):
        """Uploads a cover to coverstore and returns the response."""
        olid = key.split("/")[-1]

        if i.file is not None and hasattr(i.file, 'value'):
            data = i.file.value
        else:
            data = None

        if i.url and i.url.strip() == "http://":
            i.url = ""

        user = accounts.get_current_user()
        params = {
            "author": user and user.key,
            "data": data,
            "source_url": i.url,
            "olid": olid,
            "ip": web.ctx.ip
        }

        upload_url = '%s/%s/upload2' % (get_coverstore_url(),
                                        self.cover_category)

        if upload_url.startswith("//"):
            upload_url = "http:" + upload_url

        try:
            payload = requests.compat.urlencode(params).encode('utf-8')
            return web.storage(requests.post(upload_url, data=payload).json())
        except requests.HTTPError as e:
            return web.storage({'error': e.read()})
예제 #3
0
    def upload(self, key, i):
        """Uploads a cover to coverstore and returns the response."""
        olid = key.split("/")[-1]

        if i.file is not None and hasattr(i.file, 'value'):
            data = i.file.value
        else:
            data = None

        if i.url and i.url.strip() == "https://":
            i.url = ""

        user = accounts.get_current_user()
        params = {
            "author": user and user.key,
            "source_url": i.url,
            "olid": olid,
            "ip": web.ctx.ip
        }

        upload_url = '%s/%s/upload2' % (get_coverstore_url(),
                                        self.cover_category)

        if upload_url.startswith("//"):
            upload_url = "http:" + upload_url

        try:
            files = {'data': BytesIO(data)}
            response = requests.post(upload_url, data=params, files=files)
            return web.storage(response.json())
        except requests.HTTPError as e:
            logger.exception("Covers upload failed")
            return web.storage({'error': str(e)})
예제 #4
0
    def upload(self, key, i):
        """Uploads a cover to coverstore and returns the response."""
        olid = key.split("/")[-1]

        if i.file is not None and hasattr(i.file, 'value'):
            data = i.file.value
        else:
            data = None

        if i.url and i.url.strip() == "http://":
            i.url = ""

        user = accounts.get_current_user()
        params = {
            "author": user and user.key,
            "data": data,
            "source_url": i.url,
            "olid": olid,
            "ip": web.ctx.ip
        }

        upload_url = '%s/%s/upload2' % (get_coverstore_url(),
                                        self.cover_category)

        if upload_url.startswith("//"):
            upload_url = "http:" + upload_url

        try:
            response = urllib.request.urlopen(upload_url,
                                              urllib.parse.urlencode(params))
            out = response.read()
        except urllib.error.HTTPError as e:
            out = {'error': e.read()}

        return web.storage(simplejson.loads(out))