Cleaner way to get time and image
This commit is contained in:
parent
a41a86e298
commit
6352fb9b97
1 changed files with 14 additions and 14 deletions
28
gen_feed.py
28
gen_feed.py
|
|
@ -15,22 +15,24 @@ class Article:
|
|||
def __init__(self, title: str, link: str, is_paid: bool):
|
||||
self.title = title
|
||||
self.link = link
|
||||
res = re.search(regex_date, link)
|
||||
self.text = ""
|
||||
h_m = self._get_time_and_text()
|
||||
self.date = datetime.datetime(int(res.group(1)), int(res.group(2)), int(res.group(3)), h_m[0], h_m[1])
|
||||
self.date = self.date.replace(tzinfo=ZoneInfo("Europe/Paris"))
|
||||
self.date = self.date.astimezone(datetime.timezone.utc)
|
||||
self.date = datetime.datetime.now()
|
||||
self._get_time_and_text()
|
||||
self.is_paid = is_paid
|
||||
|
||||
def _get_time_and_text(self):
|
||||
response = requests.get(self.link)
|
||||
print(f" Retrieving {self.link} to get pub time...")
|
||||
response = requests.get(self.link)
|
||||
print(f" Response is <{response.status_code}>")
|
||||
if response.status_code != 200:
|
||||
print(f" Failed to get it ({response.status_code}, defaulting to 3AM)")
|
||||
return (3, 0) # Default to 3:00 AM
|
||||
return
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
|
||||
# Handle time
|
||||
publish_date_time = soup.find("meta", property="article:published_time")["content"]
|
||||
self.date = datetime.datetime.strptime(publish_date_time, '%Y-%m-%dT%H:%M:%SZ')
|
||||
self.date = self.date.replace(tzinfo=ZoneInfo("UTC"))
|
||||
|
||||
# Handle text
|
||||
text = []
|
||||
if soup.find("div", class_="chapo") is not None:
|
||||
|
|
@ -40,12 +42,10 @@ class Article:
|
|||
text.append(p.text)
|
||||
self.text = "\n".join(text)
|
||||
|
||||
publish = soup.find("span", class_="publish").text
|
||||
res = re.search(regex_time, publish)
|
||||
if not res:
|
||||
print(f" Failed to parse it, defaulting to 3AM)")
|
||||
return (3, 0) # Default to 3:00 AM
|
||||
return (int(res.group(1)), int(res.group(2)))
|
||||
# Handle image if any
|
||||
img_url = soup.find("meta", property="og:image")
|
||||
if img_url is not None:
|
||||
self.text = f"<img alt=\"Image illustrant l'article\" src=\"{img_url["content"]}\" />\n" + self.text
|
||||
|
||||
|
||||
def full_title(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue