def update_redis_by_class(class_name): """update redis query string of each leancloud class""" l = LeanCloudApi(class_name) for page in range(1, page_num+1): time.sleep(0.3) try: obj_list = l.get_skip_obj_list(page-1, limit_num=Img.LIMIT_NUM) except: time.sleep(1) obj_list = l.get_skip_obj_list(page-1, limit_num=Img.LIMIT_NUM) if not obj_list: return result = [] for i in obj_list: img_ID = i.get('ID') print(img_ID) img_url = i.get('File').url img_id = img_url.rsplit('/')[-1].replace('.', '_') is_gif = True if 'gif' in img_url.lower() else False 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') if is_gif: scale_width, scale_height = int(ori_width), int(ori_height) else: scale_width, scale_height = int(ori_width*SCALE), int(ori_height*SCALE) r.hset(KEY, img_id, str(scale_width)+'_'+str(scale_height)) height = width*ori_height/ori_width each_res = {'href': img_href, 'id': img_ID, 'image': img_url, 'width': width, 'height': height} result.append(each_res) res = {'total': page_num, 'result': result} try: key = class_name + ':' + str(width) r.hset(key, page, json_encode(res)) print 'set redis class', class_name, page except: print 'redis hset fail', class_name, page
def post(self): class_name = self.get_body_argument('class_name') img_ID = self.get_body_argument('img_id') l = LeanCloudApi(class_name) try: # l.del_by_ID(int(img_ID)) l.set_by_ID(int(img_ID)) # set img ID to 0 d = {'msg': 'success', 'img_id': img_ID} self.write(d) except: import traceback traceback.print_exc() d = {'msg': 'success', 'img_id': img_ID} self.write(d)
def post(self): class_name = self.get_body_argument("class_name") img_ID = self.get_body_argument("img_id") l = LeanCloudApi(class_name) try: # l.del_by_ID(int(img_ID)) l.set_by_ID(int(img_ID)) # set img ID to 0 d = {"msg": "success", "img_id": img_ID} self.write(d) except: import traceback traceback.print_exc() d = {"msg": "success", "img_id": img_ID} self.write(d)
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)
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)