mirror of
https://codeberg.org/Reuh/feather.git
synced 2025-12-14 15:29:08 +00:00
fix: skip corrupted articles instead of aborting the whole program
This commit is contained in:
parent
13d82f9913
commit
0aa10d4b02
2 changed files with 24 additions and 8 deletions
|
|
@ -10,7 +10,7 @@ from datetime import datetime
|
||||||
|
|
||||||
from feather.config import Config
|
from feather.config import Config
|
||||||
from feather.client import GReaderSession, TTRSession, ClientSession, Article, ArticleId
|
from feather.client import GReaderSession, TTRSession, ClientSession, Article, ArticleId
|
||||||
from feather.data import FileArticle
|
from feather.data import FileArticle, InvalidArticleFileError
|
||||||
|
|
||||||
|
|
||||||
async def sleep_min_max(min_sleep: float, max_sleep: float, stop_sleep_event: Event):
|
async def sleep_min_max(min_sleep: float, max_sleep: float, stop_sleep_event: Event):
|
||||||
|
|
@ -53,7 +53,12 @@ class FeatherApp:
|
||||||
"""Iterate over all the articles in local storage"""
|
"""Iterate over all the articles in local storage"""
|
||||||
config = self.config
|
config = self.config
|
||||||
for json_path in config.json_root.glob("*.json"):
|
for json_path in config.json_root.glob("*.json"):
|
||||||
|
try:
|
||||||
yield FileArticle(config, json_path)
|
yield FileArticle(config, json_path)
|
||||||
|
except InvalidArticleFileError:
|
||||||
|
print(
|
||||||
|
f"WARNING: Skipping corrupted article file {json_path}. Delete this file and its associated HTML file (if it exists) to resolve this warning."
|
||||||
|
)
|
||||||
|
|
||||||
def remove_empty_categories(self):
|
def remove_empty_categories(self):
|
||||||
"""Remove empty directories in the HTML directory"""
|
"""Remove empty directories in the HTML directory"""
|
||||||
|
|
@ -154,11 +159,16 @@ class FeatherApp:
|
||||||
article.write()
|
article.write()
|
||||||
new_articles += 1
|
new_articles += 1
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
old_article = FileArticle(config, json_path)
|
old_article = FileArticle(config, json_path)
|
||||||
if article.was_updated(old_article):
|
if article.was_updated(old_article):
|
||||||
old_article.delete()
|
old_article.delete()
|
||||||
article.write()
|
article.write()
|
||||||
updated_articles += 1
|
updated_articles += 1
|
||||||
|
except InvalidArticleFileError:
|
||||||
|
print(
|
||||||
|
f" WARNING: Skipping corrupted article file {json_path}. Delete this file and its associated HTML file (if it exists) to resolve this warning."
|
||||||
|
)
|
||||||
|
|
||||||
# Remove or mark-as-read articles that we didn't get from the server but are in the JSON directory
|
# Remove or mark-as-read articles that we didn't get from the server but are in the JSON directory
|
||||||
removed_articles = 0
|
removed_articles = 0
|
||||||
|
|
|
||||||
|
|
@ -283,13 +283,19 @@ class Article(ABC):
|
||||||
self.delete() # paths might change so we preemptively remove the old file
|
self.delete() # paths might change so we preemptively remove the old file
|
||||||
self.write(recompute_paths=True) # rewrite JSON & HTML
|
self.write(recompute_paths=True) # rewrite JSON & HTML
|
||||||
|
|
||||||
|
class InvalidArticleFileError(ValueError):
|
||||||
|
pass
|
||||||
|
|
||||||
class FileArticle(Article):
|
class FileArticle(Article):
|
||||||
def __init__(self, config: Config, json_path: Path) -> Article:
|
def __init__(self, config: Config, json_path: Path) -> Article:
|
||||||
self.config = config
|
self.config = config
|
||||||
self.json_path = json_path
|
self.json_path = json_path
|
||||||
|
|
||||||
|
try:
|
||||||
article_json = json.load(json_path.open("r"))
|
article_json = json.load(json_path.open("r"))
|
||||||
|
except ValueError as e:
|
||||||
|
raise InvalidArticleFileError(f"Can't load article file {json_path}") from e
|
||||||
|
|
||||||
for field in article_json:
|
for field in article_json:
|
||||||
setattr(self, field, article_json[field])
|
setattr(self, field, article_json[field])
|
||||||
self.category = Category.fromdict(article_json["category"])
|
self.category = Category.fromdict(article_json["category"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue