Skip to content

Commit faa9be2

Browse files
committed
update func names and docstrings
1 parent adc4178 commit faa9be2

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

Diff for: chrono24/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
# Import chrono24 and use chrono24.query to interface Chrono24
77
from chrono24.api import Chrono24 as query
88

9-
__version__ = "0.2.11"
9+
__version__ = "0.2.12"

Diff for: chrono24/api.py

+23-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
RE_PATTERN_COMMA_SEPARATED_NUM = r"\b\d{1,3}(?:,\d{3})*\b"
1919

2020

21-
def get_text_html_tag(html_tag):
21+
def get_html_tag_as_text(html_tag):
2222
"""Get the text from an HTML tag or return NULL_VALUE if the tag is None or has no text.
2323
2424
Args:
@@ -31,7 +31,7 @@ def get_text_html_tag(html_tag):
3131
return html_tag_text or NULL_VALUE
3232

3333

34-
def get_text_html_tag_attr(html_tag, attr):
34+
def get_html_tag_attribute_as_text(html_tag, attr):
3535
"""Get the attribute text from an HTML tag or return NULL_VALUE if the tag is None or has no text.
3636
3737
Args:
@@ -52,8 +52,7 @@ class Chrono24:
5252
page_size = 120
5353

5454
def __init__(self, query):
55-
"""
56-
Initialize a chrono24 object with a query.
55+
"""Initialize a chrono24 object with a query.
5756
5857
Args:
5958
query (str): The search query to be performed.
@@ -212,8 +211,7 @@ class Listings:
212211
"""A class representing a collection of listings extracted from HTML content."""
213212

214213
def __init__(self, html):
215-
"""
216-
Initialize the Listings object with HTML content.
214+
"""Initialize the Listings object with HTML content.
217215
218216
Args:
219217
html (bs4.element.ResultSet): The HTML content containing listings.
@@ -282,21 +280,21 @@ def json(self):
282280
dict: A dictionary containing the extracted listing information.
283281
"""
284282
return {
285-
"id": get_text_html_tag_attr(self.html, "data-article-id"),
286-
"url": BASE_URL + get_text_html_tag_attr(self.html, "href"),
287-
"manufacturer": get_text_html_tag_attr(self.html, "data-manufacturer"),
288-
"certification_status": get_text_html_tag_attr(
283+
"id": get_html_tag_attribute_as_text(self.html, "data-article-id"),
284+
"url": BASE_URL + get_html_tag_attribute_as_text(self.html, "href"),
285+
"manufacturer": get_html_tag_attribute_as_text(self.html, "data-manufacturer"),
286+
"certification_status": get_html_tag_attribute_as_text(
289287
self.html, "data-watch-certification-status"
290288
),
291-
"title": get_text_html_tag(
289+
"title": get_html_tag_as_text(
292290
self.html.find(
293291
"div", class_=lambda x: x and "text-bold" in x and "text-ellipsis" in x
294292
)
295293
),
296-
"description": get_text_html_tag(
294+
"description": get_html_tag_as_text(
297295
self.html.find("div", class_=lambda x: x and "m-b-2" in x and "text-ellipsis" in x)
298296
),
299-
"price": get_text_html_tag(
297+
"price": get_html_tag_as_text(
300298
(lambda x: x.parent if x else x)(self.html.find("span", {"class": "currency"}))
301299
),
302300
"shipping_price": self._shipping_price,
@@ -314,7 +312,7 @@ def _shipping_price(self):
314312
str: The shipping price extracted from the content, formatted as a string ('$X' format).
315313
"""
316314
# Extract comma-separated shipping price
317-
shipping_price_text = get_text_html_tag(
315+
shipping_price_text = get_html_tag_as_text(
318316
self.html.find("div", {"class": "text-muted text-sm"})
319317
)
320318
match = re.search(RE_PATTERN_COMMA_SEPARATED_NUM, shipping_price_text)
@@ -328,7 +326,7 @@ def _location_and_merchant_name(self):
328326
Returns:
329327
tuple: A tuple containing the location and merchant name extracted from the content.
330328
"""
331-
location = get_text_html_tag_attr(
329+
location = get_html_tag_attribute_as_text(
332330
self.html.find("button", {"class": "js-tooltip"}), "data-content"
333331
)
334332
# Possible merchant names found in listings page
@@ -352,7 +350,7 @@ def _image_urls(self):
352350
image_divs = self.html.find_all("div", {"class": "js-carousel-cell"})
353351
# Modify URLs to select for extra large images
354352
return [
355-
get_text_html_tag_attr(image_div.find("img"), "data-lazy-sweet-spot-master-src")
353+
get_html_tag_attribute_as_text(image_div.find("img"), "data-lazy-sweet-spot-master-src")
356354
.lower()
357355
.replace("square_size_", "ExtraLarge")
358356
for image_div in image_divs
@@ -366,7 +364,7 @@ def _badge(self):
366364
str: The badge information related to the listing.
367365
"""
368366
badge = self.html.find("span", {"class": "article-item-article-badge"})
369-
return get_text_html_tag(badge)
367+
return get_html_tag_as_text(badge)
370368

371369

372370
class DetailedListing:
@@ -402,16 +400,16 @@ def _product_details(self):
402400
details = [section.find_all("td") for section in detail_section.find_all("tr")]
403401
for idx, detail in enumerate(details):
404402
# Get detail key and set default detail value
405-
detail_key = get_text_html_tag(detail[0]).lower().replace(" ", "_")
403+
detail_key = get_html_tag_as_text(detail[0]).lower().replace(" ", "_")
406404
detail_description = NULL_VALUE
407405
try:
408-
detail_description = get_text_html_tag(detail[1])
406+
detail_description = get_html_tag_as_text(detail[1])
409407
product_details[detail_key] = self._tidy_product_detail(detail_description)
410408
except IndexError:
411409
# Check if `detail` is a header above description column or description body
412410
# We want to map description headers to their bodies
413411
if idx + 1 != len(details) and len(details[idx + 1]) == 1:
414-
detail_description = get_text_html_tag(details[idx + 1][0])
412+
detail_description = get_html_tag_as_text(details[idx + 1][0])
415413
product_details[detail_key] = self._tidy_product_detail(detail_description)
416414

417415
return product_details
@@ -476,7 +474,7 @@ def _anticipated_delivery(self):
476474
str: The anticipated delivery details for the listing.
477475
"""
478476
anticipated_delivery = self.html.find("span", {"class": "js-shipping-time"})
479-
return get_text_html_tag(anticipated_delivery).replace("Anticipated delivery: ", "")
477+
return get_html_tag_as_text(anticipated_delivery).replace("Anticipated delivery: ", "")
480478

481479
@property
482480
def _merchant_name(self):
@@ -486,7 +484,7 @@ def _merchant_name(self):
486484
str: The name of the merchant associated with the listing.
487485
"""
488486
merchant_name = self.html.find("button", {"class": "js-link-merchant-name"})
489-
return get_text_html_tag(merchant_name)
487+
return get_html_tag_as_text(merchant_name)
490488

491489
@property
492490
def _merchant_rating(self):
@@ -496,7 +494,7 @@ def _merchant_rating(self):
496494
str: The rating of the merchant associated with the listing.
497495
"""
498496
rating = self.html.find("span", {"class": "rating"})
499-
return get_text_html_tag(rating)
497+
return get_html_tag_as_text(rating)
500498

501499
@property
502500
def _merchant_reviews(self):
@@ -506,7 +504,7 @@ def _merchant_reviews(self):
506504
str: The number of reviews for the merchant associated with the listing.
507505
"""
508506
num_reviews = self.html.find("button", {"class": "js-link-merchant-reviews"})
509-
return get_text_html_tag(num_reviews)
507+
return get_html_tag_as_text(num_reviews)
510508

511509
@property
512510
def _merchant_badges(self):
@@ -520,6 +518,6 @@ def _merchant_badges(self):
520518
badge_html = BeautifulSoup(badge.get("data-content"), "html.parser")
521519
badge_text = badge_html.find("span", {"class": ""})
522520
if badge_text:
523-
badges.append(get_text_html_tag(badge_text))
521+
badges.append(get_html_tag_as_text(badge_text))
524522

525523
return badges

0 commit comments

Comments
 (0)