Skip to content
Merged

Devel #141

Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ async def handle(self, c: Context):
return
elif (url := is_reddit_domain(msg)):
print("is reddit url")
video_b64 = download_reddit_video_tryall_b64(url)
try:
video_b64 = download_reddit_video_tryall_b64(url)
except BaseException as ex:
if is_shutdown_exception(ex):
raise
print(f"Reddit download failed without crashing bot: {ex}")
video_b64 = None
if (video_b64):
await c.reply( LOGMSG + "Reddit URL: " + url, base64_attachments=[video_b64])
elif msg == "#":
Expand Down
38 changes: 38 additions & 0 deletions tests/test_reddit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from utils.reddit_utils import (
download_reddit_video,
download_reddit_video_tryall_b64,
download_reddit_video_with_redvid,
is_reddit_domain,
normalize_reddit_url,
Expand Down Expand Up @@ -141,6 +142,43 @@ def test_redvid_fallback_returns_local_absolute_path(self, downloader_cls):

self.assertEqual(filename, "/repo/reddit.mp4")

@patch("utils.reddit_utils.Downloader")
def test_redvid_fallback_handles_redvid_base_exception(self, downloader_cls):
downloader = downloader_cls.return_value
downloader.download.side_effect = BaseException("Incorrect URL format")

with patch("utils.reddit_utils.os.remove"):
self.assertIsNone(
download_reddit_video_with_redvid(
"https://www.reddit.com/comments/6rrwyj/",
"reddit.mp4",
)
)

@patch("utils.reddit_utils.get_video_as_base64", return_value=None)
@patch("utils.reddit_utils.download_reddit_video", side_effect=Exception("yt-dlp failed"))
def test_tryall_b64_handles_downloader_exception(self, download_mock, scrape_mock):
self.assertIsNone(download_reddit_video_tryall_b64("https://redd.it/6rrwyj"))
self.assertEqual(
[call.args[0] for call in download_mock.call_args_list],
["https://www.reddit.com/comments/6rrwyj/", "https://redd.it/6rrwyj"],
)
self.assertEqual(
[call.args[0] for call in scrape_mock.call_args_list],
["https://www.reddit.com/comments/6rrwyj/", "https://redd.it/6rrwyj"],
)

@patch("utils.reddit_utils.Downloader")
def test_redvid_fallback_reraises_shutdown_exceptions(self, downloader_cls):
downloader = downloader_cls.return_value
downloader.download.side_effect = KeyboardInterrupt()

with patch("utils.reddit_utils.os.remove"), self.assertRaises(KeyboardInterrupt):
download_reddit_video_with_redvid(
"https://www.reddit.com/comments/6rrwyj/",
"reddit.mp4",
)


if __name__ == "__main__":
unittest.main()
12 changes: 9 additions & 3 deletions utils/reddit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def normalize_reddit_url(url):
return urlunparse(("https", REDDIT_CANONICAL_HOST, parsed_url.path, "", "", ""))

return None

def is_reddit_domain(msg):
reddit_match = REDDIT_URL_RE.search(msg)
if reddit_match:
Expand All @@ -77,6 +77,10 @@ def is_reddit_domain(msg):
#print("is NOT reddit url")
return None

def is_shutdown_exception(ex):
return isinstance(ex, (KeyboardInterrupt, SystemExit, GeneratorExit))


def download_reddit_video_tryall_b64(url):
normalized_url = normalize_reddit_url(url)
urls_to_try = [normalized_url or url]
Expand Down Expand Up @@ -136,7 +140,9 @@ def download_reddit_video_with_redvid(url, fname="reddit.mp4"):
reddit.filename = fname
reddit.download()
return os.path.abspath(fname)
except Exception as ex:
except BaseException as ex:
if is_shutdown_exception(ex):
raise
print(ex)
return None

Loading