<s> import os <EOL> import re <EOL> import redis <EOL> import urlparse <EOL> from datetime import datetime <EOL> from base import BaseMatcher <EOL> redis_url = os . environ . get ( '<STR_LIT>' , None ) <EOL> DAILY_ALLOWANCE = <NUM_LIT:10> <EOL> DATE = '<STR_LIT>' <EOL> if redis_url : <EOL> urlparse . uses_netloc . append ( '<STR_LIT>' ) <EOL> url = urlparse . urlparse ( redis_url ) <EOL> r = redis . StrictRedis ( host = url . hostname , port = url . port , db = <NUM_LIT:0> , <EOL> password = url . password ) <EOL> else : <EOL> r = redis . StrictRedis ( ) <EOL> people = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:m>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:f>' , ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:f>' , ) , <EOL> ] <EOL> titles = { <EOL> '<STR_LIT:m>' : '<STR_LIT>' , <EOL> '<STR_LIT:f>' : '<STR_LIT>' <EOL> } <EOL> names = [ p [ <NUM_LIT:0> ] for p in people ] <EOL> pattern = re . compile ( "<STR_LIT>" % '<STR_LIT:|>' . join ( names ) ) <EOL> def utc_str ( ) : <EOL> return datetime . utcnow ( ) . strftime ( '<STR_LIT>' ) <EOL> def from_utc_str ( s ) : <EOL> return datetime . strptime ( s , '<STR_LIT>' ) <EOL> class PointsMatcher ( BaseMatcher ) : <EOL> name = '<STR_LIT>' <EOL> def get_points ( self , person ) : <EOL> name , gender = person <EOL> value = r . hgetall ( name ) <EOL> if not value : <EOL> value = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:left>' : DAILY_ALLOWANCE <EOL> } <EOL> r . hmset ( name , value ) <EOL> title = titles [ gender ] <EOL> return "<STR_LIT>" % ( title , name ) , int ( value [ '<STR_LIT>' ] ) <EOL> def respond ( self , message , user = None ) : <EOL> if user not in names : <EOL> return <EOL> message = message . strip ( ) <EOL> latest_date = r . get ( DATE ) <EOL> now = datetime . utcnow ( ) . date ( ) <EOL> if not latest_date : <EOL> latest_date = utc_str ( ) <EOL> r . set ( DATE , latest_date ) <EOL> else : <EOL> delta = now - from_utc_str ( latest_date ) . date ( ) <EOL> if delta . days > <NUM_LIT:0> : <EOL> for person in names : <EOL> r . hset ( person , '<STR_LIT:left>' , DAILY_ALLOWANCE ) <EOL> r . set ( DATE , utc_str ( ) ) <EOL> if message . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , ) ) : <EOL> values = map ( self . get_points , people ) <EOL> values = filter ( None , values ) <EOL> if not values : <EOL> return <EOL> values . sort ( key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> values . reverse ( ) <EOL> self . speak ( "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % v for v in values ] ) ) <EOL> return <EOL> if message . startswith ( '<STR_LIT>' ) : <EOL> value = r . hget ( user , '<STR_LIT:left>' ) <EOL> self . speak ( user + '<STR_LIT>' % value ) <EOL> return <EOL> res = re . findall ( pattern , message ) <EOL> if not res : <EOL> return <EOL> u , op , value = res [ <NUM_LIT:0> ] <EOL> value = int ( value ) <EOL> if user == u : <EOL> self . speak ( user + '<STR_LIT>' ) <EOL> return <EOL> if u not in names : <EOL> return <EOL> source_user_hash = r . hgetall ( user ) <EOL> dest_user_hash = r . hgetall ( u ) <EOL> if not source_user_hash : <EOL> source_user_hash = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:left>' : DAILY_ALLOWANCE <EOL> } <EOL> if not dest_user_hash : <EOL> dest_user_hash = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:left>' : DAILY_ALLOWANCE <EOL> } <EOL> if op == '<STR_LIT:+>' : <EOL> if int ( source_user_hash . get ( '<STR_LIT:left>' , <NUM_LIT:0> ) ) < value : <EOL> self . speak ( user + '<STR_LIT>' ) <EOL> return <EOL> if op == '<STR_LIT:+>' : <EOL> r . hincrby ( u , '<STR_LIT>' , value ) <EOL> r . hincrby ( user , '<STR_LIT:left>' , value * - <NUM_LIT:1> ) <EOL> if op == '<STR_LIT:->' : <EOL> if int ( source_user_hash . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) < value : <EOL> self . speak ( user + '<STR_LIT>' ) <EOL> else : <EOL> r . hincrby ( u , '<STR_LIT>' , value * - <NUM_LIT:1> ) <EOL> r . hincrby ( user , '<STR_LIT>' , value * - <NUM_LIT:1> ) </s>
