Skip to content

Commit

Permalink
Update recipes to remove hard-coding of date[time] format [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Jun 18, 2023
1 parent 936b817 commit 2580475
Show file tree
Hide file tree
Showing 35 changed files with 96 additions and 84 deletions.
4 changes: 2 additions & 2 deletions recipes/aeon.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import BasicNewsrackRecipe, format_title, get_date_format

from calibre.web.feeds import Feed
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes
Expand Down Expand Up @@ -73,7 +73,7 @@ def preprocess_raw_html_(self, raw_html, url):
header = soup.find("h1") or soup.find("h2")
if header:
date_ele = soup.new_tag("div", attrs={"class": "custom-date-published"})
date_ele.append(f"{published_date:%-d %B, %Y}")
date_ele.append(f"{published_date:{get_date_format()}}")
header.insert_after(date_ele)
return str(soup)

Expand Down
6 changes: 3 additions & 3 deletions recipes/atlantic-magazine.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe
from recipes_shared import BasicNewsrackRecipe, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -68,15 +68,15 @@ def json_to_html(data):
).replace(tzinfo=timezone.utc)
pub_ele = new_soup.new_tag("span", attrs={"class": "published-dt"})
pub_ele["data-published"] = f"{published_date:%Y-%m-%dT%H:%M:%SZ}"
pub_ele.append(f"{published_date:%-I:%M%p, %-d %B, %Y}")
pub_ele.append(f"{published_date:{get_datetime_format()}}")
meta.append(pub_ele)
if article.get("dateModified"):
modified_date = datetime.strptime(
article["dateModified"], "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=timezone.utc)
upd_ele = new_soup.new_tag("span", attrs={"class": "modified-dt"})
upd_ele["data-modified"] = f"{modified_date:%Y-%m-%dT%H:%M:%SZ}"
upd_ele.append(f"Updated {modified_date:%-I.%M%p, %-d %B, %Y}")
upd_ele.append(f"Updated {modified_date:{get_datetime_format()}}")
meta.append(upd_ele)

new_soup.main.append(meta)
Expand Down
6 changes: 3 additions & 3 deletions recipes/atlantic.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import BasicNewsrackRecipe, format_title, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -68,15 +68,15 @@ def json_to_html(data):
).replace(tzinfo=timezone.utc)
pub_ele = new_soup.new_tag("span", attrs={"class": "published-dt"})
pub_ele["data-published"] = f"{published_date:%Y-%m-%dT%H:%M:%SZ}"
pub_ele.append(f"{published_date:%-I:%M%p, %-d %B, %Y}")
pub_ele.append(f"{published_date:{get_datetime_format()}}")
meta.append(pub_ele)
if article.get("dateModified"):
modified_date = datetime.strptime(
article["dateModified"], "%Y-%m-%dT%H:%M:%SZ"
).replace(tzinfo=timezone.utc)
upd_ele = new_soup.new_tag("span", attrs={"class": "modified-dt"})
upd_ele["data-modified"] = f"{modified_date:%Y-%m-%dT%H:%M:%SZ}"
upd_ele.append(f"Updated {modified_date:%-I.%M%p, %-d %B, %Y}")
upd_ele.append(f"Updated {modified_date:{get_datetime_format()}}")
meta.append(upd_ele)

new_soup.main.append(meta)
Expand Down
6 changes: 3 additions & 3 deletions recipes/bloomberg-businessweek.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe
from recipes_shared import BasicNewsrackRecipe, get_datetime_format

from calibre import browser
from calibre.ebooks.BeautifulSoup import BeautifulSoup
Expand Down Expand Up @@ -155,10 +155,10 @@ def preprocess_raw_html(self, raw_html, url):
if (not self.pub_date) or date_published > self.pub_date:
self.pub_date = date_published
published_at = soup.find(class_="published-dt")
published_at.append(f"{date_published:%-I:%M%p, %-d %b, %Y}")
published_at.append(f"{date_published:{get_datetime_format()}}")
if article.get("updatedAt"):
date_updated = parse_date(article["updatedAt"], assume_utc=True)
published_at.append(f", Updated {date_updated:%-I:%M%p, %-d %b, %Y}")
published_at.append(f", Updated {date_updated:{get_datetime_format()}}")
if (not self.pub_date) or date_updated > self.pub_date:
self.pub_date = date_updated

Expand Down
13 changes: 9 additions & 4 deletions recipes/bloomberg-news.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import (
BasicNewsrackRecipe,
format_title,
get_datetime_format,
get_date_format,
)

from calibre import browser
from calibre.ebooks.BeautifulSoup import BeautifulSoup
Expand Down Expand Up @@ -163,10 +168,10 @@ def preprocess_raw_html(self, raw_html, url):
self.pub_date = date_published
self.title = format_title(_name, date_published)
published_at = soup.find(class_="published-dt")
published_at.append(f"{date_published:%-I:%M%p, %-d %b, %Y}")
published_at.append(f"{date_published:{get_datetime_format()}}")
if article.get("updatedAt"):
date_updated = parse_date(article["updatedAt"], assume_utc=True)
published_at.append(f", Updated {date_updated:%-I:%M%p, %-d %b, %Y}")
published_at.append(f", Updated {date_updated:{get_datetime_format()}}")
if (not self.pub_date) or date_updated > self.pub_date:
self.pub_date = date_updated
self.title = format_title(_name, date_updated)
Expand Down Expand Up @@ -260,7 +265,7 @@ def parse_index(self):
{
"title": url_node.find("news:title").get_text(),
"url": url_node.find("loc").get_text(),
"date": f"{article_date:%-d %B, %Y}",
"date": f"{article_date:{get_date_format()}}",
"pub_date": article_date,
}
)
Expand Down
1 change: 0 additions & 1 deletion recipes/economist.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ def populate_article_metadata(self, article, soup, first):
).replace(tzinfo=timezone.utc)
if not self.pub_date or date_published > self.pub_date:
self.pub_date = date_published
# self.title = f"{_name}: {date_published:%-d %b, %Y}"

def parse_index(self):
if edition_date:
Expand Down
8 changes: 4 additions & 4 deletions recipes/fivethirtyeight.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe, format_title
from recipes_shared import WordPressNewsrackRecipe, format_title, get_date_format

from calibre.ptempfile import PersistentTemporaryDirectory, PersistentTemporaryFile
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -93,9 +93,9 @@ def parse_index(self):
latest_post_date = post_date
self.title = format_title(_name, post_date)

section_name = f"{post_date:%-d %B, %Y}"
section_name = f"{post_date:{get_date_format()}}"
if len(self.get_feeds()) > 1:
section_name = f"{feed_name}: {post_date:%-d %B, %Y}"
section_name = f"{feed_name}: {post_date:{get_date_format()}}"
if section_name not in articles:
articles[section_name] = []

Expand All @@ -121,7 +121,7 @@ def parse_index(self):
{
"title": unescape(p["title"]["rendered"]) or "Untitled",
"url": "file://" + f.name,
"date": f"{post_date:%-d %B, %Y}",
"date": f"{post_date:{get_date_format()}}",
"description": unescape(" / ".join(verticals)),
}
)
Expand Down
4 changes: 2 additions & 2 deletions recipes/ft-paper.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicCookielessNewsrackRecipe, format_title
from recipes_shared import BasicCookielessNewsrackRecipe, format_title, get_date_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe, classes
Expand Down Expand Up @@ -161,7 +161,7 @@ def preprocess_raw_html(self, raw_html, url):
{"" if not article.get("description") else '<div class="sub-headline">' + article.get("description", "") + '</div>'}
<div class="article-meta">
<span class="author">{author}</span>
<span class="published-dt">{date_published:%-d %B, %Y}</span>
<span class="published-dt">{date_published:{get_date_format()}}</span>
</div>
{article_body}
</article>
Expand Down
4 changes: 2 additions & 2 deletions recipes/ft.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicCookielessNewsrackRecipe, format_title
from recipes_shared import BasicCookielessNewsrackRecipe, format_title, get_date_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -128,7 +128,7 @@ def preprocess_raw_html(self, raw_html, url):
{"" if not article.get("description") else '<div class="sub-headline">' + article.get("description", "") + '</div>'}
<div class="article-meta">
<span class="author">{author}</span>
<span class="published-dt">{date_published:%-d %B, %Y}</span>
<span class="published-dt">{date_published:{get_date_format()}}</span>
</div>
{article_body}
</article>
Expand Down
4 changes: 2 additions & 2 deletions recipes/fulcrum-sg.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe
from recipes_shared import WordPressNewsrackRecipe, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -129,7 +129,7 @@ def preprocess_raw_html(self, raw_html, url):
<div class="article-meta">
{f'<span class="author">{", ".join(post_authors)}</span>' if post_authors else ''}
<span class="published-dt">
{date_published_loc:%-I:%M%p, %-d %b, %Y}
{date_published_loc:{get_datetime_format()}}
</span>
</div>
</article>
Expand Down
11 changes: 8 additions & 3 deletions recipes/harvard-intl-review.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import (
BasicNewsrackRecipe,
format_title,
get_date_format,
get_datetime_format,
)

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds import Feed
Expand Down Expand Up @@ -69,7 +74,7 @@ def parse_feeds(self):
parsed_feed = parsed_feeds[0]
for i, a in enumerate(articles, start=1):
date_published = a.utctime.replace(tzinfo=timezone.utc)
article_index = f"{date_published:%-d %B, %Y}"
article_index = f"{date_published:{get_date_format()}}"
# add author and pub date
soup = BeautifulSoup(a.content)
header = None
Expand All @@ -81,7 +86,7 @@ def parse_feeds(self):
author_ele.append(a.author)
meta.append(author_ele)
pub_ele = soup.new_tag("span", attrs={"class": "published-dt"})
pub_ele.append(f"{date_published:%-I:%M%p, %-d %b, %Y}")
pub_ele.append(f"{date_published:{get_datetime_format()}}")
meta.append(pub_ele)
if header:
header.insert_after(meta)
Expand Down
4 changes: 2 additions & 2 deletions recipes/hbr.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicCookielessNewsrackRecipe
from recipes_shared import BasicCookielessNewsrackRecipe, get_date_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe, classes
Expand Down Expand Up @@ -84,7 +84,7 @@ def preprocess_raw_html(self, raw_html, _):
pub_date_ele["data-mod-date"] = mod_datetime["content"]
post_date_ele = soup.new_tag("span")
post_date_ele["class"] = "article-pub-date"
post_date_ele.append(f"{post_date:%-d %B, %Y}")
post_date_ele.append(f"{post_date:{get_date_format()}}")
# pub_date_ele.append(post_date_ele) # set this below together with the byline logic

# break author byline out of list
Expand Down
4 changes: 2 additions & 2 deletions recipes/japan-times.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import BasicNewsrackRecipe, format_title, get_datetime_format

from calibre.web.feeds.news import BasicNewsRecipe

Expand Down Expand Up @@ -138,7 +138,7 @@ def preprocess_html(self, soup):
if pub_date:
pub_date = datetime.fromisoformat(pub_date["content"])
pub_date_ele = soup.new_tag("span", attrs={"class": "published-date"})
pub_date_ele.append(f"{pub_date:%-I:%M%p, %-d %B, %Y}")
pub_date_ele.append(f"{pub_date:{get_datetime_format()}}")
meta.append(pub_date_ele)
if (not self.pub_date) or pub_date > self.pub_date:
self.pub_date = pub_date
Expand Down
4 changes: 2 additions & 2 deletions recipes/lithub.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe
from recipes_shared import WordPressNewsrackRecipe, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -112,7 +112,7 @@ def preprocess_raw_html(self, raw_html, url):
<div class="article-meta">
{f'<span class="author">{", ".join(post_authors)}</span>' if post_authors else ''}
<span class="published-dt">
{date_published_loc:%-I:%M%p, %-d %b, %Y}
{date_published_loc:{get_datetime_format()}}
</span>
</div>
</article>
Expand Down
4 changes: 2 additions & 2 deletions recipes/longreads-features.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe
from recipes_shared import WordPressNewsrackRecipe, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes
Expand Down Expand Up @@ -106,7 +106,7 @@ def preprocess_raw_html(self, raw_html, url):
<div class="article-meta">
{f'<span class="author">{", ".join(post_authors)}</span>' if post_authors else ''}
<span class="published-dt">
{date_published_loc:%-I:%M%p, %-d %b, %Y}
{date_published_loc:{get_datetime_format()}}
</span>
</div>
</article>
Expand Down
4 changes: 2 additions & 2 deletions recipes/mit-press-reader.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from recipes_shared import BasicNewsrackRecipe, format_title, get_datetime_format

from calibre.utils.date import parse_date
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -71,7 +71,7 @@ def postprocess_html(self, soup, _):
athor_ele = soup.find(class_="ma-top-shares-left")
if athor_ele:
post_date_ele = soup.new_tag("div")
post_date_ele.append(f"{post_date:%-H:%M%p, %-d %B, %Y}")
post_date_ele.append(f"{post_date:{get_datetime_format()}}")
athor_ele.append(post_date_ele)
time_parent_ele = soup.find("div", class_="author-post-cont")
if time_parent_ele:
Expand Down
4 changes: 2 additions & 2 deletions recipes/mit-tech-review-magazine.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe
from recipes_shared import WordPressNewsrackRecipe, get_date_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -122,7 +122,7 @@ def preprocess_raw_html(self, raw_html, url):
<div class="article-meta">
{f'<span class="author">{", ".join(post_authors)}</span>' if post_authors else ''}
<span class="published-dt">
{date_published_loc:%-d %b, %Y}
{date_published_loc:{get_date_format()}}
</span>
</div>
{self._extract_featured_media(post)}
Expand Down
4 changes: 2 additions & 2 deletions recipes/mit-tech-review.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import WordPressNewsrackRecipe
from recipes_shared import WordPressNewsrackRecipe, get_datetime_format

from calibre.ebooks.BeautifulSoup import BeautifulSoup
from calibre.web.feeds.news import BasicNewsRecipe
Expand Down Expand Up @@ -107,7 +107,7 @@ def preprocess_raw_html(self, raw_html, url):
<div class="article-meta">
{f'<span class="author">{", ".join(post_authors)}</span>' if post_authors else ''}
<span class="published-dt">
{date_published_loc:%-I:%M%p, %-d %b, %Y}
{date_published_loc:{get_datetime_format()}}
</span>
</div>
{self._extract_featured_media(post)}
Expand Down
1 change: 0 additions & 1 deletion recipes/nature.recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def populate_article_metadata(self, article, soup, _):
article.utctime = pub_date_utc
if not self.pub_date or pub_date_utc > self.pub_date:
self.pub_date = pub_date_utc
# self.title = f"{_name}: {pub_date_utc:%-d %b, %Y}"

def preprocess_html(self, soup):
if soup.find(name="h2", id="access-options"):
Expand Down
Loading

0 comments on commit 2580475

Please sign in to comment.