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 error;
|
||||
private MainActivity main;
|
||||
public FbEvent event;
|
||||
private FbEvent event;
|
||||
|
||||
public FbScraper(MainActivity main, String url) {
|
||||
FbScraper(MainActivity main, String url) {
|
||||
this.url = url;
|
||||
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
|
||||
protected Void doInBackground(Void... voids) {
|
||||
|
||||
@ -42,15 +74,15 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
JSONObject reader = new JSONObject(json);
|
||||
|
||||
String event_name = reader.getString("name");
|
||||
String event_start = reader.getString("startDate");
|
||||
String event_end = reader.getString("endDate");
|
||||
String event_description = reader.getString("description");
|
||||
String location = reader.getJSONObject("location").getString("name");
|
||||
String event_name = readFromJson(reader, "name");
|
||||
String event_start = readFromJson(reader, "startDate");
|
||||
String event_end = readFromJson(reader, "endDate");
|
||||
String event_description = readFromJson(reader, "description");
|
||||
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;
|
||||
throw new Exception();
|
||||
} else {
|
||||
@ -65,13 +97,11 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
|
||||
e.printStackTrace();
|
||||
this.error = "Error: URL not available";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
|
||||
super.onPreExecute();
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,6 @@ import java.net.URL;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
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_description = (TextInputEditText) findViewById(R.id.field_event_description);
|
||||
|
||||
//final MainActivity mainactivity = this;
|
||||
|
||||
paste_button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
@ -87,28 +83,20 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
try {
|
||||
//time
|
||||
String start_str = field_event_start.getText().insert(22, ":").toString();
|
||||
String end_str = field_event_end.getText().insert(22, ":").toString();
|
||||
Long start_epoch = convertTimeToEpoch(field_event_start);
|
||||
Long end_epoch = convertTimeToEpoch(field_event_end);
|
||||
|
||||
LocalDateTime start = LocalDateTime.parse(start_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
LocalDateTime end = LocalDateTime.parse(end_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
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();
|
||||
String name = parseField(field_event_name);
|
||||
String location = parseField(field_event_location);
|
||||
String description = parseField(field_event_description);
|
||||
String uri = parseField(field_uri_input);
|
||||
|
||||
Intent intent = new Intent(Intent.ACTION_EDIT);
|
||||
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_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);
|
||||
startActivity(intent);
|
||||
}
|
||||
@ -116,7 +104,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
{
|
||||
e.printStackTrace();
|
||||
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) {
|
||||
if (strNum == null) {
|
||||
|
Reference in New Issue
Block a user