diff --git a/main.py b/main.py index 00e19a9..b278b71 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) @@ -61,16 +61,22 @@ def dota_blog(message): cid = getCID(message) dota_blog_rss_url = "http://blog.dota2.com/feed/" feed = feedparser.parse(dota_blog_rss_url) + content = unicode(feed["items"][0]["summary"]) content = content.split('…', 1) - bot.send_message( - cid, - '*{title}* ```\n\n{content}...\n\n```' - .format( - title=feed["items"][0]["title"], - content=content) - + '[Read the entire blog post in your browser]({url})' - .format(url=feed["items"][0]["link"]), parse_mode="Markdown", disable_web_page_preview=True) + content = content[0] # content.split gives a list of unicode strings, content[0] is the real content + content = content.encode('utf-8') # If not encoded, unicode characters will cause an error + + text_title = feed["items"][0]["title"].encode('utf-8') + text_url = feed["items"][0]["link"].encode('utf-8') + + text_formatted = '*{title}* ```\n\n{content}...\n\n```'.format(title=text_title, content=content) + + link_text_formatted = '[Read the entire blog post in your browser]({url})'.format(url=text_url)\ + + message_text = text_formatted + link_text_formatted + + bot.send_message(cid, message_text, disable_web_page_preview=True, parse_mode="Markdown") @bot.message_handler(commands=['subscribe', 'letmeknow']) @@ -121,6 +127,40 @@ def end_subscription(message): pass +@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") + + @bot.message_handler(regexp="match (\d.*?)(\D|$)") def find_match(message): if intime(message): diff --git a/utils.py b/utils.py index c0752fa..5881356 100644 --- a/utils.py +++ b/utils.py @@ -71,3 +71,45 @@ 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