'Return selected data from current sessions.
This function processes and validates session data
Parameters required:    stream_type { track or video }
session { the session dictionary }
Output: dict'

def get_session_each(self, stream_type='', session=None):

    session_output = None
    user_data = users.Users()
    if (stream_type == 'track'):
        media_info = session.getElementsByTagName('Media')[0]
        audio_decision = 'direct    play'
        audio_channels = helpers.get_xml_attr(media_info, 'audioChannels')
        audio_codec = helpers.get_xml_attr(media_info, 'audioCodec')
        container = helpers.get_xml_attr(media_info, 'container')
        bitrate = helpers.get_xml_attr(media_info, 'bitrate')
        duration = helpers.get_xml_attr(media_info, 'duration')
        progress = helpers.get_xml_attr(session, 'viewOffset')
        if session.getElementsByTagName('TranscodeSession'):
            transcode_session = session.getElementsByTagName('TranscodeSession')[0]
            transcode_key = helpers.get_xml_attr(transcode_session, 'key')
            throttled = helpers.get_xml_attr(transcode_session, 'throttled')
            transcode_progress = helpers.get_xml_attr(transcode_session, 'progress')
            transcode_speed = helpers.get_xml_attr(transcode_session, 'speed')
            audio_decision = helpers.get_xml_attr(transcode_session, 'audioDecision')
            transcode_audio_channels = helpers.get_xml_attr(transcode_session, 'audioChannels')
            transcode_audio_codec = helpers.get_xml_attr(transcode_session, 'audioCodec')
            transcode_container = helpers.get_xml_attr(transcode_session, 'container')
            transcode_protocol = helpers.get_xml_attr(transcode_session, 'protocol')
            duration = helpers.get_xml_attr(transcode_session, 'duration')
        else:
            transcode_key = ''
            throttled = '0'
            transcode_progress = '0'
            transcode_speed = ''
            transcode_audio_channels = ''
            transcode_audio_codec = ''
            transcode_container = ''
            transcode_protocol = ''
        transcode_decision = audio_decision
        user_details = user_data.get_details(user=helpers.get_xml_attr(session.getElementsByTagName('User')[0], 'title'))
        if helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier').endswith('_Track'):
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')[:(-6)]
        else:
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')
        labels = []
        if session.getElementsByTagName('Label'):
            for label in session.getElementsByTagName('Label'):
                labels.append(helpers.get_xml_attr(label, 'tag'))
        session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'), 'section_id': helpers.get_xml_attr(session, 'librarySectionID'), 'media_index': helpers.get_xml_attr(session, 'index'), 'parent_media_index': helpers.get_xml_attr(session, 'parentIndex'), 'art': helpers.get_xml_attr(session, 'art'), 'parent_thumb': helpers.get_xml_attr(session, 'parentThumb'), 'grandparent_thumb': helpers.get_xml_attr(session, 'grandparentThumb'), 'thumb': helpers.get_xml_attr(session, 'thumb'), 'bif_thumb': '', 'user': user_details['username'], 'user_id': user_details['user_id'], 'friendly_name': user_details['friendly_name'], 'user_thumb': user_details['user_thumb'], 'ip_address': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'address').split('::ffff:')[(-1)], 'player': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'title'), 'platform': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'platform'), 'machine_id': machine_id, 'state': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'state'), 'grandparent_title': helpers.get_xml_attr(session, 'grandparentTitle'), 'parent_title': helpers.get_xml_attr(session, 'parentTitle'), 'title': helpers.get_xml_attr(session, 'title'), 'full_title': ('%s    -    %s' % (helpers.get_xml_attr(session, 'grandparentTitle'), helpers.get_xml_attr(session, 'title'))), 'year': helpers.get_xml_attr(session, 'year'), 'rating_key': helpers.get_xml_attr(session, 'ratingKey'), 'parent_rating_key': helpers.get_xml_attr(session, 'parentRatingKey'), 'grandparent_rating_key': helpers.get_xml_attr(session, 'grandparentRatingKey'), 'content_rating': helpers.get_xml_attr(session, 'contentRating'), 'labels': labels, 'transcode_key': transcode_key, 'throttled': throttled, 'transcode_progress': int(round(helpers.cast_to_float(transcode_progress), 0)), 'transcode_speed': str(round(helpers.cast_to_float(transcode_speed), 1)), 'audio_decision': audio_decision, 'audio_channels': audio_channels, 'audio_codec': audio_codec, 'video_decision': '', 'video_codec': '', 'height': '', 'width': '', 'container': container, 'bitrate': bitrate, 'video_resolution': '', 'video_framerate': '', 'aspect_ratio': '', 'transcode_decision': transcode_decision, 'transcode_audio_channels': transcode_audio_channels, 'transcode_audio_codec': transcode_audio_codec, 'transcode_video_codec': '', 'transcode_width': '', 'transcode_height': '', 'transcode_container': transcode_container, 'transcode_protocol': transcode_protocol, 'duration': duration, 'view_offset': progress, 'progress_percent': str(helpers.get_percent(progress, duration)), 'media_type': 'track', 'indexes': 0}
    elif (stream_type == 'video'):
        media_info = session.getElementsByTagName('Media')[0]
        audio_decision = 'direct    play'
        audio_channels = helpers.get_xml_attr(media_info, 'audioChannels')
        audio_codec = helpers.get_xml_attr(media_info, 'audioCodec')
        video_decision = 'direct    play'
        video_codec = helpers.get_xml_attr(media_info, 'videoCodec')
        container = helpers.get_xml_attr(media_info, 'container')
        bitrate = helpers.get_xml_attr(media_info, 'bitrate')
        video_resolution = helpers.get_xml_attr(media_info, 'videoResolution')
        video_framerate = helpers.get_xml_attr(media_info, 'videoFrameRate')
        aspect_ratio = helpers.get_xml_attr(media_info, 'aspectRatio')
        width = helpers.get_xml_attr(media_info, 'width')
        height = helpers.get_xml_attr(media_info, 'height')
        duration = helpers.get_xml_attr(media_info, 'duration')
        progress = helpers.get_xml_attr(session, 'viewOffset')
        if session.getElementsByTagName('TranscodeSession'):
            transcode_session = session.getElementsByTagName('TranscodeSession')[0]
            transcode_key = helpers.get_xml_attr(transcode_session, 'key')
            throttled = helpers.get_xml_attr(transcode_session, 'throttled')
            transcode_progress = helpers.get_xml_attr(transcode_session, 'progress')
            transcode_speed = helpers.get_xml_attr(transcode_session, 'speed')
            audio_decision = helpers.get_xml_attr(transcode_session, 'audioDecision')
            transcode_audio_channels = helpers.get_xml_attr(transcode_session, 'audioChannels')
            transcode_audio_codec = helpers.get_xml_attr(transcode_session, 'audioCodec')
            video_decision = helpers.get_xml_attr(transcode_session, 'videoDecision')
            transcode_video_codec = helpers.get_xml_attr(transcode_session, 'videoCodec')
            transcode_width = (helpers.get_xml_attr(transcode_session, 'width') or width)
            transcode_height = (helpers.get_xml_attr(transcode_session, 'height') or height)
            transcode_container = helpers.get_xml_attr(transcode_session, 'container')
            transcode_protocol = helpers.get_xml_attr(transcode_session, 'protocol')
        else:
            transcode_key = ''
            throttled = '0'
            transcode_progress = '0'
            transcode_speed = ''
            transcode_audio_channels = ''
            transcode_audio_codec = ''
            transcode_video_codec = ''
            transcode_width = ''
            transcode_height = ''
            transcode_container = ''
            transcode_protocol = ''
        if ((video_decision == 'transcode') or (audio_decision == 'transcode')):
            transcode_decision = 'transcode'
        elif ((video_decision == 'copy') or (audio_decision == 'copy')):
            transcode_decision = 'copy'
        else:
            transcode_decision = 'direct    play'
        if media_info.getElementsByTagName('Part'):
            indexes = helpers.get_xml_attr(media_info.getElementsByTagName('Part')[0], 'indexes')
            part_id = helpers.get_xml_attr(media_info.getElementsByTagName('Part')[0], 'id')
            if (indexes == 'sd'):
                bif_thumb = ((('/library/parts/' + part_id) + '/indexes/sd/') + progress)
            else:
                bif_thumb = ''
        else:
            indexes = ''
            bif_thumb = ''
        if (plexpy.CONFIG.PMS_USE_BIF and (indexes == 'sd')):
            use_indexes = 1
        else:
            use_indexes = 0
        user_details = user_data.get_details(user=helpers.get_xml_attr(session.getElementsByTagName('User')[0], 'title'))
        if helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier').endswith('_Video'):
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')[:(-6)]
        else:
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')
        labels = []
        if session.getElementsByTagName('Label'):
            for label in session.getElementsByTagName('Label'):
                labels.append(helpers.get_xml_attr(label, 'tag'))
        if (helpers.get_xml_attr(session, 'type') == 'episode'):
            session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'), 'section_id': helpers.get_xml_attr(session, 'librarySectionID'), 'media_index': helpers.get_xml_attr(session, 'index'), 'parent_media_index': helpers.get_xml_attr(session, 'parentIndex'), 'art': helpers.get_xml_attr(session, 'art'), 'parent_thumb': helpers.get_xml_attr(session, 'parentThumb'), 'grandparent_thumb': helpers.get_xml_attr(session, 'grandparentThumb'), 'thumb': helpers.get_xml_attr(session, 'thumb'), 'bif_thumb': bif_thumb, 'user': user_details['username'], 'user_id': user_details['user_id'], 'friendly_name': user_details['friendly_name'], 'user_thumb': user_details['user_thumb'], 'ip_address': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'address').split('::ffff:')[(-1)], 'player': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'title'), 'platform': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'platform'), 'machine_id': machine_id, 'state': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'state'), 'grandparent_title': helpers.get_xml_attr(session, 'grandparentTitle'), 'parent_title': helpers.get_xml_attr(session, 'parentTitle'), 'title': helpers.get_xml_attr(session, 'title'), 'full_title': ('%s    -    %s' % (helpers.get_xml_attr(session, 'grandparentTitle'), helpers.get_xml_attr(session, 'title'))), 'year': helpers.get_xml_attr(session, 'year'), 'rating_key': helpers.get_xml_attr(session, 'ratingKey'), 'parent_rating_key': helpers.get_xml_attr(session, 'parentRatingKey'), 'grandparent_rating_key': helpers.get_xml_attr(session, 'grandparentRatingKey'), 'content_rating': helpers.get_xml_attr(session, 'contentRating'), 'labels': labels, 'transcode_key': transcode_key, 'throttled': throttled, 'transcode_progress': int(round(helpers.cast_to_float(transcode_progress), 0)), 'transcode_speed': str(round(helpers.cast_to_float(transcode_speed), 1)), 'audio_decision': audio_decision, 'audio_channels': audio_channels, 'audio_codec': audio_codec, 'video_decision': video_decision, 'video_codec': video_codec, 'height': height, 'width': width, 'container': container, 'bitrate': bitrate, 'video_resolution': video_resolution, 'video_framerate': video_framerate, 'aspect_ratio': aspect_ratio, 'transcode_decision': transcode_decision, 'transcode_audio_channels': transcode_audio_channels, 'transcode_audio_codec': transcode_audio_codec, 'transcode_video_codec': transcode_video_codec, 'transcode_width': transcode_width, 'transcode_height': transcode_height, 'transcode_container': transcode_container, 'transcode_protocol': transcode_protocol, 'duration': duration, 'view_offset': progress, 'progress_percent': str(helpers.get_percent(progress, duration)), 'indexes': use_indexes}
            if helpers.get_xml_attr(session, 'ratingKey').isdigit():
                session_output['media_type'] = helpers.get_xml_attr(session, 'type')
            else:
                session_output['media_type'] = 'clip'
        elif (helpers.get_xml_attr(session, 'type') == 'movie'):
            session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'), 'section_id': helpers.get_xml_attr(session, 'librarySectionID'), 'media_index': helpers.get_xml_attr(session, 'index'), 'parent_media_index': helpers.get_xml_attr(session, 'parentIndex'), 'art': helpers.get_xml_attr(session, 'art'), 'thumb': helpers.get_xml_attr(session, 'thumb'), 'bif_thumb': bif_thumb, 'parent_thumb': helpers.get_xml_attr(session, 'parentThumb'), 'grandparent_thumb': helpers.get_xml_attr(session, 'grandparentThumb'), 'user': user_details['username'], 'user_id': user_details['user_id'], 'friendly_name': user_details['friendly_name'], 'user_thumb': user_details['user_thumb'], 'ip_address': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'address').split('::ffff:')[(-1)], 'player': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'title'), 'platform': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'platform'), 'machine_id': machine_id, 'state': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'state'), 'grandparent_title': helpers.get_xml_attr(session, 'grandparentTitle'), 'parent_title': helpers.get_xml_attr(session, 'parentTitle'), 'title': helpers.get_xml_attr(session, 'title'), 'full_title': helpers.get_xml_attr(session, 'title'), 'year': helpers.get_xml_attr(session, 'year'), 'rating_key': helpers.get_xml_attr(session, 'ratingKey'), 'parent_rating_key': helpers.get_xml_attr(session, 'parentRatingKey'), 'grandparent_rating_key': helpers.get_xml_attr(session, 'grandparentRatingKey'), 'content_rating': helpers.get_xml_attr(session, 'contentRating'), 'labels': labels, 'transcode_key': transcode_key, 'throttled': throttled, 'transcode_progress': int(round(helpers.cast_to_float(transcode_progress), 0)), 'transcode_speed': str(round(helpers.cast_to_float(transcode_speed), 1)), 'audio_decision': audio_decision, 'audio_channels': audio_channels, 'audio_codec': audio_codec, 'video_decision': video_decision, 'video_codec': video_codec, 'height': height, 'width': width, 'container': container, 'bitrate': bitrate, 'video_resolution': video_resolution, 'video_framerate': video_framerate, 'aspect_ratio': aspect_ratio, 'transcode_decision': transcode_decision, 'transcode_audio_channels': transcode_audio_channels, 'transcode_audio_codec': transcode_audio_codec, 'transcode_video_codec': transcode_video_codec, 'transcode_width': transcode_width, 'transcode_height': transcode_height, 'transcode_container': transcode_container, 'transcode_protocol': transcode_protocol, 'duration': duration, 'view_offset': progress, 'progress_percent': str(helpers.get_percent(progress, duration)), 'indexes': use_indexes}
            if helpers.get_xml_attr(session, 'ratingKey').isdigit():
                session_output['media_type'] = helpers.get_xml_attr(session, 'type')
            else:
                session_output['media_type'] = 'clip'
        elif (helpers.get_xml_attr(session, 'type') == 'clip'):
            session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'), 'section_id': helpers.get_xml_attr(session, 'librarySectionID'), 'media_index': helpers.get_xml_attr(session, 'index'), 'parent_media_index': helpers.get_xml_attr(session, 'parentIndex'), 'art': helpers.get_xml_attr(session, 'art'), 'thumb': helpers.get_xml_attr(session, 'thumb'), 'bif_thumb': bif_thumb, 'parent_thumb': helpers.get_xml_attr(session, 'parentThumb'), 'grandparent_thumb': helpers.get_xml_attr(session, 'grandparentThumb'), 'user': user_details['username'], 'user_id': user_details['user_id'], 'friendly_name': user_details['friendly_name'], 'user_thumb': user_details['user_thumb'], 'ip_address': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'address').split('::ffff:')[(-1)], 'player': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'title'), 'platform': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'platform'), 'machine_id': machine_id, 'state': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'state'), 'grandparent_title': helpers.get_xml_attr(session, 'grandparentTitle'), 'parent_title': helpers.get_xml_attr(session, 'parentTitle'), 'title': helpers.get_xml_attr(session, 'title'), 'full_title': helpers.get_xml_attr(session, 'title'), 'year': helpers.get_xml_attr(session, 'year'), 'rating_key': helpers.get_xml_attr(session, 'ratingKey'), 'parent_rating_key': helpers.get_xml_attr(session, 'parentRatingKey'), 'grandparent_rating_key': helpers.get_xml_attr(session, 'grandparentRatingKey'), 'content_rating': helpers.get_xml_attr(session, 'contentRating'), 'labels': labels, 'transcode_key': transcode_key, 'throttled': throttled, 'transcode_progress': int(round(helpers.cast_to_float(transcode_progress), 0)), 'transcode_speed': str(round(helpers.cast_to_float(transcode_speed), 1)), 'audio_decision': audio_decision, 'audio_channels': audio_channels, 'audio_codec': audio_codec, 'video_decision': video_decision, 'video_codec': video_codec, 'height': height, 'width': width, 'container': container, 'bitrate': bitrate, 'video_resolution': video_resolution, 'video_framerate': video_framerate, 'aspect_ratio': aspect_ratio, 'transcode_decision': transcode_decision, 'transcode_audio_channels': transcode_audio_channels, 'transcode_audio_codec': transcode_audio_codec, 'transcode_video_codec': transcode_video_codec, 'transcode_width': transcode_width, 'transcode_height': transcode_height, 'transcode_container': transcode_container, 'transcode_protocol': transcode_protocol, 'duration': duration, 'view_offset': progress, 'progress_percent': str(helpers.get_percent(progress, duration)), 'media_type': helpers.get_xml_attr(session, 'type'), 'indexes': 0}
    elif (stream_type == 'photo'):
        media_info = session.getElementsByTagName('Media')[0]
        video_decision = 'direct    play'
        container = helpers.get_xml_attr(media_info, 'container')
        aspect_ratio = helpers.get_xml_attr(media_info, 'aspectRatio')
        width = helpers.get_xml_attr(media_info, 'width')
        height = helpers.get_xml_attr(media_info, 'height')
        if session.getElementsByTagName('TranscodeSession'):
            transcode_session = session.getElementsByTagName('TranscodeSession')[0]
            transcode_key = helpers.get_xml_attr(transcode_session, 'key')
            throttled = helpers.get_xml_attr(transcode_session, 'throttled')
            transcode_progress = helpers.get_xml_attr(transcode_session, 'progress')
            transcode_speed = helpers.get_xml_attr(transcode_session, 'speed')
            video_decision = helpers.get_xml_attr(transcode_session, 'videoDecision')
            transcode_video_codec = helpers.get_xml_attr(transcode_session, 'videoCodec')
            transcode_width = (helpers.get_xml_attr(transcode_session, 'width') or width)
            transcode_height = (helpers.get_xml_attr(transcode_session, 'height') or height)
            transcode_container = helpers.get_xml_attr(transcode_session, 'container')
            transcode_protocol = helpers.get_xml_attr(transcode_session, 'protocol')
        else:
            transcode_key = ''
            throttled = '0'
            transcode_progress = '0'
            transcode_speed = ''
            transcode_video_codec = ''
            transcode_width = ''
            transcode_height = ''
            transcode_container = ''
            transcode_protocol = ''
        transcode_decision = video_decision
        user_details = user_data.get_details(user=helpers.get_xml_attr(session.getElementsByTagName('User')[0], 'title'))
        if helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier').endswith('_Photo'):
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')[:(-6)]
        else:
            machine_id = helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'machineIdentifier')
        labels = []
        if session.getElementsByTagName('Label'):
            for label in session.getElementsByTagName('Label'):
                labels.append(helpers.get_xml_attr(label, 'tag'))
        session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'), 'section_id': helpers.get_xml_attr(session, 'librarySectionID'), 'media_index': helpers.get_xml_attr(session, 'index'), 'parent_media_index': helpers.get_xml_attr(session, 'parentIndex'), 'art': helpers.get_xml_attr(session, 'art'), 'parent_thumb': helpers.get_xml_attr(session, 'parentThumb'), 'grandparent_thumb': helpers.get_xml_attr(session, 'grandparentThumb'), 'thumb': helpers.get_xml_attr(session, 'thumb'), 'bif_thumb': '', 'user': user_details['username'], 'user_id': user_details['user_id'], 'friendly_name': user_details['friendly_name'], 'user_thumb': user_details['user_thumb'], 'ip_address': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'address').split('::ffff:')[(-1)], 'player': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'title'), 'platform': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'platform'), 'machine_id': machine_id, 'state': helpers.get_xml_attr(session.getElementsByTagName('Player')[0], 'state'), 'grandparent_title': helpers.get_xml_attr(session, 'grandparentTitle'), 'parent_title': helpers.get_xml_attr(session, 'parentTitle'), 'title': helpers.get_xml_attr(session, 'title'), 'full_title': ('%s    -    %s' % (helpers.get_xml_attr(session, 'grandparentTitle'), helpers.get_xml_attr(session, 'title'))), 'year': helpers.get_xml_attr(session, 'year'), 'rating_key': helpers.get_xml_attr(session, 'ratingKey'), 'parent_rating_key': helpers.get_xml_attr(session, 'parentRatingKey'), 'grandparent_rating_key': helpers.get_xml_attr(session, 'grandparentRatingKey'), 'content_rating': helpers.get_xml_attr(session, 'contentRating'), 'labels': labels, 'transcode_key': transcode_key, 'throttled': throttled, 'transcode_progress': int(round(helpers.cast_to_float(transcode_progress), 0)), 'transcode_speed': str(round(helpers.cast_to_float(transcode_speed), 1)), 'audio_decision': '', 'audio_channels': '', 'audio_codec': '', 'video_decision': video_decision, 'video_codec': '', 'height': height, 'width': width, 'container': container, 'bitrate': '', 'video_resolution': '', 'video_framerate': '', 'aspect_ratio': aspect_ratio, 'transcode_decision': transcode_decision, 'transcode_audio_channels': '', 'transcode_audio_codec': '', 'transcode_video_codec': transcode_video_codec, 'transcode_width': transcode_width, 'transcode_height': transcode_height, 'transcode_container': transcode_container, 'transcode_protocol': transcode_protocol, 'duration': '', 'view_offset': '', 'progress_percent': '100', 'media_type': 'photo', 'indexes': 0}
    else:
        logger.warn(u'PlexPy    Pmsconnect    ::    No    known    stream    types    found    in    session    list.')
    session_output['platform'] = common.PLATFORM_NAME_OVERRIDES.get(session_output['platform'], session_output['platform'])
    return session_output
