Skip to content

Commit 5c89023

Browse files
committed
Fix
- issue with response not being XML
1 parent ccee581 commit 5c89023

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

custom_components/plex_recently_added/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
3636
try:
37-
client = await hass.async_add_executor_job(
38-
setup_client,
37+
client = await setup_client(
3938
hass,
4039
config_entry.data[CONF_SSL],
4140
config_entry.data[CONF_API_KEY],

custom_components/plex_recently_added/config_flow.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ async def async_step_user(
6767
if user_input is not None:
6868
self._async_abort_entries_match({CONF_API_KEY: user_input[CONF_API_KEY]})
6969
try:
70-
await self.hass.async_add_executor_job(
71-
setup_client,
70+
await setup_client(
7271
self.hass,
7372
user_input[CONF_SSL],
7473
user_input[CONF_API_KEY],

custom_components/plex_recently_added/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
USER_AGENT: Final = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36"
18-
ACCEPTS: Final = "application/xml"
18+
ACCEPTS: Final = "application/xml, text/xml;q=0.9"
1919

2020
DEFAULT_PARSE_DICT: Final = {
2121
'title_default': '$title',

custom_components/plex_recently_added/coordinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ def __init__(self, hass: HomeAssistant, client: PlexApi):
2828

2929
async def _async_update_data(self) -> Dict[str, Any]:
3030
try:
31-
return await self.hass.async_add_executor_job(self._client.update)
31+
return await self._client.update()
3232
except FailedToLogin:
3333
raise ConfigEntryError("Failed to Log-in") from err

custom_components/plex_recently_added/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from homeassistant.core import HomeAssistant
22
from .plex_api import PlexApi
33

4-
def setup_client(
4+
async def setup_client(
55
hass: HomeAssistant,
66
ssl: bool,
77
token: str,
@@ -16,5 +16,5 @@ def setup_client(
1616
):
1717
client = PlexApi(hass, ssl, token, max, on_deck, host, port, section_types, section_libraries, exclude_keywords, verify_ssl)
1818

19-
client.update()
19+
await client.update()
2020
return client

custom_components/plex_recently_added/plex_api.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import logging
1212
_LOGGER = logging.getLogger(__name__)
1313

14+
def check_headers(response):
15+
if 'text/xml' not in response.headers.get('Content-Type', '') and 'application/xml' not in response.headers.get('Content-Type', ''):
16+
raise ValueError(f"Expected XML but received different content type: {response.headers.get('Content-Type')}")
17+
1418
class PlexApi():
1519
def __init__(
1620
self,
@@ -37,8 +41,8 @@ def __init__(
3741
self._section_libraries = section_libraries
3842
self._exclude_keywords = exclude_keywords
3943
self._verify_ssl = verify_ssl
40-
41-
def update(self):
44+
45+
async def update(self):
4246
info_url = 'http{0}://{1}:{2}'.format(
4347
self._ssl,
4448
self._host,
@@ -49,18 +53,20 @@ def update(self):
4953
if not self._verify_ssl:
5054
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
5155
try:
52-
info_res = requests.get(info_url + "/", headers={
53-
"X-Plex-Token": self._token,
54-
"User-agent": USER_AGENT,
55-
"Accepts": ACCEPTS,
56-
},
57-
verify=self._verify_ssl,
58-
timeout=10)
59-
try:
60-
root = ElementTree.fromstring(info_res.text)
61-
except:
62-
_LOGGER.error(info_res.text)
63-
raise ElementTree.ParseError
56+
info_res = await self._hass.async_add_executor_job(
57+
requests.get,
58+
f'{info_url}?X-Plex-Token={self._token}',
59+
{
60+
"headers":{
61+
"User-agent": USER_AGENT,
62+
"Accept": ACCEPTS,
63+
},
64+
"verify":self._verify_ssl,
65+
"timeout":10
66+
}
67+
)
68+
check_headers(info_res)
69+
root = ElementTree.fromstring(info_res.text)
6470
identifier = root.get("machineIdentifier")
6571
except OSError as e:
6672
raise FailedToLogin
@@ -74,18 +80,20 @@ def update(self):
7480
sections = []
7581
libs = []
7682
try:
77-
libraries = requests.get(all_libraries, headers={
78-
"X-Plex-Token": self._token,
79-
"User-agent": USER_AGENT,
80-
"Accepts": ACCEPTS,
81-
},
82-
verify=self._verify_ssl,
83-
timeout=10)
84-
try:
85-
root = ElementTree.fromstring(libraries.text)
86-
except:
87-
_LOGGER.error(libraries.text)
88-
raise ElementTree.ParseError
83+
libraries = await self._hass.async_add_executor_job(
84+
requests.get,
85+
f'{all_libraries}?X-Plex-Token={self._token}',
86+
{
87+
"headers":{
88+
"User-agent": USER_AGENT,
89+
"Accept": ACCEPTS,
90+
},
91+
"verify":self._verify_ssl,
92+
"timeout":10
93+
}
94+
)
95+
check_headers(libraries)
96+
root = ElementTree.fromstring(libraries.text)
8997
for lib in root.findall("Directory"):
9098
libs.append(lib.get("title"))
9199
if lib.get("type") in self._section_types and (len(self._section_libraries) == 0 or lib.get("title") in self._section_libraries):
@@ -97,18 +105,20 @@ def update(self):
97105
data = []
98106
for library in sections:
99107
recent_or_deck = on_deck if self._on_deck else recently_added
100-
sub_sec = requests.get(recent_or_deck.format(library, self._max * 2), headers={
101-
"X-Plex-Token": self._token,
102-
"User-agent": USER_AGENT,
103-
"Accepts": ACCEPTS,
104-
},
105-
verify=self._verify_ssl,
106-
timeout=10)
107-
try:
108-
root = ElementTree.fromstring(sub_sec.text)
109-
except:
110-
_LOGGER.error(sub_sec.text)
111-
raise ElementTree.ParseError
108+
sub_sec = await self._hass.async_add_executor_job(
109+
requests.get,
110+
f'{recent_or_deck.format(library, self._max * 2)}&X-Plex-Token={self._token}',
111+
{
112+
"headers":{
113+
"User-agent": USER_AGENT,
114+
"Accept": ACCEPTS,
115+
},
116+
"verify":self._verify_ssl,
117+
"timeout":10
118+
}
119+
)
120+
check_headers(sub_sec)
121+
root = ElementTree.fromstring(sub_sec.text)
112122
data += parse_library(root)
113123

114124
return {

0 commit comments

Comments
 (0)