<s> from obj import History <EOL> import log <EOL> import config <EOL> import datetime <EOL> import sqlite3 as sqlite <EOL> import codecs <EOL> from os import path <EOL> LOG = log . get_logger ( "<STR_LIT>" ) <EOL> if config . LANG . upper ( ) == '<STR_LIT>' : <EOL> import zhuaxia . i18n . msg_cn as msg <EOL> else : <EOL> import zhuaxia . i18n . msg_en as msg <EOL> SELECT_PART = """<STR_LIT>""" <EOL> INSERT_STMT = """<STR_LIT>""" <EOL> def __getConnection ( ) : <EOL> conn = sqlite . connect ( config . HIST_DB ) <EOL> return conn <EOL> def get_history_count ( ) : <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> count = dao . get_history_count ( ) <EOL> conn . close ( ) <EOL> return count <EOL> def empty_hists ( ) : <EOL> """<STR_LIT>""" <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> hists = dao . delete_all ( ) <EOL> print log . hl ( msg . history_cleared , '<STR_LIT>' ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> def export_hists ( ) : <EOL> """<STR_LIT>""" <EOL> print log . hl ( msg . history_exporting , '<STR_LIT>' ) <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> hists = dao . get_all_histories ( ) <EOL> filename = path . join ( config . DOWNLOAD_DIR , '<STR_LIT>' + str ( datetime . datetime . today ( ) ) + "<STR_LIT>" ) <EOL> with codecs . open ( filename , '<STR_LIT:w>' , '<STR_LIT:utf-8>' ) as f : <EOL> f . write ( "<STR_LIT>" ) <EOL> for h in hists : <EOL> f . write ( h . to_csv ( ) ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> print log . hl ( msg . history_exported % filename , '<STR_LIT>' ) <EOL> conn . close ( ) <EOL> def insert_hist ( songs ) : <EOL> """<STR_LIT>""" <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> for song in songs : <EOL> if song . success : <EOL> hist = dao . get_history ( song ) <EOL> if hist : <EOL> dao . update_history ( hist , song ) <EOL> else : <EOL> dao . insert_history ( song ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> def filter_songs ( songs ) : <EOL> """<STR_LIT>""" <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> skipped , hist_list = [ ] , [ ] <EOL> for song in songs : <EOL> hist = dao . get_history ( song ) <EOL> if hist : <EOL> skipped . append ( song ) <EOL> hist_list . append ( hist ) <EOL> conn . close ( ) <EOL> return skipped , hist_list <EOL> def empty_history ( ) : <EOL> """<STR_LIT>""" <EOL> conn = __getConnection ( ) <EOL> dao = HistDao ( conn ) <EOL> dao . delete_all ( conn ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> class HistDao ( object ) : <EOL> def __init__ ( self , conn ) : <EOL> self . conn = conn <EOL> def get_history ( self , song ) : <EOL> LOG . debug ( "<STR_LIT>" , song . song_name ) <EOL> sql = SELECT_PART + """<STR_LIT>""" <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql , ( song . song_id , song . song_type , song . handler . is_hq ) ) <EOL> hist = self . __row2hist ( cur . fetchone ( ) ) <EOL> cur . close ( ) <EOL> return hist <EOL> def get_history_count ( self ) : <EOL> sql = "<STR_LIT>" <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql ) <EOL> count = cur . fetchone ( ) [ <NUM_LIT:0> ] <EOL> cur . close ( ) <EOL> return count <EOL> def get_all_histories ( self ) : <EOL> sql = SELECT_PART <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql ) <EOL> all_hists = [ self . __row2hist ( row ) for row in cur . fetchall ( ) ] <EOL> cur . close ( ) <EOL> return all_hists <EOL> def insert_history ( self , song ) : <EOL> sql = INSERT_STMT <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql , ( song . song_id , song . song_name , song . handler . is_hq , song . song_type , song . abs_path , song . dl_link , datetime . datetime . today ( ) ) ) <EOL> cur . close ( ) <EOL> def update_history ( self , hist , song ) : <EOL> sql = """<STR_LIT>""" <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql , ( datetime . datetime . today ( ) , song . abs_path , song . dl_link , hist . id ) ) <EOL> cur . close ( ) <EOL> def delete_all ( self ) : <EOL> sql = """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> cur = self . conn . cursor ( ) <EOL> cur . execute ( sql ) <EOL> cur . close ( ) <EOL> def __row2hist ( self , row ) : <EOL> if row : <EOL> hist = History ( ) <EOL> ( hist . id , hist . song_id , hist . song_name , hist . hq , hist . source , hist . location , hist . api_url , hist . dl_time , hist . times ) = row <EOL> return hist <EOL> return None </s>
