Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .
- ├── config.py (global configuration)
- ├── core
- │ ├── client.py (generic requests class)
- │ └── persist.py (mongodb layer class)
- ├── pubg
- │ ├── controller.py (handler for pubg)
- │ └── models.py (models for pubg)
- └── main.py
- class Client:
- ...
- def request(self, endpoint, params=None:
- response = self.session.get(endpoint, timeout=TIMEOUT, params=params)
- return response.json()
- class Pubg(Client):
- # Which includes the calls to the official endpoints {palyer, match, telemtry}
- ...
- def players(self, params, shard):
- res = []
- url = f'{SHARD_URL}/{shard}/players'
- response = self.request(url, params)
- res.append(Player(item))
- return res
- class PubgManager(Pubg):
- # Which is used to transform the data in the shape as I really want to be stored (either in mongo or disk)
- # I kept in the class the list of matches to process (matches_to_process), shard and if I want to use ids or real names (use_ids var)
- def __init__(self, shard, players, mongo):
- super().__init__(api_key)
- # Keep mongo instance in order to persist data later
- self.mongo = mongo
- self.mongo_pubg = mongo.get_client().pubg
- ...
- def process_matches():
- ...
- def process_matches(self, list_matches, telemetry=True, store_mongo=True, store_disk=False, return_data=False):
- # Create the Data Structure to return
- res = dict({
- 'match_data': list(),
- 'participant_data': list(),
- 'telemetry_users': list(),
- 'telemetry_match': list(),
- })
- # Iterate over the matches id
- for m_id in list_matches:
- # get info from matches
- m_info = self.match(m_id, self.shard)
- m_data = m_info.get_data()
- participants = {
- m_id: m_data.get('participants')
- }
- # Store info
- if store_mongo:
- self.mongo.insert_item('matches', m_data)
- self.mongo.insert_item('participants', participants)
- # Update response
- if return_data:
- res.get('match_data').append(m_data)
- res.get('participant_data').append(participants)
- if telemetry:
- t_url = m_data.get('telemetry')
- t_data = self.telemetry(t_url, m_id).get_data().get(
- 'telemetry')
- user_activity = list()
- match_activity = list()
- other_activity = list()
- for action in t_data:
- # We are only interested in the events AFTER lift off
- # The only event without `common` is matchStart which it's
- # been just pop out the list
- is_game = action.get('common').get('isGame') >= 0.1
- # if is_game:
- # tele_ingame_data.append(action)
- action_type = action.get('_T')
- pl = self.players
- if is_game and action_type in EVENTS_USER:
- # Is the player attacking somebody?
- try:
- attacks = action.get('attacker').get('name') in pl
- if attacks:
- user_activity.append(action)
- except AttributeError:
- pass
- # Is the player beign attacked?
- try:
- is_victim = action.get('victim').get('name') in pl
- if is_victim:
- user_activity.append(action)
- except AttributeError:
- pass
- # Is the player interacting with something ?
- try:
- user = action.get('character').get('name') in pl
- if user:
- user_activity.append(action)
- except AttributeError:
- pass
- elif is_game and action_type in EVENTS_GAME:
- match_activity.append(action)
- elif is_game:
- other_activity.append(action)
- else:
- pass
- user_info = {
- '_id': m_id,
- 'id': m_id,
- 'data': user_activity
- }
- match_info = {
- '_id': m_id,
- 'id': m_id,
- 'match_details': match_info,
- 'data': match_activity
- }
- # Update values
- if return_data:
- res.get('telemetry_users').append(user_info)
- res.get('telemetry_match').append(match_info)
- # Store info
- if store_mongo:
- self.mongo.insert_item('telemetry_users', user_info)
- self.mongo.insert_item('telemetry_match', match_info)
- if store_disk:
- path = 'output/'
- filename = f'{m_id}.json'
- data = {
- 'match_info': match_info,
- 'telemetry': t_data,
- }
- store_json(path, filename, data)
Add Comment
Please, Sign In to add comment