Skip to content

Commit

Permalink
Release: v0.8.0: search, askTheRabbi, idExtracter
Browse files Browse the repository at this point in the history
Added search(), askTheRabbi() and idExtracter()
  • Loading branch information
itsrn authored Aug 28, 2023
2 parents 9f148c6 + 9d92461 commit 1208573
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 114 deletions.
2 changes: 0 additions & 2 deletions .github/FUNDING.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ yarn.lock
.yarn/
.pnp.loader.mjs
.npmrc
build/
build/
package-lock.json
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.gitignore
tests/
test/
src/
.yarn/
.github/
Expand Down
97 changes: 95 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

> _An (unofficial) API for Hidabroot_
## Important
## Important notice

This API may be blocked at any time by Hidabroot's team. If any damage caused by this API, it is not my fault, use this API at your own risk.
This API is unofficial, meaning **I am not responisble for any damage caused by using this API**. This API may be _blocked at any time by Hidabroot's team_. **This API is never stable** as Hidabroot can change at any time their website structure that will break this API.

## Install

Expand Down Expand Up @@ -78,6 +78,99 @@ getArticle(1185334).then((result) => console.log(result));
//gets the content of the article: https://www.hidabroot.org/article/1185334 and then log the content to the console
```

### search(query, options?)

#### query

Type: `string`

The query to search for in the website.

#### options

Type: [`searchOptions`](/src/types/search.ts)

The options for the search. Recommended to get better results.

#### Example

```ts
import { search } from "hidabroot";

const response = await search("האם היו עולמות קודמים?", {
searchOptions: { searchType: "faq" }, // "faq" means "Ask The Rabbi" questions
});
console.log(response);
```

Which return to the console something like this:

```json
{
"url for result": "result title"
}
```

Currently (`v0.8.0`), the search is capable to return ~10-15 results. This may be changed in the future.

### askTheRabbi(questionID)

#### questionID

Type: `number`

The "Ask The Rabbi" question ID. Can be found in `https://www.hidabroot.org/question/X` where X is the question ID.

#### Example

```ts
import { askTheRabbi, idExtracter } from "hidabroot";

const questionID = idExtracter("https://www.hidabroot.org/question/75137", {
pageType: "askTheRabbi",
});
const response = await askTheRabbi(questionID);
console.log(response);
```

Which return to the console something like this:

```json
{
"title": "the question title",
"question": "the question",
"answer": "the rabbi answer"
}
```

(or `null` if the page wasn't found or if an error occurred)

### idExtracter(input, options)

#### input

Type: `string`

The input URL of the Hidabroot page to extract the ID from.

#### options

Type: [`idExtracterOptions`](/src/types/idExtracter.ts)

The options for the extract process.

#### Example

```ts
import { idExtracter } from "hidabroot";

const questionID = idExtracter("https://www.hidabroot.org/question/75137", {
pageType: "askTheRabbi",
});
console.log(questionID);
//=> 75137
```

## Contributing

All contributions are welcome! Feel free to open an issue (just search the [issue tracker](https://github.com/itsrn/hidabroot/issues) before opening an issue to make sure your issue hasn't already been reported or answered) or a pull request.
11 changes: 9 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hidabroot",
"version": "0.6.4-beta",
"version": "0.8.0-beta",
"description": "An unofficial API for hidabroot",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand All @@ -21,6 +21,7 @@
"url": "git+https://github.com/itsrn/hidabroot"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^16.10.2",
"chai": "^4.3.8",
Expand Down
61 changes: 61 additions & 0 deletions src/askTheRabbi/index.ts
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;
}
}
51 changes: 32 additions & 19 deletions src/getArticle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,42 @@ import cheerio from "cheerio";
* @param {number} id The id of the article
*/
export async function getArticle(id: number): Promise<string | null> {
// the url for the article
const url = "https://www.hidabroot.org/article/" + id;

try {
return fetch(url)
.then((response) => response.text())
.then((html) => {
const $ = cheerio.load(html);
const header = $(".page\\_title.no\\_border").eq(0).text();
let output: string | null = "";
if (header == "דף שגיאה 404 - אבל יש לנו הצעות אחרות!...") {
output = null;
} else {
$('p[dir="RTL"]').each((index, p) => {
output += $(p).text();
});
}
// fetches the article
return (
fetch(url)
.then((response) => response.text())
// takes the html response
.then((html) => {
// loads it into cheerio
const $ = cheerio.load(html);
// gets the header of the article
const header = $(".page\\_title.no\\_border").eq(0).text();
// create an output variable to contain the article content or null if it doesn't exist
let output: string | null = "";
// if the header says that the page is 404
if (header == "דף שגיאה 404 - אבל יש לנו הצעות אחרות!...") {
// set the output to null
output = null;
} else {
// if it does exist, search for every <p> tag with attribute dir="RTL"
$('p[dir="RTL"]').each((index, p) => {
// for every <p> tag, add the text of the found <p> tag to the variable output
output += $(p).text();
});
}

return output;
})
.catch((error) => {
console.error("Error: " + error);
return null;
});
// return the output variable
return output;
})
.catch((error) => {
console.error("Error: " + error);
return null;
})
);
} catch (err) {
console.error("Error:", err);
return null;
Expand Down
81 changes: 81 additions & 0 deletions src/idExtracter/index.ts
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;
}
}
5 changes: 4 additions & 1 deletion src/index.ts
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 };
Loading

0 comments on commit 1208573

Please sign in to comment.