tangled
alpha
login
or
join now
modamo.xyz
/
bambu
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
Fetch number of pages from Google Books
modamo-gh
1 month ago
d23695ac
873c08ac
+29
-3
3 changed files
expand all
collapse all
unified
split
app
book
[bookID]
page.tsx
components
Header.tsx
lib
books
google.ts
+10
-2
app/book/[bookID]/page.tsx
···
1
1
import Header from "@/components/Header";
2
2
import { getSession } from "@/lib/auth/session";
3
3
+
import { fetchNumberOfPages } from "@/lib/books/google";
3
4
import { Agent } from "@atproto/api";
4
5
import Image from "next/image";
5
6
import { redirect } from "next/navigation";
···
15
16
16
17
const agent = new Agent(session);
17
18
18
18
-
let book;
19
19
+
let book, numberOfPages;
19
20
20
21
try {
21
22
const response = await agent.com.atproto.repo.listRecords({
···
27
28
book = response.data.records.find(
28
29
(record) => record.value.hiveId === `bk_${bookID}`
29
30
);
31
31
+
32
32
+
numberOfPages = await fetchNumberOfPages(
33
33
+
book.value.authors,
34
34
+
book.value.title
35
35
+
);
30
36
} catch (error) {
31
37
console.error("Error fetching book:", error);
32
38
}
···
52
58
<p className="text-emerald-900">{`Title: ${book?.value.title}`}</p>
53
59
<p className="text-emerald-900">Amazon Rating:</p>
54
60
<p className="text-emerald-900">Goodreads Rating:</p>
55
55
-
<p className="text-emerald-900">Number of Pages:</p>
61
61
+
<p className="text-emerald-900">
62
62
+
Number of Pages: {numberOfPages}
63
63
+
</p>
56
64
</div>
57
65
</div>
58
66
<div className="bg-emerald-900 col-span-1 p-4 rounded-2xl"></div>
+4
-1
components/Header.tsx
···
1
1
import { getSession } from "@/lib/auth/session";
2
2
import { Agent } from "@atproto/api";
3
3
import Image from "next/image";
4
4
+
import Link from "next/link";
4
5
import { redirect } from "next/navigation";
5
6
6
7
const Header = async () => {
···
24
25
25
26
return (
26
27
<header className="bg-emerald-900 flex items-center justify-between p-4 rounded-2xl row-span-1">
27
27
-
<h1 className="text-amber-100 text-4xl">bambü</h1>
28
28
+
<Link href="/">
29
29
+
<h1 className="text-amber-100 text-4xl">bambü</h1>
30
30
+
</Link>
28
31
<div className="h-16 relative w-16">
29
32
<Image
30
33
alt={`Profile picture for ${profile?.handle}`}
+15
lib/books/google.ts
···
1
1
+
export const fetchNumberOfPages = async (authors: string, title: string) => {
2
2
+
try {
3
3
+
const response = await fetch(
4
4
+
`https://www.googleapis.com/books/v1/volumes?q=intitle:${encodeURIComponent(
5
5
+
title
6
6
+
)}+inauthor:${encodeURIComponent(authors)}&maxResults=1`
7
7
+
);
8
8
+
const json = await response.json();
9
9
+
const volumeInfo = json.items[0].volumeInfo;
10
10
+
11
11
+
return Number(volumeInfo.pageCount);
12
12
+
} catch (error) {
13
13
+
console.error("Error fetching number of pages:", error);
14
14
+
}
15
15
+
};