Skip to content

Instantly share code, notes, and snippets.

@RealYukiSan
Last active April 11, 2024 12:34
Show Gist options
  • Save RealYukiSan/b07dd0463417f8ba03f297550edb750e to your computer and use it in GitHub Desktop.
Save RealYukiSan/b07dd0463417f8ba03f297550edb750e to your computer and use it in GitHub Desktop.
fetch telegram sticker
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);
});
});
}
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")
}
})
}
})()
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")
}
})
}
})()
(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")
}
})
})()
(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")
}
}
})()
@RealYukiSan
Copy link
Author

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%

@RealYukiSan
Copy link
Author

RealYukiSan commented Apr 11, 2024

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