Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MIT License
- #
- # Copyright (c) 2017 matteo bocci aka matteob99
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in all
- # copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- from datetime import datetime as dt
- import redis
- from dateutil.parser import parse
- import config
- r = redis.StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT,
- db=config.REDIS_DB, password=config.REDIS_PASSWORD)
- class User:
- """
- User base object
- """
- def __init__(self, user):
- """
- Create an user object
- :param user: Telegram's user object
- """
- self.id = user.id
- self.rhash = 'user:' + str(user.id)
- # Redis database
- if r.hget(self.rhash, 'id') != user.id:
- r.hset(self.rhash, 'id', user.id)
- if r.hget(self.rhash, 'username') != user.username:
- r.hset(self.rhash, 'username', user.username)
- if r.hget(self.rhash, 'first_name') != user.first_name:
- r.hset(self.rhash, 'first_name', user.first_name)
- if r.hget(self.rhash, 'last_name') != user.last_name:
- r.hset(self.rhash, 'last_name', user.last_name)
- r.hset(self.rhash, 'last_update', dt.now())
- if not self.state():
- r.hset(self.rhash, 'state', 'home')
- if not self.stateold():
- r.hset(self.rhash, 'stateold', 'home')
- if not self.lastr():
- r.hset(self.rhash, "lastr", 0)
- if not self.idrichiesta():
- r.hset(self.rhash, "idrichiesta", 0)
- r.hset(self.rhash, "active", True)
- def state(self, new_state=None):
- """
- Get current user state or set a new user state
- :param new_state: new state for the user
- :return: state
- """
- if not new_state:
- return r.hget(self.rhash, 'state')
- self.stateold(self.state())
- r.hset(self.rhash, 'state', new_state)
- return True
- def stateold(self, new_stateold=None):
- if not new_stateold:
- return r.hget(self.rhash, 'state')
- r.hset(self.rhash, 'stateold', new_stateold)
- return True
- def lastr(self, time=None):
- if not time:
- return r.hget(self.rhash, "lastr")
- r.hset(self.rhash, "lastr", dt.now())
- def idrichiesta(self, idrichiesta=None):
- if not idrichiesta:
- return r.hget(self.rhash, "idrichiesta")
- r.hset(self.rhash, "idrichiesta", idrichiesta)
- def setRedis(self, key, value):
- """
- Set a redis value
- :param key: redis key
- :param value: redis value
- :return: value
- """
- return r.hset(self.rhash, key, value)
- def getRedis(self, key):
- """
- Get a redis value
- :param key: redis key
- :return: value
- """
- return r.hget(self.rhash, key)
- def delRedis(self, key):
- """
- Delete a redis value
- :param key: redis key
- :return: None
- """
- return r.hdel(self.rhash, key)
- def increaseStat(self, stat):
- """
- Increase a stat value
- :param stat: which stat increase
- :return: redis response
- """
- response = r.hincrby(self.rhash, stat)
- return response
- def isActive(self):
- return bool(r.hget(self.rhash, "active")) or False
- def first_name(self):
- r.hget(self.rhash, 'first_name')
- def last_name(self):
- r.hget(self.rhash, 'last_name')
- def userrequest(self, idrequest=None):
- if not idrequest:
- return False
- self.rhash = 'user:' + str(idrequest)
- returnv = [r.hget(self.rhash, 'id'),
- r.hget(self.rhash, 'username'),
- r.hget(self.rhash, 'first_name'),
- r.hget(self.rhash, 'last_name')]
- return returnv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement