feat(gtfs-service): initial commit
This commit is contained in:
parent
83dbe121be
commit
6b52fa1d7b
|
@ -0,0 +1,3 @@
|
|||
NODE_ENV=development
|
||||
#URL=https://soll.vbn.de/gtfs/servicedays?routeshortname=411
|
||||
URL=http://localhost:65534/servicedays?routeshortname=411
|
|
@ -0,0 +1,21 @@
|
|||
## Overview
|
||||
|
||||
tbc
|
||||
|
||||
## Preparation
|
||||
Run the following command in your favorite terminal to install dependenies.
|
||||
```
|
||||
npm i
|
||||
```
|
||||
## Development setup
|
||||
Run the following command in your favorite terminal if you fancy log messages for debugging.
|
||||
```
|
||||
export DEBUG=$DEBUG,debug,gtfs-service
|
||||
```
|
||||
Run the following command in your favorite terminal to start the service in development mode.
|
||||
```
|
||||
npm run dev
|
||||
```
|
||||
## Production deployment
|
||||
|
||||
tbc
|
|
@ -0,0 +1,47 @@
|
|||
require('dotenv').config();
|
||||
const axios=require('axios');
|
||||
const debug=require('debug')('gtfs-service');
|
||||
|
||||
/*check if http get service day response includes data*/
|
||||
function hasData(res){
|
||||
if('data' in res){
|
||||
debug('data in response available');
|
||||
return true;
|
||||
}else{
|
||||
debug('data in response NOT available');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*provide http get service day response and get trip array*/
|
||||
function getAryTripCount(res){
|
||||
if(hasData(res)){
|
||||
const objService=res.data;
|
||||
const aryService=Object.entries(objService);
|
||||
const aryTripCount=aryService.map(trip=>{
|
||||
const aryTrip=Object.keys(trip[1]);
|
||||
return aryTrip.length;
|
||||
});
|
||||
return aryTripCount;
|
||||
}
|
||||
}
|
||||
|
||||
/*provide http get service day response and get time array*/
|
||||
function getAryTime(res){
|
||||
if(hasData(res)){
|
||||
const objService=res.data;
|
||||
const aryService=Object.entries(objService);
|
||||
const arrTime=aryService.map((trips, key) => {
|
||||
//debug('key: ' + key);
|
||||
let time = parseInt(trips[0], 10);
|
||||
//debug('time: ' + time);
|
||||
return time;
|
||||
});
|
||||
return arrTime;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports={
|
||||
getAryTime,
|
||||
getAryTripCount
|
||||
};
|
|
@ -0,0 +1,25 @@
|
|||
require('dotenv').config();
|
||||
const axios=require('axios');
|
||||
const debug=require('debug')('debug');
|
||||
const GtfsService=require('./gtfs-service');
|
||||
|
||||
const URL=process.env.URL;
|
||||
debug('URL: '+URL)
|
||||
|
||||
run().catch(err => {
|
||||
debug('run: error')
|
||||
console.log(err)
|
||||
});
|
||||
|
||||
async function run() {
|
||||
debug('run started...')
|
||||
const res = await axios.get(URL);
|
||||
debug('res: '+res);
|
||||
const aryTripCount=GtfsService.getAryTripCount(res);
|
||||
debug('aryTripCount len: '+aryTripCount.length);
|
||||
debug('aryTripCount [0]: '+aryTripCount[0]);
|
||||
const aryTime=GtfsService.getAryTime(res);
|
||||
debug('aryTime len: '+aryTime.length);
|
||||
debug('aryTime [0]: '+aryTime[0]);
|
||||
debug('run done.')
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "gtfs-service",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "nodemon index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Stefan Begerad",
|
||||
"license": "GPL-3.0",
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"dotenv": "^8.2.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
const debug=require('debug')('utils');
|
||||
|
||||
function findSubStr(str, start, end) {
|
||||
var index = str.slice(start, end);
|
||||
//debug('index: '+index);
|
||||
return index;
|
||||
}
|
||||
|
||||
function gtfsDate2NodeDate(date){
|
||||
let start = 0;
|
||||
let end = 4;
|
||||
let year=findSubStr(date, start, end);
|
||||
//debug('year: '+year);
|
||||
start=4;
|
||||
end=6;
|
||||
let month=findSubStr(date, start, end);
|
||||
//debug('month: '+month);
|
||||
start=6;
|
||||
end=8;
|
||||
let day=findSubStr(date, start, end);
|
||||
//debug('day: '+day);
|
||||
let nodeDate = new Date(year, month - 1, day);
|
||||
//debug('nodeDate: '+nodeDate);
|
||||
return nodeDate;
|
||||
}
|
||||
|
||||
const dateWeekday={
|
||||
sunday:0,
|
||||
monday:1,
|
||||
tuesday:2,
|
||||
wednesday:3,
|
||||
thursday:4,
|
||||
friday:5,
|
||||
saturday:6
|
||||
}
|
||||
|
||||
function nextDay(epoch){
|
||||
debug('epoch: '+epoch);
|
||||
let epochNext=epoch + (24 * 60 * 60 * 1000) // 1 day in millisecond
|
||||
debug('epochNext: '+epochNext);
|
||||
return epochNext;
|
||||
}
|
||||
|
||||
module.exports={
|
||||
gtfsDate2NodeDate,
|
||||
nextDay,
|
||||
dateWeekday
|
||||
};
|
Loading…
Reference in New Issue