Skip to content

Commit

Permalink
✨ Add "Want to read" (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 22, 2022
1 parent 6f206c5 commit 5931bc1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/features/new-issue.ts
Expand Up @@ -40,9 +40,9 @@ export const onNewIssue = async (
try {
debug(`Searching for "${issue.data.title}"`);
const details = await search(issue.data.title);
body += `Congrats on starting **${details.title}** by ${details.authors.join(
body += `Congrats on adding **${details.title}** by ${details.authors.join(
", "
)}, I hope you enjoy it! It has an average of ${
)} to your bookshelf, I hope you enjoy it! It has an average of ${
details.averageRating || "unknown"
}/5 stars and ${(details.ratingsCount || 0).toLocaleString()} ratings on [Google Books](${
details.googleBooks.info
Expand Down
32 changes: 30 additions & 2 deletions src/features/update-summary.ts
Expand Up @@ -29,7 +29,7 @@ export const updateSummary = async (
});
debug(`Got ${issues.data.length} issues`);
let api: (BookResult & {
state: "reading" | "completed";
state: "reading" | "completed" | "want-to-read";
issueNumber: number;
startedAt: string;
progressPercent: number;
Expand All @@ -53,6 +53,10 @@ export const updateSummary = async (
} catch (error) {
console.log("JSON parsing error", error);
}
const isWantToRead = issue.labels.find((label) =>
typeof label === "string" ? label === "want to read" : label.name === "want to read"
);
if (isWantToRead) debug(`Book is in category "want to read"`);
if (json) {
debug(`Found JSON data for ${(json as BookResult).title}`);
const currentPercentage = issue.title.match(/\(\d+\%\)/g);
Expand All @@ -71,7 +75,7 @@ export const updateSummary = async (
currentPercentage && currentPercentage.length && !isNaN(parseInt(currentPercentage[0]))
? parseInt(currentPercentage[0])
: 0,
state: issue.state === "open" ? "reading" : "completed",
state: issue.state === "open" ? (isWantToRead ? "want-to-read" : "reading") : "completed",
startedAt: new Date(openedAt).toISOString(),
completedAt: issue.state === "closed" ? new Date(closedAt).toISOString() : undefined,
timeToComplete:
Expand All @@ -93,6 +97,7 @@ export const updateSummary = async (
debug(`api has length ${api.length}`);
let mdContent = "";
const apiCompleted = api.filter((i) => i.state === "completed");
const apiWantToRead = api.filter((i) => i.state === "want-to-read");
const apiReading = api.filter((i) => i.state === "reading");
if (apiReading.length)
mdContent += `### ⌛ Currently reading (${apiReading.length})\n\n${apiReading
Expand Down Expand Up @@ -131,6 +136,29 @@ export const updateSummary = async (
})}")`
)
.join("\n")}`;
if (apiWantToRead.length)
mdContent += `${apiCompleted.length ? "\n\n" : ""}### ⏭️ Want to Read (${
apiWantToRead.length
})\n\n${apiWantToRead
.map(
(i) =>
`[![Book cover of ${i.title.replace(
/\"/g,
""
)}](https://images.weserv.nl/?url=${encodeURIComponent(
i.image
)}&w=128&h=196&fit=contain)](https://github.com/${owner}/${repo}/issues/${
i.issueNumber
} "${i.title.replace(/\"/g, "")} by ${i.authors
.join(", ")
.replace(/\"/g, "")} completed in ${i.timeToCompleteFormatted} on ${new Date(
i.completedAt || ""
).toLocaleDateString("en-us", {
month: "long",
year: "numeric",
})}")`
)
.join("\n")}`;
debug(`Generated README.md content of length ${mdContent.length}`);
const content = await promises.readFile(join(".", "README.md"), "utf8");
debug(`Read README.md file of length ${content.length}`);
Expand Down

0 comments on commit 5931bc1

Please sign in to comment.