Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
Make context a WeakReference
Browse files Browse the repository at this point in the history
  • Loading branch information
HaarigerHarald committed Sep 8, 2018
1 parent ef1afa1 commit 10ef9d2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions youtubeExtractor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ dependencies {
implementation('com.github.evgenyneu:js-evaluator-for-android:v4.0.0') {
exclude module: 'appcompat-v7'
}
implementation 'com.android.support:support-annotations:27.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.SparseArray;

Expand All @@ -19,6 +20,7 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
Expand All @@ -40,12 +42,13 @@ public abstract class YouTubeExtractor extends AsyncTask<String, Void, SparseArr
private final static String CACHE_FILE_NAME = "decipher_js_funct";
private final static int DASH_PARSE_RETRIES = 5;

private Context context;
private WeakReference<Context> refContext;
private String videoID;
private VideoMeta videoMeta;
private boolean includeWebM = true;
private boolean useHttp = false;
private boolean parseDashManifest = false;
private String cacheDirPath;

private volatile String decipheredSignature;

Expand Down Expand Up @@ -84,7 +87,7 @@ public abstract class YouTubeExtractor extends AsyncTask<String, Void, SparseArr
private static final Pattern patFunction = Pattern.compile("([{; =])([a-zA-Z$_][a-zA-Z0-9$]{0,2})\\(");
private static final Pattern patDecryptionJsFile = Pattern.compile("jsbin\\\\/(player-(.+?).js)");
private static final Pattern patSignatureDecFunction = Pattern.compile("\"signature\",(.{1,3}?)\\(.{1,10}?\\)");

private static final SparseArray<Format> FORMAT_MAP = new SparseArray<>();

static {
Expand Down Expand Up @@ -146,8 +149,9 @@ public abstract class YouTubeExtractor extends AsyncTask<String, Void, SparseArr
FORMAT_MAP.put(96, new Format(96, "mp4", 1080 ,Format.VCodec.H264, Format.ACodec.AAC, 256, false, true));
}

public YouTubeExtractor(Context con) {
context = con;
public YouTubeExtractor(@NonNull Context con) {
refContext = new WeakReference<>(con);
cacheDirPath = con.getCacheDir().getAbsolutePath();
}

@Override
Expand Down Expand Up @@ -635,25 +639,23 @@ private void parseVideoMeta(String getVideoInfo) throws UnsupportedEncodingExcep
}

private void readDecipherFunctFromCache() {
if (context != null) {
File cacheFile = new File(context.getCacheDir().getAbsolutePath() + "/" + CACHE_FILE_NAME);
// The cached functions are valid for 2 weeks
if (cacheFile.exists() && (System.currentTimeMillis() - cacheFile.lastModified()) < 1209600000) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(cacheFile), "UTF-8"));
decipherJsFileName = reader.readLine();
decipherFunctionName = reader.readLine();
decipherFunctions = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
File cacheFile = new File(cacheDirPath + "/" + CACHE_FILE_NAME);
// The cached functions are valid for 2 weeks
if (cacheFile.exists() && (System.currentTimeMillis() - cacheFile.lastModified()) < 1209600000) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(cacheFile), "UTF-8"));
decipherJsFileName = reader.readLine();
decipherFunctionName = reader.readLine();
decipherFunctions = reader.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Expand Down Expand Up @@ -688,30 +690,30 @@ public void setDefaultHttpProtocol(boolean useHttp) {
}

private void writeDeciperFunctToChache() {
if (context != null) {
File cacheFile = new File(context.getCacheDir().getAbsolutePath() + "/" + CACHE_FILE_NAME);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "UTF-8"));
writer.write(decipherJsFileName + "\n");
writer.write(decipherFunctionName + "\n");
writer.write(decipherFunctions);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
File cacheFile = new File(cacheDirPath + "/" + CACHE_FILE_NAME);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(cacheFile), "UTF-8"));
writer.write(decipherJsFileName + "\n");
writer.write(decipherFunctionName + "\n");
writer.write(decipherFunctions);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private void decipherViaWebView(final SparseArray<String> encSignatures) {
if (context == null) {
final Context context = refContext.get();
if (context == null)
{
return;
}

Expand Down

0 comments on commit 10ef9d2

Please sign in to comment.