Last active
February 3, 2021 20:43
-
-
Save Holovin/ed34ad092dc3bf15f7b1e4c87da0c6c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// For use: | |
// 1) npm i telegraf got | |
// 2) replace TELEGRAM_BOT_TOKEN / YOUTUBE_TOKEN_API / YOUTUBE_VIDEO_ID / GROUP | |
const { Telegraf } = require('telegraf'); | |
const got = require('got'); | |
const URL = 'https://www.googleapis.com/youtube/v3/videos?id=YOUTUBE_VIDEO_ID&key=YOUTUBE_TOKEN_API&part=statistics'; | |
const GROUP = -0; // telegram group ip, start with -, yes | |
const TOKEN = 'TELEGRAM_BOT_TOKEN'; | |
async function main() { | |
const bot = new Telegraf(TOKEN); | |
let maxViews = 0; | |
do { | |
const ytJson = await got.get(URL).json(); | |
const views = (ytJson.items && ytJson.items[0] && ytJson.items[0].statistics && ytJson.items[0].statistics.viewCount) || 0; | |
if (views) { | |
if (Math.floor(views / 1_000_000) > Math.floor(maxViews / 1_000_000)) { | |
maxViews = views; | |
await bot.telegram.sendMessage(GROUP, `Просмотры: ${views.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')}`); | |
} | |
} | |
else { | |
await bot.telegram.sendMessage(GROUP, `Оу-оу сломался :(`); | |
break; | |
} | |
await new Promise(resolve => setTimeout(resolve, 5 * 60 * 1000)); | |
} while (true); | |
await bot.telegram.sendMessage(GROUP, `[выключился]`); | |
} | |
(async () => { | |
await main(); | |
})(); | |
process.once('SIGINT', () => bot.stop('SIGINT')); | |
process.once('SIGTERM', () => bot.stop('SIGTERM')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
уважаю братка, но Лёха сломаться не может, ауф 😈😈👊🏻🤜🏻🤛🏻 ☝🏻☝🏻☝🏻