From 74183eface339f2ba3560c2bfe903fd0fef89117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eevket=20Eren=20KAYNAR?= Date: Wed, 23 Aug 2023 17:16:57 +0300 Subject: [PATCH] v1.1.0 --- .../library/earthquake/MainActivity.java | 6 +-- library/build.gradle | 8 +-- .../library/earthquake/Constants.java | 7 --- .../library/earthquake/EarthquakeAPI.java | 24 ++------- .../library/earthquake/enums/Source.java | 35 ++++++++++-- .../library/earthquake/models/Earthquake.java | 1 + .../sources/EarthquakeAPICallable.java | 54 ++++++++++++++++++- .../AfadParser.java} | 31 ++--------- .../KandilliParser.java} | 48 ++++++----------- .../earthquake/sources/parsers/Parser.java | 10 ++++ .../TextParser.java} | 40 ++++---------- 11 files changed, 137 insertions(+), 127 deletions(-) delete mode 100644 library/src/main/java/tr/com/erenkaynar/library/earthquake/Constants.java rename library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/{afad/AfadCallable.java => parsers/AfadParser.java} (63%) rename library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/{kandilli/KandilliCallable.java => parsers/KandilliParser.java} (69%) create mode 100644 library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/Parser.java rename library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/{usgs/UsgsCallable.java => parsers/TextParser.java} (52%) diff --git a/app/src/main/java/tr/com/erenkaynar/library/earthquake/MainActivity.java b/app/src/main/java/tr/com/erenkaynar/library/earthquake/MainActivity.java index d45286e..dda5600 100644 --- a/app/src/main/java/tr/com/erenkaynar/library/earthquake/MainActivity.java +++ b/app/src/main/java/tr/com/erenkaynar/library/earthquake/MainActivity.java @@ -1,8 +1,8 @@ package tr.com.erenkaynar.library.earthquake; import android.os.Bundle; -import android.util.Log; import android.widget.TextView; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -22,7 +22,7 @@ protected void onCreate(Bundle savedInstanceState) { text = findViewById(R.id.textView); - EarthquakeAPI.initialize().setSource(Source.AFAD).setEarthquakeAPIListener(new EarthquakeAPIListener() { + EarthquakeAPI.initialize().setSource(Source.KANDILLI).setEarthquakeAPIListener(new EarthquakeAPIListener() { @Override public void onLoaded(ArrayList earthquakes) { StringBuilder output = new StringBuilder(); @@ -52,7 +52,7 @@ public void onLoaded(ArrayList earthquakes) { @Override public void onError(Exception e) { - + Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show(); } }).load(); } diff --git a/library/build.gradle b/library/build.gradle index 9a63434..3f219b4 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'com.github.sqayner' -version = '1.0.0' +version = '1.1.0' android { namespace 'tr.com.erenkaynar.library.earthquake' @@ -12,8 +12,8 @@ android { defaultConfig { minSdk 26 - versionCode 1 - versionName "1.0.0" + versionCode 2 + versionName "1.1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" @@ -56,7 +56,7 @@ afterEvaluate { from components.release groupId = 'com.github.sqayner' artifactId = 'earthquake-library-java' - version = '1.0.0' + version = '1.1.0' } } } diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/Constants.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/Constants.java deleted file mode 100644 index d290b22..0000000 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/Constants.java +++ /dev/null @@ -1,7 +0,0 @@ -package tr.com.erenkaynar.library.earthquake; - -public class Constants { - public static String KANDILLI_URL = "http://www.koeri.boun.edu.tr/scripts/sondepremler.asp"; - public static String AFAD_URL = "https://deprem.afad.gov.tr/last-earthquakes.html"; - public static String USGS_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&minlongitude=25&maxlongitude=46&minlatitude=35&maxlatitude=43"; -} diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/EarthquakeAPI.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/EarthquakeAPI.java index c26d0e0..6c97796 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/EarthquakeAPI.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/EarthquakeAPI.java @@ -4,9 +4,7 @@ import tr.com.erenkaynar.library.earthquake.enums.Source; import tr.com.erenkaynar.library.earthquake.models.Earthquake; -import tr.com.erenkaynar.library.earthquake.sources.afad.AfadCallable; -import tr.com.erenkaynar.library.earthquake.sources.kandilli.KandilliCallable; -import tr.com.erenkaynar.library.earthquake.sources.usgs.UsgsCallable; +import tr.com.erenkaynar.library.earthquake.sources.EarthquakeAPICallable; import tr.com.erenkaynar.library.earthquake.utils.Callback; import tr.com.erenkaynar.library.earthquake.utils.TaskRunner; @@ -35,31 +33,17 @@ public EarthquakeAPI setSource(Source source) { } public void load() { - switch (source) { - case KANDILLI: - taskRunner.executeAsync(new KandilliCallable(), this); - break; - case AFAD: - taskRunner.executeAsync(new AfadCallable(), this); - break; - case USGS: - taskRunner.executeAsync(new UsgsCallable(), this); - break; - default: - break; - } + taskRunner.executeAsync(new EarthquakeAPICallable(source), this); } @Override public void onLoaded(ArrayList data) { - if (earthquakeAPIListener != null) - earthquakeAPIListener.onLoaded(data); + if (earthquakeAPIListener != null) earthquakeAPIListener.onLoaded(data); } @Override public void onError(Exception e) { - if (earthquakeAPIListener != null) - earthquakeAPIListener.onError(e); + if (earthquakeAPIListener != null) earthquakeAPIListener.onError(e); } @Override diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/enums/Source.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/enums/Source.java index a5c93ec..945d9a4 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/enums/Source.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/enums/Source.java @@ -1,7 +1,36 @@ package tr.com.erenkaynar.library.earthquake.enums; public enum Source { - KANDILLI, - AFAD, - USGS + KANDILLI( + "Kandilli Rasathanesi ve Deprem Araştırma Enstitüsü", + "http://www.koeri.boun.edu.tr/scripts/sondepremler.asp" + ), + AFAD( + "Afet ve Acil Durum Yönetimi Başkanlığı", + "https://deprem.afad.gov.tr/last-earthquakes.html" + ), + USGS( + "ABD Jeoloji Araştırmaları Kurumu (United States Geological Survey)", + "https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&minlongitude=25&maxlongitude=46&minlatitude=35&maxlatitude=43&limit=100" + ), + EMSC( + "Avrupa-Akdeniz Sismoloji Merkezi (European-Mediterranean Seismological Centre)", + "https://www.seismicportal.eu/fdsnws/event/1/query?contributor=EMSC&limit=100&minlat=35&maxlat=43&minlon=25&maxlon=46&format=text&mindepth=0&minmag=0" + ); + + private final String name; + private final String url; + + Source(String name, String url) { + this.name = name; + this.url = url; + } + + public String getName() { + return name; + } + + public String getUrl() { + return url; + } } diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/models/Earthquake.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/models/Earthquake.java index e3b3ccc..ebe04c2 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/models/Earthquake.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/models/Earthquake.java @@ -86,6 +86,7 @@ public String getHash() { return "kandilli-" + MD5.encode((magnitude == null ? "" : magnitude) + "," + (coordinates == null ? "" : +coordinates.getLatitude()) + "," + (coordinates == null ? "" : +coordinates.getLongitude()) + "," + (depth == null ? "" : +depth) + "," + (datetime == null ? "" : +datetime.getEpochSecond()) + "," + (location == null ? "" : location)); if (source == Source.AFAD) return "afad-" + MD5.encode(id); if (source == Source.USGS) return "usgs-" + MD5.encode(id); + if (source == Source.EMSC) return "emsc-" + MD5.encode(id); return null; } diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/EarthquakeAPICallable.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/EarthquakeAPICallable.java index 539e719..6948d72 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/EarthquakeAPICallable.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/EarthquakeAPICallable.java @@ -1,12 +1,62 @@ package tr.com.erenkaynar.library.earthquake.sources; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import tr.com.erenkaynar.library.earthquake.enums.Source; import tr.com.erenkaynar.library.earthquake.models.Earthquake; +import tr.com.erenkaynar.library.earthquake.sources.parsers.AfadParser; +import tr.com.erenkaynar.library.earthquake.sources.parsers.KandilliParser; +import tr.com.erenkaynar.library.earthquake.sources.parsers.Parser; +import tr.com.erenkaynar.library.earthquake.sources.parsers.TextParser; import tr.com.erenkaynar.library.earthquake.utils.Callable; -public abstract class EarthquakeAPICallable extends Callable> { +public class EarthquakeAPICallable extends Callable> { - public abstract URL getUrl() throws Exception; + private Source source; + private Parser parser; + + public EarthquakeAPICallable(Source source) { + this.source = source; + } + + @Override + protected ArrayList call() throws Exception { + URLConnection connection = new URL(source.getUrl()).openConnection(); + + final Charset charset; + char a = '\n'; + switch (source) { + case KANDILLI: + charset = Charset.forName("windows-1254"); + parser = new KandilliParser(); + break; + case USGS: + case EMSC: + charset = Charset.defaultCharset(); + parser = new TextParser(); + break; + case AFAD: + charset = StandardCharsets.UTF_8; + parser = new AfadParser(); + a = ' '; + break; + default: + return null; + } + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset)); + String line; + StringBuilder data = new StringBuilder(); + while ((line = reader.readLine()) != null) + data.append(line).append(a); + reader.close(); + + return parser.parse(data.toString()); + } } diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/afad/AfadCallable.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/AfadParser.java similarity index 63% rename from library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/afad/AfadCallable.java rename to library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/AfadParser.java index 878d8ef..a6e7424 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/afad/AfadCallable.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/AfadParser.java @@ -1,45 +1,22 @@ -package tr.com.erenkaynar.library.earthquake.sources.afad; +package tr.com.erenkaynar.library.earthquake.sources.parsers; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import tr.com.erenkaynar.library.earthquake.Constants; import tr.com.erenkaynar.library.earthquake.enums.Source; import tr.com.erenkaynar.library.earthquake.models.Earthquake; import tr.com.erenkaynar.library.earthquake.models.LatLong; -import tr.com.erenkaynar.library.earthquake.sources.EarthquakeAPICallable; -public class AfadCallable extends EarthquakeAPICallable { +public class AfadParser extends Parser { @Override - public URL getUrl() throws Exception { - return new URL(Constants.AFAD_URL); - } - - @Override - protected ArrayList call() throws Exception { - URLConnection connection = getUrl().openConnection(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); - String line; - StringBuilder data = new StringBuilder(); - while ((line = reader.readLine()) != null) data.append(line); - reader.close(); - - return parse(data.toString()); - } - - private ArrayList parse(String data) { + public ArrayList parse(String data) { ArrayList earthquakes = new ArrayList<>(); Document html = Jsoup.parse(data); @@ -70,4 +47,4 @@ private ArrayList parse(String data) { } return earthquakes; } -} +} \ No newline at end of file diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/kandilli/KandilliCallable.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/KandilliParser.java similarity index 69% rename from library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/kandilli/KandilliCallable.java rename to library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/KandilliParser.java index 1b37588..11bf36f 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/kandilli/KandilliCallable.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/KandilliParser.java @@ -1,46 +1,30 @@ -package tr.com.erenkaynar.library.earthquake.sources.kandilli; +package tr.com.erenkaynar.library.earthquake.sources.parsers; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.Charset; import java.time.Instant; import java.util.ArrayList; import java.util.Calendar; -import java.util.List; import java.util.TimeZone; -import tr.com.erenkaynar.library.earthquake.Constants; import tr.com.erenkaynar.library.earthquake.enums.Source; import tr.com.erenkaynar.library.earthquake.models.Earthquake; import tr.com.erenkaynar.library.earthquake.models.LatLong; import tr.com.erenkaynar.library.earthquake.models.Revised; -import tr.com.erenkaynar.library.earthquake.sources.EarthquakeAPICallable; - -public class KandilliCallable extends EarthquakeAPICallable { - @Override - public URL getUrl() throws Exception { - return new URL(Constants.KANDILLI_URL); - } +public class KandilliParser extends Parser { @Override - protected ArrayList call() throws Exception { - URLConnection connection = getUrl().openConnection(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("windows-1254"))); - ArrayList lines = new ArrayList<>(); - String line; - while ((line = reader.readLine()) != null) - lines.add(line); - reader.close(); + public ArrayList parse(String data) { + ArrayList earthquakes = new ArrayList<>(); - return parse(lines); - } + Document document = Jsoup.parse(data); + Elements pre = document.getElementsByTag("pre"); + String[] lines = pre.html().split("\n"); - private ArrayList parse(ArrayList lines) { - ArrayList earthquakes = new ArrayList<>(); - for (int i = arrayFirstIndexOf(lines, "
") + 7; i < arrayFirstIndexOf(lines, "
") - 1; i++) { - String earthquakeString = lines.get(i); + for (int i = 6; i < lines.length; i++) { + String earthquakeString = lines[i]; Earthquake earthquake = new Earthquake(Source.KANDILLI); @@ -75,9 +59,9 @@ private Revised getRevised(String data) { return new Revised(Integer.parseInt(revised.substring(7, 8)), date.toInstant()); } - private int arrayFirstIndexOf(List array, String index) { - for (int i = 0; i < array.size(); i++) { - if (array.get(i).contains(index)) + private int arrayFirstIndexOf(String[] array, String index) { + for (int i = 0; i < array.length; i++) { + if (array[i].contains(index)) return i; } return -1; diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/Parser.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/Parser.java new file mode 100644 index 0000000..1d1022d --- /dev/null +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/Parser.java @@ -0,0 +1,10 @@ +package tr.com.erenkaynar.library.earthquake.sources.parsers; + +import java.util.ArrayList; + +import tr.com.erenkaynar.library.earthquake.models.Earthquake; + +public abstract class Parser { + + public abstract ArrayList parse(String data); +} diff --git a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/usgs/UsgsCallable.java b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/TextParser.java similarity index 52% rename from library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/usgs/UsgsCallable.java rename to library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/TextParser.java index c9e462f..904d5d7 100644 --- a/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/usgs/UsgsCallable.java +++ b/library/src/main/java/tr/com/erenkaynar/library/earthquake/sources/parsers/TextParser.java @@ -1,36 +1,23 @@ -package tr.com.erenkaynar.library.earthquake.sources.usgs; +package tr.com.erenkaynar.library.earthquake.sources.parsers; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.net.URLConnection; import java.time.Instant; import java.util.ArrayList; +import java.util.Arrays; -import tr.com.erenkaynar.library.earthquake.Constants; import tr.com.erenkaynar.library.earthquake.enums.Source; import tr.com.erenkaynar.library.earthquake.models.Earthquake; import tr.com.erenkaynar.library.earthquake.models.LatLong; -import tr.com.erenkaynar.library.earthquake.sources.EarthquakeAPICallable; - -public class UsgsCallable extends EarthquakeAPICallable { +public class TextParser extends Parser { @Override - protected ArrayList call() throws Exception { - URLConnection connection = getUrl().openConnection(); - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - ArrayList lines = new ArrayList<>(); - String line; - while ((line = reader.readLine()) != null) lines.add(line); - reader.close(); - lines.remove(0); - - return parse(lines); - } - - private ArrayList parse(ArrayList lines) { + public ArrayList parse(String data) { ArrayList earthquakes = new ArrayList<>(); + final String[] linesArr = data.split("\n"); + final ArrayList lines = new ArrayList<>(Arrays.asList(linesArr)); + lines.remove(lines.size() - 1); + lines.remove(0); + for (String line : lines) { Earthquake earthquake = parseLine(line); earthquakes.add(earthquake); @@ -55,14 +42,9 @@ private Earthquake parseLine(String line) { earthquake.setMagnitude(Double.parseDouble(values[10])); earthquake.setLocation(values[12]); - earthquake.setDatetime(Instant.parse(values[1] + "Z")); + earthquake.setDatetime(Instant.parse(values[1] + (values[1].contains("Z") ? "" : "Z"))); earthquake.setRevised(null); return earthquake; } - - @Override - public URL getUrl() throws Exception { - return new URL(Constants.USGS_URL); - } -} +} \ No newline at end of file