| Philipp Le | 30eb613 | 2022-07-04 12:59:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: MPL-2.0 |
| 3 | * Copyright (c) 2022 Philipp Le <philipp@philipple.de>. |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 7 | */ |
| 8 | |
| 9 | import nodemailer = require("nodemailer"); |
| 10 | |
| 11 | import { username, story_config } from "./config"; |
| 12 | import {Client, StoryCollection, Sleeper} from "./utils" |
| 13 | |
| 14 | async function send_email_report(stories: StoryCollection) { |
| 15 | if ((stories.length() > 0) && (story_config.email)) { |
| 16 | const smtp = nodemailer.createTransport({ |
| 17 | host: story_config.email.smtp.host, |
| 18 | port: story_config.email.smtp.port, |
| 19 | auth: { |
| 20 | user: story_config.email.smtp.user, |
| 21 | pass: story_config.email.smtp.password |
| 22 | }, |
| 23 | requireTLS: story_config.email.smtp.starttls |
| 24 | }); |
| 25 | let body = "IG Stories have been downloaded\n\nDetails:\n"; |
| 26 | stories.users.forEach((stories, user) => { |
| 27 | if (stories.length > 0) { |
| 28 | body += ` Fetched ${stories.length} items for ${user}` |
| 29 | } |
| 30 | }); |
| 31 | await smtp.sendMail({ |
| 32 | from: story_config.email.from, |
| 33 | to: story_config.email.to, |
| 34 | subject: story_config.email.subject, |
| 35 | text: body |
| 36 | }); |
| 37 | smtp.close(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const client = new Client(); |
| 42 | (async () => { |
| 43 | await client.login(username); |
| 44 | try { |
| 45 | const sleeper = new Sleeper(); |
| 46 | |
| 47 | let running = true; |
| 48 | process.on("SIGINT", (code) => { |
| 49 | running = false; |
| 50 | sleeper.cancel(); |
| 51 | }); |
| 52 | |
| 53 | while (running) { |
| 54 | const stories = await client.get_stories(story_config.users); |
| 55 | await stories.download(story_config.destination); |
| 56 | await send_email_report(stories); |
| 57 | await sleeper.sleep_ms(story_config.sleep_sec * 1000); |
| 58 | } |
| 59 | } finally { |
| 60 | await client.logout(); |
| 61 | } |
| 62 | })(); |