<s> import tgl <EOL> from telex . DatabaseMixin import DatabaseMixin , DbType <EOL> from telex . utils . decorators import group_only <EOL> from functools import partial <EOL> from telex import plugin <EOL> class ChatLogPlugin ( plugin . TelexPlugin , DatabaseMixin ) : <EOL> """<STR_LIT>""" <EOL> HISTORY_QUERY_SIZE = <NUM_LIT:1000> <EOL> patterns = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> usage = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> schema = { <EOL> '<STR_LIT>' : DbType . Integer , <EOL> '<STR_LIT>' : DbType . DateTime , <EOL> '<STR_LIT>' : DbType . Integer , <EOL> '<STR_LIT>' : DbType . Integer , <EOL> '<STR_LIT:username>' : DbType . String , <EOL> '<STR_LIT>' : DbType . String , <EOL> '<STR_LIT:message>' : DbType . String , <EOL> } <EOL> primary_key = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> DatabaseMixin . __init__ ( self ) <EOL> def pre_process ( self , msg ) : <EOL> if not hasattr ( msg , '<STR_LIT:text>' ) : <EOL> return <EOL> if hasattr ( msg . src , '<STR_LIT:username>' ) : <EOL> username = msg . src . username <EOL> else : <EOL> username = "<STR_LIT>" <EOL> self . insert ( msg_id = msg . id , timestamp = msg . date , <EOL> uid = msg . src . id , username = username , <EOL> full_name = "<STR_LIT>" . format ( msg . src . first_name , msg . src . last_name or '<STR_LIT>' ) , <EOL> chat_id = msg . dest . id , message = msg . text ) <EOL> @ group_only <EOL> def seen ( self , msg , matches ) : <EOL> chat_id = msg . dest . id <EOL> if matches . group ( <NUM_LIT:2> ) is not None : <EOL> return self . seen_by_id ( chat_id , matches . group ( <NUM_LIT:2> ) ) <EOL> elif matches . group ( <NUM_LIT:3> ) is not None : <EOL> return self . seen_by_username ( chat_id , matches . group ( <NUM_LIT:3> ) ) <EOL> else : <EOL> return self . seen_by_fullname ( chat_id , matches . group ( <NUM_LIT:4> ) ) <EOL> @ group_only <EOL> def load_history ( self , msg , matches ) : <EOL> chat = msg . dest <EOL> msg_count = <NUM_LIT:0> <EOL> tgl . get_history ( chat , msg_count , <EOL> self . HISTORY_QUERY_SIZE , <EOL> partial ( self . history_cb , msg_count , chat ) ) <EOL> def history_cb ( self , msg_count , chat , success , msgs ) : <EOL> if success : <EOL> self . insert_history ( msgs ) <EOL> msg_count += len ( msgs ) <EOL> if len ( msgs ) == self . HISTORY_QUERY_SIZE : <EOL> tgl . get_history ( chat , msg_count , <EOL> self . HISTORY_QUERY_SIZE , <EOL> partial ( self . history_cb , msg_count , chat ) ) <EOL> else : <EOL> tgl . send_msg ( chat , "<STR_LIT>" . format ( msg_count ) ) <EOL> def insert_history ( self , msgs ) : <EOL> values = [ [ msg . id , msg . date , msg . src . id , msg . src . username or '<STR_LIT>' , <EOL> "<STR_LIT>" . format ( msg . src . first_name or '<STR_LIT>' , msg . src . last_name or '<STR_LIT>' ) , <EOL> msg . dest . id , msg . text ] for msg in msgs if hasattr ( msg , '<STR_LIT:text>' ) and None not in [ msg . src , msg . dest ] ] <EOL> columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:message>' ] <EOL> self . insert_many ( columns , values ) <EOL> @ group_only <EOL> def stats_count ( self , msg , matches ) : <EOL> return self . get_stats ( msg . dest . id ) <EOL> @ group_only <EOL> def stats_pattern ( self , msg , matches ) : <EOL> return self . get_stats ( msg . dest . id , matches . group ( <NUM_LIT:1> ) ) <EOL> def get_stats ( self , chat_id , pattern = None ) : <EOL> pattern_query = "<STR_LIT>" <EOL> if pattern is not None : <EOL> pattern_query = "<STR_LIT>" <EOL> query = """<STR_LIT>""" . format ( self . table_name , self . bot . our_id , chat_id , pattern_query ) <EOL> if ( pattern is not None ) : <EOL> results = self . query ( query , parameters = ( pattern , ) ) <EOL> else : <EOL> results = self . query ( query ) <EOL> if results is None or len ( results ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> text = "<STR_LIT>" <EOL> for result in results : <EOL> text += "<STR_LIT>" . format ( name = result [ "<STR_LIT>" ] , <EOL> count = result [ "<STR_LIT:count>" ] ) <EOL> return text <EOL> def seen_by_username ( self , chat_id , username ) : <EOL> query = """<STR_LIT>""" . format ( self . table_name , chat_id ) <EOL> results = self . query ( query , parameters = ( username , ) ) <EOL> return self . print_scene ( results ) <EOL> def seen_by_fullname ( self , chat_id , name ) : <EOL> query = """<STR_LIT>""" . format ( self . table_name , chat_id ) <EOL> results = self . query ( query , parameters = ( name , ) ) <EOL> return self . print_scene ( results ) <EOL> def seen_by_id ( self , chat_id , uid ) : <EOL> query = """<STR_LIT>""" . format ( self . table_name , chat_id ) <EOL> results = self . query ( query , parameters = ( uid , ) ) <EOL> return self . print_scene ( results ) <EOL> def print_scene ( self , results ) : <EOL> if len ( results ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" . format ( full_name = results [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , timestamp = results [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , msg = results [ <NUM_LIT:0> ] [ "<STR_LIT:message>" ] ) </s>
