<commit_before>import { Request, Response } from "express";
import * as admin from "firebase-admin";
import * as msgpack from "msgpack-lite";
import nanoid = require("nanoid");
import { parse } from "url";

import { convertUrlIntoArticle } from "./article";
import { convertUrlIntoBook, isAmazonUrl } from "./book";
import { httpsFunction } from "./utils";
import { convertUrlIntoVideo } from "./video";

class File {
    private file;

    constructor(path: string) {
        this.file = admin.storage().bucket().file(path);
    }

    public read = async () => await msgpack.decode((await this.file.download())[0]);

    public write = async (content: any) => await this.file.save(msgpack.encode(content));
}

export default httpsFunction(
    async ({ query: { url } }: Request, response: Response, userId: string) => {
        let convert: (url: string) => any = convertUrlIntoArticle;
        let directory: "articles" | "videos" | "books" = "articles";

        switch (parse(url).hostname) {
            case "www.youtube.com":
                convert = convertUrlIntoVideo;
                directory = "videos";
                break;
            case "books.rakuten.co.jp":
                convert = convertUrlIntoBook;
                directory = "books";
                break;
            default:
                if (isAmazonUrl(url)) {
                    convert = convertUrlIntoBook;
                    directory = "books";
                }
        }

        const item = await convert(url);

        if (!item.name) {
            throw new Error(`Invalid item is detected: ${item}`);
        }
<commit_msg>Handle Rakuten affiliate links<commit_after>import { Request, Response } from "express";
import * as admin from "firebase-admin";
import * as msgpack from "msgpack-lite";
import nanoid = require("nanoid");
import { parse } from "url";

import { convertUrlIntoArticle } from "./article";
import { convertUrlIntoBook, isAmazonUrl } from "./book";
import { httpsFunction } from "./utils";
import { convertUrlIntoVideo } from "./video";

class File {
    private file;

    constructor(path: string) {
        this.file = admin.storage().bucket().file(path);
    }

    public read = async () => await msgpack.decode((await this.file.download())[0]);

    public write = async (content: any) => await this.file.save(msgpack.encode(content));
}

export default httpsFunction(
    async ({ query: { url } }: Request, response: Response, userId: string) => {
        let convert: (url: string) => any = convertUrlIntoArticle;
        let directory: "articles" | "videos" | "books" = "articles";

        switch (parse(url).hostname) {
            case "www.youtube.com":
                convert = convertUrlIntoVideo;
                directory = "videos";
                break;
            case "hb.afl.rakuten.co.jp":
            case "books.rakuten.co.jp":
                convert = convertUrlIntoBook;
                directory = "books";
                break;
            default:
                if (isAmazonUrl(url)) {
                    convert = convertUrlIntoBook;
                    directory = "books";
                }
        }

        const item = await convert(url);

        if (!item.name) {
            throw new Error(`Invalid item is detected: ${item}`);
        }

{'fraction_non_alphanumeric': 0.129607250755287, 'fraction_numerical': 0.0006042296072507553, 'mean_word_length': 2.1897880539499037, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '325321', 'n_tokens_mistral': 923, 'n_tokens_neox': 873, 'n_words': 243}