-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, OnModuleInit } from "@nestjs/common"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { GtfsService } from "src/modules/gtfs/gtfs.service"; | ||
|
||
@Controller("gtfs") | ||
export class GtfsController implements OnModuleInit { | ||
constructor(private readonly gtfsService: GtfsService) {} | ||
|
||
async onModuleInit(): Promise<void> { | ||
await this.gtfsService.syncGtfsData(); | ||
} | ||
|
||
@Cron(CronExpression.EVERY_7_HOURS) | ||
async cronSyncStops(): Promise<void> { | ||
await this.gtfsService.syncGtfsData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { GtfsController } from "src/modules/gtfs/gtfs.controller"; | ||
import { GtfsService } from "src/modules/gtfs/gtfs.service"; | ||
|
||
@Module({ | ||
controllers: [GtfsController], | ||
providers: [GtfsService], | ||
imports: [], | ||
}) | ||
export class GtfsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { Open as unzipperOpen } from "unzipper"; | ||
|
||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
import { parseCsvString } from "src/utils/csv.utils"; | ||
|
||
@Injectable() | ||
export class GtfsService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async syncGtfsData() { | ||
const response = await fetch("http://data.pid.cz/PID_GTFS.zip"); | ||
const arrayBuffer = await response.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
const directory = await unzipperOpen.buffer(buffer); | ||
|
||
const routes = directory.files.find( | ||
(file) => file.path === "routes.txt", | ||
); | ||
if (!routes) { | ||
console.log("routes.txt not found"); | ||
return; | ||
} | ||
const routeStops = directory.files.find( | ||
(file) => file.path === "route_stops.txt", | ||
); | ||
if (!routeStops) { | ||
console.log("route_stops.txt not found"); | ||
return; | ||
} | ||
|
||
const routesBuffer = await routes.buffer(); | ||
const routeStopsBuffer = await routeStops.buffer(); | ||
|
||
type RouteRecord = { | ||
route_id: string; | ||
route_short_name: string; | ||
route_long_name: string; | ||
route_type: string; | ||
route_color?: string | undefined; | ||
is_night: string; | ||
route_url?: string | undefined; | ||
}; | ||
// FIXME: validate with zod | ||
const routesData = await parseCsvString<RouteRecord>( | ||
routesBuffer.toString(), | ||
); | ||
|
||
type RouteStopRecord = { | ||
route_id: string; | ||
direction_id: string; | ||
stop_id: string; | ||
stop_sequence: string; | ||
}; | ||
// FIXME: validate with zod | ||
const routeStopsData = await parseCsvString<RouteStopRecord>( | ||
routeStopsBuffer.toString(), | ||
); | ||
|
||
await this.prisma.$transaction(async (transaction) => { | ||
await transaction.gtfsRouteStop.deleteMany(); | ||
await transaction.gtfsRoute.deleteMany(); | ||
|
||
await transaction.gtfsRoute.createMany({ | ||
data: routesData.map((route) => ({ | ||
id: route.route_id, | ||
shortName: route.route_short_name, | ||
longName: route.route_long_name ?? null, | ||
type: route.route_type, | ||
isNight: Boolean(route.is_night), | ||
color: route.route_color ?? null, | ||
url: route.route_url ?? null, | ||
})), | ||
}); | ||
|
||
await transaction.gtfsRouteStop.createMany({ | ||
data: routeStopsData.map((routeStop) => ({ | ||
routeId: routeStop.route_id, | ||
directionId: routeStop.direction_id, | ||
stopId: routeStop.stop_id, | ||
stopSequence: Number(routeStop.stop_sequence), | ||
})), | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { parseString } from "@fast-csv/parse"; | ||
|
||
export async function parseCsvString<T>(csvString: string): Promise<T[]> { | ||
return new Promise((resolve) => { | ||
const rows: T[] = []; | ||
|
||
parseString(csvString, { headers: true }) | ||
.on("error", (error) => console.error(error)) | ||
.on("data", (row) => rows.push(row)) | ||
.on("end", () => { | ||
resolve(rows); | ||
}); | ||
}); | ||
} |