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):
|
def __init__(self, title: str, link: str, is_paid: bool):
|
||||||
self.title = title
|
self.title = title
|
||||||
self.link = link
|
self.link = link
|
||||||
res = re.search(regex_date, link)
|
|
||||||
self.text = ""
|
self.text = ""
|
||||||
h_m = self._get_time_and_text()
|
self.date = datetime.datetime.now()
|
||||||
self.date = datetime.datetime(int(res.group(1)), int(res.group(2)), int(res.group(3)), h_m[0], h_m[1])
|
self._get_time_and_text()
|
||||||
self.date = self.date.replace(tzinfo=ZoneInfo("Europe/Paris"))
|
|
||||||
self.date = self.date.astimezone(datetime.timezone.utc)
|
|
||||||
self.is_paid = is_paid
|
self.is_paid = is_paid
|
||||||
|
|
||||||
def _get_time_and_text(self):
|
def _get_time_and_text(self):
|
||||||
response = requests.get(self.link)
|
|
||||||
print(f" Retrieving {self.link} to get pub time...")
|
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:
|
if response.status_code != 200:
|
||||||
print(f" Failed to get it ({response.status_code}, defaulting to 3AM)")
|
return
|
||||||
return (3, 0) # Default to 3:00 AM
|
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
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
|
# Handle text
|
||||||
text = []
|
text = []
|
||||||
if soup.find("div", class_="chapo") is not None:
|
if soup.find("div", class_="chapo") is not None:
|
||||||
|
|
@ -40,12 +42,10 @@ class Article:
|
||||||
text.append(p.text)
|
text.append(p.text)
|
||||||
self.text = "\n".join(text)
|
self.text = "\n".join(text)
|
||||||
|
|
||||||
publish = soup.find("span", class_="publish").text
|
# Handle image if any
|
||||||
res = re.search(regex_time, publish)
|
img_url = soup.find("meta", property="og:image")
|
||||||
if not res:
|
if img_url is not None:
|
||||||
print(f" Failed to parse it, defaulting to 3AM)")
|
self.text = f"<img alt=\"Image illustrant l'article\" src=\"{img_url["content"]}\" />\n" + self.text
|
||||||
return (3, 0) # Default to 3:00 AM
|
|
||||||
return (int(res.group(1)), int(res.group(2)))
|
|
||||||
|
|
||||||
|
|
||||||
def full_title(self):
|
def full_title(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue