Skip to content

Commit

Permalink
Limit max images per item for to image feed
Browse files Browse the repository at this point in the history
  • Loading branch information
KTachibanaM committed Dec 24, 2024
1 parent 24a819b commit 918fe72
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions rss_lambda/test_to_image_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ def test_to_image_feed(self):
'<p>also some random texts<br>but without images haha 2222</p> ',
'<p>also some random texts but with images hahahaha</p><img src="https://nitter.example.com/twitter_handle/pic/pic1.jpg" /><img src="https://nitter.example.com/twitter_handle/pic/pic2.jpg" />',
'<p>also some random texts but with images hahahaha</p><img src="https://nitter.example.com/twitter_handle/pic/pic3.jpg" />',
'<p>also some random texts but with a lot of images hahahaha</p><img src="https://nitter.example.com/twitter_handle/pic/pic4.jpg" /><img src="https://nitter.example.com/twitter_handle/pic/pic5.jpg" /><img src="https://nitter.example.com/twitter_handle/pic/pic6.jpg" /><img src="https://nitter.example.com/twitter_handle/pic/pic7.jpg" /><img src="https://nitter.example.com/twitter_handle/pic/pic8.jpg" />',
])
self.assertEqual(
to_image_feed(rss_text),
nitter_rss20_response([
'<img src="https://nitter.example.com/twitter_handle/pic/pic1.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic2.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic3.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic4.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic5.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic6.jpg"></img>',
'<img src="https://nitter.example.com/twitter_handle/pic/pic7.jpg"></img>',
])
)
4 changes: 3 additions & 1 deletion rss_lambda/to_image_feed/to_image_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ..utils.process_rss_text import ParsedRssText, process_rss_text
from ..utils.image_utils import extract_images_from_description

MAX_IMAGES_PER_ITEM = 4

def to_image_feed(rss_text: str) -> str:
def processor(parsed_rss_text: ParsedRssText):
root = parsed_rss_text.root
Expand All @@ -28,7 +30,7 @@ def handle_item(item):
images = extract_images_from_description(item, root.nsmap)
if not images:
return
for image in images:
for image in images[: MAX_IMAGES_PER_ITEM]:
handle_image(item, image)

for item in items:
Expand Down

0 comments on commit 918fe72

Please sign in to comment.