Skip to content

Commit

Permalink
feat: emulate rotating proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Feb 28, 2024
1 parent 2d8d32c commit b3404d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ includes:
tear: tasks/Taskfile.tear.yml
helm: Taskfile.helm.yml
util: tasks/Taskfile.util.yml
stop: tasks/Taskfile.stop.yml

tasks:
setup:
Expand Down
14 changes: 11 additions & 3 deletions src/domain/search_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ class SearchCore {
this.#logger = logger;
}

get proxy(): string | undefined {
const p = this.#config.proxy;
if (p == null) return undefined;
const arr = p.split(";");
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}

async mainKTMBPage(): Promise<MainPageToken> {
const referer = "https://online.ktmb.com.my/";
const resp = await f("https://shuttleonline.ktmb.com.my/Home/Shuttle", {
Expand All @@ -78,7 +86,7 @@ class SearchCore {
...htmlHeaders,
Referer: referer,
},
proxy: this.#config.proxy,
proxy: this.proxy,
method: "GET",
});
const text = await resp.text();
Expand Down Expand Up @@ -131,7 +139,7 @@ class SearchCore {
...defaultHeaders,
Referer: referer,
},
proxy: this.#config.proxy,
proxy: this.proxy,
body: stringify(queryParams),
method: "POST",
});
Expand Down Expand Up @@ -166,7 +174,7 @@ class SearchCore {
RequestVerificationToken: token,
Referer: referer,
},
proxy: this.#config.proxy,
proxy: this.proxy,
body: JSON.stringify({
SearchData: searchData,
FormValidationCode: formValidation,
Expand Down
2 changes: 0 additions & 2 deletions src/lib/populator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Logger } from "pino";
import { SearcherBuilder } from "../domain/searcher/builder.ts";
import { addDays, differenceInDays } from "date-fns";
import { RetrieveResult } from "./interfaces.ts";
import { __ } from "../utility.ts";

class Populator {
constructor(
Expand Down Expand Up @@ -44,7 +43,6 @@ class Populator {
this.logger.error({ error: e }, "Failed to retrieve schedule");
break;
}
await __(2);
}
return ret;
}
Expand Down
16 changes: 16 additions & 0 deletions tasks/Taskfile.stop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3"

tasks:
default:
desc: Stop all everything
cmds:
- task: dev
- task: test
dev:
desc: Stop the development workflow
cmds:
- tilt down
test:
desc: Stop the test workflow
cmds:
- tilt down -- --config config/test.yaml --action test

0 comments on commit b3404d1

Please sign in to comment.