Untitled

 avatar
unknown
plain_text
2 years ago
6.0 kB
12
Indexable
    class UserCacheItem {
    /**
     * @param {string} userId
     * @param {object} user
     */
    constructor(userId, user) {
        this.userId = userId;
        this.user = user;
        this.hidden = !!localStorage.getItem('hiddenUsers')?.includes(userId);

        this.photoIndex = 0;
    }

    /**
     * @returns {string | null}
     */
    getPreviousPhoto() {
        if (!this.user) return null;

        this.photoIndex = this.photoIndex - 1;

        if (this.photoIndex < 0) this.photoIndex = this.user.photos.length - 1;

        return this.user.photos[this.photoIndex].url;
    }

    /**
     * @returns {string | null}
     */
    getNextPhoto() {
        if (!this.user) return null;

        this.photoIndex = (this.photoIndex + 1) % this.user.photos.length;

        return this.user.photos[this.photoIndex].url;
    }

    /**
     * @returns {number}
     */
    getAge() {
        if (!this.user || !this.user.birth_date) return 0;

        const currentDate = new Date();
        const birthDate = Date.parse(this.user.birth_date);

        const currentYear = currentDate.getFullYear();
        const currentMonth = currentDate.getMonth();
        const currentDay = currentDate.getDay();
        const birthYear = birthDate.getFullYear();
        const birthMonth = birthDate.getMonth();
        const birthDay = birthDate.getDay();

        let age = currentYear - birthYear;

        if (currentMonth < birthMonth) age--;
        else if (currentDay < birthDay) age--;

        return age;
    }

    /**
     * @returns {boolean}
     */
    isHidden() {
        return this.hidden;
    }

    /** @param {boolean} hidden */
    setHidden(hidden) {
        this.hidden = hidden;
    }
}
    class UserCache {
        constructor() {
            /** @type {Map<string, UserCacheItem>} */
            this.cache = new Map();
        }

        /**
         * @param {string} userId
         * @param {object} user
         * @returns {UserCacheItem}
         */
        add(userId, user) {
            this.delete(userId);

            const newItem = new UserCacheItem(userId, user);
            this.cache.set(userId, newItem);

            return newItem;
        }

        /**
         * @param {string} userId
         */
        has(userId) {
            return this.cache.has(userId);
        }

        /**
         * @param {string} userId
         * @returns UserCacheItem | undefined
         */
        get(userId) {
            return this.cache.get(userId);
        }

        /**
         * @param {string} userId
         */
        delete(userId) {
            const existingUser = this.cache.get(userId);

            if (!existingUser) return;

            this.cache.delete(userId);
        }

        clear() {
            for (const userItem of this.cache.values()) {
                this.cache.delete(userItem.userId);
            }
        }
    }
const cache = new UserCache();

async function safeAwait(promise) {
    try {
        const result = await promise;
        return [null, result];
    } catch (err) {
        return [err, undefined];
    }
}

async function fetchUser(id) {
    /* disabled due to API changes, currently looking for a workaround!*/
    try {
        return fetch(`https://api.gotinder.com/user/${id}`, {
            headers: {
                'X-Auth-Token': localStorage.getItem('TinderWeb/APIToken') ?? '',
                platform: 'android',
            },
        })
            .then((res) => res.json())
            .then((res) => res.results);
    } catch (e) {
        console.log(e);
        return null;
    }
}

async function fetchTeasers() {
    return fetch('https://api.gotinder.com/v2/fast-match/teasers', {
        headers: {
            'X-Auth-Token': localStorage.getItem('TinderWeb/APIToken') ?? '',
            platform: 'android',
        },
    })
        .then((res) => res.json())
        .then((res) => res.data.results);
}

async function processTeasers() {
    const likesGridContainerEl = document.querySelector('main div.Expand > div[role="grid"]');

    if (!likesGridContainerEl) return;

    const [failedToFetchTeasersError, teasers] = await safeAwait(fetchTeasers());

    if (failedToFetchTeasersError) {
        console.error(`Could not load teasers: ${failedToFetchTeasersError.name}`);
        return;
    }

    /** @type {NodeListOf<HTMLElement>} */
    const teaserEls = document.querySelectorAll('.Expand.enterAnimationContainer > div:nth-child(1)');
    console.log(teasers)
    for (let i = 0; i < teaserEls.length; ++i) {
        const teaserUser = teasers[i].user;
        const teaserEl = teaserEls[i];
        const teaserImage = teaserUser.photos[0].url;
        const userId = teaserImage.slice(32, 56);
        const [fetchUserError, user] = await safeAwait(fetchUser(userId));
        const userItem = cache.add(userId, user ?? null);
        try {
            if (fetchUserError) {
                console.error(`Could not fetch user ${userId}: ${fetchUserError.name}`);
                continue;
            }

            const response = await fetch(`https://api.gotinder.com/like/${userItem.userId}?locale=ru`, {
                headers: {
                    'X-Auth-Token': localStorage.getItem('TinderWeb/APIToken') ?? '',
                    platform: 'android',
                    'Content-Type': 'application/json',
                },
                method: 'POST',
                body: JSON.stringify(
                    userItem.user
                        ? {
                            liked_content_id: userItem.user.photos[0].id,
                            liked_content_type: 'photo',
                            s_number: userItem.user.s_number,
                        }
                        : {
                            s_number: 0,
                        }
                ),
            });

        } catch (e) {
            console.log(e)
        }
    }
}

await processTeasers();