void main () { var session = new Soup.Session (); initialize_user_agent (session); var message = new Soup.Message ("GET", "https://www.youtube.com/watch?v=g2cF4-tHvqI"); session.send_message (message); var youtube_response = (string) message.response_body.data; var regex = /ytInitialPlayerResponse\s*=\s*({.+?})\s*;/; MatchInfo info; regex.match (youtube_response, 0, out info); var json_parser = new Json.Parser (); json_parser.load_from_data ( info.fetch (1), -1 ); var yt_initial_player_response = json_parser.get_root ().get_object (); var streaming_data = yt_initial_player_response.get_object_member("streamingData"); var adaptive_formats = streaming_data.get_array_member("formats"); adaptive_formats.foreach_element((format) => { print ("%lld\n", format.get_object_element (0).get_int_member ("itag")); }); // print ("%s\n", info.fetch (1)); } void initialize_user_agent (Soup.Session session) { var cookie_jar = new Soup.CookieJar (); session.add_feature (cookie_jar); do_main_page_youtube_request (session); if (!has_consent (cookie_jar)) { print ("No consent found\n"); consent (cookie_jar); } } void consent (Soup.CookieJar cookie_jar) { var youtube_uri = new Soup.URI ("https://youtube.com"); var cookies = cookie_jar.get_cookie_list (youtube_uri, false); unowned var cookie = cookies.search ("CONSENT", (cookie, name) => { return cookie.name == name ? 0 : 1; }); if (cookie == null || is_consent_yes (cookie.data)) return; var consent_id = extract_consent_id (cookie.data); var new_cookie_value = "YES+cb.20210328-17-p0.en+FX+" + consent_id; cookie_jar.delete_cookie (cookie.data); cookie_jar.add_cookie ( new Soup.Cookie ("CONSENT", new_cookie_value, ".youtube.com", "/", -1)); } string extract_consent_id (Soup.Cookie cookie) { var regex = /^PENDING\+(\d+)/; MatchInfo info; regex.match (cookie.@value, 0, out info); return info.fetch (1); } bool is_consent_yes (Soup.Cookie cookie) { var regex = /YES/; return regex.match (cookie.@value); } bool has_consent (Soup.CookieJar cookie_jar) { var youtube_uri = new Soup.URI ("https://youtube.com"); var cookies = cookie_jar.get_cookie_list (youtube_uri, false); foreach (var cookie in cookies) { if (cookie.name == "__Secure-3PSID") return true; } return false; } void do_main_page_youtube_request (Soup.Session session) { var message = new Soup.Message ("GET", "https://www.youtube.com"); session.send_message (message); }