Skip to content

Commit 01bff5d

Browse files
Formatting
1 parent c42f2b9 commit 01bff5d

File tree

2 files changed

+176
-150
lines changed

2 files changed

+176
-150
lines changed

functions/verse.ts

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,82 @@
1-
import axios from 'axios';
2-
import * as cheerio from 'cheerio';
1+
import axios from "axios";
2+
import * as cheerio from "cheerio";
33

4-
const versions = require('../db/versions.json');
5-
const bookList = require('../db/books.json');
4+
const versions = require("../db/versions.json");
5+
const bookList = require("../db/books.json");
66
const baseURL = "https://www.bible.com/bible";
77

88
type bookType = {
9-
book: String,
10-
aliases: Array<String>,
11-
chapters: Number
12-
}
13-
14-
export const getVerse = async (book: string, chapter: string, verses: string, version: string) => {
15-
let versionFinder: any = {
16-
version: Object.keys(versions)[Object.keys(versions).indexOf(version.toLocaleString().toLocaleUpperCase())] ??= "NIV",
17-
id: versions[version.toString().toLocaleUpperCase()] ??= 1,
9+
book: String;
10+
aliases: Array<String>;
11+
chapters: Number;
12+
};
13+
14+
export const getVerse = async (
15+
book: string,
16+
chapter: string,
17+
verses: string,
18+
version: string
19+
) => {
20+
let versionFinder: any = {
21+
version: (Object.keys(versions)[
22+
Object.keys(versions).indexOf(
23+
version.toLocaleString().toLocaleUpperCase()
24+
)
25+
] ??= "NIV"),
26+
id: (versions[version.toString().toLocaleUpperCase()] ??= 1),
27+
};
28+
29+
let bookFinder =
30+
bookList.books.find(
31+
(o: bookType) => o.book.toLowerCase() === book.toLowerCase()
32+
) ||
33+
bookList.books.find((o: bookType) =>
34+
o.aliases.includes(book.toUpperCase())
35+
);
36+
if (!bookFinder)
37+
return {
38+
code: 400,
39+
message: `Could not find book '${book}' by name or alias.`,
40+
};
41+
42+
let URL = `${baseURL}/${versionFinder.id}/${bookFinder.aliases[0]}.${chapter}.${verses}`;
43+
44+
try {
45+
const { data } = await axios.get(URL);
46+
const $ = cheerio.load(data);
47+
48+
const unavailable = $("p:contains('No Available Verses')").text();
49+
if (unavailable) return { code: 400, message: "Verse not found" };
50+
51+
// Nextjs way :)
52+
const nextWay = $("script#__NEXT_DATA__").eq(0);
53+
if (nextWay) {
54+
let json = JSON.parse(nextWay.html() || "");
55+
const verse = json.props.pageProps.verses[0].content;
56+
const reference = json.props.pageProps.verses[0].reference.human;
57+
58+
return {
59+
citation: `${reference}`,
60+
passage: verse,
61+
};
1862
}
63+
// Old way :(
64+
else {
65+
const versesArray: Array<String> = [];
66+
const wrapper = $(".text-17");
67+
68+
await wrapper.each((i, p) => {
69+
let unformattedVerse = $(p).eq(0).text();
70+
let formattedVerse = unformattedVerse.replace(/\n/g, " ");
71+
versesArray.push(formattedVerse);
72+
});
1973

20-
let bookFinder = bookList.books.find((o: bookType) => o.book.toLowerCase() === book.toLowerCase()) || bookList.books.find((o: bookType) => o.aliases.includes(book.toUpperCase()));
21-
if (!bookFinder) return { code: 400, message: `Could not find book '${book}' by name or alias.` }
22-
23-
let URL = `${baseURL}/${versionFinder.id}/${bookFinder.aliases[0]}.${chapter}.${verses}`;
24-
25-
try {
26-
const { data } = await axios.get(URL);
27-
const $ = cheerio.load(data);
28-
29-
const unavailable = $("p:contains('No Available Verses')").text();
30-
if (unavailable) return { code: 400, message: "Verse not found" };
31-
32-
// Nextjs way :)
33-
const nextWay = $("script#__NEXT_DATA__").eq(0);
34-
if (nextWay) {
35-
let json = JSON.parse(nextWay.html() || "");
36-
const verse = json.props.pageProps.verses[0].content;
37-
const reference = json.props.pageProps.verses[0].reference.human
38-
39-
return {
40-
citation: `${reference}`,
41-
passage: verse,
42-
}
43-
}
44-
// Old way :(
45-
else {
46-
const versesArray: Array<String> = [];
47-
const wrapper = $(".text-17");
48-
49-
await wrapper.each((i, p) => {
50-
let unformattedVerse = $(p).eq(0).text();
51-
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
52-
versesArray.push(formattedVerse)
53-
})
54-
55-
return {
56-
citation: `${bookFinder.book} ${chapter}:${verses}`,
57-
passage: versesArray[0]
58-
}
59-
}
60-
} catch (err) {
61-
console.error(err);
74+
return {
75+
citation: `${bookFinder.book} ${chapter}:${verses}`,
76+
passage: versesArray[0],
77+
};
6278
}
63-
}
79+
} catch (err) {
80+
console.error(err);
81+
}
82+
};

functions/votd.ts

Lines changed: 101 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,109 @@
1-
import axios, { AxiosError, AxiosResponse } from 'axios';
2-
import * as cheerio from 'cheerio';
3-
import { version } from 'os';
1+
import axios, { AxiosError, AxiosResponse } from "axios";
2+
import * as cheerio from "cheerio";
3+
import { version } from "os";
44

55
async function fetchData(language: string) {
6-
const URL = `https://www.bible.com/${language}/verse-of-the-day`;
7-
try {
8-
const response = await axios.get(URL);
9-
return response;
10-
} catch (error) {
11-
if (error instanceof AxiosError) {
12-
console.error(`Error for language '${language}': ${error.response?.status}`);
13-
} else if (error instanceof Error) {
14-
console.error(`Network error for language '${language}': ${error.message}`);
15-
}
16-
return null;
6+
const URL = `https://www.bible.com/${language}/verse-of-the-day`;
7+
try {
8+
const response = await axios.get(URL);
9+
return response;
10+
} catch (error) {
11+
if (error instanceof AxiosError) {
12+
console.error(
13+
`Error for language '${language}': ${error.response?.status}`
14+
);
15+
} else if (error instanceof Error) {
16+
console.error(
17+
`Network error for language '${language}': ${error.message}`
18+
);
1719
}
20+
return null;
21+
}
1822
}
1923

2024
export const getVotd = async (lang: string) => {
21-
const languageList = lang.split(',');
22-
let index = 0;
23-
let responseStatus = 0;
24-
let data: AxiosResponse | null = null;
25-
26-
while (index < languageList.length && responseStatus !== 200) {
27-
const language = languageList[index].trim();
28-
29-
data = await fetchData(language);
30-
if (data) {
31-
responseStatus = data.status;
32-
if (responseStatus === 200) {
33-
const $ = cheerio.load(data.data);
34-
35-
const imageArray: Array<String> = [];
36-
37-
// Nextjs way :)
38-
const nextWay = $("script#__NEXT_DATA__").eq(0);
39-
if (nextWay != null) {
40-
let json = JSON.parse(nextWay.html() || "");
41-
const verse = json.props.pageProps.verses[0].content.replace(/\n/g, ' ');
42-
const reference = json.props.pageProps.verses[0].reference.human;
43-
const version = json.props.pageProps.versionData.abbreviation;
44-
45-
const images = $("a.block");
46-
await images.each((i, p) => {
47-
let image = `https://www.bible.com${$(p).find('img').attr()?.src}`
48-
imageArray.push(image);
49-
})
50-
51-
return {
52-
citation: `${reference}`,
53-
passage: verse,
54-
images: imageArray ?? [],
55-
version: version
56-
}
57-
}
58-
// Old way :(
59-
else {
60-
const versesArray: Array<String> = [];
61-
const citationsArray: Array<String> = [];
62-
let version;
63-
64-
const verses = $("a.text-text-light.w-full.no-underline");
65-
const citations = $("p.text-gray-25");
66-
const images = $("a.block");
67-
68-
await citations.each((i, p) => {
69-
let citation = $(p).eq(0).text();
70-
71-
// cut the ending (ESV), (NIV), etc and store it in version
72-
version = citation.slice(-4).replace(/[()]/g, '');
73-
74-
// cut the version from the citation
75-
citation = citation.slice(0, -6);
76-
77-
citationsArray.push(citation)
78-
})
79-
80-
await verses.each((i, p) => {
81-
let unformattedVerse = $(p).eq(0).text();
82-
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
83-
versesArray.push(formattedVerse)
84-
})
85-
86-
await images.each((i, p) => {
87-
let image = `https://www.bible.com${$(p).find('img').attr()?.src}`
88-
imageArray.push(image);
89-
})
90-
91-
return {
92-
citation: citationsArray[0],
93-
passage: versesArray[0],
94-
image: imageArray ?? [],
95-
version: version
96-
}
97-
}
98-
}
25+
const languageList = lang.split(",");
26+
let index = 0;
27+
let responseStatus = 0;
28+
let data: AxiosResponse | null = null;
29+
30+
while (index < languageList.length && responseStatus !== 200) {
31+
const language = languageList[index].trim();
32+
33+
data = await fetchData(language);
34+
if (data) {
35+
responseStatus = data.status;
36+
if (responseStatus === 200) {
37+
const $ = cheerio.load(data.data);
38+
39+
const imageArray: Array<String> = [];
40+
41+
// Nextjs way :)
42+
const nextWay = $("script#__NEXT_DATA__").eq(0);
43+
if (nextWay != null) {
44+
let json = JSON.parse(nextWay.html() || "");
45+
const verse = json.props.pageProps.verses[0].content.replace(
46+
/\n/g,
47+
" "
48+
);
49+
const reference = json.props.pageProps.verses[0].reference.human;
50+
const version = json.props.pageProps.versionData.abbreviation;
51+
52+
const images = $("a.block");
53+
await images.each((i, p) => {
54+
let image = `https://www.bible.com${$(p).find("img").attr()?.src}`;
55+
imageArray.push(image);
56+
});
57+
58+
return {
59+
citation: `${reference}`,
60+
passage: verse,
61+
images: imageArray ?? [],
62+
version: version,
63+
};
64+
}
65+
// Old way :(
66+
else {
67+
const versesArray: Array<String> = [];
68+
const citationsArray: Array<String> = [];
69+
let version;
70+
71+
const verses = $("a.text-text-light.w-full.no-underline");
72+
const citations = $("p.text-gray-25");
73+
const images = $("a.block");
74+
75+
await citations.each((i, p) => {
76+
let citation = $(p).eq(0).text();
77+
78+
// cut the ending (ESV), (NIV), etc and store it in version
79+
version = citation.slice(-4).replace(/[()]/g, "");
80+
81+
// cut the version from the citation
82+
citation = citation.slice(0, -6);
83+
84+
citationsArray.push(citation);
85+
});
86+
87+
await verses.each((i, p) => {
88+
let unformattedVerse = $(p).eq(0).text();
89+
let formattedVerse = unformattedVerse.replace(/\n/g, " ");
90+
versesArray.push(formattedVerse);
91+
});
92+
93+
await images.each((i, p) => {
94+
let image = `https://www.bible.com${$(p).find("img").attr()?.src}`;
95+
imageArray.push(image);
96+
});
97+
98+
return {
99+
citation: citationsArray[0],
100+
passage: versesArray[0],
101+
image: imageArray ?? [],
102+
version: version,
103+
};
99104
}
100-
index++;
105+
}
101106
}
102-
}
107+
index++;
108+
}
109+
};

0 commit comments

Comments
 (0)