From 007f8b9e39927be22af96b248aa8f37a16d215d8 Mon Sep 17 00:00:00 2001 From: Ondrej Zacha Date: Thu, 12 Nov 2020 18:40:54 +0100 Subject: [PATCH] Refactor db_query_api.py --- climabot/db_query_api.py | 59 +++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/climabot/db_query_api.py b/climabot/db_query_api.py index d14daac..b306795 100644 --- a/climabot/db_query_api.py +++ b/climabot/db_query_api.py @@ -18,13 +18,36 @@ group.add_argument("-u", "--user_handle", help="user handle to query") group.add_argument("-f", "--csv_file", help="path to the csv file with the twitter handles to parse in the first (0) column") -# get variables -args = parser.parse_args() -config_db = args.mariadb_group -user_handle = args.user_handle -## the config file for mariadb databases -config = (ConfigObj(expanduser('~/.my.cnf'))) +def main(): + # get variables + args = parser.parse_args() + config_db = args.mariadb_group + user_handle = args.user_handle + + ## the config file for mariadb databases + config = (ConfigObj(expanduser('~/.my.cnf'))) + + db = connect(conf_db=config_db, database='hackathon') + c = db.cursor() + + if args.user_handle: + ## to insert just one user in the db + fill_database(args.user_handle) + + elif args.csv_file: + ## to insert all users from a csv + files_path = args.csv_file + twitter_handles = pd.read_csv(files_path) + + count = 0 + # feed that db!! + for i, row in twitter_handles.iterrows(): + count += 1 + import_user = (row[0][1:]) + print(import_user) + fill_database(import_user) + # Setup API: @@ -56,9 +79,6 @@ def connect(conf_db, database=""): password=config[conf_db]['password'], database=database) -db = connect(conf_db=config_db, database='hackathon') -c = db.cursor() - def fill_database(user): # IPCC_CH @@ -145,22 +165,5 @@ def fill_database(user): db.commit() -if args.user_handle: - ## to insert just one user in the db - fill_database(args.user_handle) - -elif args.csv_file: - ## to insert all users from a csv - files_path = args.csv_file - twitter_handles = pd.read_csv(files_path) - - count = 0 - # feed that db!! - for i, row in twitter_handles.iterrows(): - count += 1 - import_user = (row[0][1:]) - print(import_user) - fill_database(import_user) - - - +if __name__ == "__main__": + main()