feat: Implemented fetching stories

Signed-off-by: Philipp Le <philipp@philipple.de>
Change-Id: I3841b5ca69f304c52c6bc6f32d3a0329b0547db4
diff --git a/src/story.ts b/src/story.ts
new file mode 100644
index 0000000..2163555
--- /dev/null
+++ b/src/story.ts
@@ -0,0 +1,62 @@
+/*
+ * SPDX-License-Identifier: MPL-2.0
+ *   Copyright (c) 2022 Philipp Le <philipp@philipple.de>.
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+import nodemailer = require("nodemailer");
+
+import { username, story_config } from "./config";
+import {Client, StoryCollection, Sleeper} from "./utils"
+
+async function send_email_report(stories: StoryCollection) {
+    if ((stories.length() > 0) && (story_config.email)) {
+        const smtp = nodemailer.createTransport({
+            host: story_config.email.smtp.host,
+            port: story_config.email.smtp.port,
+            auth: {
+                user: story_config.email.smtp.user,
+                pass: story_config.email.smtp.password
+            },
+            requireTLS: story_config.email.smtp.starttls
+        });
+        let body = "IG Stories have been downloaded\n\nDetails:\n";
+        stories.users.forEach((stories, user) => {
+            if (stories.length > 0) {
+                body += `    Fetched ${stories.length} items for ${user}`
+            }
+        });
+        await smtp.sendMail({
+            from: story_config.email.from,
+            to: story_config.email.to,
+            subject: story_config.email.subject,
+            text: body
+        });
+        smtp.close();
+    }
+}
+
+const client = new Client();
+(async () => {
+    await client.login(username);
+    try {
+        const sleeper = new Sleeper();
+
+        let running = true;
+        process.on("SIGINT", (code) => {
+            running = false;
+            sleeper.cancel();
+        });
+
+        while (running) {
+            const stories = await client.get_stories(story_config.users);
+            await stories.download(story_config.destination);
+            await send_email_report(stories);
+            await sleeper.sleep_ms(story_config.sleep_sec * 1000);
+        }
+    } finally {
+        await client.logout();
+    }
+})();