Beispiel #1
0
class UpdatingLogger(object):
    date_format = '%Y.%m.%d..%H.%M.%S'

    def __init__(self, connection, prefix, name):
        self.conn = connection
        self.prefix = prefix
        self.name = name
        self.id_key = RedisKey('%s:id' % self.prefix, self.conn)
        self.set_key = RedisKey('%s:set' % self.prefix, self.conn)

    def save(self, date, value, t):
        id = self.id_key.incr()
        key = '%s:obj:%s' % (self.prefix, id)
        date_str = date.strftime(self.date_format)
        self.conn.hmset(key, {'value': value, 'date': date_str, 'time': t})
        self.set_key.sadd(id)

    def __getitem__(self, k):
        """
        Retrieves an item or slice from the set of results.
        """
        if not isinstance(k, slice):
            raise TypeError

        num = k.stop - k.start
        key = self.set_key.redis_key
        p = self.prefix
        get = [
            '%s:obj:*->value' % p,
            '%s:obj:*->date' % p,
            '%s:obj:*->time' % p
        ]
        return self._to_python(
            self.conn.sort(key, get=get, start=k.start, num=num, desc=True))

    def count(self):
        return self.set_key.scard()

    def _get_date(self, value):
        return datetime.datetime.strptime(value, self.date_format)

    def _clone(self):
        return self

    def _to_python(self, result):
        output = []

        for i in range(len(result))[::3]:
            obj = {}
            obj['value'] = result[i]
            obj['date'] = self._get_date(result[i + 1])
            obj['time'] = result[i + 2]
            output.append(obj)

        return output
class UpdatingLogger(object):
    date_format = '%Y.%m.%d..%H.%M.%S' 
    
    def __init__(self, connection, prefix, name):
        self.conn = connection
        self.prefix = prefix
        self.name = name
        self.id_key = RedisKey('%s:id' % self.prefix, self.conn)
        self.set_key = RedisKey('%s:set' % self.prefix, self.conn)
        
    def save(self, date, value, t):
        id = self.id_key.incr()
        key = '%s:obj:%s' % (self.prefix, id)
        date_str = date.strftime(self.date_format)
        self.conn.hmset(key, {'value': value, 'date': date_str, 'time': t})
        self.set_key.sadd(id)

    def __getitem__(self, k):
        """
        Retrieves an item or slice from the set of results.
        """
        if not isinstance(k, slice):
            raise TypeError
        
        num = k.stop - k.start
        key = self.set_key.redis_key
        p = self.prefix
        get = ['%s:obj:*->value' % p, '%s:obj:*->date' % p, '%s:obj:*->time' % p]
        return self._to_python(self.conn.sort(key, get=get, start=k.start, num=num, desc=True))
    
    def count(self):
        return self.set_key.scard()
    
    def _get_date(self, value):
        return datetime.datetime.strptime(value, self.date_format)
    
    def _clone(self):
        return self
    
    def _to_python(self, result):
        output = []
        
        for i in range(len(result))[::3]:
            obj = {}
            obj['value'] = result[i]
            obj['date'] = self._get_date(result[i+1])
            obj['time'] = result[i+2]
            output.append(obj)
            
        return output
Beispiel #3
0
 def list(self):
     for key in self.key_set.smembers():
         obj = {}
         key_parts = key.split(':')
         obj['cls'], obj['method'] = key_parts[3], key_parts[4]
         obj['value'] = RedisKey(key).val
         yield obj
Beispiel #4
0
def update_subtitles_fetch_counter(video, sub_lang=None, date=None):
    if sub_lang:
        lang = sub_lang.language
    else:
        lang = None
    key = get_fetch_subtitles_key(video, lang, date)
    sub_fetch_keys_set.sadd(key)
    RedisKey(key).incr()
    sub_fetch_total_counter.incr()
Beispiel #5
0
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.
from datetime import datetime
from utils.redis_utils import default_connection, RedisKey

SUBTITLES_FETCH_PREFIX = 'StSubFetch'

video_view_counter = RedisKey('StVideoView')
sub_fetch_total_counter = RedisKey('StSubFetchTotal')
widget_views_total_counter = RedisKey('StWidgetViewsTotal')
sub_fetch_keys_set = RedisKey('StSubFetchKeys')

changed_video_set = RedisKey('StChangedVideos')


def get_fetch_subtitles_key(video, lang=None, date=None):
    if not date:
        date = datetime.today()

    key = '%s:%s' % (SUBTITLES_FETCH_PREFIX, video.video_id)

    if lang:
        key += ':%s' % lang
Beispiel #6
0
    def _get_key(self, key):
        if not key in self.keys:
            self.keys[key] = RedisKey(
                '%s:%s:%s' % (self.site_name, self.prefix, key), self.conn)

        return self.keys[key]
 def __init__(self, connection, prefix, name):
     self.conn = connection
     self.prefix = prefix
     self.name = name
     self.id_key = RedisKey('%s:id' % self.prefix, self.conn)
     self.set_key = RedisKey('%s:set' % self.prefix, self.conn)
Beispiel #8
0
 def __init__(self, connection, prefix, name):
     self.conn = connection
     self.prefix = prefix
     self.name = name
     self.id_key = RedisKey('%s:id' % self.prefix, self.conn)
     self.set_key = RedisKey('%s:set' % self.prefix, self.conn)
Beispiel #9
0
 def _create_total_key(self):
     return RedisKey('%s:total' % self.prefix, self.connection)
Beispiel #10
0
 def _create_set_key(self):
     return RedisKey('%s:set' % self.prefix, self.connection)