This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
forked from kevincon/dotaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotabot2.py
99 lines (79 loc) · 3.32 KB
/
dotabot2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os, logging, argparse
from dota2py import api
from util import get_game_mode_string
from pymongo import MongoClient
from time import sleep
from sys import exit
client = MongoClient(os.getenv('DOTABOT_DB_SERVER', 'localhost'), 27017)
db = client[os.getenv('DOTABOT_DB_NAME', 'dotabot')]
match_collection = db.matches
logging.basicConfig(filename='/home/kcon/dota2project/log.txt')
logger = logging.getLogger('dotabot')
def setup():
'''Setup the API, etc.'''
logger.setLevel(logging.DEBUG)
API_KEY = os.getenv('DOTABOT_API_KEY')
if not API_KEY:
raise NameError('Please set the DOTABOT_API_KEY environment variables.')
api.set_api_key(API_KEY)
def is_valid_match(gmd_result):
'''Returns True if the given match details result should be considered,
and False otherwise.'''
for player in gmd_result['players']:
if player['leaver_status'] is not 0:
return False
return True
def process_replay(match_id):
'''Download, parse, and record data from the replay of the given match_id.'''
# TODO
pass
def process_match_details(match_id):
'''Get the details of the given match_id, check if it's valid, and
if it is, add it as a record in the database and spawn a thread to
download and parse the corresponding replay.'''
gmd = api.get_match_details(match_id)['result']
if not is_valid_match(gmd):
logger.debug('Not considering match %s.' % match_id)
return
match_collection.insert(gmd)
game_mode = get_game_mode_string(gmd['game_mode'])
logger.debug('Processed Match ID: %s - Game Mode: %s' % (match_id, game_mode))
# TODO:
# Spawn replay parser thread if there aren't too many already
def main():
'''The main entry point of dotabot.'''
start_match_id = None
while True:
# Note: GetMatchHistory returns a list of matches in descending order,
# going back in time.
sleep(1.0)
logger.debug('Doing GMH query for start_at_match_id=%s' % start_match_id)
gmh = api.get_match_history(start_at_match_id=start_match_id,
skill=3,
game_mode=2,
min_players=10)['result']
error_code = gmh['status']
matches = gmh['matches']
if error_code is not 1:
msg = 'GMH query at match_id %s had error code %s. Retrying.' % (start_match_id, error_code)
logger.debug(msg)
continue
if len(matches) is 0:
logger.debug('Finished processing all 500 most recent matches.')
exit(0)
for match in matches:
match_id = match['match_id']
if match_collection.find_one({'match_id':match_id}) != None:
logger.debug('Encountered match %s already in database, exiting.' % match_id)
exit(0)
sleep(1.0)
process_match_details(match_id)
last_match_id = matches[-1]['match_id']
logger.debug('Match_id of last match of GMH query: %s' % last_match_id)
# We don't want to record the last match twice, so subtract 1
start_match_id = last_match_id - 1
if __name__ == '__main__':
p = argparse.ArgumentParser(description='Bot for collecting data from 500 most recent DOTA2 matches')
args = p.parse_args()
setup()
main()