Skip to content

Commit

Permalink
feat: add hk_sfc workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
linxin committed Oct 20, 2024
1 parent 4dee989 commit ee6b64a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/hk_sfc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Update SFC Companies List And Detail
on:
workflow_dispatch:
inputs:
ids:
description: "update SFC List with ids split by comma, e.g. 1,2,3"
required: false
repository_dispatch:
types:
- update_sfc_list

jobs:
export:
name: Start update SFC Companies list
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
branch: main

- uses: oven-sh/setup-bun@v1

- name: Run update SFC Companies list and detail
env:
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
SUPABASE_PASSWORD: ${{ secrets.SUPABASE_PASSWORD }}
SUPABASE_EMAIL: ${{ secrets.SUPABASE_EMAIL }}
run: |
bun install
bun run src/sites/hk_sfc/index.ts --ids=${{ inputs.ids }}
3 changes: 1 addition & 2 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ async function writeFileWithDirs(filePath: string, data: any) {
export const writeFile = (data: any, filePath = "data") => {
writeFileWithDirs(filePath, JSON.stringify(data, null, 2))
.then(() => {
console.log("File has been saved!");
// console.log("File has been saved!");
})
.catch((err) => {
console.error("Error writing file:", err);
});
};

export const readJsonFile = async (filePath: string, default_info = {}) => {
const fullPath = path.join(__dirname, filePath);
let json = default_info;

try {
Expand Down
38 changes: 27 additions & 11 deletions src/sites/hk_sfc/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ import {

export interface PARTIAL {
ceref: string;
isCorp: boolean;
isRi: boolean;
isEo: boolean;
isCorp?: boolean;
isRi?: boolean;
isEo?: boolean;
}

export class HK_SFC {
private data_list: any[] = [];

public async check_list() {
// 检查接口列表
const start_time = new Date();
this.data_list = await getList();
const end_time = new Date();
console.log(
"🚀 ~ HK_SFC ~ check_list ~ duration:",
end_time.getTime() - start_time.getTime()
);
const previous_total_counts = await getPreviousListCount();

if (previous_total_counts !== this.data_list.length) {
// 前后两次数量不一致就记录
const diff_list = await getDiffList(this.data_list);
if (diff_list.length > 0) {
this.insert_meta(diff_list);
this.insert_history(diff_list);

this.insert_meta(this.data_list);
this.insert_history(diff_list);

// 增量更新
this.get_full_detail_from_file(diff_list);
// 增量更新
this.get_full_detail_from_file(diff_list);
}
}
}

Expand All @@ -43,7 +50,15 @@ export class HK_SFC {
* 生成完后会记录到 backup/hk_sfc/xxx 目录下
*/
public async get_detail_from_page(data_list: any[], batch_size = 3) {
const start_time = new Date();
await processInBatches(data_list, batch_size);
const end_time = new Date();
console.log(
"🚀 ~ HK_SFC ~ get_detail_from_page ~ duration: ",
end_time.getTime() - start_time.getTime(),
" list_length:",
data_list.length
);
}

public async get_full_detail_from_file(
Expand All @@ -62,9 +77,10 @@ export class HK_SFC {
}
}

public async update_by_partial(partial: PARTIAL[]) {
const partial_list = await get_partial_list(partial);
await processInBatches(partial_list, 3);
public async update_by_partial(partial_ids: string) {
const partial_list = await get_partial_list(partial_ids);

await this.get_detail_from_page(partial_list, 3);

await this.get_full_detail_from_file(partial_list, true);
}
Expand Down
27 changes: 26 additions & 1 deletion src/sites/hk_sfc/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { HK_SFC } from "./entry";
import { uniq } from "lodash-es";

const hk_sfc = new HK_SFC();

hk_sfc.check_list();
// 使用 yargs 解析命令行参数
const argv = yargs(hideBin(process.argv))
.option("ids", {
alias: "d",
type: "string",
describe: "JSON formatted data",
coerce: (arg) => {
try {
return arg.split(",").filter(Boolean);
} catch (e) {
return [];
}
},
})
.help().argv;
// @ts-ignore
const init_ids = argv.ids || [];

if (init_ids.length > 0) {
hk_sfc.update_by_partial(uniq(init_ids).join(","));
} else {
hk_sfc.check_list();
}
6 changes: 3 additions & 3 deletions src/sites/hk_sfc/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,14 @@ function filter_u0000(str: string) {
return str ? str.replace(/\u0000/g, "") : "";
}

export const get_partial_list = async (partial: PARTIAL[]) => {
export const get_partial_list = async (partial_ids: string) => {
let data_list: any[] = [];
for (const item of partial) {
for (const item of partial_ids.split(",")) {
const body = getBody({
licstatus: "all",
lictype: "all",
searchbyoption: "byceref",
searchtext: get(item, "ceref"),
searchtext: item,
page: 1,
start: 0,
limit: 20,
Expand Down

0 comments on commit ee6b64a

Please sign in to comment.