diff --git a/main.py b/main.py index 00e19a9..57ee37e 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ import feedparser import dota2api -from utils import intime, getCID, getContent, loadjson, addUser, deljson +from utils import intime, getCID, getContent, loadjson, addUser, deljson, match_short_description from settings import BOT_TOKEN, DOTA2API_TOKEN bot = telebot.TeleBot(BOT_TOKEN) @@ -190,6 +190,40 @@ def find_match(message): telebot.logger.error(ex) +@bot.message_handler(commands=['proMatches', 'recentProMatches']) +def pro_matches(message): + """Gets recent pro matches, will give a number of matches equal to argument.""" + + default_number_of_posts = 5 + posts_max = 20 + + if intime(message): + cid = getCID(message) + + param = getContent(message) + try: + param = int(param) + except ValueError: + param = 0 + + number_of_posts = param if 0 < param <= posts_max else default_number_of_posts + + open_dota_url = 'https://api.opendota.com/api/proMatches' + response = requests.get(open_dota_url) + response_json = response.json() # Array of 100 most recent pro matches + matches_json = response_json[:number_of_posts] + + matches_text = [] + for match_json in matches_json: + matches_text.append(match_short_description(match_json)) + + message_text = 'Last {number} pro matches:'.format(number=number_of_posts) + for match_text in matches_text: + message_text = message_text + '\n{match}'.format(match=match_text) + + bot.send_message(cid, message_text, disable_web_page_preview=True, parse_mode="Markdown") + + if __name__ == '__main__': print("main started") while True: diff --git a/utils.py b/utils.py index c0752fa..8afcf35 100644 --- a/utils.py +++ b/utils.py @@ -71,3 +71,46 @@ def intime(message): return True else: return False + + +def match_short_description(match): + """Creates a short Markdown description for given match with ID, Dotabuff link, league, teams, and winner""" + # Match ID with link + # Match league if exists + # Teams if both exist + # Winner + + dotabuff_url = 'https://www.dotabuff.com/matches/' + + match_id = match['match_id'] + match_url = dotabuff_url + unicode(match_id) + match_text = '[{id}]({url})' + match_text = match_text.format(id=match_id, url=match_url) + + league = match['league_name'] + league_text = u'\n{league}' + league_text = league_text.format(league=league) if league is not None else '' + + radiant_name = match['radiant_name'] + dire_name = match['dire_name'] + team_names = u'\n{radiant} vs. {dire}' + team_names = team_names.format(radiant=radiant_name, dire=dire_name) if radiant_name and dire_name else '' + + if match['radiant_win']: + if radiant_name is not None: + winner = radiant_name + else: + winner = 'Radiant' + else: + if dire_name is not None: + winner = dire_name + else: + winner = 'Dire' + + winner_text = '\nWinner: {winner}' + winner_text = winner_text.format(winner=winner) + + text = '{id}```{league}{names}{winner}```' + text = text.format(id=match_text, league=league_text, names=team_names, winner=winner_text) + + return text \ No newline at end of file