Skip to content

Commit

Permalink
Future Proof with Nextjs scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowstudent777 committed Sep 13, 2024
1 parent c667ee9 commit 4f623b3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@glowstudent/youversion",
"version": "1.3.1",
"version": "2.0.0",
"description": "A simple module to get the Verse of the Day and any verse you would like.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
41 changes: 28 additions & 13 deletions src/functions/verse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getVerse = async (book: string, chapter: string, verses: string, ve
}

let bookFinder = bookList.books.find((o: bookType) => o.book.toLowerCase() === book.toLowerCase()) || bookList.books.find((o: bookType) => o.aliases.includes(book.toUpperCase()));
if (!bookFinder) return {code: 400, message: `Could not find book '${book}' by name or alias.`}
if (!bookFinder) return { code: 400, message: `Could not find book '${book}' by name or alias.` }

let URL = `${baseURL}/${versionFinder.id}/${bookFinder.aliases[0]}.${chapter}.${verses}`;

Expand All @@ -29,18 +29,33 @@ export const getVerse = async (book: string, chapter: string, verses: string, ve
const unavailable = $("p:contains('No Available Verses')").text();
if (unavailable) return { code: 400, message: "Verse not found" };

const versesArray: Array<String> = [];
const wrapper = $(".text-19");

await wrapper.each((i, p) => {
let unformattedVerse = $(p).eq(0).text();
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
versesArray.push(formattedVerse)
})

return {
citation: `${bookFinder.book} ${chapter}:${verses}`,
passage: versesArray[0]
// Nextjs way :)
const nextWay = $("script#__NEXT_DATA__").eq(0);
if (nextWay) {
let json = JSON.parse(nextWay.html() || "");
const verse = json.props.pageProps.verses[0].content;
const reference = json.props.pageProps.verses[0].reference.human

return {
citation: `${reference}`,
passage: verse,
}
}
// Old way :(
else {
const versesArray: Array<String> = [];
const wrapper = $(".text-17");

await wrapper.each((i, p) => {
let unformattedVerse = $(p).eq(0).text();
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
versesArray.push(formattedVerse)
})

return {
citation: `${bookFinder.book} ${chapter}:${verses}`,
passage: versesArray[0]
}
}
} catch (err) {
console.error(err);
Expand Down
86 changes: 60 additions & 26 deletions src/functions/votd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,68 @@ export const getVotd = async () => {
const { data } = await axios.get(URL);
const $ = cheerio.load(data);

const versesArray: Array<String> = [];
const citationsArray: Array<String> = [];
const imageArray: Array<String> = [];

const verses = $("a.text-text-light.w-full.no-underline");
const citations = $("p.text-gray-25");
const images = $("a.block");

await citations.each((i, p) => {
let citation = $(p).eq(0).text();
citationsArray.push(citation)
})

await verses.each((i, p) => {
let unformattedVerse = $(p).eq(0).text();
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
versesArray.push(formattedVerse)
})

await images.each((i, p) => {
let image = `https://www.bible.com${$(p).find('img').attr()?.src}`
imageArray.push(image);
})

return {
citation: citationsArray[0],
passage: versesArray[0],
image: imageArray ?? []
// Nextjs way :)
const nextWay = $("script#__NEXT_DATA__").eq(0);
if (nextWay != null) {
let json = JSON.parse(nextWay.html() || "");
const verse = json.props.pageProps.verses[0].content.replace(/\n/g, ' ');
const reference = json.props.pageProps.verses[0].reference.human;
const version = json.props.pageProps.versionData.abbreviation;

const images = $("a.block");
await images.each((i, p) => {
let image = `https://www.bible.com${$(p).find('img').attr()?.src}`
imageArray.push(image);
})

return {
citation: `${reference}`,
passage: verse,
images: imageArray ?? [],
version: version
}
}
// Old way :(
else {
const versesArray: Array<String> = [];
const citationsArray: Array<String> = [];
let version;

const verses = $("a.text-text-light.w-full.no-underline");
const citations = $("p.text-gray-25");
const images = $("a.block");

await citations.each((i, p) => {
let citation = $(p).eq(0).text();

// cut the ending (ESV), (NIV), etc and store it in version
version = citation.slice(-4).replace(/[()]/g, '');

// cut the version from the citation
citation = citation.slice(0, -6);

citationsArray.push(citation)
})

await verses.each((i, p) => {
let unformattedVerse = $(p).eq(0).text();
let formattedVerse = unformattedVerse.replace(/\n/g, ' ');
versesArray.push(formattedVerse)
})

await images.each((i, p) => {
let image = `https://www.bible.com${$(p).find('img').attr()?.src}`
imageArray.push(image);
})

return {
citation: citationsArray[0],
passage: versesArray[0],
image: imageArray ?? [],
version: version
}
}
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 4f623b3

Please sign in to comment.