From c6a71195ad7172aaf25354beede20e938b98f14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aliberk=20Sand=C4=B1k=C3=A7=C4=B1?= Date: Fri, 27 Dec 2024 12:27:42 +0300 Subject: [PATCH] WIP: yt-login implementation - Merged manually from PipePipe (https://codeberg.org/NullPointerException/PipePipeClient/commit/f0cb4e33ab8f46d23642137b47ab5436296c7a9f#diff-f57d0ffc8afaa16442bbe82ca85dd5c8b7c8f120) --- app/src/main/AndroidManifest.xml | 4 + .../YouTubeAccountSettingsFragment.java | 52 ++++++++++++ .../schabi/newpipe/util/ServiceHelper.java | 6 ++ .../views/YouTubeLoginWebViewActivity.java | 82 +++++++++++++++++++ app/src/main/res/values/settings_keys.xml | 5 ++ app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/account_settings.xml | 10 +++ .../main/res/xml/account_settings_youtube.xml | 30 +++++++ 8 files changed, 191 insertions(+) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/YouTubeAccountSettingsFragment.java create mode 100644 app/src/main/java/org/schabi/newpipe/views/YouTubeLoginWebViewActivity.java create mode 100644 app/src/main/res/xml/account_settings.xml create mode 100644 app/src/main/res/xml/account_settings_youtube.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3035b3a19a..c0d54bb8f6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -134,6 +134,10 @@ android:exported="false" android:label="@string/recaptcha" /> + + { + // Open a webview to login and then get cookies + // and save them to the shared preferences + Intent intent = new Intent(this.getContext(), YouTubeLoginWebViewActivity.class); + startActivityForResult(intent, REQUEST_LOGIN); + return true; + }); + logout.setOnPreferenceClickListener(preference -> { + // Clear cookies + defaultPreferences.edit().putString(getString(R.string.youtube_cookies_key), "").apply(); +// defaultPreferences.edit().putString(getString(R.string.youtube_po_token), "").apply(); + return true; + }); + if (defaultPreferences.getString(getString(R.string.youtube_cookies_key), "").equals("")) { + logout.setEnabled(false); + } else { + login.setEnabled(false); + } + +// Preference override_cookies_niconico_value = findPreference(getString(R.string.override_cookies_youtube_value_key)); +// override_cookies_niconico_value.setEnabled(defaultPreferences.getBoolean(getString(R.string.override_cookies_youtube_key), false)); + } + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == REQUEST_LOGIN && resultCode == RESULT_OK) { + String cookies = data.getStringExtra("cookies"); + String pot = data.getStringExtra("pot"); + // save cookies to shared preferences + defaultPreferences.edit().putString(getString(R.string.youtube_cookies_key), cookies).apply(); +// defaultPreferences.edit().putString(getString(R.string.youtube_po_token), pot).apply(); + } + } + +} diff --git a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java index c712157b35..d8e9b83d50 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java @@ -202,6 +202,12 @@ public static void initService(final Context context, final int serviceId) { final String url = jsonObject.getString("url"); final PeertubeInstance instance = new PeertubeInstance(url, name); ServiceList.PeerTube.setInstance(instance); + } else if (serviceId == ServiceList.YouTube.getServiceId()) { + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(context); + final String tokens = sharedPreferences.getString(context.getString( + R.string.youtube_cookies_key), null); + ServiceList.YouTube.setTokens(tokens); } } diff --git a/app/src/main/java/org/schabi/newpipe/views/YouTubeLoginWebViewActivity.java b/app/src/main/java/org/schabi/newpipe/views/YouTubeLoginWebViewActivity.java new file mode 100644 index 0000000000..1bfb67b45e --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/views/YouTubeLoginWebViewActivity.java @@ -0,0 +1,82 @@ +package org.schabi.newpipe.views; + +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.webkit.*; +import androidx.appcompat.app.AppCompatActivity; +import org.schabi.newpipe.R; + +public class YouTubeLoginWebViewActivity extends AppCompatActivity { + WebView webView; + String cookies; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.login_webview); + + WebView webView = findViewById(R.id.login_webview); + this.webView = webView; + WebSettings webSettings = webView.getSettings(); + webSettings.setJavaScriptEnabled(true); + webSettings.setUserAgentString("Mozilla/5.0 (Linux; Android 10; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36"); + + webView.setWebViewClient(new MyWebViewClient()); + webView.loadUrl("https://www.youtube.com/signin"); + } + + private class MyWebViewClient extends WebViewClient { + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + if (url.equals("https://m.youtube.com/?noapp=1") || url.equals("https://m.youtube.com/")) { + setCookies(CookieManager.getInstance().getCookie(url)); + webView.loadUrl("https://music.youtube.com/watch?v=09839DpTctU"); + } + } + + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + String url = request.getUrl().toString(); + // Filter specific requests + if (url.contains("googlevideo.com/videoplayback")) { + Uri uri = Uri.parse(url); + String pot = uri.getQueryParameter("pot"); + Intent intent = new Intent(); + intent.putExtra("cookies", cookies); + intent.putExtra("pot", pot); + setResult(RESULT_OK, intent); + finish(); + } + // Return null to allow the WebView to load the request as usual + return null; + } + } + +// private void showNonDismissableDialog() { +// AlertDialog.Builder builder = new AlertDialog.Builder(this); +// builder.setTitle(getString(R.string.continue_title)); +// builder.setMessage(getString(R.string.youtube_login_instruction)); +// builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { +// @Override +// public void onClick(DialogInterface dialog, int which) { +// webView.loadUrl("https://music.youtube.com/watch?v=09839DpTctU"); +// } +// }); +// AlertDialog dialog = builder.create(); +// dialog.setCancelable(false); // This makes the dialog non-dismissable +// dialog.setCanceledOnTouchOutside(false); // Prevents dismissal when touching outside +// dialog.show(); +// } + + public void setCookies(String cookies) { + this.cookies = cookies; + } + + public String getCookies() { + return cookies; + } +} diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 6858e5d62f..c2ccc06a0d 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -1526,4 +1526,9 @@ return_youtube_dislike_override_like_count return_youtube_dislike_override_view_count return_youtube_dislike_show_dislikes_as_percentage + + + youtube_cookies_key + override_cookies_youtube_key + override_cookies_youtube_value_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0f862ef524..8a279c09b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -978,4 +978,6 @@ ReturnYouTubeDislike Security FAQ https://returnyoutubedislike.com/ https://www.returnyoutubedislike.com/faq/ + + YouTube Account diff --git a/app/src/main/res/xml/account_settings.xml b/app/src/main/res/xml/account_settings.xml new file mode 100644 index 0000000000..95dc8e8d68 --- /dev/null +++ b/app/src/main/res/xml/account_settings.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/xml/account_settings_youtube.xml b/app/src/main/res/xml/account_settings_youtube.xml new file mode 100644 index 0000000000..464b6fbe47 --- /dev/null +++ b/app/src/main/res/xml/account_settings_youtube.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + +