add shortener redirection, replace m. with mbasic.

This commit is contained in:
akaessens 2020-10-03 21:45:19 +02:00
parent 2479cd9c72
commit 748cf3c074
8 changed files with 99 additions and 9 deletions

View File

@ -212,6 +212,7 @@ public class FbEventScraper extends AsyncTask<Void, Void, Void> {
* *
* @param aVoid * @param aVoid
*/ */
@Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid); super.onPostExecute(aVoid);

View File

@ -67,12 +67,12 @@ public class FbPageScraper extends AsyncTask<Void, Void, Void> {
.getElementsByAttributeValueMatching("href", Pattern.compile(regex)) .getElementsByAttributeValueMatching("href", Pattern.compile(regex))
.eachAttr("href"); .eachAttr("href");
for (String link : event_links_href) { for (String event_id : event_links_href) {
this.event_links.add("https://www.facebook.com" + link); 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 SharedPreferences shared_prefs = PreferenceManager
.getDefaultSharedPreferences(scraper.main.get()); .getDefaultSharedPreferences(scraper.main.get());
@ -120,6 +120,7 @@ public class FbPageScraper extends AsyncTask<Void, Void, Void> {
* *
* @param aVoid * @param aVoid
*/ */
@Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid); super.onPostExecute(aVoid);

View File

@ -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<Void, Void, Void> {
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);
}
}

View File

@ -5,10 +5,12 @@ import android.os.AsyncTask;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -33,6 +35,28 @@ public class FbScraper {
this.tasks = new ArrayList<>(); 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, * Checks if valid URL,
* strips the facebook page id from the input link and create an URL that can be scraped from. * 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 * Start scraping input url
*/ */
void run() { 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 // check if input url is an event
try { try {
String event_url = getEventUrl(input_url); String event_url = getEventUrl(input_url);
@ -212,5 +260,5 @@ public class FbScraper {
} }
// enum for storing url type in this class // enum for storing url type in this class
enum url_type_enum {EVENT, PAGE, INVALID} enum url_type_enum {SHORT, EVENT, PAGE, INVALID}
} }

View File

@ -9,7 +9,7 @@
<string name="button_add">Zum Kalender hinzufügen</string> <string name="button_add">Zum Kalender hinzufügen</string>
<string name="tooltip_paste">Einfügen von Inhalten aus der Zwischenablage in das URL-Eingabefeld</string> <string name="tooltip_paste">Einfügen von Inhalten aus der Zwischenablage in das URL-Eingabefeld</string>
<string name="preferences_url_setting">Welcher URL-Präfix ist zu verwenden?</string> <string name="preferences_url_setting">Welcher URL-Präfix ist zu verwenden?</string>
<string name="preferences_url_setting_summary">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.</string> <string name="preferences_url_setting_summary">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.</string>
<string name="error_clipboard_empty">Fehler: Zwischenablage leer</string> <string name="error_clipboard_empty">Fehler: Zwischenablage leer</string>
<string name="error_scraping">Fehler: Veranstaltungsdaten nicht gefunden</string> <string name="error_scraping">Fehler: Veranstaltungsdaten nicht gefunden</string>
<string name="error_url">Fehler: URL ungültig</string> <string name="error_url">Fehler: URL ungültig</string>

View File

@ -1,12 +1,12 @@
<resources> <resources>
<!-- Reply Preference --> <!-- Reply Preference -->
<string-array name="url_to_scrape"> <string-array name="url_to_scrape">
<item>m.facebook.com</item> <item>mbasic.facebook.com</item>
<item>www.facebook.com</item> <item>www.facebook.com</item>
</string-array> </string-array>
<string-array name="url_prefix"> <string-array name="url_prefix">
<item>https://m.</item> <item>https://mbasic.</item>
<item>https://www.</item> <item>https://www.</item>
</string-array> </string-array>

View File

@ -24,7 +24,7 @@
<!-- Preferences --> <!-- Preferences -->
<string name="preferences_scraper_header" translatable="false">Scraper</string> <string name="preferences_scraper_header" translatable="false">Scraper</string>
<string name="preferences_url_setting">Which URL prefix to use</string> <string name="preferences_url_setting">Which URL prefix to use</string>
<string name="preferences_url_setting_summary">"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. "</string> <string name="preferences_url_setting_summary">"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."</string>
<string name="preferences_events_header">Events</string> <string name="preferences_events_header">Events</string>
<string name="preferences_event_setting">Clear event list</string> <string name="preferences_event_setting">Clear event list</string>

View File

@ -6,7 +6,7 @@
<ListPreference <ListPreference
android:summary="@string/preferences_url_setting_summary" android:summary="@string/preferences_url_setting_summary"
app:defaultValue="https://m." app:defaultValue="https://mbasic."
app:entries="@array/url_to_scrape" app:entries="@array/url_to_scrape"
app:entryValues="@array/url_prefix" app:entryValues="@array/url_prefix"
app:key="url_preference" app:key="url_preference"