(baseURL, path, options={}, authtoken='', enableGzip=False)
| 427 | returned XML or 'False' in case of error |
| 428 | """ |
| 429 | def getXMLFromPMS(baseURL, path, options={}, authtoken='', enableGzip=False): |
| 430 | xargs = {} |
| 431 | if not options==None: |
| 432 | xargs = getXArgsDeviceInfo(options) |
| 433 | if not authtoken=='': |
| 434 | xargs['X-Plex-Token'] = authtoken |
| 435 | |
| 436 | dprint(__name__, 1, "URL: {0}{1}", baseURL, path) |
| 437 | dprint(__name__, 1, "xargs: {0}", xargs) |
| 438 | |
| 439 | method = 'GET' |
| 440 | if options is not None and 'PlexConnectMethod' in options: |
| 441 | dprint(__name__, 1, 'Custom method ' + method) |
| 442 | method = options['PlexConnectMethod'] |
| 443 | |
| 444 | request = urllib2.Request(baseURL+path , None, xargs) |
| 445 | request.add_header('User-agent', 'PlexConnect') |
| 446 | |
| 447 | request.get_method = lambda: method |
| 448 | if enableGzip: |
| 449 | request.add_header('Accept-encoding', 'gzip') |
| 450 | |
| 451 | try: |
| 452 | response = urllib2.urlopen(request, timeout=20) |
| 453 | except (urllib2.URLError, httplib.HTTPException) as e: |
| 454 | dprint(__name__, 1, 'No Response from Plex Media Server') |
| 455 | if hasattr(e, 'reason'): |
| 456 | dprint(__name__, 1, "We failed to reach a server. Reason: {0}", e.reason) |
| 457 | elif hasattr(e, 'code'): |
| 458 | dprint(__name__, 1, "The server couldn't fulfill the request. Error code: {0}", e.code) |
| 459 | dprint(__name__, 1, 'Traceback:\n{0}', traceback.format_exc()) |
| 460 | return False |
| 461 | except IOError: |
| 462 | dprint(__name__, 0, 'Error loading response XML from Plex Media Server:\n{0}', traceback.format_exc()) |
| 463 | return False |
| 464 | |
| 465 | if response.info().get('Content-Encoding') == 'gzip': |
| 466 | buf = StringIO.StringIO(response.read()) |
| 467 | file = gzip.GzipFile(fileobj=buf) |
| 468 | XML = etree.parse(file) |
| 469 | else: |
| 470 | # parse into etree |
| 471 | XML = etree.parse(response) |
| 472 | |
| 473 | dprint(__name__, 1, "====== received PMS-XML ======") |
| 474 | dprint(__name__, 1, XML.getroot()) |
| 475 | dprint(__name__, 1, "====== PMS-XML finished ======") |
| 476 | |
| 477 | #XMLTree = etree.ElementTree(etree.fromstring(response)) |
| 478 | |
| 479 | return XML |
| 480 | |
| 481 | |
| 482 |
no test coverage detected