-
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
Luca Mattiazzi
committed
Feb 4, 2023
1 parent
ff572f0
commit 1d0f891
Showing
12 changed files
with
248 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ node_modules | |
.env | ||
*.log | ||
db.sqlite | ||
build | ||
build | ||
.ipynb_checkpoints | ||
tracker.jsonlines |
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ COPY ./.env ./ | |
RUN yarn | ||
|
||
COPY ./bin ./bin | ||
RUN mkdir db | ||
RUN yarn setup | ||
|
||
COPY ./src ./src | ||
|
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 |
---|---|---|
@@ -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()) |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -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 | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export const GOODBYE = "CIAONE" | ||
export const TRACK_LOG = "./db/tracker.jsonlines" | ||
|
||
export const provinces = [ | ||
{ | ||
|
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,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) | ||
} |
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 |
---|---|---|
|
@@ -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" "*" | ||
|
||
|
@@ -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" | ||
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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== | ||
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|