Пример #1
0
    def _photo_like(photo_like):
        """
        re calculate user level here
        """
        user = models.User().find(models.Photo().find(
            photo_like.photo_id).user_id)

        redis_client = get_redis_client()
        redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, 1)

        current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'],
                                                photo_like.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id,
                          int(current_likes_count) + 1)

        calculated_user_level = calculate_user_level(user)
        if calculated_user_level > user.level:
            redis_key = REDIS_KEY['USER_MESSAGE'].format(user_id=user.id)
            models.Invite_Key().gen_by_level(user, calculated_user_level,
                                             user.level)
            msg = u"{0}|恭喜你,成功升级到{1}".format(
                'info', USER_LEVEL_CN[calculated_user_level])
            redis_client.lpush(redis_key, msg)
            user.level = calculated_user_level
            user.save()
Пример #2
0
 def hot_tags(self):
     redis_key = REDIS_KEY['HOT_TAGS']
     redis_client = get_redis_client()
     if not redis_client.scard(redis_key):
         hot_tags = self.query(
                 '''
                 SELECT tag, count(id) 
                 FROM photo_tag 
                 GROUP BY tag 
                 ORDER BY count(id) DESC
                 LIMIT 100
                 '''
                 ).fetchall()
         for item in hot_tags:
             redis_client.sadd(redis_key, item.tag)
         # expires 1 hour later
         redis_client.expire(redis_key, 3600)
     
     tags = []
     if redis_client.smembers(redis_key):
         length = min(10, redis_client.scard(redis_key))
         for i in range(length):
             tag = redis_client.spop(redis_key)
             tags.append(tag)
         for i in range(length):
             redis_client.sadd(redis_key, tags[i])
     return tags
Пример #3
0
def run():
    redis_client = get_redis_client()
    redis_client.delete(REDIS_KEY['USER_LIKED_COUNT'])
    redis_client.delete(REDIS_KEY['USER_LIKES_COUNT'])
    redis_client.delete(REDIS_KEY['USER_PHOTO_COUNT'])
    photos = Photo().select(['id', 'user_id',
                             'likes_count']).where('status', '=',
                                                   0).findall(limit=1000000)
    for photo in progress.bar(photos):
        current_liked_count = redis_client.hget(REDIS_KEY['USER_LIKED_COUNT'],
                                                photo.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKED_COUNT'], photo.user_id,
                          int(current_liked_count) + int(photo.likes_count))

        current_photo_count = redis_client.hget(REDIS_KEY['USER_PHOTO_COUNT'],
                                                photo.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id,
                          int(current_photo_count) + 1)

        photo_like = Photo_Like().findall_by_photo_id(photo.id)
        for item in photo_like:
            current_likes_count = redis_client.hget(
                REDIS_KEY['USER_LIKES_COUNT'], item.user_id) or 0
            redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], item.user_id,
                              int(current_likes_count) + 1)
Пример #4
0
    def hot_tags(self):
        redis_key = REDIS_KEY['HOT_TAGS']
        redis_client = get_redis_client()
        if not redis_client.scard(redis_key):
            hot_tags = self.query('''
                    SELECT tag, count(id) 
                    FROM photo_tag 
                    GROUP BY tag 
                    ORDER BY count(id) DESC
                    LIMIT 100
                    ''').fetchall()
            for item in hot_tags:
                redis_client.sadd(redis_key, item.tag)
            # expires 1 hour later
            redis_client.expire(redis_key, 3600)

        tags = []
        if redis_client.smembers(redis_key):
            length = min(10, redis_client.scard(redis_key))
            for i in range(length):
                tag = redis_client.spop(redis_key)
                tags.append(tag)
            for i in range(length):
                redis_client.sadd(redis_key, tags[i])
        return tags
Пример #5
0
 def __init__(self):
     # 购买服务时,网站给出的提取ip的api,替换成自己的
     self.api_url = 'http://xxxxxxxxxxxxxxxxxxxxxxxxxx'
     self.logger = utils.get_logger(getattr(self.__class__, '__name__'))
     self.proxy_list = []
     self.good_proxy_list = []
     self.pool = Pool(5)
     self.server = utils.get_redis_client()
Пример #6
0
 def __init__(self):
     # 购买服务时,网站给出的提取ip的api,替换成自己的
     self.api_url = 'http://api.xdaili.cn/xdaili-api//greatRecharge/getGreatIp?spiderId=5ae27e4e2ede44f8826cb1224fdb963b&orderno=YZ20203250648EyVEao&returnType=1&count=10'
     self.logger = utils.get_logger(getattr(self.__class__, '__name__'))
     self.proxy_list = []
     self.good_proxy_list = []
     self.pool = Pool(5)
     self.server = utils.get_redis_client()
Пример #7
0
def get_message(handler):
    if handler.current_user:
        redis_client = get_redis_client()
        redis_key = REDIS_KEY['USER_MESSAGE'].format(user_id = handler.current_user.id)
        if redis_client.llen(redis_key):
            return redis_client.rpop(redis_key).split('|')

    message = handler.get_cookie('message')
    handler.clear_cookie('message')
    return message.split('|') if message else None
Пример #8
0
 def __init__(self):
     self.logger = utils.get_logger(getattr(self.__class__, '__name__'))
     self.server = utils.get_redis_client()
     self.ip_pool_key = settings.IP_POOL_KEY
     # 区别对待使用密码和不使用密码的配置模板
     if settings.USE_PASSWORD:
         self.peer_conf = "cache_peer %s parent %s 0 no-query proxy-only login={}:{} never_direct allow all round-robin weight=1 connect-fail-limit=2 allow-miss max-conn=5\n".format(
             settings.USERNAME, settings.PASSWORD)
     else:
         self.peer_conf = "cache_peer %s parent %s 0 no-query proxy-only never_direct allow all round-robin weight=1 connect-fail-limit=2 allow-miss max-conn=5\n"
Пример #9
0
def get_message(handler):
    if handler.current_user:
        redis_client = get_redis_client()
        redis_key = REDIS_KEY['USER_MESSAGE'].format(
            user_id=handler.current_user.id)
        if redis_client.llen(redis_key):
            return redis_client.rpop(redis_key).split('|')

    message = handler.get_cookie('message')
    handler.clear_cookie('message')
    return message.split('|') if message else None
Пример #10
0
    def _photo_unlike(photo_like):
        """
        re calculate user level here
        """
        user = models.User().find(models.Photo().find(photo_like.photo_id).user_id)

        redis_client = get_redis_client()
        redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, -1)

        current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id, int(current_likes_count) - 1)
Пример #11
0
def run():
    redis_client = get_redis_client()
    for table in ("photo", "user"):
        redis_key = REDIS_KEY["TABLE_ITEMS"].format(table=table)
        if table == "photo":
            result = Photo().findall_by_status(0, limit=1000000)
        elif table == "user":
            result = User().findall(limit=1000000)

        for item in progress.bar(result):
            redis_client.hset(redis_key, item.id, json.dumps(item.to_dict()))
Пример #12
0
def run():
    redis_client = get_redis_client()
    for table in ('photo', 'user'):
        redis_key = REDIS_KEY['TABLE_ITEMS'].format(table=table)
        if table == 'photo':
            result = Photo().findall_by_status(0, limit=1000000)
        elif table == 'user':
            result = User().findall(limit=1000000)

        for item in progress.bar(result):
            redis_client.hset(redis_key, item.id, json.dumps(item.to_dict()))
Пример #13
0
    def _photo_unlike(photo_like):
        """
        re calculate user level here
        """
        user = models.User().find(models.Photo().find(
            photo_like.photo_id).user_id)

        redis_client = get_redis_client()
        redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, -1)

        current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'],
                                                photo_like.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id,
                          int(current_likes_count) - 1)
Пример #14
0
def run():
    redis_client = get_redis_client()
    redis_client.delete(REDIS_KEY['USER_LIKED_COUNT'])
    redis_client.delete(REDIS_KEY['USER_LIKES_COUNT'])
    redis_client.delete(REDIS_KEY['USER_PHOTO_COUNT'])
    photos = Photo().select(['id', 'user_id', 'likes_count']).where('status', '=', 0).findall(limit=1000000)
    for photo in progress.bar(photos):
        current_liked_count = redis_client.hget(REDIS_KEY['USER_LIKED_COUNT'], photo.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKED_COUNT'], photo.user_id, int(current_liked_count) + int(photo.likes_count))

        current_photo_count = redis_client.hget(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id, int(current_photo_count) + 1)

        photo_like = Photo_Like().findall_by_photo_id(photo.id)
        for item in photo_like:
            current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'], item.user_id) or 0
            redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], item.user_id, int(current_likes_count) + 1)
Пример #15
0
    def _photo_like(photo_like):
        """
        re calculate user level here
        """
        user = models.User().find(models.Photo().find(photo_like.photo_id).user_id)

        redis_client = get_redis_client()
        redis_client.hincrby(REDIS_KEY['USER_LIKED_COUNT'], user.id, 1)

        current_likes_count = redis_client.hget(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id) or 0
        redis_client.hset(REDIS_KEY['USER_LIKES_COUNT'], photo_like.user_id, int(current_likes_count) + 1)

        calculated_user_level = calculate_user_level(user)
        if calculated_user_level > user.level:
            redis_key = REDIS_KEY['USER_MESSAGE'].format(user_id = user.id)
            models.Invite_Key().gen_by_level(user, calculated_user_level, user.level)
            msg = u"{0}|恭喜你,成功升级到{1}".format('info', USER_LEVEL_CN[calculated_user_level])
            redis_client.lpush(redis_key, msg)
            user.level = calculated_user_level
            user.save()
Пример #16
0
 def _photo_delete(photo):
     redis_client = get_redis_client()
     current_photo_count = redis_client.hget(REDIS_KEY['USER_PHOTO_COUNT'],
                                             photo.user_id) or 0
     redis_client.hset(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id,
                       int(current_photo_count) - 1)
Пример #17
0
 def __init__(self):
     self.logger = utils.get_logger(getattr(self.__class__, '__name__'))
     self.server = utils.get_redis_client()
Пример #18
0
 def _before_find(self, primary_key_value):
     if primary_key_value and self.table in ('user', 'photo'):
         redis_key = REDIS_KEY['TABLE_ITEMS'].format(table = self.table)
         redis_client = get_redis_client()
         result = redis_client.hget(redis_key, primary_key_value)
         return attr_dict(json.loads(result)) if result else None
Пример #19
0
 def _after_insert(self):
     if self.table in ('user', 'photo'):
         redis_key = REDIS_KEY['TABLE_ITEMS'].format(table = self.table)
         redis_client = get_redis_client()
         redis_client.hset(redis_key, getattr(self, self._primary_key), json.dumps(self.to_dict()))
Пример #20
0
 def before_request():
     g.redis_client = get_redis_client()
Пример #21
0
 def photo_count(self):
     return get_redis_client().hget(REDIS_KEY['USER_PHOTO_COUNT'], self.id) or 0
Пример #22
0
 def likes_count(self):
     return get_redis_client().hget(REDIS_KEY['USER_LIKES_COUNT'], self.id) or 0
Пример #23
0
 def likes_count(self):
     return get_redis_client().hget(REDIS_KEY['USER_LIKES_COUNT'],
                                    self.id) or 0
Пример #24
0
# -*- coding: utf-8 -*-
# @File  : web_api.py
# @Author: AaronJny
# @Date  : 18-12-14 上午11:22
# @Desc  : 提供http接口的web程序

import utils
import settings
import flask
import random
import time

redis_client = utils.get_redis_client()
ip_pool_key = settings.IP_POOL_KEY
app = flask.Flask(__name__)


@app.route('/random/')
def random_ip():
    """
    获取一个随机ip
    :return:
    """
    # 获取redis中仍可用的全部ip
    proxy_ips = redis_client.zrangebyscore(
        ip_pool_key, int(time.time()),
        int(time.time()) + settings.PROXY_IP_TTL * 10)
    if proxy_ips:
        ip = random.choice(proxy_ips)
        # 如果ip需要密码访问,则添加
        if settings.USE_PASSWORD:
Пример #25
0
 def _photo_delete(photo):
     redis_client = get_redis_client()
     current_photo_count = redis_client.hget(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id) or 0
     redis_client.hset(REDIS_KEY['USER_PHOTO_COUNT'], photo.user_id, int(current_photo_count) - 1)
Пример #26
0
 def photo_count(self):
     return get_redis_client().hget(REDIS_KEY['USER_PHOTO_COUNT'],
                                    self.id) or 0
Пример #27
0
 def search_count(self):
     return get_redis_client().hget(REDIS_KEY['USER_LIKED_COUNT'], self.id) or 0