Exemple #1
0
    def get_all_lives(cls):
        """所有直播间列表
        """
        # 后台配置是否开启直播服务, 默认开启
        api_url = Config.fetch('live_api_url', None, str)
        if not api_url:
            return []

        api_url = urljoin(api_url, '/events')
        lives = Redis.get(cls.ALL_LIVES)
        if not lives:
            try:
                resp = requests.get(api_url, timeout=2)
            except requests.exceptions.Timeout:
                print_log('xlive', '[get_all_lives]: connection timeout')
                return []
            except:
                print_log('xlive', '[get_all_lives]: connection error')
                return []

            if resp.status_code != requests.codes.ok:
                print_log('xlive', '[get_all_lives]: status_code not ok')
                return []

            lives = resp.content
            lives = json.loads(lives)['data']['live']
            Redis.setex(cls.ALL_LIVES, 5, json.dumps(lives))
        else:
            lives = json.loads(lives)

        # 按照人气排序
        lives.sort(key=lambda x: x['count'], reverse=True)
        return lives
Exemple #2
0
 def get_used_today(cls, aid):
     today = datetime.date.today().strftime('%y%m%d')
     key = cls.USED_TODAY % ({'day': today, 'aid': aid})
     if not Redis.exists(key):
         Redis.set(key, 0)
         Redis.expire(key, 60 * 60 * 24)
     return int(Redis.get(key))
Exemple #3
0
 def get_limit_config(cls, report_type):
     key = cls.REPORT_CONFIG % {'rtype': report_type}
     if not Redis.exists(key):
         cfg = cls.collection.find_one({'report_type': report_type})
         _id = cfg and str(cfg['_id'])
         Redis.set(key, _id, 86400)
     return Redis.get(key)
Exemple #4
0
 def get_user_live_history(cls, uid):
     today = datetime.date.today().strftime('%y%m%d')
     key = cls.USER_LIVE_DAILY_HISTORY % {'today': today, 'uid': uid}
     if not Redis.exists(key):
         cls._load_user_live_history(uid, key)
     logs = Redis.get(key)
     history = cjson.loads(logs) if logs else []
     return history
Exemple #5
0
 def get_colors(cls):
     colors = Redis.get(BARRAGE_COLOR_KEY)
     if not colors:
         colors = cls._load_colors()
     else:
         colors = cjson.loads(colors)
     color_list = [dict_to_model(BarrageColor, color) for color in colors]
     return color_list
Exemple #6
0
 def activity_status(cls, aid):
     key = cls.ACTIVITY_CONFIG % ({'aid': str(aid)})
     if not Redis.exists(key):
         cls._load_activity_status(aid, key)
     try:
         activity_status = Redis.get(key)
     except exceptions.ResponseError:
         activity_status = 'begin'
     return activity_status
Exemple #7
0
 def get_redpacket_items(cls, lrp_id):
     key = cls.LRP_ITEM_KEY % ({'lrp_id': lrp_id})
     items = Redis.get(key)
     if items:
         items = cjson.loads(items)
     else:
         items = list(cls.collection.find({'lrp_id': ObjectId(lrp_id)}))
         Redis.setex(key, 86400, cjson.dumps(items, 2))
     return [cls(i) for i in items]
Exemple #8
0
 def get_task_items(cls, wlt_id):
     key = cls.WLT_ITEM_KEY % ({'wlt_id': wlt_id})
     items = Redis.get(key)
     if items:
         items = cjson.loads(items)
     else:
         items = list(cls.collection.find({'wlt_id': ObjectId(wlt_id)}))
         Redis.setex(key, 86400, cjson.dumps(items, 2))
     return [cls(i) for i in items]
Exemple #9
0
 def test_cached_set(self):
     self._cached_set()
     self.assertEqual(Redis.get(self.SET_KEY), 'empty')
     with self.assertRaises(exceptions.ResponseError):
         Redis.sadd(self.SET_KEY, ('dog', 'pig'))
         Redis.delete(self.SET_KEY)
     self._cached_set(('dog', 'pig'))
     self.assertTrue(Redis.sismember(self.SET_KEY, 'dog'))
     self.assertTrue(Redis.sismember(self.SET_KEY, 'pig'))
Exemple #10
0
 def test_cached_list(self):
     self._cached_list()
     self.assertEqual(Redis.get(self.LIST_KEY), 'empty')
     with self.assertRaises(exceptions.ResponseError):
         Redis.rpush(self.SET_KEY, ['dog', 'pig'])
         Redis.delete(self.SET_KEY)
     self._cached_list(['dog', 'pig'])
     self.assertListEqual(Redis.lrange(self.LIST_KEY, 0, -1),
                          ['dog', 'pig'])
Exemple #11
0
    def get_onsale_gifts(cls):
        gifts = Redis.get(ONSALE_GIFT_KEY)
        if not gifts:
            gifts = cls._load_onsale_gifts()
        else:
            gifts = cjson.loads(gifts)

        gifts = [dict_to_model(Gift, gf) for gf in gifts]
        return gifts
Exemple #12
0
 def test_cached_zset(self):
     self._cached_zset()
     self.assertEqual(Redis.get(self.ZSET_KEY), 'empty')
     with self.assertRaises(exceptions.ResponseError):
         Redis.zadd(self.ZSET_KEY, 1, 'dog', 2, 'pig')
         Redis.delete(self.ZSET_KEY)
     self._cached_zset((1, 'dog', 2, 'pig'))
     self.assertListEqual(Redis.zrevrange(self.ZSET_KEY, 0, -1),
                          ['pig', 'dog'])
Exemple #13
0
    def get_all_tasks(cls):
        tasks = Redis.get(TASK_KEY)
        if not tasks:
            tasks = cls._load_all_tasks()
        else:
            tasks = cjson.loads(tasks)

        tasks = [dict_to_model(Task, task) for task in tasks]
        return tasks
Exemple #14
0
 def all_ids_by_os(cls, os):
     key = cls.ALL_IDS % ({'os': os})
     if not Redis.exists(key):
         cls._load_ids_by_os(os, key)
     try:
         _ids = Redis.get(key)
     except exceptions.ResponseError:
         _ids = []
     return cjson.loads(_ids)
Exemple #15
0
    def game_activity_ids(cls, aid):
        key = cls.GAME_ACTIVITY % ({'activity_id': aid})
        if not Redis.exists(key):
            cls._load_game_activity_ids(aid, key)
        try:
            game_activity = Redis.get(key)
        except exceptions.ResponseError:
            game_activity = []

        return cjson.loads(game_activity)
Exemple #16
0
 def get_by_type(cls, activity_type):
     key = cls.ACTIVITY_CONFIG_TYPE % ({'activity_type': str(activity_type)})
     activity_config = Redis.get(key)
     if activity_config:
         activity_config = cjson.loads(activity_config)
     else:
         activity_config = cls.collection.find({'type': activity_type})
         activity_config = [str(a['_id']) for a in activity_config]
         Redis.setex(key, 86400, cjson.dumps(activity_config, 2))
     return activity_config
Exemple #17
0
    def get_user_tasks(cls, user_id):
        key = USER_TASK_KEY % (user_id)
        utasks = Redis.get(key)
        if not utasks:
            utasks = cls._load_user_tasks(user_id)
        else:
            utasks = cjson.loads(utasks)

        utasks = [dict_to_model(UserTask, utask) for utask in utasks]
        return utasks
Exemple #18
0
 def update_counter(cls, countid):
     key = cls.H5KEY.format(countid)
     if not Redis.exists(key):
         num = 1
     else:
         num = int(Redis.get(key)) + 1
         # 限制数字不能超过八位
         if num > 99999999:
             num = 1
     Redis.set(key, num)
     return num
Exemple #19
0
    def get_all_products(cls):
        products = Redis.get(PRODUCT_KEY)
        if products:
            products = cjson.loads(products)
            products = [dict_to_model(Product, p) for p in products]
        else:
            products = list(cls.select())
            _products = [model_to_dict(p) for p in products]
            Redis.setex(PRODUCT_KEY, 86400, cjson.dumps(_products, 2))

        return products
Exemple #20
0
    def get_all_stores(cls):
        stores = Redis.get(ALL_STORE_KEY)
        if stores:
            stores = cjson.loads(stores)
            stores = [dict_to_model(Store, s) for s in stores]
        else:
            stores = list(cls.select())
            _stores = [model_to_dict(s) for s in stores]
            Redis.setex(ALL_STORE_KEY, 86400, cjson.dumps(_stores, 2))

        return stores
Exemple #21
0
    def get_user_products(cls, user_id):
        key = USER_PRODUCT_KEY % (user_id)
        uproducts = Redis.get(key)
        if uproducts:
            uproducts = cjson.loads(uproducts)
            uproducts = [dict_to_model(UserProduct, up) for up in uproducts]
            return uproducts

        uproducts = list(cls.select().where(UserProduct.user_id == user_id))
        _uproducts = [model_to_dict(up) for up in uproducts]
        Redis.setex(key, 86400, cjson.dumps(_uproducts, 2))
        return uproducts
Exemple #22
0
    def get_store_items(cls, store_id):
        key = STORE_ITEM_KEY % ({'store_id': store_id})
        items = Redis.get(key)
        if items:
            items = cjson.loads(items)
            items = [dict_to_model(StoreItem, i) for i in items]
        else:
            items = list(cls.select().where(cls.store_id == int(store_id)))
            _items = [model_to_dict(i) for i in items]
            Redis.setex(key, 86400, cjson.dumps(_items, 2))

        return sorted(items, key=lambda x: x.order)
Exemple #23
0
 def check_user_activity(cls, aid, phone, device):
     # 每个手机号码/设备ID在此类活动历史只能参与1次活动,所以将aid强制改为all
     aid = 'all'
     for k, xid in {'phone': phone, 'device': device}.iteritems():
         key = cls.USER_ACTIVITY % ({'aid': aid, 'xid': xid})
         if not Redis.exists(key):
             cnt = cls._get_user_activity(aid, k, xid)
             Redis.set(key, cnt, 60 * 60 * 24)
         cnt = int(Redis.get(key))
         if cnt:
             return True
     return False
Exemple #24
0
 def get_user_total_gold(cls, uid, begin_at, end_at):
     key = USER_TOTAL_GOLD % ({'uid': uid})
     total_gold = Redis.get(key)
     if not total_gold:
         total_gold = pw.fn.Sum(UserGiftLog.gold_price)
         total_gold = cls.select(total_gold.alias('gold')).where(
             UserGiftLog.user_id == uid,
             UserGiftLog.gift_from == const.FROM_LIVE,
             UserGiftLog.create_at > datetime.fromtimestamp(int(begin_at)),
             UserGiftLog.create_at < datetime.fromtimestamp(int(end_at)))
         total_gold = total_gold.get()
         total_gold = 0 if not total_gold.gold else total_gold.gold
         Redis.setex(key, 86400, total_gold)
     return int(total_gold)
Exemple #25
0
    def get_all_value(cls):
        mon = time.strftime('%Y-%m', time.localtime(time.time()))

        key = 'PayForGift:all_exchange_value:{0}'.format(mon)

        value = Redis.get(key)
        if not value:
            value = 0
            pay_for_gift_logs = list(cls.select())
            for log in pay_for_gift_logs:
                value += log.exchange_value

                Redis.set(key, value)

        return value
Exemple #26
0
    def fetch(cls, key, default, func=None):
        try:
            cache_key = cls.CONFIG_PREFIX % (key)
            value = Redis.get(cache_key)
            if value is None:
                obj = cls.collection.find_one({'key': key})
                value = obj['value'] if obj else None
                if value:
                    Redis.setex(cache_key, 7 * 24 * 3600, value)

            if value is None:
                return default

            return apply(func, [value]) if callable(func) else value
        except:
            return default
Exemple #27
0
    def get_one(cls, oid, check_online=False):
        obj = None
        if not oid:
            return obj

        key = cls.OBJECT_KEY % ({
            'name': cls.__name__.lower(),
            'oid': str(oid)
        })
        # 先从本地内存中获取
        if cls.ENABLE_LOCAL_CACHE and hasattr(cls, 'CACHED_OBJS'):
            obj = cls.CACHED_OBJS.get(key)

        # 从缓存中获取
        if not obj:
            obj = Redis.get(key)
            obj = cls(cjson.loads(obj)) if obj else cls._load_object(oid)
            # 存入本地内存
            if cls.ENABLE_LOCAL_CACHE and hasattr(cls, 'CACHED_OBJS') and obj:
                cls.CACHED_OBJS[key] = obj

        if not obj or (check_online and obj.offline):
            return None
        return obj
Exemple #28
0
 def verify_block(cls, device):
     bkey = cls.BLOCK_KEY % ({'device': device})
     count = Redis.get(bkey)
     return not count or count <= 0
Exemple #29
0
def upload_put_file(file_id):
    """大文件断点上传(PUT&LOGIN)

    :uri: /upload/<file_id>
    :header Range: 片段信息(bytes=offset-length)
    :header Content-Type: application/octet-stream
    :returns: {'size': int, 'total': tuple, 'ranges': list, 'complete': bool, 'url': url}
    """
    meta = _get_upload_file_meta(file_id)
    if not os.path.exists(meta['path']):
        return error.UploadFailed('临时文件不存在')

    new_range = None
    start_bytes = 0
    # 'Range: bytes=offset-length'
    if 'Range' in request.headers:
        range_str = request.headers['Range']
        start_bytes = int(range_str.split('=')[1].split('-')[0])
        if start_bytes < 0 or start_bytes >= meta['size']:
            return error.UploadFailed('上传Range数据有问题')

    with open(meta['path'], 'r+') as f:
        f.seek(start_bytes)
        f.write(request.data)
        new_range = [start_bytes, len(request.data)]

    url = None
    # 处理多线程问题, 进行上锁
    key = 'lock:upload:%s' % (file_id)
    while Redis.get(key):
        time.sleep(0.1)

    with util.Lockit(Redis, key) as locked:
        if locked:
            return error.RedisFailed
        meta = _get_upload_file_meta(file_id)
        file_range = meta['ranges']
        file_range.append(new_range)
        file_range.sort()
        meta['ranges'] = reduce(_range_merge, file_range, [[0, 0]])
        try:
            _set_upload_file_meta(file_id, meta)
        except:
            return error.RedisFailed

    total = reduce(lambda x, y: (0, x[1] + y[1]), meta['ranges'], [0, 0])
    complete = False
    if len(meta['ranges']) == 1 and meta['ranges'][0][1] == meta['size']:
        _path = 'videos' if meta['type'] == 'videos.url' else 'images'
        _file = Media(app.config.get("STATIC_BASE"), _path, None)
        try:
            url = _file.upload_large_file(meta['path'], None,
                                          '.%s' % meta['ext'])
        except:
            return error.UploadFailed
        # 更新对象
        obj, attr = meta['type'].split(".")
        data = {attr: url}
        if obj == 'videos':
            if attr == 'url':
                data['status'] = const.ONLINE
            model = Video.get_one(meta['target_id'], check_online=False)
        elif obj == 'users':
            model = User.get_one(meta['target_id'], check_online=False)

        model.update_model({"$set": data})
        complete = True

    abs_url = urljoin(app.config.get('STATIC_URL'), url) if url else None
    return {
        'size': len(request.data),
        'total': total,
        'url': abs_url,
        'ranges': meta['ranges'],
        'complete': complete
    }
Exemple #30
0
def _get_upload_file_meta(file_id):
    key = "upload:%s" % file_id
    data = Redis.get(key)
    return json.loads(data) if data else None