From 36abb297c2e9f6b74c7ec9f9c4199f92d3b2c3cd Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sat, 25 Sep 2021 10:27:10 +0200 Subject: [PATCH] Initial commit. --- meson.build | 13 ++++++++ src/main.vala | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 meson.build create mode 100755 src/main.vala diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..533fa2f --- /dev/null +++ b/meson.build @@ -0,0 +1,13 @@ +project('vala app', 'vala', 'c') + +dependencies = [ + dependency('glib-2.0'), + dependency('json-glib-1.0'), + dependency('gobject-2.0'), + dependency('libsoup-2.4'), +] + + +sources = files('src/main.vala') + +executable('youtube-extractor', sources, dependencies: dependencies) diff --git a/src/main.vala b/src/main.vala new file mode 100755 index 0000000..a64bf27 --- /dev/null +++ b/src/main.vala @@ -0,0 +1,86 @@ +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); +}