Skip to content

Commit

Permalink
WIP: yt-login implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
asandikci committed Dec 27, 2024
1 parent 73cf490 commit c6a7119
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
android:exported="false"
android:label="@string/recaptcha" />

<activity android:name=".views.YouTubeLoginWebViewActivity"
android:configChanges="orientation|screenSize"
android:theme="@style/Theme.AppCompat.NoActionBar" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.schabi.newpipe.settings;

import android.content.Intent;
import android.os.Bundle;
import androidx.preference.Preference;
import org.schabi.newpipe.R;
import org.schabi.newpipe.views.YouTubeLoginWebViewActivity;

import static android.app.Activity.RESULT_OK;

public class YouTubeAccountSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_LOGIN = 1;

@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
addPreferencesFromResource(R.xml.account_settings_youtube);
Preference login = findPreference(getString(R.string.login_key));
Preference logout = findPreference(getString(R.string.logout_key));
login.setOnPreferenceClickListener(preference -> {
// 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();
}
}

}
6 changes: 6 additions & 0 deletions app/src/main/java/org/schabi/newpipe/util/ServiceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,9 @@
<string name="return_youtube_dislike_override_like_count_key" translatable="false">return_youtube_dislike_override_like_count</string>
<string name="return_youtube_dislike_override_view_count_key" translatable="false">return_youtube_dislike_override_view_count</string>
<string name="return_youtube_dislike_show_dislikes_as_percentage_key" translatable="false">return_youtube_dislike_show_dislikes_as_percentage</string>

<!-- Merged from PipePipe -->
<string name="youtube_cookies_key">youtube_cookies_key</string>
<string name="override_cookies_youtube_key">override_cookies_youtube_key</string>
<string name="override_cookies_youtube_value_key">override_cookies_youtube_value_key</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -978,4 +978,6 @@
<string name="return_youtube_dislike_privacy_policy_text">ReturnYouTubeDislike Security FAQ</string>
<string name="return_youtube_dislike_home_page_url">https://returnyoutubedislike.com/</string>
<string name="return_youtube_dislike_security_faq_url">https://www.returnyoutubedislike.com/faq/</string>
<!-- Merged from PipePipe -->
<string name="settings_category_youtube_account_title">YouTube Account</string>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/account_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:key="general_preferences"
android:title="@string/settings">
<PreferenceScreen
android:fragment="org.schabi.newpipe.settings.YouTubeAccountSettingsFragment"
android:title="YouTube"
app:iconSpaceReserved="false" />
</PreferenceScreen>
30 changes: 30 additions & 0 deletions app/src/main/res/xml/account_settings_youtube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/settings_category_youtube_account_title">
<Preference
android:key="@string/login_key"
android:summary="@string/login_warning"
android:title="@string/login_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<Preference
android:key="@string/logout_key"
android:title="@string/logout_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<!-- <SwitchPreferenceCompat-->
<!-- android:defaultValue="false"-->
<!-- android:key="@string/override_cookies_youtube_key"-->
<!-- android:summary="@string/override_cookies_summary"-->
<!-- android:title="@string/override_cookies_title"-->
<!-- app:singleLineTitle="false"-->
<!-- app:iconSpaceReserved="false" />-->
<!-- <EditTextPreference-->
<!-- android:defaultValue=""-->
<!-- android:inputType="text"-->
<!-- android:key="@string/override_cookies_youtube_value_key"-->
<!-- android:title="@string/override_cookies_value_title"-->
<!-- app:singleLineTitle="false"-->
<!-- app:iconSpaceReserved="false" />-->
</PreferenceScreen>

0 comments on commit c6a7119

Please sign in to comment.