first running version
This commit is contained in:
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
declare type MarmitonSearchResult = {
|
||||
name: string;
|
||||
url: string;
|
||||
score: number;
|
||||
commentCount: number;
|
||||
};
|
||||
|
||||
declare type SearchRecipeType = "season" | "recipe";
|
||||
|
||||
declare type SearchDishType =
|
||||
| "entree"
|
||||
| "platprincipal"
|
||||
| "dessert"
|
||||
| "amusegueule"
|
||||
| "accompagnement"
|
||||
| "sauce"
|
||||
| "boisson"
|
||||
| "confiserie"
|
||||
| "conseil";
|
||||
|
||||
declare type SearchOptions = {
|
||||
type?: SearchRecipeType | SearchRecipeType[];
|
||||
dt?: SearchDishType | SearchDishType[];
|
||||
page?: number;
|
||||
};
|
||||
|
||||
declare type MarmitonSearchInput = {
|
||||
query: string;
|
||||
} & SearchOptions;
|
||||
@@ -0,0 +1,96 @@
|
||||
/* eslint-disable */
|
||||
// Phantombuster configuration {
|
||||
"phantombuster command: nodejs"
|
||||
"phantombuster package: 5"
|
||||
// }
|
||||
/* eslint-enable */
|
||||
|
||||
import Buster from "phantombuster";
|
||||
import puppeteer from "puppeteer";
|
||||
|
||||
const MARMITON_URL = "https://www.marmiton.org";
|
||||
|
||||
const buster = new Buster();
|
||||
|
||||
const optionsToUrlParams = (
|
||||
opts: Record<string, unknown | unknown[]>
|
||||
): string => {
|
||||
return Object.keys(opts)
|
||||
.map((key) => {
|
||||
const value = opts[key];
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((val) => `${key}=${val}`).join("&");
|
||||
} else {
|
||||
return `${key}=${value}`;
|
||||
}
|
||||
})
|
||||
.join("&");
|
||||
};
|
||||
|
||||
export const search = async (
|
||||
search: string,
|
||||
opts?: SearchOptions
|
||||
): Promise<MarmitonSearchResult[]> => {
|
||||
const browser = await puppeteer.launch({
|
||||
args: ["--no-sandbox"],
|
||||
});
|
||||
|
||||
const page = await browser.newPage();
|
||||
|
||||
const argsString = optionsToUrlParams({
|
||||
...opts,
|
||||
aqt: search,
|
||||
});
|
||||
|
||||
await page.goto(`${MARMITON_URL}/recettes/recherche.aspx?${argsString}`);
|
||||
const list = await page.$$eval(
|
||||
"div>a[href^='/recettes/recette']",
|
||||
(links: Element[], urlStart: string) =>
|
||||
links.map((element) => {
|
||||
const titleElem = element.querySelector("h4");
|
||||
|
||||
const bottomString =
|
||||
titleElem?.parentElement?.lastElementChild?.textContent ?? "";
|
||||
|
||||
const [scorePart, commentString] = bottomString.split("(");
|
||||
|
||||
return {
|
||||
name: titleElem?.innerText ?? "",
|
||||
url: `${urlStart}${element.getAttribute("href")}` ?? "",
|
||||
score: Number.parseFloat(scorePart.split("/")[0]),
|
||||
commentCount: Number.parseInt(commentString.split(" ")[0]),
|
||||
};
|
||||
}),
|
||||
MARMITON_URL
|
||||
);
|
||||
|
||||
const results = list;
|
||||
|
||||
await page.close();
|
||||
await browser.close();
|
||||
|
||||
return results;
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
const { query, ...opts } = buster.argument as MarmitonSearchInput;
|
||||
|
||||
let results: unknown[] = [];
|
||||
|
||||
try {
|
||||
results = await search(query ?? "crepe", opts);
|
||||
} catch (err) {
|
||||
console.log("could not get result from marmiton:", err);
|
||||
}
|
||||
|
||||
try {
|
||||
await buster.setResultObject(results);
|
||||
} catch (err) {
|
||||
console.log("Could not set the result object:", err);
|
||||
}
|
||||
|
||||
process.exit();
|
||||
};
|
||||
|
||||
main();
|
||||
Vendored
+60
@@ -0,0 +1,60 @@
|
||||
declare module "phantombuster" {
|
||||
class Buster {
|
||||
public agentId: number;
|
||||
public apiKey: string;
|
||||
public argument: object;
|
||||
public containerId: number;
|
||||
public retryCount: number;
|
||||
public maxRetries: number;
|
||||
public proxyAddress: string;
|
||||
|
||||
public download(
|
||||
url: string,
|
||||
saveAs?: string,
|
||||
headers?: { [name: string]: string }
|
||||
): Promise<string>;
|
||||
public save(
|
||||
url: string,
|
||||
saveAs?: string,
|
||||
headers?: { [name: string]: string }
|
||||
): Promise<string>;
|
||||
public saveFolder(
|
||||
localFolder?: string,
|
||||
storageFolder?: string
|
||||
): Promise<string>;
|
||||
public saveText(
|
||||
text: string,
|
||||
saveAs: string,
|
||||
mime?: string
|
||||
): Promise<string>;
|
||||
public saveBase64(
|
||||
text: string,
|
||||
saveAs: string,
|
||||
mime?: string
|
||||
): Promise<string>;
|
||||
public solveCaptcha(selector: string): Promise<string>;
|
||||
public solveCaptchaBase64(base64: string): Promise<string>;
|
||||
public solveNoCaptcha(
|
||||
url: string,
|
||||
key: string,
|
||||
secret?: string
|
||||
): Promise<string>;
|
||||
public solveCaptchaImage(
|
||||
url: string,
|
||||
headers?: { [name: string]: string }
|
||||
): Promise<string>;
|
||||
public mail(subject: string, text: string, to?: string): Promise<void>;
|
||||
public pushover(message: string, options?: object): Promise<void>;
|
||||
public progressHint(progress: number, label?: string): void;
|
||||
public overrideTimeLimit(seconds: number): Promise<void>;
|
||||
public getTimeLeft(): Promise<number>;
|
||||
public setAgentObject(agentId: number, object: object): Promise<void>;
|
||||
public setAgentObject(object: object): Promise<void>;
|
||||
public getAgentObject(agentId?: number): Promise<unknown>;
|
||||
public setGlobalObject(object: object): Promise<void>;
|
||||
public getGlobalObject(): Promise<unknown>;
|
||||
public setResultObject(object: object): Promise<void>;
|
||||
}
|
||||
|
||||
export = Buster;
|
||||
}
|
||||
Reference in New Issue
Block a user