-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoundIt.cs
More file actions
89 lines (75 loc) · 2.95 KB
/
SoundIt.cs
File metadata and controls
89 lines (75 loc) · 2.95 KB
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
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text;
using Visitordata;
namespace Soundit;
public class SoundIt{
private const string PATTERN = @"(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})";
private string Youtube;
private string URL;
private Dictionary<string, dynamic> Payload;
public SoundIt(string url)
{
this.Payload = new Dictionary<string, dynamic>();
this.URL = url;
this.Youtube = @"https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8";
}
private async Task<JObject> MakeRequest(string id){
var jsonSer = JsonConvert.SerializeObject(this.Payload);
var data = new StringContent(jsonSer, Encoding.UTF8, "application/json");
using var client = new HttpClient();
var response = await client.PostAsync(this.Youtube, data);
var reader = await response.Content.ReadAsByteArrayAsync();
var result = Encoding.UTF8.GetString(reader, 0, reader.Length);
JObject content = JObject.Parse(result);
return content;
}
public async Task<string[]> Execute(){
string[] elements = new string[2];
if(string.IsNullOrEmpty(URL)) return elements;
string id = isCorrectPattern(URL);
if(string.IsNullOrEmpty(id)) return elements;
AddPayload(id);
string audio = string.Empty;
string videoName = string.Empty;
var content = await MakeRequest(id);
var res = content["streamingData"]["adaptiveFormats"].ToList();
foreach(var r in res){
if(r["audioQuality"] != null){
string temp = r["audioQuality"].ToString();
if(temp.Contains("AUDIO_QUALITY_MEDIUM")){
elements[0] = r["url"].ToString();
elements[1] = content["videoDetails"]["title"].ToString();
break;
}
}
}
return elements;
}
private string isCorrectPattern(string s){
Match m = Regex.Match(s, PATTERN);
if(m.Success) return m.Groups[1].Value;
return string.Empty;
}
private void AddPayload(string id){
this.Payload.Add("videoId", id);
var context = new Dictionary<string, Dictionary<string, dynamic>>(){
{"client", new Dictionary<string, dynamic>(){
{"clientName", "IOS"},
{"clientVersion", "19.45.4"},
{"androidSdkVersion", 30},
{"deviceMake", "Apple"},
{"deviceModel", "iPhone16,2"},
{"platform", "MOBILE"},
{"osName", "IOS"},
{"osVersion", "18.1.0.22B83"},
{"visitorData", VisitorData.Generate()}
}}
};
this.Payload.Add("context", context);
}
}