mirror of
https://github.com/akaessens/NoFbEventScraper
synced 2025-06-05 23:29:13 +02:00
add location address
fix for events without end date
This commit is contained in:
@ -21,14 +21,46 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
|
|||||||
private String url;
|
private String url;
|
||||||
private String error;
|
private String error;
|
||||||
private MainActivity main;
|
private MainActivity main;
|
||||||
public FbEvent event;
|
private FbEvent event;
|
||||||
|
|
||||||
public FbScraper(MainActivity main, String url) {
|
FbScraper(MainActivity main, String url) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.main = main;
|
this.main = main;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private String readFromLocJson(String location_json) {
|
||||||
|
|
||||||
|
String name = "";
|
||||||
|
String street_address = "";
|
||||||
|
String postal_code = "";
|
||||||
|
String address_locality = "";
|
||||||
|
try {
|
||||||
|
JSONObject reader = new JSONObject(location_json);
|
||||||
|
|
||||||
|
name = reader.getString("name");
|
||||||
|
|
||||||
|
JSONObject address = reader.getJSONObject("address");
|
||||||
|
street_address = ", " + address.getString("streetAddress");
|
||||||
|
postal_code = ", " + address.getString("postalCode");
|
||||||
|
address_locality = " " + address.getString("addressLocality");
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return name + street_address + postal_code + address_locality;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String readFromJson(JSONObject reader, String field) {
|
||||||
|
try {
|
||||||
|
return reader.getString(field);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... voids) {
|
protected Void doInBackground(Void... voids) {
|
||||||
|
|
||||||
@ -42,15 +74,15 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
|
|||||||
|
|
||||||
JSONObject reader = new JSONObject(json);
|
JSONObject reader = new JSONObject(json);
|
||||||
|
|
||||||
String event_name = reader.getString("name");
|
String event_name = readFromJson(reader, "name");
|
||||||
String event_start = reader.getString("startDate");
|
String event_start = readFromJson(reader, "startDate");
|
||||||
String event_end = reader.getString("endDate");
|
String event_end = readFromJson(reader, "endDate");
|
||||||
String event_description = reader.getString("description");
|
String event_description = readFromJson(reader, "description");
|
||||||
String location = reader.getJSONObject("location").getString("name");
|
String location_json = readFromJson(reader, "location");
|
||||||
|
|
||||||
//String image_url = reader.getString("image");
|
String location = readFromLocJson(location_json);
|
||||||
|
|
||||||
if (event_name == null || event_start == null || event_end == null) {
|
if (event_name == null) {
|
||||||
this.event = null;
|
this.event = null;
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
} else {
|
} else {
|
||||||
@ -65,13 +97,11 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
this.error = "Error: URL not available";
|
this.error = "Error: URL not available";
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPreExecute() {
|
protected void onPreExecute() {
|
||||||
|
|
||||||
super.onPreExecute();
|
super.onPreExecute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +20,6 @@ import java.net.URL;
|
|||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
@ -55,8 +53,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
field_event_location = (TextInputEditText) findViewById(R.id.field_event_location);
|
field_event_location = (TextInputEditText) findViewById(R.id.field_event_location);
|
||||||
field_event_description = (TextInputEditText) findViewById(R.id.field_event_description);
|
field_event_description = (TextInputEditText) findViewById(R.id.field_event_description);
|
||||||
|
|
||||||
//final MainActivity mainactivity = this;
|
|
||||||
|
|
||||||
paste_button.setOnClickListener(new View.OnClickListener() {
|
paste_button.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
@ -87,28 +83,20 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
try {
|
try {
|
||||||
//time
|
Long start_epoch = convertTimeToEpoch(field_event_start);
|
||||||
String start_str = field_event_start.getText().insert(22, ":").toString();
|
Long end_epoch = convertTimeToEpoch(field_event_end);
|
||||||
String end_str = field_event_end.getText().insert(22, ":").toString();
|
|
||||||
|
|
||||||
LocalDateTime start = LocalDateTime.parse(start_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
String name = parseField(field_event_name);
|
||||||
LocalDateTime end = LocalDateTime.parse(end_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
String location = parseField(field_event_location);
|
||||||
|
String description = parseField(field_event_description);
|
||||||
ZoneId zoneId = ZoneId.systemDefault();
|
String uri = parseField(field_uri_input);
|
||||||
long start_epoch = start.atZone(zoneId).toEpochSecond() * 1000;
|
|
||||||
long end_epoch = end.atZone(zoneId).toEpochSecond() * 1000;
|
|
||||||
|
|
||||||
String name = field_event_name.getText().toString();
|
|
||||||
String location = field_event_location.getText().toString();
|
|
||||||
String description = field_event_description.getText().toString();
|
|
||||||
String uri = field_uri_input.getText().toString();
|
|
||||||
|
|
||||||
Intent intent = new Intent(Intent.ACTION_EDIT);
|
Intent intent = new Intent(Intent.ACTION_EDIT);
|
||||||
intent.setType("vnd.android.cursor.item/event");
|
intent.setType("vnd.android.cursor.item/event");
|
||||||
intent.putExtra(CalendarContract.Events.TITLE, field_event_name.getText().toString());
|
intent.putExtra(CalendarContract.Events.TITLE, name);
|
||||||
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start_epoch);
|
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start_epoch);
|
||||||
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end_epoch);
|
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end_epoch);
|
||||||
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, field_event_location.getText().toString());
|
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, location);
|
||||||
intent.putExtra(CalendarContract.Events.DESCRIPTION, uri + "\n" + description);
|
intent.putExtra(CalendarContract.Events.DESCRIPTION, uri + "\n" + description);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
@ -116,7 +104,6 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
toast("Error: Invalid fields");
|
toast("Error: Invalid fields");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -144,6 +131,30 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
private String parseField(TextInputEditText field) {
|
||||||
|
try {
|
||||||
|
return field.getText().toString();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Long convertTimeToEpoch (TextInputEditText field) {
|
||||||
|
try {
|
||||||
|
String time_str = field.getText().insert(22, ":").toString();
|
||||||
|
|
||||||
|
LocalDateTime datetime = LocalDateTime.parse(time_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||||
|
ZoneId zoneId = ZoneId.systemDefault();
|
||||||
|
return datetime.atZone(zoneId).toEpochSecond() * 1000;
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
toast("Error: Invalid Time");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isNumeric(String strNum) {
|
public static boolean isNumeric(String strNum) {
|
||||||
if (strNum == null) {
|
if (strNum == null) {
|
||||||
|
Reference in New Issue
Block a user