YAMAPの登山計画データをCompassSourceに変換するscript
実装
情報を抽出して、CompassSourceに変換する
code:sh
code:mod.ts
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="dom" />
import { CompassSource, PrefectureId } from "../コンパスに自動入力するscript/mod.ts";
declare const __NUXT__: YAMAP;
export const scrape = (): CompassSource | undefined => {
const plan = __NUXT__.data0.plan; const root = plan.coords;
const startAt = new Date(plan.start_at * 1000);
// 前のポイントからの移動時間を計算するのに必要な変数
let cursor = new Date(startAt);
let prev = 0;
// stayTimeの単位は秒
const checkpoints = plan.checkpoints.flatMap((point) => {
const coord = point.landmark.coord;
const name = point.landmark.name;
const isSleep = point.stay_type === "sleep";
const prefecture = point.landmark.prefectures0.id; /** その日の最初のポイントかどうか */
const isStart = point.cumulative_down === 0 && point.cumulative_up === 0;
const now = point.arrival_time_in_seconds;
const arrived = !isStart ?
new Date(cursor.getTime() + Math.round((now - prev) * 1000 * plan.time_multiplier)) :
new Date(startAt.getTime() + now * 1000);
arrived.setDate(arrived.getDate() + point.arrival_day_number - 1);
prev = now + point.stay_time;
cursor = new Date(arrived.getTime() + point.stay_time * 1000);
return { coord, name, prefecture, arrived, left: new Date(cursor), isSleep };
});
// 泊地を一つの地点にまとめる
const points = checkpoints.flatMap((point, i) => {
if (!point.isSleep) return point; if (checkpointsi - 1?.isSleep) return []; point.left = checkpointsi + 1.arrived; });
const start = points.shift()!;
const end = points.pop()!;
return {
purpose: 2, // 登山
route: {
start: {
prefecture: start.prefecture,
name: start.name,
date: start.arrived.getTime() /1000,
},
points: points.map((point) => ({
name: point.name,
isSleep: point.isSleep,
arrived: {
hours: point.arrived.getHours(),
minutes: point.arrived.getMinutes(),
},
left: {
hours: point.left.getHours(),
minutes: point.left.getMinutes(),
},
})),
end: {
prefecture: end.prefecture,
name: end.name,
date: end.arrived.getTime() / 1000,
},
},
members: {
malesCount: 0,
femalesCount: 0,
members: [],
},
emergencies: [],
foodCount: plan.food,
trailSnackCount: plan.trail_snack ? 1 : 0,
hasWater: plan.water > 0,
personalEquipment: "",
partyEquipment: "",
hasRadioEquipment: false,
escapeRoot: "",
note: "",
};
};
使わないpropertiesは略している
code:mod.ts
export interface YAMAP {
data: {
plan: Plan;
timezone: string | undefined;
}[];
}
export interface Plan {
get title(): string;
get coords(): Coord[];
get checkpoints(): Checkpoint[];
get maps(): Map[];
get code(): string;
get id(): number;
get created_at(): number;
get time_multiplier(): number;
get start_at(): number;
get plan_members(): Member[];
get food(): number;
get trail_snack(): boolean;
get water(): number;
}
export interface Checkpoint {
get arrival_day_number(): number;
get arrival_time_in_seconds(): number;
get stay_time(): number;
get stay_type(): string | null;
get cumulative_down(): number;
get cumulative_up(): number;
get distance(): number;
get id(): number;
get is_entrance(): boolean;
get is_exit(): boolean;
get landmark(): Landmark;
get pass_at(): number;
get pass_time(): number;
}
export interface Landmark {
get altitude(): number;
get coord(): Coord;
get created_at(): number;
get updated_at(): number;
get description(): string;
get landmark_type_id(): number;
get is_summit(): boolean;
get name(): string;
get name_en(): string;
get name_ja(): string;
get prefectures(): Prefecture[];
}
export interface Prefecture {
get name(): string;
get slug(): string;
get full_name(): string;
get description(): string | null;
get id(): PrefectureId;
get plan_submittable(): boolean;
get created_at(): number;
}
export interface Map {
get name(): string;
}
export interface Member {
get address(): string | null;
get birth_year(): string | null;
get created_at(): number;
get name(): string | null;
get phone_number(): string;
get user(): User | null;
}
export interface User {
get birth_year(): string | null;
get created_at(): number;
get description(): string;
get name(): string;
}