Skip to content

Instantly share code, notes, and snippets.

@thespad
Forked from 7596ff/dotabrag.py
Last active August 29, 2015 14:05
Show Gist options
  • Save thespad/79c10abd53f614e31a16 to your computer and use it in GitHub Desktop.
Save thespad/79c10abd53f614e31a16 to your computer and use it in GitHub Desktop.
Tweet Dota Match Results
import urllib
import json
import string
import sys
import ConfigParser
import tweepy
def craftTweet(match_id, level, hero, kills, deaths, assists, result, lobby):
unformatted = '{} some {} Dota as Lv{} {} KDA: {}/{}/{} #dotabrag\nDotabuff: {}'
url = 'http://www.dotabuff.com/matches/' + match_id
formatted = unformatted.format(result, lobby, level, hero, kills, deaths, assists, url)
return formatted
def tweet(status):
config = ConfigParser.RawConfigParser()
config.read('dota2_tweet.cfg')
# http://dev.twitter.com/apps/myappid
CONSUMER_KEY = config.get('API Information', 'CONSUMER_KEY')
CONSUMER_SECRET = config.get('API Information', 'CONSUMER_SECRET')
# http://dev.twitter.com/apps/myappid/my_token
ACCESS_TOKEN_KEY = config.get('API Information', 'ACCESS_TOKEN_KEY')
ACCESS_TOKEN_SECRET = config.get('API Information', 'ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
result = api.update_status(status)
config = ConfigParser.RawConfigParser()
config.read('dota2_tweet.cfg')
STEAM_API_KEY = config.get('Steam Information', 'STEAM_API_KEY')
ACCOUNT_ID = config.get('Steam Information', 'ACCOUNT_ID')
url = 'https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/?key=' + STEAM_API_KEY + '&language=en_us'
response = urllib.urlopen(url)
hero_json_obj = json.load(response)
account_id = ACCOUNT_ID
args = {
'key': STEAM_API_KEY,
'format': 'JSON',
'account_id': account_id,
'matches_requested': '1',
}
encoded_args = urllib.urlencode(args)
url = 'http://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/V001/?' + encoded_args
response = urllib.urlopen(url)
json_obj = json.load(response)
match_id = json_obj['result']['matches'][0]['match_id']
# check if we already tweeted this match
last_match = config.get('config', 'LAST_MATCH')
if str(match_id) == str(last_match):
sys.exit('You already tweeted this match')
config.set('config', 'LAST_MATCH', match_id)
with open('dota2_tweet.cfg', 'w') as f:
config.write(f)
args = {
'key': STEAM_API_KEY,
'format': 'JSON',
'match_id': match_id,
}
encoded_args = urllib.urlencode(args)
url = 'http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?' + encoded_args
response = urllib.urlopen(url)
json_obj = json.load(response)
my_player = {}
hero_string = ''
for player in json_obj['result']['players']:
if str(player['account_id']) == account_id:
my_player = player
for hero in hero_json_obj['result']['heroes']:
if hero['id'] == my_player['hero_id']:
hero_string = hero['localized_name']
if my_player['player_slot'] < 10:
if str(json_obj['result']['radiant_win']) == "False":
my_result = "Lost"
else:
my_result = "Won"
else:
if str(json_obj['result']['radiant_win']) == "False":
my_result = "Won"
else:
my_result = "Lost"
if json_obj['result']['lobby_type'] == -1:
my_lobby = "Invalid"
elif json_obj['result']['lobby_type'] == 0:
my_lobby = "Public Matchmaking"
elif json_obj['result']['lobby_type'] == 1:
my_lobby = "Practice"
elif json_obj['result']['lobby_type'] == 2:
my_lobby = "Tournament"
elif json_obj['result']['lobby_type'] == 3:
my_lobby = "Tutorial"
elif json_obj['result']['lobby_type'] == 4:
my_lobby = "Co-op With Bots"
elif json_obj['result']['lobby_type'] == 5:
my_lobby = "Team Match"
elif json_obj['result']['lobby_type'] == 6:
my_lobby = "Solo Queue"
elif json_obj['result']['lobby_type'] == 7:
my_lobby = "Ranked"
elif json_obj['result']['lobby_type'] == 8:
my_lobby = "Solo Mid 1v1"
if my_player['deaths'] == 0:
my_kda = float(my_player['kills'] + my_player['assists'])/float(my_player['deaths']+1)
else:
my_kda = float(my_player['kills'] + my_player['assists'])/float(my_player['deaths'])
if my_kda > 10.0 or my_kda < 0.2:
tweet(craftTweet(
str(json_obj['result']['match_id']),
str(my_player['level']),
hero_string,
str(my_player['kills']),
str(my_player['deaths']),
str(my_player['assists']),
my_result,
my_lobby
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment