<commit_before>import logging
import re
import socket
import urllib2
import xml.etree.ElementTree as ET

'''
    This is a helper class to provide assistance with all Plex API calls
'''


class Plex(object):
    def __init__(self, cfg):

        self.logger = logging.getLogger(__name__)
        self.cfg = cfg

    '''
        Get Media metadata from Plex users playing sessions.
        
        @param l_id     : Media id
        @param user_id  : Plex user id

        @return         : Media metadata
    '''

    def get_media_metadata_from_sessions(self, l_id, user_id):

        url = '{url}/status/sessions?X-Plex-Token={plex_token}'.format(url=self.cfg.get('plex-trakt-scrobbler',
                                                                                        'mediaserver_url'),
                                                                       plex_token=self.cfg.get('plex-trakt-scrobbler',
                                                                                               'plex_token'))
        self.logger.info('Fetching sessions status from {url}'.format(url=url))

        try:
            metadata = urllib2.urlopen(url, timeout=2)
        except urllib2.URLError, e:
            logger.error('urllib2 error reading from {url} \'{error}\''.format(url=url, error=e))
            return None
        except socket.timeout, e:
            logger.error('Timeout reading from {url} \'{error}\''.format(url=url, error=e))
            return None

        tree = ET.fromstring(metadata.read())
        videos = tree.findall('Video')

        if videos is None:
            logger.error('No videos found in sessions status feed.')
        else:
            for video in videos:
                user = video.find('User')
                m_id = video.get('key')

                if user.get('id') != user_id:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is from another user.'
                                     .format(m_id=m_id))
                    continue

                if '/library/metadata/' + l_id != m_id:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is not the wanted item.'
                                     .format(m_id=m_id))
                    return None

                media_type = video.get('type')
                if video.get('type') != media_type:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is not a ' + media_type + '.'
                                     .format(m_id=m_id))
                    return None

                transcode = video.find('TranscodeSession')
                if transcode is None:
                    self.logger.info(
                        'Ignoring played item library-id={m_id}, could not determine transcoding information.'
                        .format(m_id=m_id))
                    return None

                player = video.find('Player')
                if player is None:
                    self.logger.info('Ignoring played item library-id={m_id}, could not determine player information.'
                                     .format(m_id=m_id))
                    return None

                guid = video.get('guid')
                regex_str = 'com.plexapp.agents.thetvdb://([0-9]+)/([0-9]+)/([0-9]+).*'
                if media_type == 'movie':<commit_msg>Fix for Plex's change from TranscoderSession to Part ¬¬<commit_after>import logging
import re
import socket
import urllib2
import xml.etree.ElementTree as ET

'''
    This is a helper class to provide assistance with all Plex API calls
'''


class Plex(object):
    def __init__(self, cfg):

        self.logger = logging.getLogger(__name__)
        self.cfg = cfg

    '''
        Get Media metadata from Plex users playing sessions.
        
        @param l_id     : Media id
        @param user_id  : Plex user id

        @return         : Media metadata
    '''

    def get_media_metadata_from_sessions(self, l_id, user_id):

        url = '{url}/status/sessions?X-Plex-Token={plex_token}'.format(url=self.cfg.get('plex-trakt-scrobbler',
                                                                                        'mediaserver_url'),
                                                                       plex_token=self.cfg.get('plex-trakt-scrobbler',
                                                                                               'plex_token'))
        self.logger.info('Fetching sessions status from {url}'.format(url=url))

        try:
            metadata = urllib2.urlopen(url, timeout=2)
        except urllib2.URLError, e:
            logger.error('urllib2 error reading from {url} \'{error}\''.format(url=url, error=e))
            return None
        except socket.timeout, e:
            logger.error('Timeout reading from {url} \'{error}\''.format(url=url, error=e))
            return None

        tree = ET.fromstring(metadata.read())
        videos = tree.findall('Video')

        if videos is None:
            logger.error('No videos found in sessions status feed.')
        else:
            for video in videos:
                user = video.find('User')
                m_id = video.get('key')

                if user.get('id') != user_id:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is from another user.'
                                     .format(m_id=m_id))
                    continue

                if '/library/metadata/' + l_id != m_id:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is not the wanted item.'
                                     .format(m_id=m_id))
                    return None

                media_type = video.get('type')
                if video.get('type') != media_type:
                    self.logger.info('Ignoring played item library-id={m_id}, because it is not a ' + media_type + '.'
                                     .format(m_id=m_id))
                    return None

                transcode = video.find('Part')
                if transcode is None:
                    self.logger.info(
                        'Ignoring played item library-id={m_id}, could not determine transcoding information.'
                        .format(m_id=m_id))
                    return None

                player = video.find('Player')
                if player is None:
                    self.logger.info('Ignoring played item library-id={m_id}, could not determine player information.'
                                     .format(m_id=m_id))
                    return None

                guid = video.get('guid')
                regex_str = 'com.plexapp.agents.thetvdb://([0-9]+)/([0-9]+)/([0-9]+).*'
                if media_type == 'movie':
{'fraction_non_alphanumeric': 0.10333235895936861, 'fraction_numerical': 0.003215434083601286, 'mean_word_length': 1.355593803786575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5130459', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1707, 'n_words': 435}