Last active
April 11, 2024 12:34
-
-
Save RealYukiSan/b07dd0463417f8ba03f297550edb750e to your computer and use it in GitHub Desktop.
fetch telegram sticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { get } = require("node:https"); | |
(async () => { | |
const stickerName = ['Elaina_san', 'bagasgoyang'] | |
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs" | |
const domain = "https://api.telegram.org" | |
const file_baseurl = `${domain}/file/${token}` | |
const api_baseurl = `${domain}/${token}` | |
stickerName.forEach(async (name) => { | |
try { | |
let files = await fetch(`${api_baseurl}/getStickerSet?name=${name}`) | |
files = JSON.parse(files.toString()) | |
files.result.stickers.forEach(async (sticker) => { | |
try { | |
let metadata = await fetch(`${api_baseurl}/getFile?file_id=${sticker.file_id}`) | |
metadata = JSON.parse(metadata.toString()) | |
const file = Buffer.from(await fetch(`${file_baseurl}/${metadata.result.file_path}`)) | |
console.log(file.length) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.log("don't exit the loop"); | |
} | |
}) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.error("don't exit the program") | |
} | |
}) | |
})() | |
function fetch(link) { | |
return new Promise((resolve, reject) => { | |
get(link, { timeout: 60e3 }, (res) => { | |
let buff = Buffer.alloc(0); | |
res.on( | |
'data', | |
(chunk) => (buff = Buffer.concat([buff, Buffer.from(chunk)])) | |
); | |
res.on('error', reject); | |
res.on('end', () => resolve(buff)); | |
}).on('error', (e) => { | |
if (e.code != 'ECONNRESET') reject(e); | |
console.log("warn: peer decided to close socket!") | |
fetch(link).then(resolve).catch(reject); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require("node:child_process"); | |
(async () => { | |
const stickerName = ['Elaina_san', 'bagasgoyang'] | |
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs" | |
const domain = "https://api.telegram.org" | |
const file_baseurl = `${domain}/file/${token}` | |
const api_baseurl = `${domain}/${token}` | |
for (let index = 0; index < 3; index++) { | |
stickerName.forEach(async (name) => { | |
try { | |
let files = '' | |
let curl = spawn("curl", ["-s", `${api_baseurl}/getStickerSet?name=${name}`]) | |
curl.stdout.on("data", (chunk) => files += chunk) | |
curl.stdout.on("end", () => { | |
files = JSON.parse(files) | |
files.result.stickers.forEach(async (sticker) => { | |
try { | |
let metadata = '' | |
curl = spawn("curl", ["-s", `${api_baseurl}/getFile?file_id=${sticker.file_id}`]) | |
curl.stdout.on("data", (chunk) => metadata += chunk) | |
curl.stdout.on("end", () => { | |
metadata = JSON.parse(metadata) | |
let file = Buffer.alloc(0) | |
curl = spawn("curl", ["-s", "-o-", `${file_baseurl}/${metadata.result.file_path}`]) | |
curl.stdout.on("data", (chunk) => { | |
file = Buffer.concat([file, Buffer.from(chunk)]) | |
}) | |
curl.stdout.on("end", () => { | |
console.log(file.length) | |
}) | |
}) | |
} catch (error) { | |
if (error.name == 'TypeError') process.stdout.write(error.cause.code + ' - ') | |
else if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} | |
console.log("don't exit the loop"); | |
} | |
}) | |
}) | |
} catch (error) { | |
if (error.name == 'TypeError') process.stdout.write(error.cause.code + ' - ') | |
else if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} | |
console.error("don't exit the program") | |
} | |
}) | |
} | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawnSync } = require("node:child_process"); | |
(async () => { | |
const stickerName = ['Elaina_san', 'bagasgoyang'] | |
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs" | |
const domain = "https://api.telegram.org" | |
const file_baseurl = `${domain}/file/${token}` | |
const api_baseurl = `${domain}/${token}` | |
for (let index = 0; index < 3; index++) { | |
stickerName.forEach(async (name) => { | |
try { | |
let files = spawnSync("curl", ["-s", `${api_baseurl}/getStickerSet?name=${name}`]) | |
files = JSON.parse(files.stdout.toString()) | |
files.result.stickers.forEach(async (sticker) => { | |
try { | |
let metadata = spawnSync("curl", ["-s", `${api_baseurl}/getFile?file_id=${sticker.file_id}`]) | |
metadata = JSON.parse(metadata.stdout.toString()) | |
let file = spawnSync("curl", ["-s", "-o-", `${file_baseurl}/${metadata.result.file_path}`]) | |
file = Buffer.from(file.stdout) | |
console.log(file.length) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.log("don't exit the loop"); | |
} | |
}) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.error("don't exit the program") | |
} | |
}) | |
} | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const stickerName = ['Elaina_san', 'bagasgoyang'] | |
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs" | |
const domain = "https://api.telegram.org" | |
const file_baseurl = `${domain}/file/${token}` | |
const api_baseurl = `${domain}/${token}` | |
stickerName.forEach(async (name) => { | |
try { | |
const files = await fetch(`${api_baseurl}/getStickerSet?name=${name}`).then(res => res.json()) | |
files.result.stickers.forEach(async (sticker) => { | |
try { | |
const metadata = await fetch(`${api_baseurl}/getFile?file_id=${sticker.file_id}`).then(res => res.json()) | |
const file = Buffer.from(await fetch(`${file_baseurl}/${metadata.result.file_path}`).then(res => res.arrayBuffer())) | |
console.log(file.length) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.log("don't exit the loop"); | |
} | |
}) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} else process.stdout.write(error.cause.code + ' - ') | |
console.error("don't exit the program") | |
} | |
}) | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const stickerName = ['Elaina_san', 'bagasgoyang'] | |
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs" | |
const domain = "https://api.telegram.org" | |
const file_baseurl = `${domain}/file/${token}` | |
const api_baseurl = `${domain}/${token}` | |
for (const key in stickerName) { | |
try { | |
const name = stickerName[key] | |
const files = await fetch(`${api_baseurl}/getStickerSet?name=${name}`).then(res => res.json()) | |
for (const key in files.result.stickers) { | |
try { | |
const sticker = files.result.stickers[key] | |
const metadata = await fetch(`${api_baseurl}/getFile?file_id=${sticker.file_id}`).then(res => res.json()) | |
const file = Buffer.from(await fetch(`${file_baseurl}/${metadata.result.file_path}`).then(res => res.arrayBuffer())) | |
console.log(file.length) | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} | |
else process.stdout.write(error.cause.code + ' - ') | |
console.log("don't exit the loop"); | |
} | |
} | |
} catch (error) { | |
if (error.name == 'AggregateError') { | |
for (let index = 0; index < error.errors.length; index++) { | |
process.stdout.write(error.errors[i].cause.code + ' - ') | |
} | |
} | |
else process.stdout.write(error.cause.code + ' - ') | |
console.error("don't exit the program") | |
} | |
} | |
})() |
network condition
not sure if this information helpful for nodejs/undici#2777
speedtest output
Speedtest by Ookla
Server: Dihostingin.com - Bogor (id: 55042)
ISP: Biznet Networks
Idle Latency: 8.95 ms (jitter: 1.48ms, low: 8.64ms, high: 11.58ms)
Download: 2.61 MB/s (data used: 34.7 MB)
363.84 ms (jitter: 73.94ms, low: 60.87ms, high: 1226.97ms)
Upload: 2.08 MB/s (data used: 26.3 MB)
58.50 ms (jitter: 26.97ms, low: 12.97ms, high: 496.04ms)
Packet Loss: 0.7%
case closed nodejs/undici#2777 (comment)
code
const { Agent } = require('undici');
(async () => {
const stickerName = ['Elaina_san', 'bagasgoyang']
const token = "bot7003873933:AAFKl0LwWViMJIA34-qjbTh7nZwcNQr2hFs"
const domain = "https://api.telegram.org"
const file_baseurl = `${domain}/file/${token}`
const api_baseurl = `${domain}/${token}`
stickerName.forEach(async (name) => {
try {
const files = await fetch(`${api_baseurl}/getStickerSet?name=${name}`, { dispatcher: new Agent({ connectTimeout: 60e3 }) }).then(res => res.json())
files.result.stickers.forEach(async (sticker) => {
try {
const metadata = await fetch(`${api_baseurl}/getFile?file_id=${sticker.file_id}`,{ dispatcher: new Agent({ connectTimeout: 60e3 }) }).then(res => res.json())
const file = Buffer.from(await fetch(`${file_baseurl}/${metadata.result.file_path}`,{ dispatcher: new Agent({ connectTimeout: 60e3 }) }).then(res => res.arrayBuffer()))
console.log(file.length)
} catch (error) {
if (error.name == 'AggregateError') {
process.stdout.write(error.errors[0].cause.code + ' - ')
} else process.stdout.write(error.cause.code + ' - ')
console.log("don't exit the loop");
}
})
} catch (error) {
if (error.name == 'AggregateError') {
process.stdout.write(error.errors[0].cause.code + ' - ')
} else process.stdout.write(error.cause.code + ' - ')
console.error("don't exit the program")
}
})
})()
result
$ time node fetch.js
9270
36806
26200
10660
58244
10230
27646
16030
20066
26092
20302
29818
129980
19674
12072
39342
30186
32422
33732
22022
18712
24086
10594
12314
90031
20262
20910
29864
24750
22916
21744
59916
18512
15612
22148
10678
18790
11736
11226
9240
14288
32406
30086
36287
31766
22156
19112
10342
30796
31796
36254
23358
145874
21420
22418
137983
28638
19280
36624
15622
19480
30458
17894
33626
19606
44675
19806
254152
15762
13160
12588
27038
26861
100291
38702
10896
15120
28874
36701
32358
18198
9160
31404
93881
184388
182587
14612
18666
25712
15930
30846
21672
29862
25904
24726
17756
48150
35103
21398
29522
70790
108254
160240
20184
29688
81961
27640
199542
23404
163021
23140
22640
22766
25690
21146
15794
32512
17826
41429
30064
26880
15512
143223
31564
28646
12718
83572
27066
90918
13260
125909
19922
25318
14444
23834
75084
108253
18692
33452
244773
15380
36920
30949
29430
99452
107692
21650
239082
19172
83520
8678
161005
41089
23078
246160
22914
21536
22978
188485
19914
14902
30952
42998
7550
55596
121670
148411
26246
10464
15530
real 0m32.852s
user 0m6.576s
sys 0m0.632s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
environment
test result - curl vs fetch
curl sample
$ time node curl.js 30949 33452 26861 30064 44675 42998 26246 22640 41429 36287 81961 26200 35103 30949 48150 70790 33626 93881 163021 10678 8678 33452 9160 100291 41089 58244 108254 75084 145874 55596 36920 30952 31404 32406 29864 30458 14612 24750 14444 21744 13260 39342 83572 22766 22418 22148 20262 25904 25712 12588 21672 32512 10660 22156 20066 23358 27646 21650 19674 20184 12718 21398 21146 19914 15512 13160 29522 23140 10464 22022 19480 15380 15120 26092 160240 90031 188485 48150 145874 143223 83520 137983 129980 75084 90918 161005 199542 148411 41089 148411 121670 125909 108253 83572 59916 44675 254152 246160 184388 239082 17894 99452 18666 36701 129980 21420 19922 18512 28874 18692 125909 19112 17826 22914 29862 26861 9270 7550 10896 10230 11736 9240 11226 14288 12072 10342 15530 15622 12314 25318 15930 18198 10594 184388 161005 108253 107692 244773 121670 188485 81961 32358 246160 182587 83520 58244 42998 199542 41429 33626 36287 26246 137983 36806 32422 90918 21536 20302 36624 29430 29818 30846 28646 25690 19606 23834 30796 182587 93881 22978 163021 70790 99452 59916 100291 30064 107692 22640 160240 18712 143223 90031 55596 108254 22916 27038 15612 23078 36254 26200 24726 33732 28638 14902 30186 15762 16030 29688 23404 20910 19806 17756 15794 38702 31766 30086 19280 24086 27640 26880 19172 31796 27066 35103 18790 36701 31564 254152 239082 244773 7550 36254 32406 18712 16030 23078 39342 36806 15612 30458 29688 22916 21744 18790 19806 30952 20302 27038 17756 32422 20910 31404 10230 31766 10896 21398 25904 25712 29864 27640 24750 32512 21146 10660 24726 12588 10342 38702 13260 23358 33732 28646 29818 30846 15530 15930 10678 29522 10464 22156 21672 10594 12314 30086 22766 14444 15794 15762 12072 19922 22022 29430 31796 19674 22148 15622 27066 9160 21650 36624 15512 17894 28638 11226 25318 25690 19112 20066 8678 9240 7550 20262 32358 26880 29862 21420 9270 8678 19606 10342 9160 18666 30949 14612 22914 26092 26861 15120 21536 14902 27646 10230 17826 29688 35103 26200 30064 9240 21536 21744 10678 10594 18712 11736 12588 12072 21146 15762 15530 29862 25712 10464 15120 16030 25318 39342 17894 28646 19172 33732 18666 18512 19480 19922 24750 27066 20184 30796 36920 23404 18692 15380 44675 20262 36624 19674 27640 17756 32358 10896 21420 22978 25904 20302 33626 23834 31766 29522 14902 30186 14612 15612 28874 12314 23404 31404 18198 32512 26092 26880 15930 11736 27646 20066 19112 15622 21672 15794 31564 23358 13160 14288 15380 17826 9270 30186 18198 108253 23140 19280 10660 20184 29818 22418 19606 21398 31796 24086 41429 13260 11226 14444 24726 33452 23834 12718 28874 22978 13160 18692 19172 36701 19480 31564 22914 18512 246160 36287 24086 19914 239082 137983 30846 22148 19914 48150 26246 22640 22916 58244 30458 27038 55596 32406 36806 23078 32422 20910 38702 22766 22156 22418 29430 23140 19280 22022 14288 28638 30086 83572 184388 93881 81961 108254 59916 41089 70790 42998 100291 36254 143223 254152 29864 182587 125909 163021 12718 160240 199542 90031 75084 90918 129980 148411 107692 30952 161005 15512 30796 188485 244773 145874 18790 25690 21650 121670 83520 99452 36920 19806 real 0m35.988s user 0m52.009s sys 0m18.063s
fetch sample