-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_methods.py
More file actions
61 lines (51 loc) · 2.92 KB
/
setup_methods.py
File metadata and controls
61 lines (51 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import logging
import pickle
import os
import psycopg2
import DB
import logger_config
from DB_functions import load_players, write_scoreboard, write_players
from persistence.postgres_connection import setup_connection, close_connection
from players_data_helpers import insert_players_into_database
from schedule_extraction_helpers import read_schedule
from scoreboard_helpers import initialize_scoreboard, scoreboard_table
def init_schedule(update, context):
if len(context.args) > 0:
logger_config.logger.log(logging.INFO, "Extra arguments have been discarded")
DB.schedule, DB.team_mappings = read_schedule()
schedule = pickle.dumps(DB.schedule)
team_mappings = pickle.dumps(DB.team_mappings)
cur, conn = setup_connection()
cur.execute("""UPDATE ipl_data SET schedule = %s WHERE year = 2021""", (psycopg2.Binary(schedule), ))
cur.execute("""UPDATE ipl_data SET team_mappings = %s WHERE year = 2021""", (psycopg2.Binary(team_mappings),))
conn.commit()
close_connection(cur, conn)
context.bot.send_message(chat_id=update.effective_chat.id, text='Initialized Schedule')
def init_scoreboard(update, context):
try:
DB.scoreboard = initialize_scoreboard(context.args)
write_scoreboard(DB.scoreboard)
context.bot.send_message(chat_id=update.effective_chat.id, text='Initialized scoreboard')
context.bot.send_message(chat_id=update.effective_chat.id, text=scoreboard_table(DB.scoreboard, load_players()))
except IOError as error:
context.bot.send_message(chat_id=update.effective_chat.id, text=repr(error))
except ValueError as error:
context.bot.send_message(chat_id=update.effective_chat.id, text=repr(error))
except Exception as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="An exception occured: {}".format(repr(e)))
def add_players(update, context):
try:
DB.players = insert_players_into_database(context.args)
write_players(DB.players)
context.bot.send_message(chat_id=update.effective_chat.id, text='Added players')
context.bot.send_message(chat_id=update.effective_chat.id, text="Current players: {}".format(
', '.join([player.get_name() for player in DB.players.values()])))
except IOError:
context.bot.send_message(chat_id=update.effective_chat.id, text='Input Error: please ensure that all players '
'are entered in the following format'
'"first_name last_name, alias[, ..]" without '
'quotes')
except ValueError as error:
context.bot.send_message(chat_id=update.effective_chat.id, text=repr(error))
except Exception as e:
context.bot.send_message(chat_id=update.effective_chat.id, text="An exception occured: {}".format(repr(e)))