Skip to content

Commit

Permalink
fix(cron): update work chronicles cron (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineneff authored Mar 29, 2024
1 parent 3ecdbea commit b5b082c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/crons/WorkChronicles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,17 @@ export default new Cron({
});

/**
* Subset of the fields returned by the WordPress Posts API.
* Subset of the fields returned by the Substack API.
*/
interface WordPressPost {
interface SubstackPost {
id: number;
date_gmt: string;
link: string;
title: {
rendered: string;
};
content: {
rendered: string;
};
yoast_head_json?: {
og_image: Array<{ url: string }>;
};
canonical_url: string;
title: string;
cover_image: string;
}

/**
* Chronicle information extracted from the WordPress post.
* Chronicle information extracted from the Substack post.
*/
interface WorkChronicle {
/**
Expand All @@ -83,8 +75,8 @@ interface WorkChronicle {
* Otherwise, or if the post does not contain any image URL, returns null.
*/
export async function getLastChronicle(): Promise<WorkChronicle | null> {
const { body: posts } = await got<WordPressPost[]>(
'https://workchronicles.com/wp-json/wp/v2/posts?per_page=1',
const { body: posts } = await got<SubstackPost[]>(
'https://workchronicles.substack.com/api/v1/archive?sort=new&limit=1',
{ responseType: 'json', https: { rejectUnauthorized: false } },
);

Expand All @@ -94,20 +86,16 @@ export async function getLastChronicle(): Promise<WorkChronicle | null> {

const [chronicle] = posts;

let imageUrl = chronicle.yoast_head_json?.og_image.at(0)?.url;
if (!imageUrl) {
const chronicleImageUrlReg = /src="([^"]+)"/;
const urlMatch = chronicleImageUrlReg.exec(chronicle.content.rendered);
if (!urlMatch) {
return null;
}
imageUrl = urlMatch[1];
if (!chronicle.title.startsWith('(comic)')) {
return null;
}

const title = chronicle.title.replace('(comic)', '').trim();

return {
id: chronicle.id,
link: chronicle.link,
title: chronicle.title.rendered,
imageUrl,
link: chronicle.canonical_url,
title,
imageUrl: chronicle.cover_image,
};
}
File renamed without changes.

0 comments on commit b5b082c

Please sign in to comment.