-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchInfo.js
More file actions
135 lines (130 loc) · 3.76 KB
/
fetchInfo.js
File metadata and controls
135 lines (130 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { ZenkakuAlpha } from "https://code4fukui.github.io/mojikiban/ZenkakuAlpha.js";
import { getEnv } from "https://js.sabae.cc/getEnv.js";
const api = async (path, req) => {
const token = await getEnv("GBIZ_ACCESS_TOKEN");
const opt = {
method: req ? "POST" : "GET",
mode: "cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
"X-hojinInfo-api-token": token,
},
};
if (req) {
opt.body = JSON.stringify(req);
}
const url = "https://info.gbiz.go.jp/hojin/v1/" + path;
const res = await (await fetch(url, opt)).json();
return res;
};
export const fetchInfoBasic = async (cid, outpath) => {
const fn = outpath + cid + ".json";
//const data = await api("hojin/" + cid);
//console.log(data);
//const list = ["", "certification", "commendation", "finance", "patent", "procurement", "subsidy", "workplace"];
const data = await api("hojin/" + cid);
console.log(data);
const res = data["hojin-infos"][0];
return res;
};
export const fetchInfo = async (cid, outpath) => {
const fn = outpath + cid + ".json";
try {
await Deno.readTextFile(fn);
return false;
} catch (e) {
}
//const data = await api("hojin/" + cid);
//console.log(data);
let res = null;
//const list = ["", "certification", "commendation", "finance", "patent", "procurement", "subsidy", "workplace"];
const list = ["certification", "commendation", "finance", "patent", "procurement", "subsidy", "workplace"];
for (const name of list) {
const data = await api("hojin/" + cid + (name ? "/" + name : ""));
//console.log(data);
//await Deno.writeTextFile(cid + "_" + name + ".json", JSON.stringify(data, null, 2));
if (!res) {
res = data["hojin-infos"][0];
} else {
res[name] = data["hojin-infos"][0][name];
}
}
if (outpath) {
await Deno.writeTextFile(fn, JSON.stringify(res, null, 2));
return true;
}
return res;
};
export const fetchInfoSummary = async (cid) => {
const addUnique = (array, s) => {
if (array.indexOf(s) == -1) {
array.push(s);
}
};
const limit10 = (array) => {
const limit = 5;
const res = [];
for (let i = 0; i < Math.min(array.length, limit); i++) {
res.push(array[i]);
}
return ZenkakuAlpha.toHan(res.join(" / ")) + (array.length >= limit ? " 他" : "");
}
const info = await fetchInfo(cid);
const patent = []; // 特許
const design = []; // 意匠
const trademark = []; // 商標
info.patent.sort((a, b) => b.application_date.localeCompare(a.application_date));
for (const d of info.patent) {
switch (d.patent_type) {
case "特許": {
addUnique(patent, d.title);
break;
}
case "意匠": {
addUnique(design, d.title);
break;
}
case "商標": {
addUnique(trademark, d.title);
break;
}
default: {
throw "unknown patent_type";
}
}
//console.log(d.application_date, d.title);
}
/*
return {
特許: limit10(patent),
意匠: limit10(design),
商標: limit10(trademark),
};
*/
const summary = (name, array) => {
if (array.length == 0) {
return "";
}
return `${name} ${array.length}件(${limit10(array)})`;
};
const pinfo = [
summary("特許", patent),
summary("意匠", design),
summary("商標", trademark),
].join(" ").trim();
//console.log(pinfo);
return {
法人番号: info.corporate_number,
住所: info.location,
法人名: info.name,
"法人名(カナ)": info.kana,
更新日時: info.update_date,
企業ホームページ: info.company_url,
事業概要: info.business_summary,
従業員数: info.employee_number,
代表者名: info.representative_name,
特許情報: pinfo,
};
};