Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import binascii
- import redis
- import sys
- # user state
- class UserStatus:
- def __init__(self):
- self.current_user = 'guest'
- self.logged = False
- def get_user_id(login):
- return 'usr_' + login
- # create short link using hash
- def generate_short_link(long_link):
- DB.incr('links_cnt')
- ID = DB.get('links_cnt')
- short_link = 'our_site.com/' + str(abs(binascii.crc32(long_link))) + str(ID)
- return short_link
- # DB interface functions
- def create_user(login, password):
- user_ID = get_user_id(login)
- if DB.exists(user_ID):
- print('User wit login ' + login + ' already exists')
- else:
- user = {'login': login, 'password': password, 'links_created': 0}
- DB.hmset(user_ID, user)
- print('User ' + login + ' has been created!')
- def try_login(login, password):
- user_ID = get_user_id(login)
- if DB.exists(user_ID):
- if DB.hget(user_ID, 'password') == password:
- return True
- return False
- def add_link(short_link, long_link, user_login):
- user_ID = get_user_id(user_login)
- value = {'link': long_link, 'counter': 0}
- DB.hincrby(user_ID, 'links_created');
- DB.hmset(short_link, value)
- def get_link(short_link):
- DB.hincrby(short_link, "counter")
- return DB.hget(short_link, 'link')
- def get_stat(short_link):
- return DB.hget(short_link, 'counter')
- def get_user_links_count(login):
- user_ID = get_user_id(login)
- if DB.exists(user_ID):
- return DB.hget(user_ID, 'links_created')
- else:
- return -1;
- # parse commands functions
- def get_command_name(full_command):
- parsed_command = full_command.split(" ")
- return parsed_command[0]
- def process_command(command_name, full_command, user_state):
- parsed_command = full_command.split(" ")
- if user_state.logged is False:
- if command_name == 'create_user':
- login = parsed_command[1]
- password = parsed_command[2]
- create_user(login, password)
- elif command_name == 'login':
- if user_state.logged == False:
- login = parsed_command[1]
- password = parsed_command[2]
- if try_login(login, password):
- user_state.current_user = login
- user_state.logged = True
- print ('Login successful!')
- else:
- print ('Incorrect login info!')
- else:
- print('You already logged as ' + user_state.current_user)
- elif command_name == 'exit':
- print('Aborting...')
- else:
- print('On guest user you can only create user or login!')
- else:
- if command_name == 'logout':
- user_state.logged = False
- user_state.current_user = 'guest'
- elif command_name == 'get_user_stat':
- user_to_check = parsed_command[1]
- links_count = get_user_links_count(user_to_check)
- if links_count != -1:
- print('User ' + user_to_check + ' created ' + links_count + " links.")
- else:
- print('Invalid user name!')
- elif command_name == 'add_link':
- long_link = parsed_command[1]
- short_link = generate_short_link(long_link)
- add_link(short_link, long_link, user_state.current_user)
- print('Added ' + short_link + ' to database.')
- elif command_name == 'get_link':
- short_link = parsed_command[1]
- print('Corresponding link: ' + get_link(short_link))
- elif command_name == 'get_link_stat':
- short_link = parsed_command[1]
- print('Link ' + short_link + ' used ' + get_stat(short_link) + ' times')
- elif command_name == 'exit':
- print('Aborting...')
- else:
- print('Invalid command!')
- # main program text
- command = 'empty'
- user_state = UserStatus()
- DB = redis.StrictRedis(host='localhost', port=6379, db=0)
- while command != 'exit':
- command = raw_input(user_state.current_user + " $: ")
- command_name = get_command_name(command)
- try:
- process_command(command_name, command, user_state)
- except IndexError:
- print 'Not enough command parameters!'
- print("\n");
- print('See you around!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement