Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs in rss client #11586

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix bugs in rss client
Strips all non-ascii characters when writing the xml document.

Writes a new xml document if the existing document is empty or just whitespace
kestrel-x86 authored Dec 13, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 534fec8eff0283f0ce52fec8843f09df44196c03
102 changes: 65 additions & 37 deletions medusa/clients/rss/__init__.py
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
log.logger.addHandler(logging.NullHandler())


XMLNS = {'medusa': 'https://pymedusa.com'}
XMLNS = {"medusa": "https://pymedusa.com"}


def add_result_to_feed(result):
@@ -30,10 +30,10 @@ def add_result_to_feed(result):
for k, v in XMLNS.items():
ElemTree.register_namespace(k, v)

file_path = os.path.join(app.RSS_DIR, u'medusa.xml')
file_path = os.path.join(app.RSS_DIR, "medusa.xml")

if result.provider is None:
log.error(u'Invalid provider name - this is a coding error, report it please')
log.error("Invalid provider name - this is a coding error, report it please")
return False

root_element = _read_existing_xml(file_path)
@@ -64,7 +64,7 @@ def __element(name, text, **attribs):
:return: ElemTree.Element
"""
elem = ElemTree.Element(name, attribs)
elem.text = str(text) if text is not None else ''
elem.text = str(text) if text is not None else ""
return elem


@@ -74,7 +74,9 @@ def _pubdate():

:return: string
"""
return (datetime.now() + timedelta(hours=time.timezone / 3600)).strftime('%a, %d %b %Y %H:%M:%S') + ' GMT'
return (datetime.now() + timedelta(hours=time.timezone / 3600)).strftime(
"%a, %d %b %Y %H:%M:%S"
) + " GMT"


def _result_to_item(result):
@@ -83,24 +85,43 @@ def _result_to_item(result):

:return: ElemTree.Element
"""
item_root = __element('item', None)
item_root.append(__element('title', result.name))
item_root.append(__element('link', result.url))
item_root.append(__element('guid', result.identifier, isPermalink='false'))
item_root.append(__element('pubDate', _pubdate()))
item_root = __element("item", None)
item_root.append(__element("title", result.name))
item_root.append(__element("link", result.url))
item_root.append(__element("guid", result.identifier, isPermalink="false"))
item_root.append(__element("pubDate", _pubdate()))
if len(result.episodes) == 1:
item_root.append(__element('description', f'{result.episodes[0].name} | {result.episodes[0].description}'))
item_root.append(
__element(
"description",
f"{result.episodes[0].name} | {result.episodes[0].description}",
)
)
else:
item_root.append(__element('description', result.name))
item_root.append(__element('enclosure', None, url=result.url, length='0', type='application/x-bittorrent' if result.result_type else 'application/x-nzb'))
item_root.append(__element('medusa:series',
result.series.name,
isAnime=str(result.series.anime),
tvdb=str(result.series.tvdb_id),
imdb=result.series.imdb_id))
item_root.append(__element('medusa:season', result.actual_season))
item_root.append(__element('medusa:episode', result.actual_episode))
item_root.append(__element('medusa:provider', result.provider.name))
item_root.append(__element("description", result.name))
item_root.append(
__element(
"enclosure",
None,
url=result.url,
length="0",
type="application/x-bittorrent"
if result.result_type
else "application/x-nzb",
)
)
item_root.append(
__element(
"medusa:series",
result.series.name,
isAnime=str(result.series.anime),
tvdb=str(result.series.tvdb_id),
imdb=result.series.imdb_id,
)
)
item_root.append(__element("medusa:season", result.actual_season))
item_root.append(__element("medusa:episode", result.actual_episode))
item_root.append(__element("medusa:provider", result.provider.name))

return item_root

@@ -112,7 +133,7 @@ def _find_item_start_index(channel_element):
:return: int
"""
for i, child in enumerate(channel_element):
if child.tag == 'item':
if child.tag == "item":
return i
return len(channel_element)

@@ -130,15 +151,22 @@ def _read_existing_xml(file_path):
return root

try:
with open(file_path, 'r') as f:
with open(file_path, "r") as f:
xml_string = f.read()
except OSError as e:
log.error(u'Error reading RSS file at {0}: {1}', file_path, ex(e))
log.error("Error reading RSS file at {0}: {1}", file_path, ex(e))

if len(xml_string.strip()) == 0:
root = _make_empty_xml()
if not _write_xml(root, file_path):
return None
return root

try:
root = ElemTree.fromstring(xml_string)
return root
except ElemTree.ParseError as e:
log.error(u'Error parsing RSS file at {0}: {1}', file_path, ex(e))
log.error("Error parsing RSS file at {0}: {1}", file_path, ex(e))
return None


@@ -148,14 +176,14 @@ def _make_empty_xml():

:return: ElemTree.Element root 'rss' element
"""
root = ElemTree.Element('rss')
root = ElemTree.Element("rss")
for k, v in XMLNS.items():
root.attrib['xmlns:' + k] = v
root.attrib['version'] = '2.0'
root.append(__element('title', 'Medusa RSS Feed'))
root.append(__element('link', 'https://pymedusa.com/'))
root.append(__element('description', 'Medusa RSS Feed'))
root.append(__element('channel', None))
root.attrib["xmlns:" + k] = v
root.attrib["version"] = "2.0"
root.append(__element("title", "Medusa RSS Feed"))
root.append(__element("link", "https://pymedusa.com/"))
root.append(__element("description", "Medusa RSS Feed"))
root.append(__element("channel", None))
return root


@@ -166,13 +194,13 @@ def _write_xml(root_element, file_path):
:return: bool representing success
"""
try:
xml_string = ElemTree.tostring(root_element, encoding='unicode')
xml_string = xml_string.replace('\n', '')
with open(file_path, 'w') as f:
xml_string = ElemTree.tostring(root_element, encoding="unicode")
xml_string = xml_string.replace("\n", "").encode("ascii", "ignore")
with open(file_path, "wb") as f:
f.write(xml_string)
return True
except OSError as e:
log.error(u'Error writing RSS file at {0}: {1}', file_path, ex(e))
log.error("Error writing RSS file at {0}: {1}", file_path, ex(e))
return False


@@ -182,4 +210,4 @@ def _find_channel_element(root_element):

:return: ElemTree.Element channel
"""
return root_element.find('channel')
return root_element.find("channel")