Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Add /proMatches to get recent pro matches as proposed in #7 #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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