Example #1
0
    def get(self, class_name=None):
        width = Img.WIDTH
        uuid_str = gen_uuid_32()
        key = self._class_name + ':' + str(width)
        try:
            page = int(self.get_query_argument('page'))
        except:
            page = 1

        try:
            res_str = self._redis.hget(key, page)
        except:
            res_str = ''
        if res_str:
            res_b64 = base64.standard_b64encode(res_str)
            encrypt_str = uuid_str[:13] + res_b64 + uuid_str[:22]
            self.write(encrypt_str)

        else:
            l = self._leancloud_api
            limit_num = Img.LIMIT_NUM

            obj_list = l.get_skip_obj_list(page - 1, limit_num=limit_num)

            result = []
            for i in obj_list:

                img_ID = i.get('ID')
                img_url = i.get('File').url
                img_url = img_url + '?imageMogr2/thumbnail/%sx' % width
                ori_width = i.get('width')
                ori_height = i.get('height')
                try:
                    height = width * ori_height / ori_width
                    each_res = {
                        'id': img_ID,
                        'image': img_url,
                        'width': width,
                        'height': height
                    }
                except TypeError:
                    each_res = random.choice(default_res)

                result.append(each_res)

            res = {'total': limit_num, 'result': result}
            res_str = json_encode(res)

            try:
                self._redis.hset(key, page, res_str)
            except:
                pass
            res_b64 = base64.standard_b64encode(res_str)
            encrypt_str = uuid_str[:13] + res_b64 + uuid_str[:22]
            self.set_header("Content-Type", "text/plain")
            self.write(encrypt_str)
Example #2
0
    def get(self, *args):
        is_mobile = self.get_query_argument('is_mobile', False)
        max_page = Img.MOBILE_PAGE if is_mobile else Img.MAXPAGE
        class_name = self.get_query_argument('class_name', 'Girls')
        page = int(self.get_query_argument('page', 1))
        try:
            p = int(self.get_query_argument('p', 1))
        except:
            p = 1
        p = 1 if p <= 0 else p
        # page = (p-1) * maxPage + page; maxPage defined in waterfall.js
        page = (int(p) - 1) * max_page + page
        if (page <= 0) or (page > Img.PAGE_NUM * 2):
            page = 1

        width = Img.WIDTH
        uuid_str = gen_uuid_32()
        key = class_name + ':' + str(width)

        try:
            res_str = self._redis.hget(key, page)
        except:
            res_str = ''
        if res_str:
            res_b64 = base64.standard_b64encode(res_str)
            encrypt_str = uuid_str[:13] + res_b64 + uuid_str[:22]
            print 'from redis', page
            self.set_header("Content-Type", "text/plain")
            self.write(encrypt_str)

        else:
            l = LeanCloudApi(class_name)
            limit_num = Img.LIMIT_NUM

            obj_list = l.get_skip_obj_list(page - 1, limit_num=limit_num)

            result = []
            for i in obj_list:
                img_ID = i.get('ID')
                img_url = i.get('File').url
                img_href = img_url.split('/')[-1].replace('.', '_')
                if 'gif' in img_href.lower():
                    img_url = img_url + '?vframe/jpg/offset/0|imageMogr2/thumbnail/%sx/interlace/1' % width
                else:
                    img_url = img_url + '?imageMogr2/thumbnail/%sx/interlace/1' % width

                ori_width = i.get('width')
                ori_height = i.get('height')
                try:
                    height = width * ori_height / ori_width
                    each_res = {
                        'href': img_href,
                        'id': img_ID,
                        'image': img_url,
                        'width': width,
                        'height': height
                    }
                except TypeError:
                    each_res = random.choice(default_res)

                result.append(each_res)

            if result:
                res = {'total': limit_num, 'result': result}
            else:
                res = {'total': 0}
            res_str = json_encode(res)

            try:
                self._redis.hset(key, page, res_str)
            except:
                pass
            res_b64 = base64.standard_b64encode(res_str)
            encrypt_str = uuid_str[:13] + res_b64 + uuid_str[:22]
            self.set_header("Content-Type", "text/plain")
            self.write(encrypt_str)