Resolve embedded URLs

This commit is contained in:
Thomas 2020-02-18 12:09:18 +01:00
parent f5728f9325
commit d013365aca
4 changed files with 59 additions and 36 deletions

View File

@ -55,7 +55,7 @@ public class TransformActivity extends Activity {
private Thread thread;
private String notShortnedURLDialog;
private ArrayList<String> notShortnedURLDialog;
final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)");
final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)");
final Pattern maps = Pattern.compile("/maps/place/[^@]+@([\\d.,z]{3,}).*");
@ -69,6 +69,7 @@ public class TransformActivity extends Activity {
finish();
return;
}
notShortnedURLDialog = new ArrayList<>();
assert intent != null;
//Dealing with URLs
if( Objects.requireNonNull(intent.getAction()).equals(Intent.ACTION_VIEW)){
@ -91,11 +92,11 @@ public class TransformActivity extends Activity {
unshortenAlertBuilder.setView(view);
unshortenAlertBuilder.setIcon(R.mipmap.ic_launcher);
unshortenAlertBuilder.setPositiveButton(R.string.open, (dialog, id) -> {
if( notShortnedURLDialog != null){
if( notShortnedURLDialog.size() > 0){
URL url_1;
String realHost = null;
try {
url_1 = new URL(notShortnedURLDialog);
url_1 = new URL(notShortnedURLDialog.get(notShortnedURLDialog.size()-1));
realHost = url_1.getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
@ -157,7 +158,7 @@ public class TransformActivity extends Activity {
}
}else {
Intent delegate = new Intent(Intent.ACTION_VIEW);
delegate.setData(Uri.parse(notShortnedURLDialog));
delegate.setData(Uri.parse(notShortnedURLDialog.get(notShortnedURLDialog.size()-1)));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
@ -178,17 +179,26 @@ public class TransformActivity extends Activity {
thread = new Thread() {
@Override
public void run() {
notShortnedURLDialog = Utils.checkUrl(url);
if( notShortnedURLDialog == null) {
notShortnedURLDialog = url;
}
notShortnedURLDialog = new ArrayList<>();
notShortnedURLDialog.add(url);
Utils.checkUrl(notShortnedURLDialog);
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
positiveButton.setEnabled(true);
String message = getString(R.string.try_to_redirect, url, notShortnedURLDialog);
StringBuilder message;
if( notShortnedURLDialog.size() <= 1 ) {
message = new StringBuilder(getString(R.string.the_app_failed_shortened));
}else {
message = new StringBuilder(getString(R.string.try_to_redirect, notShortnedURLDialog.get(0), notShortnedURLDialog.get(1)));
if( notShortnedURLDialog.size() > 2 ){
for(int i=2 ; i < notShortnedURLDialog.size(); i++){
message.append("\n\n").append(getString(R.string.try_to_redirect_again, notShortnedURLDialog.get(i)));
}
}
}
TextView indications = view.findViewById(R.id.indications);
RelativeLayout progress = view.findViewById(R.id.progress);
indications.setText(message);
indications.setText(message.toString());
indications.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
};
@ -483,15 +493,13 @@ public class TransformActivity extends Activity {
Thread thread = new Thread() {
@Override
public void run() {
String notShortnedURL = Utils.checkUrl(finalUrl);
if( notShortnedURL == null) {
notShortnedURL = finalUrl;
}
notShortnedURLDialog.add(finalUrl);
Utils.checkUrl(notShortnedURLDialog);
URL url_;
String host = null;
try {
url_ = new URL(notShortnedURL);
url_ = new URL(notShortnedURLDialog.get(notShortnedURLDialog.size()-1));
host = url_.getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
@ -501,8 +509,8 @@ public class TransformActivity extends Activity {
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
if(nitter_enabled && Arrays.asList(twitter_domains).contains(host)) {
String newUrlFinal = notShortnedURL;
Matcher matcher = nitterPattern.matcher(notShortnedURL);
Matcher matcher = nitterPattern.matcher(notShortnedURLDialog.get(notShortnedURLDialog.size()-1));
String newUrlFinal = notShortnedURLDialog.get(notShortnedURLDialog.size()-1);
while (matcher.find()) {
final String nitter_directory = matcher.group(2);
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
@ -515,8 +523,8 @@ public class TransformActivity extends Activity {
sendIntent.setType("text/plain");
startActivity(sendIntent);
}else if( invidious_enabled && Arrays.asList(youtube_domains).contains(host)) {
Matcher matcher = youtubePattern.matcher(notShortnedURL);
String newUrlFinal = notShortnedURL;
Matcher matcher = youtubePattern.matcher(notShortnedURLDialog.get(notShortnedURLDialog.size()-1));
String newUrlFinal = notShortnedURLDialog.get(notShortnedURLDialog.size()-1);
while (matcher.find()) {
final String youtubeId = matcher.group(3);
String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase();
@ -532,9 +540,9 @@ public class TransformActivity extends Activity {
sendIntent.putExtra(Intent.EXTRA_TEXT, newExtraText);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}else if( osm_enabled && notShortnedURL.contains("/maps/place/")) {
String newUrlFinal = notShortnedURL;
Matcher matcher = maps.matcher(notShortnedURL);
}else if( osm_enabled && notShortnedURLDialog.get(notShortnedURLDialog.size()-1).contains("/maps/place/")) {
String newUrlFinal = notShortnedURLDialog.get(notShortnedURLDialog.size()-1);
Matcher matcher = maps.matcher(notShortnedURLDialog.get(notShortnedURLDialog.size()-1));
while (matcher.find()) {
final String localization = matcher.group(1);
assert localization != null;
@ -558,7 +566,7 @@ public class TransformActivity extends Activity {
sendIntent.setType("text/plain");
startActivity(sendIntent);
}else{
String newExtraText = finalExtraText.replaceAll(Pattern.quote(finalUrl), Matcher.quoteReplacement(notShortnedURL));
String newExtraText = finalExtraText.replaceAll(Pattern.quote(finalUrl), Matcher.quoteReplacement(notShortnedURLDialog.get(notShortnedURLDialog.size()-1)));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, newExtraText);

View File

@ -13,8 +13,11 @@ package app.fedilab.nitterizeme;
*
* You should have received a copy of the GNU General Public License along with NitterizeMe; if not,
* see <http://www.gnu.org/licenses>. */
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
@ -22,6 +25,8 @@ import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import static app.fedilab.nitterizeme.MainActivity.shortener_domains;
class Utils {
static final Pattern urlPattern = Pattern.compile(
@ -31,14 +36,19 @@ class Utils {
/**
* Returns the unshortened URL
* @param urlStr String URL to check
* @return String the Unshortened URL
* @param urls ArrayList<String> URL to check
*/
static String checkUrl(String urlStr){
static void checkUrl(ArrayList<String> urls){
URL url;
String redirect = null;
String newURL = null;
String comingURl;
try {
url = new URL(urlStr);
comingURl = urls.get(urls.size()-1);
if( comingURl.startsWith("http://")){
comingURl = comingURl.replace("http://", "https://");
}
url = new URL(comingURl);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
httpsURLConnection.setInstanceFollowRedirects(false);
@ -49,24 +59,25 @@ class Utils {
if (entry.toString().toLowerCase().startsWith("location")) {
Matcher matcher = urlPattern.matcher(entry.toString());
if (matcher.find()) {
redirect = matcher.group(1);
urls.add(matcher.group(1));
newURL = matcher.group(1);
}
}
}
}
httpsURLConnection.getInputStream().close();
if (redirect != null && redirect.compareTo(urlStr)!=0){
URL redirectURL = new URL(redirect);
if (newURL != null && newURL.compareTo(comingURl)!=0){
URL redirectURL = new URL(newURL);
String host = redirectURL.getHost();
String protocol = redirectURL.getProtocol();
if( protocol == null || host == null){
redirect = null;
if( protocol != null && host != null){
if( Arrays.asList(shortener_domains).contains(host)) {
checkUrl(urls);
}
}
}
return redirect;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -34,6 +34,8 @@
<string name="shortened_detected">Shortened URL</string>
<string name="open">Open</string>
<string name="dismiss">Dismiss</string>
<string name="resolving_shortened">Resolving shortened URL, please wait...</string>
<string name="resolving_shortened">Resolving shortened URL, please wait</string>
<string name="try_to_redirect">%1$s \n\nwill redirect you to\n\n<b>%2$s</b></string>
<string name="try_to_redirect_again">Then it will redirect you to\n\n<b>%1$s</b></string>
<string name="the_app_failed_shortened">The app didn\'t manage to retrieve the full URL</string>
</resources>

View File

@ -2,6 +2,8 @@ NitterizeMe once set by default to open Twitter and YouTube links will transform
It also supports Google Maps links to transform them into OpenStreetMap links or Geo URI.
The app will detect shortened URLs and will display the full URL before visiting the site.
When sharing a Twitter, YouTube or Google Maps link just pick up NitterizeMe to transform it before sharing it with another app.
What is Nitter: