Add feed URL change functionality and related tests
This commit is contained in:
parent
24d4d7a293
commit
d87341d729
9 changed files with 176 additions and 14 deletions
|
|
@ -2,10 +2,13 @@ from __future__ import annotations
|
|||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from discord_rss_bot.filter.utils import is_regex_match, is_word_in_text
|
||||
from discord_rss_bot.filter.utils import is_regex_match
|
||||
from discord_rss_bot.filter.utils import is_word_in_text
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from reader import Entry, Feed, Reader
|
||||
from reader import Entry
|
||||
from reader import Feed
|
||||
from reader import Reader
|
||||
|
||||
|
||||
def feed_has_blacklist_tags(custom_reader: Reader, feed: Feed) -> bool:
|
||||
|
|
|
|||
|
|
@ -2,10 +2,13 @@ from __future__ import annotations
|
|||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from discord_rss_bot.filter.utils import is_regex_match, is_word_in_text
|
||||
from discord_rss_bot.filter.utils import is_regex_match
|
||||
from discord_rss_bot.filter.utils import is_word_in_text
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from reader import Entry, Feed, Reader
|
||||
from reader import Entry
|
||||
from reader import Feed
|
||||
from reader import Reader
|
||||
|
||||
|
||||
def has_white_tags(custom_reader: Reader, feed: Feed) -> bool:
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ def export_state(reader: Reader, backup_path: Path) -> None:
|
|||
|
||||
try:
|
||||
webhooks: list[str | int | float | bool | dict[str, Any] | list[Any] | None] = list(
|
||||
reader.get_tag((), "webhooks", [])
|
||||
reader.get_tag((), "webhooks", []),
|
||||
)
|
||||
except TagNotFoundError:
|
||||
webhooks = []
|
||||
|
|
|
|||
|
|
@ -31,8 +31,10 @@ from markdownify import markdownify
|
|||
from reader import Entry
|
||||
from reader import EntryNotFoundError
|
||||
from reader import Feed
|
||||
from reader import FeedExistsError
|
||||
from reader import FeedNotFoundError
|
||||
from reader import Reader
|
||||
from reader import ReaderError
|
||||
from reader import TagNotFoundError
|
||||
from starlette.responses import RedirectResponse
|
||||
|
||||
|
|
@ -697,6 +699,45 @@ async def post_set_update_interval(
|
|||
return RedirectResponse(url=f"/feed?feed_url={urllib.parse.quote(clean_feed_url)}", status_code=303)
|
||||
|
||||
|
||||
@app.post("/change_feed_url")
|
||||
async def post_change_feed_url(
|
||||
old_feed_url: Annotated[str, Form()],
|
||||
new_feed_url: Annotated[str, Form()],
|
||||
) -> RedirectResponse:
|
||||
"""Change the URL for an existing feed.
|
||||
|
||||
Args:
|
||||
old_feed_url: Current feed URL.
|
||||
new_feed_url: New feed URL to change to.
|
||||
|
||||
Returns:
|
||||
RedirectResponse: Redirect to the feed page for the resulting URL.
|
||||
|
||||
Raises:
|
||||
HTTPException: If the old feed is not found, the new URL already exists, or change fails.
|
||||
"""
|
||||
clean_old_feed_url: str = old_feed_url.strip()
|
||||
clean_new_feed_url: str = new_feed_url.strip()
|
||||
|
||||
if not clean_old_feed_url or not clean_new_feed_url:
|
||||
raise HTTPException(status_code=400, detail="Feed URLs cannot be empty")
|
||||
|
||||
if clean_old_feed_url == clean_new_feed_url:
|
||||
return RedirectResponse(url=f"/feed?feed_url={urllib.parse.quote(clean_old_feed_url)}", status_code=303)
|
||||
|
||||
try:
|
||||
reader.change_feed_url(clean_old_feed_url, clean_new_feed_url)
|
||||
except FeedNotFoundError as e:
|
||||
raise HTTPException(status_code=404, detail=f"Feed not found: {clean_old_feed_url}") from e
|
||||
except FeedExistsError as e:
|
||||
raise HTTPException(status_code=409, detail=f"Feed already exists: {clean_new_feed_url}") from e
|
||||
except ReaderError as e:
|
||||
raise HTTPException(status_code=400, detail=f"Failed to change feed URL: {e}") from e
|
||||
|
||||
commit_state_change(reader, f"Change feed URL from {clean_old_feed_url} to {clean_new_feed_url}")
|
||||
return RedirectResponse(url=f"/feed?feed_url={urllib.parse.quote(clean_new_feed_url)}", status_code=303)
|
||||
|
||||
|
||||
@app.post("/reset_update_interval")
|
||||
async def post_reset_update_interval(
|
||||
feed_url: Annotated[str, Form()],
|
||||
|
|
@ -804,7 +845,7 @@ async def get_feed(feed_url: str, request: Request, starting_after: str = ""):
|
|||
except EntryNotFoundError as e:
|
||||
current_entries = list(reader.get_entries(feed=clean_feed_url))
|
||||
msg: str = f"{e}\n\n{[entry.id for entry in current_entries]}"
|
||||
html: str = create_html_for_feed(current_entries)
|
||||
html: str = create_html_for_feed(current_entries, clean_feed_url)
|
||||
|
||||
# Get feed and global intervals for error case too
|
||||
feed_interval: int | None = None
|
||||
|
|
@ -860,7 +901,7 @@ async def get_feed(feed_url: str, request: Request, starting_after: str = ""):
|
|||
last_entry = entries[-1]
|
||||
|
||||
# Create the html for the entries.
|
||||
html: str = create_html_for_feed(entries)
|
||||
html: str = create_html_for_feed(entries, clean_feed_url)
|
||||
|
||||
try:
|
||||
should_send_embed: bool = bool(reader.get_tag(feed, "should_send_embed"))
|
||||
|
|
@ -907,11 +948,12 @@ async def get_feed(feed_url: str, request: Request, starting_after: str = ""):
|
|||
return templates.TemplateResponse(request=request, name="feed.html", context=context)
|
||||
|
||||
|
||||
def create_html_for_feed(entries: Iterable[Entry]) -> str:
|
||||
def create_html_for_feed(entries: Iterable[Entry], current_feed_url: str = "") -> str: # noqa: PLR0914
|
||||
"""Create HTML for the search results.
|
||||
|
||||
Args:
|
||||
entries: The entries to create HTML for.
|
||||
current_feed_url: The feed URL currently being viewed in /feed.
|
||||
|
||||
Returns:
|
||||
str: The HTML for the search results.
|
||||
|
|
@ -940,6 +982,12 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
|
|||
if entry_is_whitelisted(entry):
|
||||
whitelisted = "<span class='badge bg-success'>Whitelisted</span>"
|
||||
|
||||
source_feed_url: str = getattr(entry, "original_feed_url", None) or entry.feed.url
|
||||
|
||||
from_another_feed: str = ""
|
||||
if current_feed_url and source_feed_url != current_feed_url:
|
||||
from_another_feed = f"<span class='badge bg-warning text-dark'>From another feed: {source_feed_url}</span>"
|
||||
|
||||
entry_id: str = urllib.parse.quote(entry.id)
|
||||
to_discord_html: str = f"<a class='text-muted' href='/post_entry?entry_id={entry_id}'>Send to Discord</a>"
|
||||
|
||||
|
|
@ -966,14 +1014,14 @@ def create_html_for_feed(entries: Iterable[Entry]) -> str:
|
|||
image_html: str = f"<img src='{first_image}' class='img-fluid'>" if first_image else ""
|
||||
|
||||
html += f"""<div class="p-2 mb-2 border border-dark">
|
||||
{blacklisted}{whitelisted}<a class="text-muted text-decoration-none" href="{entry.link}"><h2>{entry.title}</h2></a>
|
||||
{blacklisted}{whitelisted}{from_another_feed}<a class="text-muted text-decoration-none" href="{entry.link}"><h2>{entry.title}</h2></a>
|
||||
{f"By {entry.author} @" if entry.author else ""}{published} - {to_discord_html}
|
||||
|
||||
{text}
|
||||
{video_embed_html}
|
||||
{image_html}
|
||||
</div>
|
||||
"""
|
||||
""" # noqa: E501
|
||||
return html.strip()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -16,4 +16,4 @@ body {
|
|||
|
||||
.interval-input {
|
||||
max-width: 120px;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,6 +76,22 @@
|
|||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<!-- Feed URL Configuration -->
|
||||
<div class="mt-4 border-top border-secondary pt-3">
|
||||
<h5 class="mb-3">Feed URL</h5>
|
||||
<p class="text-muted mb-2">Change the URL for this feed. This can be useful if a feed has moved.</p>
|
||||
<form action="/change_feed_url" method="post" class="mb-2">
|
||||
<input type="hidden" name="old_feed_url" value="{{ feed.url }}" />
|
||||
<div class="input-group input-group-sm mb-2">
|
||||
<input type="url"
|
||||
class="form-control form-control-sm"
|
||||
name="new_feed_url"
|
||||
value="{{ feed.url }}"
|
||||
required />
|
||||
<button class="btn btn-warning" type="submit">Update URL</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<!-- Feed Metadata -->
|
||||
<div class="mt-4 border-top border-secondary pt-3">
|
||||
<h5 class="mb-3">Feed Information</h5>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue