From 748cf3c074d86dcc33ccd768f0f6b810088d1864 Mon Sep 17 00:00:00 2001 From: akaessens <24660231+akaessens@users.noreply.github.com> Date: Sat, 3 Oct 2020 21:45:19 +0200 Subject: [PATCH] add shortener redirection, replace m. with mbasic. --- .../nofbeventscraper/FbEventScraper.java | 1 + .../akdev/nofbeventscraper/FbPageScraper.java | 7 +-- .../FbRedirectionResolver.java | 40 +++++++++++++++ .../com/akdev/nofbeventscraper/FbScraper.java | 50 ++++++++++++++++++- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values/arrays.xml | 4 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/root_preferences.xml | 2 +- 8 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/akdev/nofbeventscraper/FbRedirectionResolver.java diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbEventScraper.java b/app/src/main/java/com/akdev/nofbeventscraper/FbEventScraper.java index 3337c26..3c4dcce 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/FbEventScraper.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbEventScraper.java @@ -212,6 +212,7 @@ public class FbEventScraper extends AsyncTask { * * @param aVoid */ + @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbPageScraper.java b/app/src/main/java/com/akdev/nofbeventscraper/FbPageScraper.java index 90d8269..c2b46b7 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/FbPageScraper.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbPageScraper.java @@ -67,12 +67,12 @@ public class FbPageScraper extends AsyncTask { .getElementsByAttributeValueMatching("href", Pattern.compile(regex)) .eachAttr("href"); - for (String link : event_links_href) { - this.event_links.add("https://www.facebook.com" + link); + for (String event_id : event_links_href) { + this.event_links.add("https://mbasic.facebook.com" + event_id); } /* - * check if more events should scraped + * check if more events should be scraped */ SharedPreferences shared_prefs = PreferenceManager .getDefaultSharedPreferences(scraper.main.get()); @@ -120,6 +120,7 @@ public class FbPageScraper extends AsyncTask { * * @param aVoid */ + @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbRedirectionResolver.java b/app/src/main/java/com/akdev/nofbeventscraper/FbRedirectionResolver.java new file mode 100644 index 0000000..f484ff8 --- /dev/null +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbRedirectionResolver.java @@ -0,0 +1,40 @@ +package com.akdev.nofbeventscraper; + +import android.os.AsyncTask; + +import java.net.HttpURLConnection; +import java.net.URL; + +public class FbRedirectionResolver extends AsyncTask { + + private String input_url; + private FbScraper scraper; + private String redirected_url; + + public FbRedirectionResolver (FbScraper scraper, String input_url) { + this.input_url = input_url; + this.scraper = scraper; + } + + protected Void doInBackground(Void... voids) { + try { + HttpURLConnection con = (HttpURLConnection) new URL(input_url).openConnection(); + + con.setInstanceFollowRedirects(false); + + con.connect(); + + redirected_url = con.getHeaderField("Location"); + + } catch (Exception e) { + } + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + + scraper.redirectionResultCallback(redirected_url); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java b/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java index 7d098d2..2613cda 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java @@ -5,10 +5,12 @@ import android.os.AsyncTask; import androidx.preference.PreferenceManager; +import java.io.IOException; import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -33,6 +35,28 @@ public class FbScraper { this.tasks = new ArrayList<>(); } + protected String getShortened(String url) throws IOException, URISyntaxException { + // check for url format + new URL(url).toURI(); + + String regex = "(fb.me/)(e/)?([^/?]*)|(facebook.com/event_invite/[a-zA-Z0-9]*)"; + + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(url); + + if (matcher.find()) { + //only mbasic does have event ids displayed in HTML + String url_prefix = "https://mbasic."; + + // create URL + return url_prefix + matcher.group(); + + } else { + throw new URISyntaxException(url, "Does not contain page."); + } + + } + /** * Checks if valid URL, * strips the facebook page id from the input link and create an URL that can be scraped from. @@ -183,11 +207,35 @@ public class FbScraper { } } + protected void redirectUrl (String url) { + FbRedirectionResolver resolver = new FbRedirectionResolver(this, url); + + resolver.execute(); + } + protected void redirectionResultCallback(String url) { + this.input_url = url; + + // now try again with expanded url + this.run(); + } + /** * Start scraping input url */ void run() { + // check if shortened url + try { + String shortened = getShortened(input_url); + url_type = url_type_enum.SHORT; + redirectUrl(shortened); + + return; + + } catch (IOException | URISyntaxException e) { + url_type = url_type_enum.INVALID; + } + // check if input url is an event try { String event_url = getEventUrl(input_url); @@ -212,5 +260,5 @@ public class FbScraper { } // enum for storing url type in this class - enum url_type_enum {EVENT, PAGE, INVALID} + enum url_type_enum {SHORT, EVENT, PAGE, INVALID} } \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 754de86..5c8eae0 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -9,7 +9,7 @@ Zum Kalender hinzufügen Einfügen von Inhalten aus der Zwischenablage in das URL-Eingabefeld Welcher URL-Präfix ist zu verwenden? - Die Nutzung von m.facebook.com ist stabiler und schneller. Die Verwendung von www.facebook.com funktioniert besser bei Ereignissen mit mehreren Instanzen und zeigt eine hochauflösende Vorschau an, geht aber irgendwann kaputt, wenn Facebook das klassische Design deaktiviert. + Die Nutzung von mbasic.facebook.com ist stabiler und schneller. Die Verwendung von www.facebook.com funktioniert besser bei Ereignissen mit mehreren Instanzen und zeigt eine hochauflösende Vorschau an, geht aber irgendwann kaputt, wenn Facebook das klassische Design deaktiviert. Fehler: Zwischenablage leer Fehler: Veranstaltungsdaten nicht gefunden Fehler: URL ungültig diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 3ff6f6f..c0d9a7d 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -1,12 +1,12 @@ - m.facebook.com + mbasic.facebook.com www.facebook.com - https://m. + https://mbasic. https://www. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 285e730..cc0a3f5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,7 +24,7 @@ Scraper Which URL prefix to use - "Using m.facebook.com is more stable and faster. Using www.facebook.com works better with multiple instance events and will display a high resolution preview but will eventually break when Facebook disables the classic design. " + "Using mbasic.facebook.com is more stable and faster. Using www.facebook.com works better with multiple instance events and will display a high resolution preview but will eventually break when Facebook disables the classic design." Events Clear event list diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index d7d18e0..ee413af 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -6,7 +6,7 @@