File size: 2,773 Bytes
42ebd36
 
 
4fe2044
42ebd36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5182d78
42ebd36
 
 
 
 
 
 
 
c935a34
 
 
 
 
 
 
 
 
 
42ebd36
c935a34
bfba9ef
 
 
c935a34
 
 
 
 
42ebd36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import got from 'got';
import {
    ConvertResponseSchema,
    LinkItemSchema,
    YoutubedlResponseSchema,
} from '../types/youtubedl-v1.js';
import {
    YoutubeDownloaderArgsSchema,
    YoutubedlSchema,
} from '../types/youtube-v1.js';
import { DEFAULT_HEADERS } from '../constant.js';
import { parseFileSize } from './util.js';

export async function youtubedl(url, server) {
    YoutubeDownloaderArgsSchema.parse(arguments);

    const form = {
        k_query: url,
        k_page: 'home',
        hl: server || 'en',
        q_auto: 0
    };

    const data = await got.post('https://www.y2mate.com/mates/analyzeV2/ajax', {
        headers: {
            ...DEFAULT_HEADERS,
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
            cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0',
            origin: 'https://www.y2mate.com'
        },
        form
    }).json();

    const json = YoutubedlResponseSchema.parse(data);
    const video = {};
    const audio = {};
    const other = {};

    for (const key in json.links) {
        for (const tag in json.links[key]) {
            const data = json.links[key][tag];
            const quality = data.q;
            const type = data.f;
            const fileSizeH = data.size;
            const fileSize = parseFileSize(fileSizeH);
            (type === 'mp4' ? video : type === 'mp3' ? audio : other)[quality.toLowerCase()] = {
                quality,
                type,
                fileSizeH,
                fileSize,
                download: convert.bind(convert, json.vid, data.k)
            };
        }
    }

    const result = {
        id: json.vid,
        thumbnail: `https://i.ytimg.com/vi/${json.vid}/0.jpg`,
        title: json.title,
        duration: json.t,
        video,
        audio,
        other
    };
    console.log(result);
    return YoutubedlSchema.parse(result);
}

export async function convert(vid, k) {
    const form = {
        vid,
        k
    };
    try {
        const data = await got.post('https://www.y2mate.com/mates/convertV2/index', {
            headers: {
                ...DEFAULT_HEADERS,
                'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
                cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0',
                origin: 'https://www.y2mate.com'
            },
            form
        }).json();

        const json = ConvertResponseSchema.parse(data);
        if (!json.dlink) {
            throw new Error("Download link is missing");
        }
        return json.dlink; 
    } catch (error) {
        console.error("Error in convert function:", error);
        return "";
    }
}