def _get_sitemap_from_mc(self): '''从memcache中获取sitemap''' _sitemap = PicsHandler().mc.get('%s_sitemap.xml' % MEMCACHE_KEY) if not _sitemap: _sitemap = self._get_all_pic_from_db() PicsHandler().mc.set('%s_sitemap.xml' % MEMCACHE_KEY, _sitemap) return _sitemap
def get_count(cls, source=None, admin=None, status=None): '''获取总数量''' _data = PicsHandler().mc.get('%s_counts' % MEMCACHE_KEY) if not _data: _data = PicsHandler().get_count(admin, source=source, status=status) return _data
def update_pic_height(cls): '''更新无高度图片的高度''' _data = PicsHandler().get_top10_no_height_pic() _list = [] if _data: for item in _data: _img = get_remote_image_width_height(item["thumbnail_pic"]) _width = _img[0] _height = int(_img[1] * 255 / _img[0]) _list.append((_width, _height, item["id"])) if _list: return PicsHandler().update_many_url_height(_list) return '无需更新数据'
def pass_pic(cls): '''通过审核''' _ids = request.POST.get('ids', '') if '' == str(_ids): return json.dumps(dict(pas=False, message=u'要通过的数据不能为空')) PicsHandler().pass_pic(ids=_ids) return json.dumps(dict(pas=True, message=u'已删除'))
def delete_pic(cls): '''删除图片''' _ids = request.POST.get('ids', '') if '' == str(_ids): return json.dumps(dict(delete=False, message=u'要删除的数据不能为空')) PicsHandler().delete_pic(ids=_ids) return json.dumps(dict(delete=True, message=u'已删除'))
def like_pic(cls): '''like图片''' _id = request.POST.get('id', None) if not _id: return None _count = PicsHandler().update_pic_likes(id=_id) json.dumps(dict(like=True, message=_count))
def get_list_by_page(cls, page, source=None, admin=None, status=None): '''分页获取数据''' if admin or int(page) > MEMCACHE_MAX_PAGE_NUMB: _data = PicsHandler().get_list_by_page(admin, page=page, source=source, status=status) else: _data = PicsHandler().mc.get('%s_page_%s' % (MEMCACHE_KEY, page)) if not _data: _data = PicsHandler().get_list_by_page(admin, page=page, source=source, status=status) cls().init_memcache() return _data
def update_memcache(cls): '''更新memcache''' _sitemap = cls()._get_all_pic_from_db() if _sitemap: PicsHandler().mc.set('%s_sitemap.xml' % MEMCACHE_KEY, _sitemap) return json.dumps(dict(done=True, message=u'sitemap已同步')) return json.dumps(dict(done=False, message=u'sitemap同步error'))
def init_memcache(cls): '''更新memcache中数据''' for i in xrange(1, MEMCACHE_MAX_PAGE_NUMB + 1): _data = PicsHandler().get_list_by_page(False, page=i, source=None, status=1) if _data: PicsHandler().mc.set('%s_page_%s' % (MEMCACHE_KEY, i), _data, time=60 * 60) return json.dumps(dict(done=True, message=u'pages已同步')) _counts = PicsHandler().get_count(False, source=None, status=1) if _counts: PicsHandler().mc.set('%s_counts' % MEMCACHE_KEY, _counts, time=60 * 60) return json.dumps(dict(done=False, message=u'pages同步error'))
def _get_unique_items(self, url_ids, lists): '''获取去重后的内容''' _result = [] _ids = ','.join(url_ids) _items = PicsHandler().get_existed_url_id(_ids) if _items: for _item in _items: url_ids.remove(str(_item["url_id"])) for _url_id in url_ids: for _list in lists: if _list.get(str(_url_id), None): _result.append(_list[str(_url_id)]) return _result
def add(cls): '''抓取新浪微博数据''' items = cls()._get_data_from_sina() _black_list = BlackListService.get_all_black() _status = 2 #默认数据是未审核 if not items: return "None" for item in items: i = Dict(item) u = Dict(i.user) _is_in_black = False for _black in _black_list: if (_black["type"] == 0 and int(_black["value"]) == u.id) or ( _black["type"] == 1 and _black["value"] in i.text): _is_in_black = True continue if _is_in_black: continue try: _img = get_remote_image_width_height(i.thumbnail_pic) _width = _img[0] _height = int(_img[1] * 255 / _img[0]) PicsHandler().insert(id=i.id, user_id=u.id, text=i.text, user_name=u.name, create_date=formattime(i.created_at), thumbnail_pic=i.thumbnail_pic, bmiddle_pic=i.bmiddle_pic, original_pic=i.original_pic, profile_image_url=u.profile_image_url, url_id=url2id(i.thumbnail_pic), source=1, status=_status, height=_height, width=_width) except: continue return "Success %s" % len(items)
def get_by_id(cls, id): '''根据id获取数据''' if not id: return None return PicsHandler().get_by_id(id=id)
def _get_all_pic_from_db(self): '''获取所有图片数据''' _data = PicsHandler().get_all_pic() return _data
def get_next(cls, create_date): '''获取下一张图片''' if not create_date: return None return PicsHandler().get_next(create_date=create_date)
def get_most_likes(cls): '''获取最受欢迎数据''' return PicsHandler().get_most_likes()
def get_prev(cls, create_date): '''获取上一张图片''' if not create_date: return None return PicsHandler().get_prev(create_date=create_date)
def clean_pic(cls): '''清空回收站''' PicsHandler().clean_pic() return json.dumps(dict(clean=True, message=u'已清空'))