tlang-c/examples/youtube-downloader.tlang

109 lines
2.6 KiB
Plaintext
Raw Permalink Normal View History

2023-05-10 11:18:40 +00:00
func stream_yt_json(id)
{
ret "{\"videoId\": \""+ id + "\",\"context\": {\"client\": {\"clientName\": \"ANDROID\",\"clientVersion\": \"17.10.35\",\"androidSdkVersion\": 30,\"hl\": \"en\",\"gl\": \"US\",\"utcOffsetMinutes\": 0}}}";
}
func tytd_streams_get(id)
{
streams = create.dict();
streams.success = false;
str = stream_yt_json(id);
curl = net.curl();
curl.method = "POST";
//curl.url = "http://127.0.0.1:9999/";
curl.url = "https://www.youtube.com/youtubei/v1/player?key=AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w";
jsonStr="";
curl.followlocation = true;
headers = create.array();
headers.add("Connection: close");
headers.add("Content-Type: application/json");
headers.add("User-Agent: com.google.android.youtube/17.10.35 (Linux; U; Android 12; GB) gzip");
headers.add("Cookie: CONSENT=YES+cb; YSC=DwKYllHNwuw");
curl.requestheaders = headers;
curl.postfields = str;
console.writeln(str);
curl.writefunction = func(buffer,length) {
for(i = 0;i<length;i++)
{
c=buffer[i].toChar();
d=c;
if(typeof(d) == "char")
{
jsonStr += d;
}
}
}
if(curl.perform() == 0)
{
streams.data = json.decode(jsonStr);
streams.success =true;
}
ret streams;
}
func tytd_title(id)
{
curl = net.curl();
curl.url = "https://www.youtube.com/watch?v=" + id;
htmlStr = "";
curl.followlocation = true;
curl.writefunction = func(buffer,length) {
for(i = 0;i<length;i++)
{
byte=buffer[i];
htmlStr += byte.toChar();
}
}
if(curl.perform() == 0)
{
a= htmlStr.split("<title>",false,2);
a2 = a[1];
a3 = a2.split("</title>",false,2);
ret a3[0];
}
ret "Unknown";
}
func download_video(id)
{
title = tytd_title(id);
console.writeln("Downloading: " + title);
strms=tytd_streams_get(id);
if(strms.success && strms.data)
{
each(strms.data.streamingData.formats)
{
if(item.itag == 22)
{
fileName = title + "-" + id + ".mp4";
curl = net.curl();
curl.url = item.url;
f = fs.create(fileName);
curl.writefunction = f.write;
curl.perform();
f.close();
console.writeln("Downloaded: " + title);
}
}
}
}
func main($$args)
{
each(args)
{
download_video(item);
}
}