Added last update, cache strategy, telegram fail notifications, teachers photos

This commit is contained in:
VityaSchel
2023-10-02 18:54:26 +04:00
parent f6daee6201
commit 755654cf9d
19 changed files with 579 additions and 124 deletions

View File

@@ -4,6 +4,7 @@ import contentTypeParser from 'content-type'
// import { parse } from 'node-html-parser'
import { JSDOM } from 'jsdom'
import { content as mockContent } from './mock'
import { reportParserError } from '@/app/logger'
// ПС-7: 146
export async function getSchedule(groupID: number, groupName: string): Promise<Day[]> {
@@ -17,11 +18,13 @@ export async function getSchedule(groupID: number, groupName: string): Promise<D
return parsePage(root, groupName)
} catch(e) {
console.error('Error while parsing lk.ks.psuti.ru')
reportParserError(new Date().toISOString(), 'Не удалось сделать парсинг для группы', groupName)
throw e
}
} else {
console.error(page.status, contentType)
console.error(content.length > 500 ? content.slice(0, 500 - 3) + '...' : content)
reportParserError(new Date().toISOString(), 'Не удалось получить страницу для группы', groupName)
throw new Error('Error while fetching lk.ks.psuti.ru')
}
}

18
src/app/logger.ts Normal file
View File

@@ -0,0 +1,18 @@
import TelegramBot from 'node-telegram-bot-api'
const token = process.env.PARSING_FAILURE_NOTIFICATIONS_TELEGRAM_BOTAPI_TOKEN
const ownerID = process.env.PARSING_FAILURE_NOTIFICATIONS_TELEGRAM_CHAT_ID
let bot: TelegramBot
if (!token || !ownerID) {
console.warn('Telegram Token is not specified. This means you won\'t get any notifications about parsing failures.')
} else {
bot = new TelegramBot(token, { polling: false })
}
export async function reportParserError(...text: string[]) {
if (!token || !ownerID) return
await bot.sendMessage(ownerID, text.join(' '))
}