Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. import binascii
  2. import redis
  3. import sys
  4.  
  5. # user state
  6.  
  7. class UserStatus:
  8.     def __init__(self):
  9.         self.current_user = 'guest'
  10.         self.logged = False
  11.  
  12. def get_user_id(login):
  13.     return 'usr_' + login
  14.  
  15. # create short link using hash
  16.  
  17. def generate_short_link(long_link):
  18.     DB.incr('links_cnt')
  19.     ID = DB.get('links_cnt')
  20.     short_link = 'our_site.com/' + str(abs(binascii.crc32(long_link))) + str(ID)
  21.     return short_link
  22.  
  23. # DB interface functions
  24.  
  25. def create_user(login, password):
  26.     user_ID = get_user_id(login)
  27.     if DB.exists(user_ID):
  28.         print('User wit login ' + login + ' already exists')
  29.     else:
  30.         user = {'login': login, 'password': password, 'links_created': 0}
  31.         DB.hmset(user_ID, user)
  32.         print('User ' + login + ' has been created!')
  33.  
  34. def try_login(login, password):
  35.     user_ID = get_user_id(login)
  36.     if DB.exists(user_ID):
  37.         if DB.hget(user_ID, 'password') == password:
  38.             return True
  39.     return False
  40.  
  41. def add_link(short_link, long_link, user_login):
  42.     user_ID = get_user_id(user_login)
  43.     value = {'link': long_link, 'counter': 0}
  44.     DB.hincrby(user_ID, 'links_created');
  45.     DB.hmset(short_link, value)
  46.  
  47. def get_link(short_link):
  48.     DB.hincrby(short_link, "counter")
  49.     return DB.hget(short_link, 'link')
  50.  
  51. def get_stat(short_link):
  52.     return DB.hget(short_link, 'counter')
  53.  
  54. def get_user_links_count(login):
  55.     user_ID = get_user_id(login)
  56.     if DB.exists(user_ID):
  57.         return DB.hget(user_ID, 'links_created')
  58.     else:
  59.         return -1;
  60.  
  61. # parse commands functions
  62.  
  63. def get_command_name(full_command):
  64.     parsed_command = full_command.split(" ")
  65.     return parsed_command[0]
  66.  
  67. def process_command(command_name, full_command, user_state):
  68.     parsed_command = full_command.split(" ")
  69.  
  70.     if user_state.logged is False:
  71.         if command_name == 'create_user':
  72.             login = parsed_command[1]
  73.             password = parsed_command[2]
  74.             create_user(login, password)
  75.         elif command_name == 'login':
  76.             if user_state.logged == False:
  77.                 login = parsed_command[1]
  78.                 password = parsed_command[2]
  79.                 if try_login(login, password):
  80.                     user_state.current_user = login
  81.                     user_state.logged = True
  82.                     print ('Login successful!')
  83.                 else:
  84.                     print ('Incorrect login info!')
  85.             else:
  86.                 print('You already logged as ' + user_state.current_user)
  87.         elif command_name == 'exit':
  88.             print('Aborting...')
  89.         else:
  90.             print('On guest user you can only create user or login!')
  91.     else:
  92.         if command_name == 'logout':
  93.             user_state.logged = False
  94.             user_state.current_user = 'guest'
  95.         elif command_name == 'get_user_stat':
  96.             user_to_check = parsed_command[1]
  97.             links_count = get_user_links_count(user_to_check)
  98.             if links_count != -1:
  99.                 print('User ' + user_to_check + ' created ' + links_count + " links.")
  100.             else:
  101.                 print('Invalid user name!')
  102.         elif command_name == 'add_link':
  103.             long_link = parsed_command[1]
  104.             short_link = generate_short_link(long_link)
  105.             add_link(short_link, long_link, user_state.current_user)
  106.             print('Added ' + short_link + ' to database.')
  107.         elif command_name == 'get_link':
  108.             short_link = parsed_command[1]
  109.             print('Corresponding link: ' + get_link(short_link))
  110.         elif command_name == 'get_link_stat':
  111.             short_link = parsed_command[1]
  112.             print('Link ' + short_link + ' used ' + get_stat(short_link) + ' times')
  113.         elif command_name == 'exit':
  114.             print('Aborting...')
  115.         else:
  116.             print('Invalid command!')
  117.  
  118. # main program text
  119.  
  120. command = 'empty'
  121. user_state = UserStatus()
  122. DB = redis.StrictRedis(host='localhost', port=6379, db=0)
  123.  
  124. while command != 'exit':
  125.     command = raw_input(user_state.current_user + " $: ")
  126.     command_name = get_command_name(command)
  127.     try:
  128.         process_command(command_name, command, user_state)
  129.     except IndexError:
  130.         print 'Not enough command parameters!'
  131.     print("\n");
  132.  
  133. print('See you around!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement