|
| 1 | +import {createClient} from 'hafas-client' |
| 2 | +import {profile as dbProfile} from 'hafas-client/p/db/index.js' |
| 3 | +import {DateTime, Duration} from "luxon"; |
| 4 | + |
| 5 | +import sqlite3 from 'sqlite3'; |
| 6 | + |
| 7 | +const {Database, verbose} = sqlite3; |
| 8 | +verbose(); |
| 9 | + |
| 10 | +let args_array = process.argv.slice(2) |
| 11 | +let args = {"fromName":args_array[0],"toName":args_array[1],"days":args_array[2],"dbFile":args_array[3]} |
| 12 | + |
| 13 | +const userAgent = 'github.com/thigg/fahrpreis-plotter' |
| 14 | + |
| 15 | +const client = createClient(dbProfile, userAgent) |
| 16 | + |
| 17 | +async function getFromTo(from, to) { |
| 18 | + let loc1 = await client.locations(from) |
| 19 | + let loc2 = await client.locations(to) |
| 20 | + let from_id = loc1[0].id; |
| 21 | + let to_id = loc2[0].id; |
| 22 | + return {from_id, to_id}; |
| 23 | +} |
| 24 | + |
| 25 | +let {from_id, to_id} = await getFromTo(args.fromName, args.toName); |
| 26 | + |
| 27 | +let now = DateTime.now() |
| 28 | + |
| 29 | +async function queryPrices(from, to, now, number_of_days) { |
| 30 | + let all_prices = [] |
| 31 | + for (let day_offset = 0; day_offset < number_of_days; day_offset++) { |
| 32 | + let query_day = now.plus(Duration.fromObject({days: day_offset})) |
| 33 | + let prices = await client.bestPrices(from, to, query_day.toJSDate()) |
| 34 | + let compact_prices = prices.bestPrices |
| 35 | + .map(d => ({ |
| 36 | + "when": d.fromDate, |
| 37 | + "price": d.bestPrice?.amount |
| 38 | + })).filter(d => d.price !== undefined) |
| 39 | + all_prices = all_prices.concat(compact_prices) |
| 40 | + |
| 41 | + } |
| 42 | + return all_prices; |
| 43 | +} |
| 44 | + |
| 45 | +let all_prices = await queryPrices(from_id, to_id, now, args.days); |
| 46 | + |
| 47 | +function persistPrices(from, to, allPrices, queried_at, db_path) { |
| 48 | + let from_int = parseInt(from); |
| 49 | + let to_int = parseInt(to); |
| 50 | + let db = new sqlite3.Database(db_path, (err) => { |
| 51 | + if (err) { |
| 52 | + console.error(err) |
| 53 | + return console.error(err.message); |
| 54 | + } |
| 55 | + }); |
| 56 | + db.serialize(() => { |
| 57 | + |
| 58 | + db.run('BEGIN TRANSACTION'); |
| 59 | + db.run(`CREATE TABLE IF NOT EXISTS \`fahrpreise\` ( |
| 60 | + \`id\` integer not null primary key autoincrement, |
| 61 | + \`from\` INT not null, |
| 62 | + \`to\` INT not null, |
| 63 | + \`when\` DATETIME not null, |
| 64 | + \`price_cents\` INT not null, |
| 65 | + \`queried_at\` datetime not null |
| 66 | + )` |
| 67 | + ) |
| 68 | + const stmt = db.prepare('INSERT INTO fahrpreise (`from`,`to`,`when`,`price_cents`,`queried_at`) VALUES (?,?,?,?,?);'); |
| 69 | + for (let data of allPrices) { |
| 70 | + stmt.run(from_int, to_int, new Date(data.when), Math.trunc(data.price * 100), queried_at |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + stmt.finalize() |
| 75 | + |
| 76 | + db.run('COMMIT', (err) => { |
| 77 | + if (err) { |
| 78 | + console.error('Error committing transaction', err); |
| 79 | + } else { |
| 80 | + console.log(`Wrote ${allPrices.length} entries`); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +persistPrices(from_id, to_id, all_prices, now.toJSDate(), args.dbFile); |
| 87 | + |
0 commit comments