-
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.
Release: v0.8.0: search, askTheRabbi, idExtracter
Added search(), askTheRabbi() and idExtracter()
- Loading branch information
Showing
16 changed files
with
557 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ yarn.lock | |
.yarn/ | ||
.pnp.loader.mjs | ||
.npmrc | ||
build/ | ||
build/ | ||
package-lock.json |
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,5 +1,5 @@ | ||
.gitignore | ||
tests/ | ||
test/ | ||
src/ | ||
.yarn/ | ||
.github/ | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { askTheRabbiResponse } from "../types/askTheRabbi"; | ||
import cheerio from "cheerio"; | ||
|
||
/** | ||
* Gets the question and answer of "ask the rabbi" question. | ||
* | ||
* @param {number} questionID The ID of the question. Can be found in `https://www.hidabroot.org/question/X` (X is the question ID). | ||
*/ | ||
export async function askTheRabbi( | ||
questionID: number | ||
): Promise<askTheRabbiResponse | null> { | ||
// the url for the question | ||
const url = "https://www.hidabroot.org/question/" + questionID; | ||
|
||
try { | ||
// fetches the url of the question | ||
return ( | ||
fetch(url) | ||
.then((response) => response.text()) | ||
// takees the html response | ||
.then((html) => { | ||
// loads it into cheerio | ||
const $ = cheerio.load(html); | ||
// gets the header of the question | ||
const header = $(".page\\_title.no\\_border").eq(0).text(); | ||
// if the header indicates that the page was not found | ||
if (header == "דף שגיאה 404 - אבל יש לנו הצעות אחרות!...") { | ||
// return null | ||
return null; | ||
} else { | ||
// gets the whole div with id "article_inner" and save only the question div in the question variable | ||
const question = $("[id=article_inner]").contents().eq(1); | ||
// format the question div text content | ||
const formattedQuestion = | ||
question.text().replace(/\s+/g, " ").replace(/<br>/g, "\n") || ""; | ||
// gets the whole div with id "article_inner" and save only the answer div in the answer variable | ||
const answer = $("[id=article_inner]").contents().eq(1); | ||
// format the answer div text content | ||
const formattedAnswer = | ||
answer.text().replace(/\s+/g, " ").replace(/<br>/g, "\n") || ""; | ||
// saves the json response | ||
const jsonResponse = { | ||
title: header, | ||
question: formattedQuestion, | ||
answer: formattedAnswer, | ||
}; | ||
|
||
// returns the json response | ||
return jsonResponse; | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("Error: " + error); | ||
return null; | ||
}) | ||
); | ||
} catch (e) { | ||
console.error("Error: " + e); | ||
return null; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { idExtracterOptions } from "../types/idExtracter"; | ||
|
||
/** | ||
* Extract IDs from any article, "Ask The Rabbi" question page or anything else that exists on Hidabroot's website. Returns null if the page isn't a valid Hidabroot page URL. | ||
* | ||
* @param {string} input A URL for the page you want to extract the ID from. | ||
* @param {idExtracterOptions} options Options for extracting the ID. | ||
*/ | ||
export function idExtracter( | ||
input: string, | ||
options: idExtracterOptions | ||
): number | null { | ||
if ( | ||
input.startsWith("www.hidabroot.org/") || | ||
input.startsWith("http://www.hidabroot.org/") || | ||
input.startsWith("https://www.hidabroot.org/") || | ||
input.startsWith("hidabroot.org/") | ||
) { | ||
if (options.pageType == "askTheRabbi") { | ||
if (input.startsWith("http://www.hidabroot.org/")) { | ||
const id = input | ||
.replace("http://www.hidabroot.org/question/", "") | ||
.trim(); | ||
return Number(id); | ||
} else if (input.startsWith("https://www.hidabroot.org/")) { | ||
const id = input | ||
.replace("https://www.hidabroot.org/question/", "") | ||
.trim(); | ||
return Number(id); | ||
} else if (input.startsWith("www.hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/question/", "").trim(); | ||
return Number(id); | ||
} else if (input.startsWith("hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/question/", "").trim(); | ||
return Number(id); | ||
} else { | ||
return null; | ||
} | ||
} else if (options.pageType == "article") { | ||
if (input.startsWith("http://www.hidabroot.org/")) { | ||
const id = input | ||
.replace("http://www.hidabroot.org/article/", "") | ||
.trim(); | ||
return Number(id); | ||
} else if (input.startsWith("https://www.hidabroot.org/")) { | ||
const id = input | ||
.replace("https://www.hidabroot.org/article/", "") | ||
.trim(); | ||
return Number(id); | ||
} else if (input.startsWith("www.hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/article/", "").trim(); | ||
return Number(id); | ||
} else if (input.startsWith("hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/article/", "").trim(); | ||
return Number(id); | ||
} else { | ||
return null; | ||
} | ||
} else if (options.pageType == "video") { | ||
if (input.startsWith("http://www.hidabroot.org/")) { | ||
const id = input.replace("http://www.hidabroot.org/video/", "").trim(); | ||
return Number(id); | ||
} else if (input.startsWith("https://www.hidabroot.org/")) { | ||
const id = input.replace("https://www.hidabroot.org/video/", "").trim(); | ||
return Number(id); | ||
} else if (input.startsWith("www.hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/video/", "").trim(); | ||
return Number(id); | ||
} else if (input.startsWith("hidabroot.org/")) { | ||
const id = input.replace("hidabroot.org/video/", "").trim(); | ||
return Number(id); | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} |
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,4 +1,7 @@ | ||
import { todayNews } from "./todayNews"; | ||
import { getArticle } from "./getArticle"; | ||
import { askTheRabbi } from "./askTheRabbi"; | ||
import { search } from "./search"; | ||
import { idExtracter } from "./idExtracter"; | ||
|
||
export { todayNews, getArticle }; | ||
export { todayNews, getArticle, askTheRabbi, search, idExtracter }; |
Oops, something went wrong.