diff --git a/zhuaxia/hist_handler.py b/zhuaxia/hist_handler.py index b23057b..2d64204 100644 --- a/zhuaxia/hist_handler.py +++ b/zhuaxia/hist_handler.py @@ -25,6 +25,22 @@ def __getConnection(): conn = sqlite.connect(config.HIST_DB) return conn +def get_history_count(): + conn = __getConnection() + dao = HistDao(conn) + count = dao.get_history_count() + conn.close() + return count + +def empty_hists(): + """empty history table""" + conn = __getConnection() + dao = HistDao(conn) + hists = dao.delete_all() + print log.hl(msg.history_cleared,'cyan') + conn.commit() + conn.close() + def export_hists(): """ export all history data @@ -106,6 +122,14 @@ def get_history(self,song): cur.close() return hist + def get_history_count(self): + sql = "select count(*) from History" + cur = self.conn.cursor() + cur.execute(sql) + count = cur.fetchone()[0] + cur.close() + return count + def get_all_histories(self): sql = SELECT_PART cur = self.conn.cursor() diff --git a/zhuaxia/i18n/msg_cn.py b/zhuaxia/i18n/msg_cn.py index 83e301b..459b5bc 100644 --- a/zhuaxia/i18n/msg_cn.py +++ b/zhuaxia/i18n/msg_cn.py @@ -13,6 +13,9 @@ summary_prompt_err = u" 无效输入\n" summary_saved = u" 下载报告保存于: %s" +history_clear_confirm = u" 找到 %d 条下载记录, 确认要清空所有下载历史记录? [y/n]" +history_clearing = u" 忽略其它选项,清空zhuaxia下载历史记录..." +history_cleared = u" zhuaxia所有下载记录已清空" history_exporting = u" 忽略其它选项, 正在导出下载历史记录..." history_exported = u" 下载历史记录导出到: %s" diff --git a/zhuaxia/i18n/msg_en.py b/zhuaxia/i18n/msg_en.py index 6898327..1c1d3b3 100644 --- a/zhuaxia/i18n/msg_en.py +++ b/zhuaxia/i18n/msg_en.py @@ -13,6 +13,9 @@ summary_prompt_err = u"Invalid input.\n" summary_saved = u" summary was saved at: %s" +history_clear_confirm = u" %d downloading histories found. Are you sure to remove all these histories? [y/n]" +history_clearing = u" Clear zhuaxia downloading hisotory (other options will be ignored)" +history_cleared = u" All zhuaxia download history has been cleared." history_exporting = u" Exporting download history (other options will be ignored)..." history_exported = u" Zhuaxia download-history was exported to: %s" diff --git a/zx b/zx index 01f72f9..2fec50f 100755 --- a/zx +++ b/zx @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding:utf-8 -*- import zhuaxia.config as config +import sys #load config at very beginning config.load_config() @@ -36,6 +37,23 @@ def version(): """print version""" print msg.ver_text + zxver.version +def export_or_clear_hist(export_hist, empty_hist): + if export_hist and empty_hist : raise getopt.GetoptError("option -e and -d cannot be used together!") + if export_hist: + hist_handler.export_hists() + sys.exit(0) + elif empty_hist: + print log.hl(msg.history_clearing ,'cyan') + while True: + count = hist_handler.get_history_count() + sys.stdout.write(msg.history_clear_confirm % count) + choice = raw_input().lower() + if choice == 'n': + sys.exit(0) + elif choice == 'y': + hist_handler.empty_hists() + sys.exit(0) + if __name__ == '__main__': log.setup_log(logger_name, config.LOG_LVL_CONSOLE, config.LOG_LVL_FILE) LOG = log.get_logger(logger_name) @@ -47,10 +65,12 @@ if __name__ == '__main__': needProxyPool = False dl_lyric = False incremental_dl = False + export_hist = False + empty_hist = False if len(sys.argv)<2: raise getopt.GetoptError("parameters are missing...") - opts, args = getopt.getopt(sys.argv[1:],":f:Hhvplie") + opts, args = getopt.getopt(sys.argv[1:],":f:Hhvplied") for o, a in opts: if o == '-h': usage() @@ -70,10 +90,14 @@ if __name__ == '__main__': elif o == '-i': incremental_dl = True elif o == '-e': #export history data - hist_handler.export_hists() - sys.exit(0) - + export_hist = True + elif o == '-d': #empty history + empty_hist = True + + #handle the export and empty + export_or_clear_hist(export_hist, empty_hist) + ## -f check file if not inFile: if not len(args): raise getopt.GetoptError("input file or xiami url is needed") @@ -92,9 +116,10 @@ if __name__ == '__main__': #print debug option.debug_me() + # ok, let's go! commander.shall_I_begin(option) except getopt.GetoptError as e: LOG.error(str(e)) - usage() + print log.hl("\nTo know how to use zhuaxia, run:\n\tzx -h\n",'cyan') sys.exit(2)