From 882e5dc73b4ced3f9839703a6e1289b74fc9586e Mon Sep 17 00:00:00 2001 From: Ajay Singh Rana Date: Thu, 1 Apr 2021 19:33:49 +0530 Subject: [PATCH 1/2] Add a command line argument catching functionality Use the sys module from Python standard library to read the command-line arguments and store them in a list. --- download_tourments.py | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/download_tourments.py b/download_tourments.py index 53af32c..f15665d 100644 --- a/download_tourments.py +++ b/download_tourments.py @@ -2,27 +2,25 @@ """Downloading chess puzzles for lichess.org""" +# import sys to handle command line arguments +import sys + import logging import requests -# tourments -tourment_ids = ['25MtoToy', - 'E14kHVwX', - 'tdntXNhy', - 'sj5GoEdS', - 'C4zdQLax', - 'wobqi6QP', - 'T4RW1ux2', - 'nzw7OKBq'] +# tournaments +tournament_ids = sys.argv[1:] -all_games = open("games.pgn", "w") -pgn = "" -for id in tourment_ids: - print('https://lichess.org/api/tournament/' + id + '/games') - response = requests.get('https://lichess.org/api/tournament/' + id + '/games') - pgn = pgn + '\n' + str(response.text) +with open('games.pgn','w') as file: + for id_number in tournament_ids: # using id_number instead of id as a name cause id is a built-in python function + url = f'https://lichess.org/api/tournament/{id_number}/games' + print('\r' + f'Downloading tournament for {id_number}...',end = '') + response = requests.get(url) + if(response.status_code != 404): + file.write(str(response.text) + '\n') + print('\r' + f'Downloading complete for {id_number}...',end = '') + else: + print('\r' + f'{id_number} Not Found..!') -all_games.write(pgn) -all_games.close() -logging.debug("Finished. Pgn is in games.pgn ") +logging.debug("Finished... Pgn is in games.pgn") \ No newline at end of file From 8dc92a5f65596c5ead3d92b7f4c756127abffd8d Mon Sep 17 00:00:00 2001 From: Ajay Singh Rana Date: Thu, 1 Apr 2021 19:40:01 +0530 Subject: [PATCH 2/2] Fix the console output issues --- download_tourments.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/download_tourments.py b/download_tourments.py index f15665d..4655076 100644 --- a/download_tourments.py +++ b/download_tourments.py @@ -19,8 +19,8 @@ response = requests.get(url) if(response.status_code != 404): file.write(str(response.text) + '\n') - print('\r' + f'Downloading complete for {id_number}...',end = '') + print('\r' + f'Downloading complete for {id_number}......') else: - print('\r' + f'{id_number} Not Found..!') + print('\r' + f'Failed..! Tournament for {id_number} not found...') logging.debug("Finished... Pgn is in games.pgn") \ No newline at end of file