improve high-res image url scraping

This commit is contained in:
akaessens 2020-08-31 16:13:43 +02:00
parent bb1e7579d8
commit 320d1787aa
4 changed files with 16 additions and 10 deletions

View File

@ -52,7 +52,8 @@ public class FbEvent {
*/ */
static String dateTimeToString(Date date) { static String dateTimeToString(Date date) {
try { try {
SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm z", Locale.getDefault()); SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm z",
Locale.getDefault());
return formatter.format(date); return formatter.format(date);
} catch (Exception e) { } catch (Exception e) {
return ""; return "";

View File

@ -35,7 +35,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
/** /**
* Constructor with WeakReference to the main activity, to update it's text fields. * Constructor with WeakReference to the main activity, to update it's text fields.
* *
* @param main WeakReference of main activity to prevent context leak * @param main WeakReference of main activity to prevent context leak
* @param input_url Input url to scrape from * @param input_url Input url to scrape from
*/ */
FbScraper(WeakReference<MainActivity> main, String input_url) { FbScraper(WeakReference<MainActivity> main, String input_url) {
@ -48,7 +48,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
* *
* @param url input url * @param url input url
* @return facebook event url String if one was found * @return facebook event url String if one was found
* @throws URISyntaxException if event not found * @throws URISyntaxException if event not found
* @throws MalformedURLException * @throws MalformedURLException
*/ */
protected String fixURI(String url) throws URISyntaxException, MalformedURLException { protected String fixURI(String url) throws URISyntaxException, MalformedURLException {
@ -64,7 +64,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
if (matcher.find()) { if (matcher.find()) {
String url_prefix = "https://m."; String url_prefix = "https://m.";
if (main != null){ if (main != null) {
SharedPreferences shared_prefs = PreferenceManager.getDefaultSharedPreferences(main.get()); SharedPreferences shared_prefs = PreferenceManager.getDefaultSharedPreferences(main.get());
url_prefix = shared_prefs.getString("url_preference", url_prefix); url_prefix = shared_prefs.getString("url_preference", url_prefix);
} }
@ -74,7 +74,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
if (matcher.group(2) != null) { if (matcher.group(2) != null) {
ret += matcher.group(2); ret += matcher.group(2);
} }
return ret; return ret;
} else { } else {
throw new URISyntaxException(url, "Does not contain event."); throw new URISyntaxException(url, "Does not contain event.");
} }
@ -84,6 +84,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
/** /**
* Strips the event location from the json string. * Strips the event location from the json string.
* This can be a name only or a complete postal address. * This can be a name only or a complete postal address.
*
* @param location_json JSON formatted string * @param location_json JSON formatted string
* @return String representation of the location. * @return String representation of the location.
*/ */
@ -164,8 +165,9 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
/** /**
* Read a single field from a JSONObject * Read a single field from a JSONObject
*
* @param reader JSONObject to read from * @param reader JSONObject to read from
* @param field Which field to read * @param field Which field to read
* @return String of the value of the field or empty string * @return String of the value of the field or empty string
*/ */
private String readFromJson(JSONObject reader, String field) { private String readFromJson(JSONObject reader, String field) {
@ -180,6 +182,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
/** /**
* Started by scraper.execute(). * Started by scraper.execute().
* Gets the HTML doc from the input string and scrapes the event information from it. * Gets the HTML doc from the input string and scrapes the event information from it.
*
* @param voids * @param voids
* @return * @return
*/ */
@ -212,14 +215,14 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
try { try {
// possibly get higher res image from event header // possibly get higher res image from event header
image_url = document.getElementsByClass("scaledImageFitWidth") image_url = document.select("img[class*=scaledImageFit]")
.first().attr("src"); .first().attr("src");
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} }
event = new FbEvent(url, name,start_date, end_date, description, location, image_url); event = new FbEvent(url, name, start_date, end_date, description, location, image_url);
} catch (URISyntaxException | MalformedURLException e) { } catch (URISyntaxException | MalformedURLException e) {
@ -247,6 +250,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
/** /**
* When scraping is finished, main activity will be updated. * When scraping is finished, main activity will be updated.
* If an error occurred, main activity is given an error string. * If an error occurred, main activity is given an error string.
*
* @param aVoid * @param aVoid
*/ */
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {

View File

@ -331,7 +331,7 @@ public class MainActivity extends AppCompatActivity {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu); getMenuInflater().inflate(R.menu.menu_main, menu);
if(menu instanceof MenuBuilder){ if (menu instanceof MenuBuilder) {
MenuBuilder m = (MenuBuilder) menu; MenuBuilder m = (MenuBuilder) menu;
//noinspection RestrictedApi //noinspection RestrictedApi
m.setOptionalIconsVisible(true); m.setOptionalIconsVisible(true);

View File

@ -60,7 +60,8 @@
app:layout_anchorGravity="bottom|end" app:layout_anchorGravity="bottom|end"
app:icon="@drawable/ic_content_paste" app:icon="@drawable/ic_content_paste"
android:text="@android:string/paste" android:text="@android:string/paste"
android:tooltipText="@string/tooltip_paste"/> android:tooltipText="@string/tooltip_paste"
tools:ignore="UnusedAttribute" />
<include layout="@layout/content_main" /> <include layout="@layout/content_main" />