Skip to content

Commit e2a6954

Browse files
committed
Make secondstreet service respect item count and add second image
1 parent 6cc3f03 commit e2a6954

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

postalservice/services/secondstreet.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def parse_response(response_text: str, **kwargs) -> list:
157157
158158
Args:
159159
response_text (str): The response text from the API.
160+
**kwargs: Additional parameters including item_count.
160161
161162
Returns:
162163
list: A list of dictionaries containing item information.
@@ -168,7 +169,11 @@ def parse_response(response_text: str, **kwargs) -> list:
168169
# Find all item cards in the search results
169170
item_cards = soup.select(".itemCardList.-wrap .itemCard_inner")
170171

171-
for card in item_cards:
172+
# Get the item count limit from kwargs, default to a reasonable number if not specified
173+
item_count = kwargs.get("item_count", 36)
174+
175+
# Limit the number of items to process
176+
for card in item_cards[:item_count]:
172177
try:
173178
item = {}
174179

@@ -233,7 +238,19 @@ def parse_response(response_text: str, **kwargs) -> list:
233238
img_src = f"https:{img_src}"
234239
elif img_src.startswith("/"):
235240
img_src = f"https://www.2ndstreet.jp{img_src}"
236-
item["img"] = [img_src]
241+
242+
# Remove "_tn" from the image URL
243+
img_src = img_src.replace("_tn", "")
244+
245+
# Create a list with the first image and a second image
246+
images = [img_src]
247+
248+
# Generate second image by changing "1.jpg" to "2.jpg"
249+
if img_src.endswith("1.jpg"):
250+
second_img = img_src.replace("1.jpg", "2.jpg")
251+
images.append(second_img)
252+
253+
item["img"] = images
237254
else:
238255
item["img"] = []
239256
else:
@@ -277,11 +294,12 @@ async def parse_response_async(response_text: str, **kwargs) -> list:
277294
278295
Args:
279296
response_text (str): The response text from the API.
297+
**kwargs: Additional parameters including item_count.
280298
281299
Returns:
282300
list: A list of dictionaries containing item information.
283301
"""
284-
return SecondStreetService.parse_response(response_text)
302+
return SecondStreetService.parse_response(response_text, **kwargs)
285303

286304
@staticmethod
287305
def get_base_details() -> dict:

0 commit comments

Comments
 (0)