1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-17 15:24:55 +00:00

ageRestrictedContent cookie only sent for youtube

Now the age restricted content cookie is only sent when sending a
request to youtube. There's no need to remove the cookie when the
service changes because whether to add the cookie is determined by
looking at the url the request is being sent to.
This commit is contained in:
Vincent Nagel 2020-03-15 20:04:34 -05:00
parent de4d6037d3
commit 430d4e1ccd
3 changed files with 29 additions and 17 deletions

View File

@ -7,14 +7,12 @@ import android.preference.PreferenceManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request; import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.util.CookieUtils; import org.schabi.newpipe.util.CookieUtils;
import org.schabi.newpipe.util.InfoCache; import org.schabi.newpipe.util.InfoCache;
import org.schabi.newpipe.util.ServiceHelper;
import org.schabi.newpipe.util.TLSSocketFactoryCompat; import org.schabi.newpipe.util.TLSSocketFactoryCompat;
import java.io.IOException; import java.io.IOException;
@ -49,6 +47,7 @@ public final class DownloaderImpl extends Downloader {
public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY = "youtube_age_restricted_content_cookie_key"; public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY = "youtube_age_restricted_content_cookie_key";
public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE = "PREF=f2=8000000"; public static final String YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE = "PREF=f2=8000000";
public static final String YOUTUBE_DOMAIN = "youtube.com";
private static DownloaderImpl instance; private static DownloaderImpl instance;
private Map<String, String> mCookies; private Map<String, String> mCookies;
@ -131,31 +130,42 @@ public final class DownloaderImpl extends Downloader {
} }
} }
public String getCookies() { public String getCookies(final String url) {
return CookieUtils.concatCookies(mCookies.values()); List<String> resultCookies = new ArrayList<>();
if (url.contains(YOUTUBE_DOMAIN)) {
String youtubeCookie = getCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY);
if (youtubeCookie != null) {
resultCookies.add(youtubeCookie);
}
}
// Recaptcha cookie is always added TODO: not sure if this is necessary
String recaptchaCookie = getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY);
if (recaptchaCookie != null) {
resultCookies.add(recaptchaCookie);
}
return CookieUtils.concatCookies(resultCookies);
} }
public String getCookie(final String key){ public String getCookie(final String key){
return mCookies.get(key); return mCookies.get(key);
} }
public void setCookie(final String key, final String value){ public void setCookie(final String key, final String cookie){
mCookies.put(key, value); mCookies.put(key, cookie);
} }
public void removeCookie(final String key){ public void removeCookie(final String key){
mCookies.remove(key); mCookies.remove(key);
} }
public void updateAgeRestrictedContentCookies(Context context){ public void updateAgeRestrictedContentCookies(final Context context){
String showAgeRestrictedContentKey = context.getString(R.string.show_age_restricted_content); String showAgeRestrictedContentKey = context.getString(R.string.show_age_restricted_content);
int currentServiceId = ServiceHelper.getSelectedServiceId(context);
boolean showAgeRestrictedContent = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(showAgeRestrictedContentKey, false); boolean showAgeRestrictedContent = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(showAgeRestrictedContentKey, false);
updateAgeRestrictedContentCookies(currentServiceId, showAgeRestrictedContent); updateAgeRestrictedContentCookies(showAgeRestrictedContent);
} }
public void updateAgeRestrictedContentCookies(int currentServiceId, boolean showAgeRestrictedContent) { public void updateAgeRestrictedContentCookies(boolean showAgeRestrictedContent) {
if (currentServiceId == ServiceList.YouTube.getServiceId() && !showAgeRestrictedContent) { if (!showAgeRestrictedContent) {
setCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY, YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE); setCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY, YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE);
} else { } else {
removeCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY); removeCookie(YOUTUBE_AGE_RESTRICTED_CONTENT_COOKIE_KEY);
@ -186,8 +196,9 @@ public final class DownloaderImpl extends Downloader {
.method("GET", null).url(siteUrl) .method("GET", null).url(siteUrl)
.addHeader("User-Agent", USER_AGENT); .addHeader("User-Agent", USER_AGENT);
if (!mCookies.isEmpty()) { String cookies = getCookies(siteUrl);
requestBuilder.addHeader("Cookie", getCookies()); if (!cookies.isEmpty()) {
requestBuilder.addHeader("Cookie", cookies);
} }
final okhttp3.Request request = requestBuilder.build(); final okhttp3.Request request = requestBuilder.build();
@ -226,8 +237,9 @@ public final class DownloaderImpl extends Downloader {
.method(httpMethod, requestBody).url(url) .method(httpMethod, requestBody).url(url)
.addHeader("User-Agent", USER_AGENT); .addHeader("User-Agent", USER_AGENT);
if (!mCookies.isEmpty()) { String cookies = getCookies(url);
requestBuilder.addHeader("Cookie", getCookies()); if (!cookies.isEmpty()) {
requestBuilder.addHeader("Cookie", cookies);
} }
for (Map.Entry<String, List<String>> pair : headers.entrySet()) { for (Map.Entry<String, List<String>> pair : headers.entrySet()) {

View File

@ -242,9 +242,9 @@ public class MainActivity extends AppCompatActivity {
drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)) drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this))
.setChecked(false); .setChecked(false);
ServiceHelper.setSelectedServiceId(this, item.getItemId()); ServiceHelper.setSelectedServiceId(this, item.getItemId());
drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)).setChecked(true);
drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this)) drawerItems.getMenu().getItem(ServiceHelper.getSelectedServiceId(this))
.setChecked(true); .setChecked(true);
DownloaderImpl.getInstance().updateAgeRestrictedContentCookies(getApplicationContext());
} }
private void tabSelected(final MenuItem item) throws ExtractionException { private void tabSelected(final MenuItem item) throws ExtractionException {

View File

@ -92,7 +92,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
if (preference.getKey().equals(showAgeRestrictedContentKey)) { if (preference.getKey().equals(showAgeRestrictedContentKey)) {
Context context = getContext(); Context context = getContext();
if(context != null){ if (context != null) {
DownloaderImpl.getInstance().updateAgeRestrictedContentCookies(context); DownloaderImpl.getInstance().updateAgeRestrictedContentCookies(context);
} }
} }