From f5c57d8e73f33ca1d7374a2662fbc7a4592eb7cd Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Tue, 7 May 2024 16:34:14 +0200 Subject: Python code style --- Commands.py | 94 ------------------------------------------------------- DeviceAuth.py | 22 ------------- Formatter.py | 66 +++++++++++++++++++------------------- FortniteClient.py | 2 +- FortniteEvents.py | 2 +- commands.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ device_auth.py | 22 +++++++++++++ tgbot.py | 6 ++-- 8 files changed, 154 insertions(+), 154 deletions(-) delete mode 100644 Commands.py delete mode 100644 DeviceAuth.py create mode 100644 commands.py create mode 100644 device_auth.py diff --git a/Commands.py b/Commands.py deleted file mode 100644 index e49d333..0000000 --- a/Commands.py +++ /dev/null @@ -1,94 +0,0 @@ -import telebot -from TelegramBot import * -from Formatter import * -from persistence import * -from FortniteClient import FortniteClient -from pythonFortniteStatus.FortniteStatus import * - -class StartCommand(CommandHandler): - - __telegramBot: TelegramBot - __userRepository: UserRepository - - def __init__(self, telegramBot: TelegramBot, userRepository: UserRepository): - self.__telegramBot = telegramBot - self.__userRepository = userRepository - - async def handle(self, message: telebot.types.Message): - if message.chat.type == 'private': - alias = message.chat.username - else: - alias = message.chat.title - self.__userRepository.putUser(message.chat.id, alias) - await self.__telegramBot.reply(message, 'This chat successfully registered to receive Fortnite updates') - -class GetStatusCommand(CommandHandler): - - __telegramBot: TelegramBot - __fortniteStatus: FortniteStatus - - def __init__(self, telegramBot: TelegramBot): - self.__telegramBot = telegramBot - self.__fortniteStatus = FortniteStatus() - - async def handle(self, message: telebot.types.Message): - await self.__telegramBot.reply(message, formatFortniteStatus(self.__fortniteStatus.getStatus())) - -class GetFriendsCommand(CommandHandler): - - __telegramBot: TelegramBot - __fortniteClient: FortniteClient - - def __init__(self, telegramBot: TelegramBot, fortniteClient: FortniteClient): - self.__telegramBot = telegramBot - self.__fortniteClient = fortniteClient - - async def handle(self, message: telebot.types.Message): - friends = await self.__fortniteClient.get_friends() - await self.__telegramBot.reply(message, formatUsers(friends)) - - -class GetStatsCommand(CommandHandler): - - __telegramBot: TelegramBot - __fortniteClient: FortniteClient - - def __init__(self, telegramBot: TelegramBot, fortniteClient: FortniteClient): - self.__telegramBot = telegramBot - self.__fortniteClient = fortniteClient - - async def handle(self, message: telebot.types.Message): - friends = await self.__fortniteClient.get_friends() - stats = [await friend.fetch_stats() for friend in friends] - await self.__telegramBot.reply(message, formatUserStatsList(stats)) - -class GetTodayStatsCommand(CommandHandler): - - __telegramBot: TelegramBot - __fortniteClient: FortniteClient - __statsRepository: StatsRepository - - def __init__(self, telegramBot: TelegramBot, fortniteClient: FortniteClient, statsRepository: StatsRepository): - self.__telegramBot = telegramBot - self.__fortniteClient = fortniteClient - self.__statsRepository = statsRepository - - async def handle(self, message: telebot.types.Message): - persisted_stats = self.__statsRepository.getStats() - friends = await self.__fortniteClient.get_friends() - current_stats = [await friend.fetch_stats() for friend in friends] - await self.__telegramBot.reply(message, formatUserStatsDifference(persisted_stats, current_stats)) - -class RecordStatsCommand(CommandHandler): - - __fortniteClient: FortniteClient - __statsRepository: StatsRepository - - def __init__(self, fortniteClient: FortniteClient, statsRepository: StatsRepository): - self.__fortniteClient = fortniteClient - self.__statsRepository = statsRepository - - async def handle(self, message: telebot.types.Message): - friends = await self.__fortniteClient.get_friends() - for friend in friends: - await self.__statsRepository.putStats(friend) \ No newline at end of file diff --git a/DeviceAuth.py b/DeviceAuth.py deleted file mode 100644 index 3374956..0000000 --- a/DeviceAuth.py +++ /dev/null @@ -1,22 +0,0 @@ -import json -import os - -class DeviceAuth: - - __filename__ = 'device-auth.json' - - def device_auth_file_exists(self): - return os.path.isfile(self.__filename__) - - def get_device_auth_details(self): - if os.path.isfile(self.__filename__): - with open(self.__filename__, 'r') as fp: - return json.load(fp) - return {} - - def store_device_auth_details(self, email, details): - existing = self.get_device_auth_details() - existing[email] = details - - with open(self.__filename__, 'w') as fp: - json.dump(existing, fp) \ No newline at end of file diff --git a/Formatter.py b/Formatter.py index fc8b8ad..fa7a316 100644 --- a/Formatter.py +++ b/Formatter.py @@ -3,84 +3,84 @@ import typing from Types import * # Status -def formatFortniteStatus(fortniteStatus): - statuses = [__formatFortniteServiceStatus(serviceStatus) for serviceStatus in fortniteStatus.serviceStatuses] +def format_fortnite_status(fortnite_status): + statuses = [__format_fortnite_service_status(service_status) for service_status in fortnite_status.serviceStatuses] return formatting.format_text( formatting.mbold("Fortnite status"), "", '\n'.join(statuses), separator='\n') -def __formatStatus(status): +def __format_status(status): if (status == True): return u'\u2705' else: return u'\u274c' -def __formatFortniteServiceStatus(fortniteServiceStatus): +def __format_fortnite_service_status(fortnite_service_status): return formatting.format_text( - formatting.mbold(fortniteServiceStatus.serviceName), - __formatStatus(fortniteServiceStatus.status), + formatting.mbold(fortnite_service_status.serviceName), + __format_status(fortnite_service_status.status), separator=': ') # User -def formatUsers(users: typing.List[User]): - users_formatted = [formatUser(user) for user in users] +def format_users(users: typing.List[User]): + users_formatted = [format_user(user) for user in users] return formatting.format_text( '\n\n'.join(users_formatted), separator='\n') -def formatUser(user: User): +def format_user(user: User): return formatting.format_text( formatting.mbold("User: ") + user.display_name, formatting.mbold("ID: ") + user.id, separator='\n') # Stats -def formatUserStatsList(stats: typing.List[UserStats]): - stats_formatted = [__formatStats(singleStats) for singleStats in stats] +def format_user_stats_list(stats: typing.List[UserStats]): + stats_formatted = [__format_stats(single_stats) for single_stats in stats] return formatting.format_text( '\n\n'.join(stats_formatted), separator='\n') -def formatUserStatsDifference(oldUserStats: typing.List[UserStats], newUserStats: typing.List[UserStats]): +def format_user_stats_difference(old_user_stats: typing.List[UserStats], new_user_stats: typing.List[UserStats]): stats_formatted = [] - for stats in oldUserStats: - matched = [x for x in newUserStats if x.user_id == stats.user_id][0] - stats_formatted.append(__formatStatsDifference(stats, matched)) + for stats in old_user_stats: + matched = [x for x in new_user_stats if x.user_id == stats.user_id][0] + stats_formatted.append(__format_stats_difference(stats, matched)) return formatting.format_text( '\n\n'.join(stats_formatted), separator='\n') -def __formatStatsDifference(oldUserStats: UserStats, newUserStats: UserStats): +def __format_stats_difference(old_user_stats: UserStats, new_user_stats: UserStats): return formatting.format_text( - formatting.mbold("User: ") + newUserStats.user_display_name, - formatting.mbold("ID: ") + newUserStats.user_id, - formatting.mbold("Level: ") + "{}{}".format(str(newUserStats.level), __formatStatDifference(oldUserStats.level, newUserStats.level)), - formatting.mbold("Matches played: ") + "{}{}".format(str(newUserStats.matches_played), __formatStatDifference(oldUserStats.matches_played, newUserStats.matches_played)), - formatting.mbold("Total kills: ") + "{}{}".format(str(newUserStats.kills), __formatStatDifference(oldUserStats.kills, newUserStats.kills)), - formatting.mbold("Wins: ") + "{}{}".format(str(newUserStats.wins), __formatStatDifference(oldUserStats.wins, newUserStats.wins)), + formatting.mbold("User: ") + new_user_stats.user_display_name, + formatting.mbold("ID: ") + new_user_stats.user_id, + formatting.mbold("Level: ") + "{}{}".format(str(new_user_stats.level), __format_stat_difference(old_user_stats.level, new_user_stats.level)), + formatting.mbold("Matches played: ") + "{}{}".format(str(new_user_stats.matches_played), __format_stat_difference(old_user_stats.matches_played, new_user_stats.matches_played)), + formatting.mbold("Total kills: ") + "{}{}".format(str(new_user_stats.kills), __format_stat_difference(old_user_stats.kills, new_user_stats.kills)), + formatting.mbold("Wins: ") + "{}{}".format(str(new_user_stats.wins), __format_stat_difference(old_user_stats.wins, new_user_stats.wins)), separator='\n') -def __formatStatDifference(oldStatValue: int, newStatValue: int): - if oldStatValue != newStatValue: - return " \(\+ {}\)".format(str(newStatValue - oldStatValue)) +def __format_stat_difference(old_stat_value: int, new_stat_value: int): + if old_stat_value != new_stat_value: + return " \(\+ {}\)".format(str(new_stat_value - old_stat_value)) else: return "" -def __formatStats(userStats: UserStats): +def __format_stats(user_stats: UserStats): return formatting.format_text( - formatting.mbold("User: ") + userStats.user_display_name, - formatting.mbold("ID: ") + userStats.user_id, - formatting.mbold("Level: ") + str(userStats.level), - formatting.mbold("Matches played: ") + str(userStats.matches_played), - formatting.mbold("Total kills: ") + str(userStats.kills), - formatting.mbold("Wins: ") + str(userStats.wins), + formatting.mbold("User: ") + user_stats.user_display_name, + formatting.mbold("ID: ") + user_stats.user_id, + formatting.mbold("Level: ") + str(user_stats.level), + formatting.mbold("Matches played: ") + str(user_stats.matches_played), + formatting.mbold("Total kills: ") + str(user_stats.kills), + formatting.mbold("Wins: ") + str(user_stats.wins), separator='\n') -def formatFriendOnline(display_name: str, party_size: int): +def format_friend_online(display_name: str, party_size: int): if party_size == 1: text = 'is playing Fortnite\!' elif party_size == 2: diff --git a/FortniteClient.py b/FortniteClient.py index 1f700b2..c52aacb 100755 --- a/FortniteClient.py +++ b/FortniteClient.py @@ -4,7 +4,7 @@ import fortnitepy import json import os import typing -from DeviceAuth import DeviceAuth +from device_auth import DeviceAuth from Types import * __fortnite_account_key__ = 'fornite-account-key' diff --git a/FortniteEvents.py b/FortniteEvents.py index 77452cc..c096818 100644 --- a/FortniteEvents.py +++ b/FortniteEvents.py @@ -32,4 +32,4 @@ class FriendPresenceObserverImpl(FriendPresenceObserver): self.__presenceRepository.setLastUserPresence(display_name, time.time()) async def __notifyFriendPlaying(self, display_name: str, party_size: int): - await self.__telegramBot.send_message_to_all(formatFriendOnline(display_name, party_size)) \ No newline at end of file + await self.__telegramBot.send_message_to_all(format_friend_online(display_name, party_size)) \ No newline at end of file diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..4cf330b --- /dev/null +++ b/commands.py @@ -0,0 +1,94 @@ +import telebot +from TelegramBot import * +from Formatter import * +from persistence import * +from FortniteClient import FortniteClient +from pythonFortniteStatus.FortniteStatus import * + +class StartCommand(CommandHandler): + + __telegram_bot: TelegramBot + __user_repository: UserRepository + + def __init__(self, telegram_bot: TelegramBot, user_repository: UserRepository): + self.__telegram_bot = telegram_bot + self.__user_repository = user_repository + + async def handle(self, message: telebot.types.Message): + if message.chat.type == 'private': + alias = message.chat.username + else: + alias = message.chat.title + self.__user_repository.putUser(message.chat.id, alias) + await self.__telegram_bot.reply(message, 'This chat successfully registered to receive Fortnite updates') + +class GetStatusCommand(CommandHandler): + + __telegram_bot: TelegramBot + __fortnite_status: FortniteStatus + + def __init__(self, telegram_bot: TelegramBot): + self.__telegram_bot = telegram_bot + self.__fortnite_status = FortniteStatus() + + async def handle(self, message: telebot.types.Message): + await self.__telegram_bot.reply(message, format_fortnite_status(self.__fortnite_status.getStatus())) + +class GetFriendsCommand(CommandHandler): + + __telegram_bot: TelegramBot + __fortnite_client: FortniteClient + + def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient): + self.__telegram_bot = telegram_bot + self.__fortnite_client = fortnite_client + + async def handle(self, message: telebot.types.Message): + friends = await self.__fortnite_client.get_friends() + await self.__telegram_bot.reply(message, format_users(friends)) + + +class GetStatsCommand(CommandHandler): + + __telegram_bot: TelegramBot + __fortnite_client: FortniteClient + + def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient): + self.__telegram_bot = telegram_bot + self.__fortnite_client = fortnite_client + + async def handle(self, message: telebot.types.Message): + friends = await self.__fortnite_client.get_friends() + stats = [await friend.fetch_stats() for friend in friends] + await self.__telegram_bot.reply(message, format_user_stats_list(stats)) + +class GetTodayStatsCommand(CommandHandler): + + __telegram_bot: TelegramBot + __fortnite_client: FortniteClient + __stats_repository: StatsRepository + + def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository): + self.__telegram_bot = telegram_bot + self.__fortnite_client = fortnite_client + self.__stats_repository = stats_repository + + async def handle(self, message: telebot.types.Message): + persisted_stats = self.__stats_repository.getStats() + friends = await self.__fortnite_client.get_friends() + current_stats = [await friend.fetch_stats() for friend in friends] + await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats)) + +class RecordStatsCommand(CommandHandler): + + __fortnite_client: FortniteClient + __stats_repository: StatsRepository + + def __init__(self, fortnite_client: FortniteClient, stats_repository: StatsRepository): + self.__fortnite_client = fortnite_client + self.__stats_repository = stats_repository + + async def handle(self, message: telebot.types.Message): + friends = await self.__fortnite_client.get_friends() + for friend in friends: + await self.__stats_repository.putStats(friend) \ No newline at end of file diff --git a/device_auth.py b/device_auth.py new file mode 100644 index 0000000..4d46918 --- /dev/null +++ b/device_auth.py @@ -0,0 +1,22 @@ +import json +import os + +__filename__ = 'device-auth.json' + +class DeviceAuth: + + def device_auth_file_exists(self): + return os.path.isfile(__filename__) + + def get_device_auth_details(self): + if os.path.isfile(__filename__): + with open(__filename__, 'r') as fp: + return json.load(fp) + return {} + + def store_device_auth_details(self, email, details): + existing = self.get_device_auth_details() + existing[email] = details + + with open(__filename__, 'w') as fp: + json.dump(existing, fp) \ No newline at end of file diff --git a/tgbot.py b/tgbot.py index 360f3b0..a0e248b 100755 --- a/tgbot.py +++ b/tgbot.py @@ -1,13 +1,13 @@ #!/usr/bin/python3 -import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio, sys, logging, time +import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio, sys from FortniteStatusNotifier import * from Formatter import * from FortniteClient import * from FortniteEvents import * from persistence import UserRepository, StatsRepository, PresenceRepository from TelegramBot import TelegramBot, CommandHandler -from Commands import * +from commands import * class FortniteStatusObserverImpl(FortniteStatusObserver): @@ -17,7 +17,7 @@ class FortniteStatusObserverImpl(FortniteStatusObserver): self.__telegram_bot = telegram_bot async def update(self, fortnite_status) -> None: - await self.__telegram_bot.send_message_to_all(formatFortniteStatus(fortnite_status)) + await self.__telegram_bot.send_message_to_all(format_fortnite_status(fortnite_status)) user_repository = UserRepository() stats_repository = StatsRepository() -- cgit v1.2.3