-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c42f2b9
commit 01bff5d
Showing
2 changed files
with
176 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,82 @@ | ||
import axios from 'axios'; | ||
import * as cheerio from 'cheerio'; | ||
import axios from "axios"; | ||
import * as cheerio from "cheerio"; | ||
|
||
const versions = require('../db/versions.json'); | ||
const bookList = require('../db/books.json'); | ||
const versions = require("../db/versions.json"); | ||
const bookList = require("../db/books.json"); | ||
const baseURL = "https://www.bible.com/bible"; | ||
|
||
type bookType = { | ||
book: String, | ||
aliases: Array<String>, | ||
chapters: Number | ||
} | ||
|
||
export const getVerse = async (book: string, chapter: string, verses: string, version: string) => { | ||
let versionFinder: any = { | ||
version: Object.keys(versions)[Object.keys(versions).indexOf(version.toLocaleString().toLocaleUpperCase())] ??= "NIV", | ||
id: versions[version.toString().toLocaleUpperCase()] ??= 1, | ||
book: String; | ||
aliases: Array<String>; | ||
chapters: Number; | ||
}; | ||
|
||
export const getVerse = async ( | ||
book: string, | ||
chapter: string, | ||
verses: string, | ||
version: string | ||
) => { | ||
let versionFinder: any = { | ||
version: (Object.keys(versions)[ | ||
Object.keys(versions).indexOf( | ||
version.toLocaleString().toLocaleUpperCase() | ||
) | ||
] ??= "NIV"), | ||
id: (versions[version.toString().toLocaleUpperCase()] ??= 1), | ||
}; | ||
|
||
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.`, | ||
}; | ||
|
||
let URL = `${baseURL}/${versionFinder.id}/${bookFinder.aliases[0]}.${chapter}.${verses}`; | ||
|
||
try { | ||
const { data } = await axios.get(URL); | ||
const $ = cheerio.load(data); | ||
|
||
const unavailable = $("p:contains('No Available Verses')").text(); | ||
if (unavailable) return { code: 400, message: "Verse not found" }; | ||
|
||
// 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); | ||
}); | ||
|
||
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.` } | ||
|
||
let URL = `${baseURL}/${versionFinder.id}/${bookFinder.aliases[0]}.${chapter}.${verses}`; | ||
|
||
try { | ||
const { data } = await axios.get(URL); | ||
const $ = cheerio.load(data); | ||
|
||
const unavailable = $("p:contains('No Available Verses')").text(); | ||
if (unavailable) return { code: 400, message: "Verse not found" }; | ||
|
||
// 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); | ||
return { | ||
citation: `${bookFinder.book} ${chapter}:${verses}`, | ||
passage: versesArray[0], | ||
}; | ||
} | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,109 @@ | ||
import axios, { AxiosError, AxiosResponse } from 'axios'; | ||
import * as cheerio from 'cheerio'; | ||
import { version } from 'os'; | ||
import axios, { AxiosError, AxiosResponse } from "axios"; | ||
import * as cheerio from "cheerio"; | ||
import { version } from "os"; | ||
|
||
async function fetchData(language: string) { | ||
const URL = `https://www.bible.com/${language}/verse-of-the-day`; | ||
try { | ||
const response = await axios.get(URL); | ||
return response; | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
console.error(`Error for language '${language}': ${error.response?.status}`); | ||
} else if (error instanceof Error) { | ||
console.error(`Network error for language '${language}': ${error.message}`); | ||
} | ||
return null; | ||
const URL = `https://www.bible.com/${language}/verse-of-the-day`; | ||
try { | ||
const response = await axios.get(URL); | ||
return response; | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
console.error( | ||
`Error for language '${language}': ${error.response?.status}` | ||
); | ||
} else if (error instanceof Error) { | ||
console.error( | ||
`Network error for language '${language}': ${error.message}` | ||
); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export const getVotd = async (lang: string) => { | ||
const languageList = lang.split(','); | ||
let index = 0; | ||
let responseStatus = 0; | ||
let data: AxiosResponse | null = null; | ||
|
||
while (index < languageList.length && responseStatus !== 200) { | ||
const language = languageList[index].trim(); | ||
|
||
data = await fetchData(language); | ||
if (data) { | ||
responseStatus = data.status; | ||
if (responseStatus === 200) { | ||
const $ = cheerio.load(data.data); | ||
|
||
const imageArray: Array<String> = []; | ||
|
||
// 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 | ||
} | ||
} | ||
} | ||
const languageList = lang.split(","); | ||
let index = 0; | ||
let responseStatus = 0; | ||
let data: AxiosResponse | null = null; | ||
|
||
while (index < languageList.length && responseStatus !== 200) { | ||
const language = languageList[index].trim(); | ||
|
||
data = await fetchData(language); | ||
if (data) { | ||
responseStatus = data.status; | ||
if (responseStatus === 200) { | ||
const $ = cheerio.load(data.data); | ||
|
||
const imageArray: Array<String> = []; | ||
|
||
// 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, | ||
}; | ||
} | ||
index++; | ||
} | ||
} | ||
} | ||
index++; | ||
} | ||
}; |