Last active
August 29, 2015 14:05
-
-
Save jaredrummler/8cb3bdd846a81736c29e to your computer and use it in GitHub Desktop.
Grab the direct link from mediafire on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Jared Rummler <jared@jrummyapps.com> | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.jrummyapps.utils.webview; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.webkit.JsResult; | |
import android.webkit.WebChromeClient; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
/** | |
* Loads a mediafire URL in a {@link WebView} and then gets the direct download link. | |
* <br><br>Example usage: | |
* <pre> | |
* <code>final String url = "http://www.mediafire.com/download/v24hhydvijc7o0c/test.zip"; | |
* | |
* new MediaFireLoader(context).load(url, new MediaFireLoader.Callback() { | |
* | |
* {@literal @}Override | |
* public void onFailed() { | |
* // The url loaded but no download link was found. | |
* } | |
* | |
* {@literal @}Override | |
* public void onDownloadLinkFound(String downloadLink) { | |
* // We successfully found the direct download link. | |
* } | |
* }); | |
* </code> | |
* </pre> | |
* | |
* @author Jared Rummler | |
*/ | |
public class MediaFireLoader { | |
private static final String MESSAGE_NO_DOWNLOAD_LINK = "NO_DOWNLOAD_LINK_FOUND"; | |
/** | |
* Callback that gets invoked when the URL is done loading in the {@link WebView} | |
*/ | |
public interface Callback { | |
/** | |
* | |
* @param downloadLink The value of href for the Download button that usually shows on mediafire. | |
*/ | |
void onDownloadLinkFound(String downloadLink); | |
/** | |
* Called when no download link was found | |
*/ | |
void onFailed(); | |
} | |
private Context mContext; | |
public MediaFireLoader(Context context) { | |
mContext = context; | |
} | |
/** | |
* Load a mediafire URL in a {@link WebView} and attempt to retrieve the download link. | |
* | |
* @param url A URL to a file hosted on https://www.mediafire.com/ | |
* @param callback The {@link Callback} that gets invoked after the page is loaded. | |
*/ | |
@SuppressLint("SetJavaScriptEnabled") | |
public void load(final String url, final Callback callback) { | |
final WebView webView = new WebView(mContext); | |
webView.getSettings().setJavaScriptEnabled(true); | |
// Allow the WebView to show alerts and handle those alerts | |
webView.setWebChromeClient(new WebChromeClient() { | |
@Override | |
public boolean onJsAlert(WebView view, String url, String message, JsResult result) { | |
result.confirm(); | |
if (message.equals(MESSAGE_NO_DOWNLOAD_LINK)) { | |
callback.onFailed(); | |
return true; | |
} | |
callback.onDownloadLinkFound(message); | |
return true; | |
} | |
}); | |
// WebViewClient must be set before calling loadUrl to inject JavaScript | |
webView.setWebViewClient(new WebViewClient() { | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
webView.loadUrl("javascript:(function() { " | |
+ " var v = document.getElementsByClassName(\"download_link\");" | |
+ " if (v.length > 0) {" | |
+ " var a = v[0].getElementsByTagName(\"a\");" | |
+ " if (a.length > 0) {" | |
+ " var link = a[0];" | |
+ " alert(link.href);" // Send the download link as an alert | |
+ " return;" | |
+ " }" | |
+ " }" | |
+ " alert(\"" + MESSAGE_NO_DOWNLOAD_LINK + "\");" | |
+ "})()"); | |
} | |
}); | |
webView.loadUrl(url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment