'Handles updating the Plex Media Server host via HTTP API
Plex Media Server currently only supports updating the whole video library and not a specific path.
Returns:
Returns None for no issue, else a string of host with connection issues'

def update_library(self, ep_obj=None, host=None, username=None, password=None, plex_server_token=None, force=True):

    if (sickrage.srCore.srConfig.USE_PLEX and sickrage.srCore.srConfig.PLEX_UPDATE_LIBRARY):
        if (not sickrage.srCore.srConfig.PLEX_SERVER_HOST):
            sickrage.srCore.srLogger.debug(u'PLEX:    No    Plex    Media    Server    host    specified,    check    your    settings')
            return False
        if (not host):
            host = sickrage.srCore.srConfig.PLEX_SERVER_HOST
        if (not username):
            username = sickrage.srCore.srConfig.PLEX_USERNAME
        if (not password):
            password = sickrage.srCore.srConfig.PLEX_PASSWORD
        if (not plex_server_token):
            plex_server_token = sickrage.srCore.srConfig.PLEX_SERVER_TOKEN
        token_arg = u''
        if plex_server_token:
            token_arg = (u'?X-Plex-Token=' + plex_server_token)
        elif (username and password):
            sickrage.srCore.srLogger.debug((u'PLEX:    fetching    plex.tv    credentials    for    user:    ' + username))
            req = urllib2.Request(u'https://plex.tv/users/sign_in.xml', data=u'')
            authheader = (u'Basic    %s' % base64.encodestring((u'%s:%s' % (username, password)))[:(-1)])
            req.add_header(u'Authorization', authheader)
            req.add_header(u'X-Plex-Device-Name', u'SiCKRAGE')
            req.add_header(u'X-Plex-Product', u'SiCKRAGE    Notifier')
            req.add_header(u'X-Plex-Client-Identifier', sickrage.srCore.USER_AGENT)
            req.add_header(u'X-Plex-Version', u'1.0')
            try:
                response = urllib2.urlopen(req)
                auth_tree = ElementTree.parse(response)
                token = auth_tree.findall(u'.//authentication-token')[0].text
                token_arg = (u'?X-Plex-Token=' + token)
            except urllib2.URLError as e:
                sickrage.srCore.srLogger.debug((u'PLEX:    Error    fetching    credentials    from    from    plex.tv    for    user    %s:    %s' % (username, e)))
            except (ValueError, IndexError) as e:
                sickrage.srCore.srLogger.debug((u'PLEX:    Error    parsing    plex.tv    response:    ' + e))
        file_location = (u'' if (None is ep_obj) else ep_obj.location)
        host_list = [x.strip() for x in host.split(u',')]
        hosts_all = {}
        hosts_match = {}
        hosts_failed = []
        for cur_host in host_list:
            url = (u'http://%s/library/sections%s' % (cur_host, token_arg))
            try:
                xml_tree = ElementTree.parse(urllib.urlopen(url))
                media_container = xml_tree.getroot()
            except IOError as e:
                sickrage.srCore.srLogger.warning(u'PLEX:    Error    while    trying    to    contact    Plex    Media    Server:    {}'.format(e.message))
                hosts_failed.append(cur_host)
                continue
            except Exception as e:
                if (u'invalid    token' in str(e)):
                    sickrage.srCore.srLogger.error(u'PLEX:    Please    set    TOKEN    in    Plex    settings:    ')
                else:
                    sickrage.srCore.srLogger.error(u'PLEX:    Error    while    trying    to    contact    Plex    Media    Server:    {}'.format(e.message))
                continue
            sections = media_container.findall(u'.//Directory')
            if (not sections):
                sickrage.srCore.srLogger.debug((u'PLEX:    Plex    Media    Server    not    running    on:    ' + cur_host))
                hosts_failed.append(cur_host)
                continue
            for section in sections:
                if (u'show' == section.attrib[u'type']):
                    keyed_host = [(str(section.attrib[u'key']), cur_host)]
                    hosts_all.update(keyed_host)
                    if (not file_location):
                        continue
                    for section_location in section.findall(u'.//Location'):
                        section_path = re.sub(u'[/\\\\]+', u'/', section_location.attrib[u'path'].lower())
                        section_path = re.sub(u'^(.{,2})[/\\\\]', u'', section_path)
                        location_path = re.sub(u'[/\\\\]+', u'/', file_location.lower())
                        location_path = re.sub(u'^(.{,2})[/\\\\]', u'', location_path)
                        if (section_path in location_path):
                            hosts_match.update(keyed_host)
        hosts_try = (hosts_all.copy(), hosts_match.copy())[bool(hosts_match)]
        host_list = []
        for (section_key, cur_host) in hosts_try.items():
            url = (u'http://%s/library/sections/%s/refresh%s' % (cur_host, section_key, token_arg))
            try:
                (force and urllib.urlopen(url))
                host_list.append(cur_host)
            except Exception as e:
                sickrage.srCore.srLogger.warning(u'PLEX:    Error    updating    library    section    for    Plex    Media    Server:    {}'.format(e.message))
                hosts_failed.append(cur_host)
        if hosts_match:
            sickrage.srCore.srLogger.debug((u'PLEX:    Updating    hosts    where    TV    section    paths    match    the    downloaded    show:    ' + u',    '.join(set(host_list))))
        else:
            sickrage.srCore.srLogger.debug((u'PLEX:    Updating    all    hosts    with    TV    sections:    ' + u',    '.join(set(host_list))))
        return (u',    '.join(set(hosts_failed)), None)[(not len(hosts_failed))]
