Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Mattiazzi committed Feb 4, 2023
1 parent ff572f0 commit 1d0f891
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 39 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
.env
*.log
db.sqlite
build
build
.ipynb_checkpoints
tracker.jsonlines
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ COPY ./.env ./
RUN yarn

COPY ./bin ./bin
RUN mkdir db
RUN yarn setup

COPY ./src ./src
Expand Down
2 changes: 1 addition & 1 deletion bin/generateDb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as sqlite3 from "sqlite3"
sqlite3.verbose()
const db = new sqlite3.Database("./db.sqlite")
const db = new sqlite3.Database("./db/db.sqlite")

db.run("CREATE TABLE subscriptions (telegram_id TEXT, province TEXT, UNIQUE(telegram_id, province))", () => db.close())
Empty file added db/.keep
Empty file.
109 changes: 109 additions & 0 deletions events.ipynb

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as Sentry from "@sentry/node"
import { config } from "dotenv"
config()

import * as Sentry from "@sentry/node"
import * as cron from "node-cron"
import { handleGoodbye, handleSubscription, onLeaveMember, onMessage, onNewChatMember, sendMessage, startBot } from "./src/bot"
import { check } from "./src/checker"
import { GOODBYE } from "./src/constants"
import { removeSubscriptions } from "./src/db"
import { findProvince } from "./src/provinces"

config()
import { startServer } from "./src/server"

Sentry.init({
dsn: process.env.SENTRY_URL,
Expand All @@ -30,4 +31,5 @@ onLeaveMember(async (chatId: number) => {
})

cron.schedule("* * * * *", () => check())
startBot()
startBot()
startServer()
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
},
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.15",
"@types/express": "^4.17.17",
"@types/fast-levenshtein": "^0.0.2",
"@types/node": "^18.11.18",
"@types/node-cron": "^3.0.7",
"@types/serve-index": "^1.9.1",
"@types/sqlite3": "^3.1.8",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
Expand All @@ -42,6 +43,7 @@
"node-cron": "^3.0.2",
"node-html-parser": "^6.1.4",
"nodemon": "^2.0.20",
"serve-index": "^1.9.1",
"sqlite3": "^5.1.4",
"ts-node": "^10.9.1",
"tsc": "^2.0.4"
Expand Down
49 changes: 31 additions & 18 deletions src/checker.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import { appendFile } from "fs/promises"
import { retrieveAvailable } from "./availability"
import { sendMessage } from "./bot"
import { provinces, TRACK_LOG } from "./constants"
import { retrieveSubscriptionsByProvince } from "./db"
import { AvailablePlace } from "./types"

const currentlyAvailableByProvince: Record<string, AvailablePlace[]> = {}
const previouslyAvailableByProvince: Record<string, AvailablePlace[]> = {}

function trackStatus(difference: AvailablePlace[], code: string, openSpots: number, timestamp: number) {
const status = {
newInstances: difference.map(d => ({ city: d.city, address: d.address })),
openSpots: openSpots,
timestamp: timestamp,
province: code,
}
const line = JSON.stringify(status)
appendFile(TRACK_LOG, `${line}\n`)
}

export async function check(): Promise<void> {
const subscriptionsByProvince = await retrieveSubscriptionsByProvince()
if (subscriptionsByProvince.length === 0) return
for (const provinceSubscriptions of subscriptionsByProvince) {
const province = provinceSubscriptions.province
const timestamp = Date.now()
provinces.forEach(async (province) => {
const { code } = province
const provinceSubscriptions = subscriptionsByProvince.find(s => s.province === code)
const previouslyAvailable = previouslyAvailableByProvince[code] || [] as AvailablePlace[]
const currentlyAvailable = await retrieveAvailable(code)
const previouslyAvailableDescriptions = previouslyAvailable.map(c => c.description)
const difference = currentlyAvailable.filter(a => !previouslyAvailableDescriptions.includes(a.description))
trackStatus(difference, code, currentlyAvailable.length, timestamp)
const shouldSend = Boolean(previouslyAvailableByProvince[code])
previouslyAvailableByProvince[code] = currentlyAvailable
if (!provinceSubscriptions || !shouldSend) return
const telegramIds = provinceSubscriptions.ids.split(",")
const currentlyAvailable = currentlyAvailableByProvince[province] || [] as AvailablePlace[]
const available = await retrieveAvailable(province)
const currentlyAvailableDescriptions = currentlyAvailable.map(c => c.description)
const difference = available.filter(a => !currentlyAvailableDescriptions.includes(a.description))
console.log("difference", difference)
const shouldSend = Boolean(currentlyAvailableByProvince[province])
if (shouldSend) {
for (const telegramId of telegramIds) {
for (const newlyAvailablePlace of difference) {
const text = `Adesso c'è posto a ${newlyAvailablePlace.city} - ${newlyAvailablePlace.address}\nApri: ${newlyAvailablePlace.url}`
sendMessage(parseInt(telegramId), text)
}
for (const telegramId of telegramIds) {
for (const newlyAvailablePlace of difference) {
const text = `Adesso c'è posto a ${newlyAvailablePlace.city} - ${newlyAvailablePlace.address}\nApri: ${newlyAvailablePlace.url}`
sendMessage(parseInt(telegramId), text)
}
}
currentlyAvailableByProvince[province] = available
}
})
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const GOODBYE = "CIAONE"
export const TRACK_LOG = "./db/tracker.jsonlines"

export const provinces = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sqlite3 from "sqlite3"
import { SubscriptionGroup } from "./types"

sqlite3.verbose()
const db = new sqlite3.Database("./db.sqlite")
const db = new sqlite3.Database("./db/db.sqlite")

export async function retrieveSubscriptionsByProvince(): Promise<SubscriptionGroup[]> {
return new Promise((resolve, reject) => {
Expand Down
29 changes: 29 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import express from "express"
import serveIndex from "serve-index"
import { retrieveSubscriptionsByProvince } from "./db"

express.static.mime.define({
"application/json": ["jsonlines"]
})
const app = express()


app.use("/db", express.static("./db"), serveIndex("./db", { "icons": true }))

app.get("/subscriptions", async (_req, res) => {
const subscriptions = await retrieveSubscriptionsByProvince()
res.json(subscriptions)
})

app.get("/", async (_req, res) => {
const html = `
<a href="/db">Databases</a>
</br>
<a href="/subscriptions">Subscriptions</a>
`
res.send(html)
})

export function startServer() {
app.listen(8000)
}
76 changes: 63 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,22 @@
dependencies:
dotenv "*"

"@types/express-serve-static-core@^4.17.31":
version "4.17.32"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.32.tgz#93dda387f5516af616d8d3f05f2c4c79d81e1b82"
integrity sha512-aI5h/VOkxOF2Z1saPy0Zsxs5avets/iaiAJYznQFm5By/pamU31xWKL//epiF4OfUA2qTOc9PV6tCUjhO8wlZA==
"@types/express-serve-static-core@^4.17.33":
version "4.17.33"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.33.tgz#de35d30a9d637dc1450ad18dd583d75d5733d543"
integrity sha512-TPBqmR/HRYI3eC2E5hmiivIzv+bidAfXofM+sbonAGvyDhySGw9/PQZFt2BLOrjUUR++4eJVpx6KnLQK1Fk9tA==
dependencies:
"@types/node" "*"
"@types/qs" "*"
"@types/range-parser" "*"

"@types/express@^4.17.15":
version "4.17.15"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.15.tgz#9290e983ec8b054b65a5abccb610411953d417ff"
integrity sha512-Yv0k4bXGOH+8a+7bELd2PqHQsuiANB+A8a4gnQrkRWzrkKlb6KHaVvyXhqs04sVW/OWlbPyYxRgYlIXLfrufMQ==
"@types/express@*", "@types/express@^4.17.17":
version "4.17.17"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.17.tgz#01d5437f6ef9cfa8668e616e13c2f2ac9a491ae4"
integrity sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==
dependencies:
"@types/body-parser" "*"
"@types/express-serve-static-core" "^4.17.31"
"@types/express-serve-static-core" "^4.17.33"
"@types/qs" "*"
"@types/serve-static" "*"

Expand Down Expand Up @@ -281,6 +281,13 @@
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.13.tgz#da4bfd73f49bd541d28920ab0e2bf0ee80f71c91"
integrity sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==

"@types/serve-index@^1.9.1":
version "1.9.1"
resolved "https://registry.yarnpkg.com/@types/serve-index/-/serve-index-1.9.1.tgz#1b5e85370a192c01ec6cec4735cf2917337a6278"
integrity sha512-d/Hs3nWDxNL2xAczmOVZNj92YZCS6RGxfBPjKzuu/XirCgXdpKEb88dYNbrYGint6IVWLNP+yonwVAuRC0T2Dg==
dependencies:
"@types/express" "*"

"@types/serve-static@*":
version "1.15.0"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.15.0.tgz#c7930ff61afb334e121a9da780aac0d9b8f34155"
Expand Down Expand Up @@ -391,7 +398,7 @@ abort-controller@^3.0.0:
dependencies:
event-target-shim "^5.0.0"

accepts@~1.3.8:
accepts@~1.3.4, accepts@~1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==
Expand Down Expand Up @@ -528,6 +535,11 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

[email protected]:
version "0.6.1"
resolved "https://registry.yarnpkg.com/batch/-/batch-0.6.1.tgz#dc34314f4e679318093fc760272525f94bf25c16"
integrity sha512-x+VAiMRL6UPkx+kudNvxTl6hB2XNNCG2r+7wixVfIYwu/2HKRXimwQyaumLjMveWvT2Hkd/cAJw+QBMfJ/EKVw==

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
Expand Down Expand Up @@ -778,7 +790,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==

depd@^1.1.2:
depd@^1.1.2, depd@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==
Expand Down Expand Up @@ -1352,6 +1364,16 @@ [email protected]:
statuses "2.0.1"
toidentifier "1.0.1"

http-errors@~1.6.2:
version "1.6.3"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.6.3.tgz#8b55680bb4be283a0b5bf4ea2e38580be1d9320d"
integrity sha512-lks+lVC8dgGyh97jxvxeYTWQFvh4uw4yC12gVl63Cg30sjPX4wuGcdkICVXDAESr6OJGjqGA8Iz5mkeN6zlD7A==
dependencies:
depd "~1.1.2"
inherits "2.0.3"
setprototypeof "1.1.0"
statuses ">= 1.4.0 < 2"

http-proxy-agent@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz#8a8c8ef7f5932ccf953c296ca8291b95aa74aa3a"
Expand Down Expand Up @@ -1436,6 +1458,11 @@ inherits@2, [email protected], inherits@^2.0.3:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

[email protected]:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==

ip@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ip/-/ip-2.0.0.tgz#4cf4ab182fee2314c75ede1276f8c80b479936da"
Expand Down Expand Up @@ -1611,7 +1638,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12, mime-types@~2.1.24, mime-types@~2.1.34:
mime-types@^2.1.12, mime-types@~2.1.17, mime-types@~2.1.24, mime-types@~2.1.34:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
Expand Down Expand Up @@ -1895,7 +1922,7 @@ parent-module@^1.0.0:
dependencies:
callsites "^3.0.0"

parseurl@~1.3.3:
parseurl@~1.3.2, parseurl@~1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
integrity sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==
Expand Down Expand Up @@ -2099,6 +2126,19 @@ [email protected]:
range-parser "~1.2.1"
statuses "2.0.1"

serve-index@^1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/serve-index/-/serve-index-1.9.1.tgz#d3768d69b1e7d82e5ce050fff5b453bea12a9239"
integrity sha512-pXHfKNP4qujrtteMrSBb0rc8HJ9Ms/GrXwcUtUtD5s4ewDJI8bT3Cz2zTVRMKtri49pLx2e0Ya8ziP5Ya2pZZw==
dependencies:
accepts "~1.3.4"
batch "0.6.1"
debug "2.6.9"
escape-html "~1.0.3"
http-errors "~1.6.2"
mime-types "~2.1.17"
parseurl "~1.3.2"

[email protected]:
version "1.15.0"
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.15.0.tgz#faaef08cffe0a1a62f60cad0c4e513cff0ac9540"
Expand All @@ -2114,6 +2154,11 @@ set-blocking@^2.0.0:
resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7"
integrity sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.0.tgz#d0bd85536887b6fe7c0d818cb962d9d91c54e656"
integrity sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==

[email protected]:
version "1.2.0"
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.2.0.tgz#66c9a24a73f9fc28cbe66b09fed3d33dcaf1b424"
Expand Down Expand Up @@ -2202,6 +2247,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-2.0.1.tgz#55cb000ccf1d48728bd23c685a063998cf1a1b63"
integrity sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==

"statuses@>= 1.4.0 < 2":
version "1.5.0"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
Expand Down

0 comments on commit 1d0f891

Please sign in to comment.