diff --git a/README.md b/README.md index 84f87de..a42abe3 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ print(result) from TaiwanLottery import TaiwanLotteryCrawler lottery = TaiwanLotteryCrawler() -result = lottery.super_lotto(False, False, ['112', '6']) +result = lottery.super_lotto(['112', '6']) print(result) ``` @@ -92,7 +92,7 @@ print(result) from TaiwanLottery import TaiwanLotteryCrawler lottery = TaiwanLotteryCrawler() -result = lottery.lotto649(False, False, ['112', '6']) +result = lottery.lotto649(['112', '6']) print(result) ``` @@ -102,7 +102,7 @@ print(result) from TaiwanLottery import TaiwanLotteryCrawler lottery = TaiwanLotteryCrawler() -result = lottery.daily_cash(False, False, ['112', '6']) +result = lottery.daily_cash(['112', '6']) print(result) ``` @@ -112,31 +112,7 @@ print(result) from TaiwanLottery import TaiwanLotteryCrawler lottery = TaiwanLotteryCrawler() -result = lottery.lotto1224(False, False, ['112', '6']) -print(result) -``` - ---- - -### 參數設定 - -關閉 Terminal 顯示 - -```python -from TaiwanLottery import TaiwanLotteryCrawler - -lottery = TaiwanLotteryCrawler() -result = lottery.lotto649(False) -print(result) -``` - -儲存開獎結果成 JSON 檔案 - -```python -from TaiwanLottery import TaiwanLotteryCrawler - -lottery = TaiwanLotteryCrawler() -result = lottery.lotto649(False, True) +result = lottery.lotto1224(['112', '6']) print(result) ``` diff --git a/TaiwanLottery/__init__.py b/TaiwanLottery/__init__.py index 0a653b8..158824e 100644 --- a/TaiwanLottery/__init__.py +++ b/TaiwanLottery/__init__.py @@ -13,7 +13,7 @@ class TaiwanLotteryCrawler(): no_data = '查無資料' # 威力彩 - def super_lotto(self, is_print_data=True, is_output=False, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): + def super_lotto(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): URL = 'https://www.taiwanlottery.com.tw/Lotto/SuperLotto638/history.aspx' title = '威力彩_' + str(back_time[0]) + '_' + str(back_time[1]) @@ -66,14 +66,10 @@ def super_lotto(self, is_print_data=True, is_output=False, back_time=[utils.get_ logging.warning(self.no_data + title) return - if is_print_data: - utils.print_to_table(title, datas) - if is_output: - utils.output_to_json(title, datas) return datas # 大樂透 - def lotto649(self, is_print_data=True, is_output=False, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): + def lotto649(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): URL = 'https://www.taiwanlottery.com.tw/Lotto/Lotto649/history.aspx' title = '大樂透_' + str(back_time[0]) + '_' + str(back_time[1]) @@ -126,14 +122,10 @@ def lotto649(self, is_print_data=True, is_output=False, back_time=[utils.get_cur logging.warning(self.no_data + title) return - if is_print_data: - utils.print_to_table(title, datas) - if is_output: - utils.output_to_json(title, datas) return datas # 今彩539 - def daily_cash(self, is_print_data=True, is_output=False, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): + def daily_cash(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): URL = 'https://www.taiwanlottery.com.tw/Lotto/Dailycash/history.aspx' title = '今彩539_' + str(back_time[0]) + '_' + str(back_time[1]) @@ -183,14 +175,11 @@ def daily_cash(self, is_print_data=True, is_output=False, back_time=[utils.get_c if len(datas) == 0: logging.warning(self.no_data + title) return - if is_print_data: - utils.print_to_table(title, datas) - if is_output: - utils.output_to_json(title, datas) + return datas # 雙贏彩 - def lotto1224(self, is_print_data=True, is_output=False, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): + def lotto1224(self, back_time=[utils.get_current_republic_era(), utils.get_current_month()]): URL = 'https://www.taiwanlottery.com.tw/Lotto/Lotto1224/history.aspx' title = '雙贏彩_' + str(back_time[0]) + '_' + str(back_time[1]) @@ -240,36 +229,33 @@ def lotto1224(self, is_print_data=True, is_output=False, back_time=[utils.get_cu if len(datas) == 0: logging.warning(self.no_data + title) return - if is_print_data: - utils.print_to_table(title, datas) - if is_output: - utils.output_to_json(title, datas) + return datas # 威力彩歷史查詢 - def super_lotto_back(self, is_print_data=True, is_output=True, back_month='0'): + def super_lotto_back(self, back_month='0'): for i in range(int(back_month), -1, -1): time.sleep(random.random()) - self.super_lotto(is_print_data, is_output, utils.month_diff(i)) + self.super_lotto(utils.month_diff(i)) logging.debug(str(utils.month_diff(i)[0]) + '_' + str(utils.month_diff(i)[1])) # 大樂透歷史查詢 - def lotto649_back(self, is_print_data=True, is_output=True, back_month='0'): + def lotto649_back(self, back_month='0'): for i in range(int(back_month), -1, -1): time.sleep(random.random()) - self.lotto649(is_print_data, is_output, utils.month_diff(i)) + self.lotto649(utils.month_diff(i)) logging.debug(str(utils.month_diff(i)[0]) + '_' + str(utils.month_diff(i)[1])) # 今彩539歷史查詢 - def daily_cash_back(self, is_print_data=True, is_output=True, back_month='0'): + def daily_cash_back(self, back_month='0'): for i in range(int(back_month), -1, -1): time.sleep(random.random()) - self.daily_cash(is_print_data, is_output, utils.month_diff(i)) + self.daily_cash(utils.month_diff(i)) logging.debug(str(utils.month_diff(i)[0]) + '_' + str(utils.month_diff(i)[1])) # 雙贏彩歷史查詢 - def lotto1224_back(self, is_print_data=True, is_output=True, back_month='0'): + def lotto1224_back(self, back_month='0'): for i in range(int(back_month), -1, -1): time.sleep(random.random()) - self.lotto1224(is_print_data, is_output, utils.month_diff(i)) + self.lotto1224(utils.month_diff(i)) logging.debug(str(utils.month_diff(i)[0]) + '_' + str(utils.month_diff(i)[1])) diff --git a/crawl.py b/crawl.py deleted file mode 100644 index 74bd08a..0000000 --- a/crawl.py +++ /dev/null @@ -1,85 +0,0 @@ -# -*- coding: utf-8 -*- -import argparse -import logging -from TaiwanLottery import TaiwanLotteryCrawler -from TaiwanLottery import utils - -logging.basicConfig(level=logging.INFO) - -LOTTERY_GAME_MAPPING = { - 'SUPER_LOTTO': '威力彩', - 'LOTTO649': '大樂透', - 'DAILY_CASH': '今彩539', - 'LOTTO1224': '雙贏彩', -} - - -def main(): - parser = argparse.ArgumentParser( - description='Taiwan Lottery 台灣彩券爬蟲', - epilog='https://github.com/stu01509/TaiwanLottery') - - parser.add_argument('game', nargs='?', default='', - help='爬取指定的彩券類型, 威力彩 大樂透 今彩539 雙贏彩') - parser.add_argument('-b', '--back', default=0, - help='爬取幾個月前的資料') - parser.add_argument('-t', '--time', default=0, - help='爬取指定年月份的資料 格式(YYYY-MM)') - parser.add_argument('-o', '--output', action='store_true', - help='將爬取資料輸出成 json') - - args = parser.parse_args() - logging.debug(args) - - lottery = TaiwanLotteryCrawler() - - if (args.output is False and args.game == '' and args.back == 0 and args.time == 0): - lottery.super_lotto() - lottery.lotto649() - lottery.daily_cash() - lottery.lotto1224() - elif (args.output is True and args.game == '' and args.back == 0 and args.time == 0): - lottery.super_lotto(True, True) - lottery.lotto649(True, True) - lottery.daily_cash(True, True) - lottery.lotto1224(True, True) - elif (args.output is False and args.game != '' and args.back != 0 and args.time == 0): - if (args.game == LOTTERY_GAME_MAPPING['SUPER_LOTTO']): - lottery.super_lotto_back(True, False, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO649']): - lottery.lotto649_back(True, False, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['DAILY_CASH']): - lottery.daily_cash_back(True, False, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO1224']): - lottery.lotto1224_back(True, False, args.back) - elif (args.output is True and args.game != '' and args.back != 0 and args.time == 0): - if (args.game == LOTTERY_GAME_MAPPING['SUPER_LOTTO']): - lottery.super_lotto_back(True, True, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO649']): - lottery.lotto649_back(True, True, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['DAILY_CASH']): - lottery.daily_cash_back(True, True, args.back) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO1224']): - lottery.lotto1224_back(True, True, args.back) - elif (args.output is False and args.game != '' and args.back == 0 and args.time != 0): - if (args.game == LOTTERY_GAME_MAPPING['SUPER_LOTTO']): - lottery.super_lotto(True, False, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO649']): - lottery.lotto649(True, False, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['DAILY_CASH']): - lottery.daily_cash(True, False, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO1224']): - lottery.lotto1224(True, False, utils.convert_to_republic_era_month(args.time)) - elif (args.output is True and args.game != '' and args.back == 0 and args.time != 0): - if (args.game == LOTTERY_GAME_MAPPING['SUPER_LOTTO']): - lottery.super_lotto(True, True, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO649']): - lottery.lotto649(True, True, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['DAILY_CASH']): - lottery.daily_cash(True, True, utils.convert_to_republic_era_month(args.time)) - elif (args.game == LOTTERY_GAME_MAPPING['LOTTO1224']): - lottery.lotto1224(True, True, utils.convert_to_republic_era_month(args.time)) - - -if __name__ == '__main__': - main() diff --git a/tests/test_lottery.py b/tests/test_lottery.py index aaed416..4a4f697 100644 --- a/tests/test_lottery.py +++ b/tests/test_lottery.py @@ -7,7 +7,7 @@ def test_super_lotto(): lottery = TaiwanLotteryCrawler() # When user get the 威力彩 2023-06 result without print and output to json - super_lotto_result = lottery.super_lotto(False, False, [112, 6]) + super_lotto_result = lottery.super_lotto([112, 6]) # Then the super_lotto_result should be equal to test result assert super_lotto_result == [ @@ -28,7 +28,7 @@ def test_lotto649(): lottery = TaiwanLotteryCrawler() # When user get the 大樂透 2023-06 result without print and output to json - lotto649_result = lottery.lotto649(False, False, [112, 6]) + lotto649_result = lottery.lotto649([112, 6]) # Then the lotto649_result should be equal to test result assert lotto649_result == [ @@ -49,7 +49,7 @@ def test_daily_cash(): lottery = TaiwanLotteryCrawler() # When user get the 今彩539 2023-06 result without print and output to json - daily_cash_result = lottery.daily_cash(False, False, [112, 6]) + daily_cash_result = lottery.daily_cash([112, 6]) # Then the daily_cash_result should be equal to test result assert daily_cash_result == [ @@ -87,7 +87,7 @@ def test_lotto1224(): lottery = TaiwanLotteryCrawler() # When user get the 雙贏彩 2023-06 result without print and output to json - lotto1224_result = lottery.lotto1224(False, False, [112, 6]) + lotto1224_result = lottery.lotto1224([112, 6]) # Then the lotto1224_result should be equal to test result assert lotto1224_result == [