-def parse_stats_submission(body):
- """
- Parses the POST request body for a stats submission
- """
- # storage vars for the request body
- game_meta = {}
- events = {}
- players = []
- teams = []
-
- # we're not in either stanza to start
- in_P = in_Q = False
-
- for line in body.split('\n'):
- try:
- (key, value) = line.strip().split(' ', 1)
-
- # Server (S) and Nick (n) fields can have international characters.
- if key in 'S' 'n':
- value = unicode(value, 'utf-8')
-
- if key not in 'P' 'Q' 'n' 'e' 't' 'i':
- game_meta[key] = value
-
- if key == 'Q' or key == 'P':
- #log.debug('Found a {0}'.format(key))
- #log.debug('in_Q: {0}'.format(in_Q))
- #log.debug('in_P: {0}'.format(in_P))
- #log.debug('events: {0}'.format(events))
-
- # check where we were before and append events accordingly
- if in_Q and len(events) > 0:
- #log.debug('creating a team (Q) entry')
- teams.append(events)
- events = {}
- elif in_P and len(events) > 0:
- #log.debug('creating a player (P) entry')
- players.append(events)
- events = {}
-
- if key == 'P':
- #log.debug('key == P')
- in_P = True
- in_Q = False
- elif key == 'Q':
- #log.debug('key == Q')
- in_P = False
- in_Q = True
-
- events[key] = value
-
- if key == 'e':
- (subkey, subvalue) = value.split(' ', 1)
- events[subkey] = subvalue
- if key == 'n':
- events[key] = value
- if key == 't':
- events[key] = value
- except:
- # no key/value pair - move on to the next line
- pass
-
- # add the last entity we were working on
- if in_P and len(events) > 0:
- players.append(events)
- elif in_Q and len(events) > 0:
- teams.append(events)
-
- return (game_meta, players, teams)