Skip to content

Commit

Permalink
Refactor: Handle parsing exceptions at a higher level
Browse files Browse the repository at this point in the history
  • Loading branch information
tomb7890 committed Sep 13, 2024
1 parent 39f01eb commit 3b16252
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 3 additions & 5 deletions parser.py
Original file line number Diff line number Diff line change
@@ -9,15 +9,13 @@ class Parser:
def __init__(self):
# this may be obsolete.
self.strict = True
self.tree = None

def parse(self, filename):
self.filename = filename
try:
self.tree = ET.parse(self.filename)
except ET.ParseError as pe:
logging.info(f"Can't parse file {self.filename} : {pe}")
self.tree = ET.parse(self.filename)

def channel_image(self):
def channel_image(self):
x = self.tree.findall(".//image//url")
if len(x) > 0:
return x[0].text
22 changes: 14 additions & 8 deletions report.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import parser
from lib import initialize_subscription
from subscription import Subscription
import logging


class ReportDatum:
@@ -70,14 +71,19 @@ def sort_reverse_chronologically(reportdata):

def enumerate_all_downloaded_episodes(subscription, reportdata):
p = parser.Parser()
p.parse(subscription.rssfile)
channel_image = p.channel_image()
episodes = p.episodes()

for e in episodes:
if subscription.database.find(e.guid):
d = ReportDatum(e, subscription, channel_image)
reportdata.append(d)
inputfile = subscription.rssfile
try:
p.parse(inputfile)
channel_image = p.channel_image()
episodes = p.episodes()

for e in episodes:
if subscription.database.find(e.guid):
d = ReportDatum(e, subscription, channel_image)
reportdata.append(d)

except xml.etree.ElementTree.ParseError as pe:
logging.info(f"Can't parse file {inputfile} : {pe}")


def episode_href(episode):

0 comments on commit 3b16252

Please sign in to comment.