1
0
mirror of https://github.com/akaessens/NoFbEventScraper synced 2025-06-05 23:29:13 +02:00

rewrite layout and catch url errors

This commit is contained in:
akaessens
2020-03-25 23:48:37 +01:00
parent 065cc04667
commit 3c6c51e781
9 changed files with 287 additions and 360 deletions

View File

@ -1,40 +1,37 @@
package com.akdev.nofbeventscraper;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.provider.CalendarContract;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private Button paste_button;
private Button cancel_button;
private Button ok_button;
private TextInputEditText field_uri_input;
private TextInputEditText field_event_name;
private TextInputEditText field_event_start;
private TextInputEditText field_event_end;
private TextInputEditText field_event_location;
private TextInputEditText field_event_description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -42,17 +39,110 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
cancel_button = (Button) findViewById(R.id.cancel_button);
ok_button = (Button) findViewById(R.id.ok_button);
paste_button = (Button) findViewById(R.id.paste_button);
fab.setOnClickListener(new View.OnClickListener() {
field_uri_input = (TextInputEditText) findViewById(R.id.field_uri_input);
field_event_name = (TextInputEditText) findViewById(R.id.field_event_name);
field_event_start = (TextInputEditText) findViewById(R.id.field_event_start);
field_event_end = (TextInputEditText) findViewById(R.id.field_event_end);
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) {
onButtonShowPopupWindowClick(view);
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null) {
String str = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
try {
new URL(str).toURI();
if (str.substring(0,32).equals("https://www.facebook.com/events/")) {
str = str.replace("www.", "m.");
}
else if (str.substring(0,30).equals("https://m.facebook.com/events/")) {
}
else {
throw new Exception();
}
field_uri_input.setText(str);
FbScraper scraper = new FbScraper(mainactivity, field_uri_input.getText().toString());
scraper.execute();
}
catch (Exception e) {
clear();
}
}
}
});
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clear();
}
});
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// time
String start_str = field_event_start.getText().insert(22, ":").toString();
String end_str = field_event_end.getText().insert(22, ":").toString();
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();
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.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.DESCRIPTION, uri + "\n" + description);
startActivity(intent);
}
});
}
@Override
public void clear() {
field_uri_input.setText("");
field_event_name.setText("");
field_event_start.setText("");
field_event_end.setText("");
field_event_location.setText("");
field_event_description.setText("");
}
public void update(FbEvent event) {
field_event_name.setText(event.name);
field_event_start.setText(event.start_date);
field_event_end.setText(event.end_date);
field_event_location.setText(event.location);
field_event_description.setText(event.description);
}
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
@ -72,101 +162,5 @@ public class MainActivity extends AppCompatActivity {
}
return super.onOptionsItemSelected(item);
}
}*/
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.popup_window, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window token
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
Button cancel_button = (Button)popupView.findViewById(R.id.cancel_button);
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
Button ok_button = (Button)popupView.findViewById(R.id.ok_button);
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextInputEditText uri_input = (TextInputEditText) popupView.findViewById(R.id.text_input);
TextInputEditText field_name = (TextInputEditText) popupView.findViewById(R.id.field_event_name);
TextInputEditText field_start = (TextInputEditText) popupView.findViewById(R.id.field_event_start);
TextInputEditText field_end = (TextInputEditText) popupView.findViewById(R.id.field_event_end);
TextInputEditText field_location = (TextInputEditText) popupView.findViewById(R.id.field_event_location);
// time
String start_str = field_start.getText().insert(22, ":").toString();
String end_str = field_end.getText().insert(22, ":").toString();
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;
//Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, field_name.getText().toString());
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_location.getText().toString());
intent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI, uri_input.getText().toString());
startActivity(intent);
popupWindow.dismiss();
}
});
Button paste_button = (Button)popupView.findViewById(R.id.paste_button);
final TextInputEditText text_input = (TextInputEditText) popupView.findViewById(R.id.text_input);
paste_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null) {
String str = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
str = "https://m.facebook.com/events/2402761143327832/";
text_input.setText(str);
}
FbScraper scraper = new FbScraper(popupView, text_input.getText().toString());
FbEvent event;
scraper.execute();
}
});
/* dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});*/
}
}