From e65c453bdb262abe59f4d14dbf423c38b48efd41 Mon Sep 17 00:00:00 2001 From: la_you <1665571328@qq.com> Date: Sat, 2 Jan 2021 18:45:00 +0800 Subject: [PATCH] Create UpdateCheck Function --- .idea/vcs.xml | 6 ++++ src/main/java/lau/NovelDL/Main.java | 7 +++-- src/main/java/lau/NovelDL/UpdateCheck.java | 30 +++++++++++++++++++ src/main/java/lau/NovelDL/Weber.java | 2 +- .../kotlin/lau/NovelDL/KotlinFunctions.kt | 3 -- 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/lau/NovelDL/UpdateCheck.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/lau/NovelDL/Main.java b/src/main/java/lau/NovelDL/Main.java index 41cb3fc..d6aa8b9 100644 --- a/src/main/java/lau/NovelDL/Main.java +++ b/src/main/java/lau/NovelDL/Main.java @@ -34,6 +34,7 @@ public static void main(String[] args) throws InterruptedException { "| | \\ | | |_| | | |/ / | |___ | |___ | |_| | | |___ \n" + "|_| \\_| \\_____/ |___/ |_____| |_____| |_____/ |_____| " ); + UpdateCheck.updateCheck(); System.out.print("Enter the first page URL of the novel: "); firstPageURL = KotlinFunctionsKt.readln().trim(); try { @@ -65,7 +66,7 @@ public static void main(String[] args) throws InterruptedException { try { // First dumping String s = replacer(Weber.getWebString(currentPageURL, "UTF-8")); charset = NovelInfosKt.getCharset(s); - if (charset != "UTF.8") + if (!charset.equals("UTF.8")) s = replacer(Weber.getWebString(currentPageURL, charset)); String chapter = NovelInfosKt.getChapterTitle(s); fileOutputStream.write(chapter.getBytes(StandardCharsets.UTF_8)); @@ -77,7 +78,7 @@ public static void main(String[] args) throws InterruptedException { } catch (IOException e) { System.out.println(e.getMessage()); } catch (HTTPException e) { - System.out.println(dumpCount + " FAILED:Target website responsed Code " + e.getStatusCode()); + System.out.println(dumpCount + " FAILED:Target website responded Code " + e.getStatusCode()); } catch (NullPointerException e) { System.out.println("It doesn't seem to be a novel. Do your IP got banned?"); return; @@ -98,7 +99,7 @@ public static void main(String[] args) throws InterruptedException { } catch (IOException e) { System.out.println(e.getMessage()); } catch (HTTPException e) { - System.out.println(dumpCount + " FAILED:Target website responsed Code " + e.getStatusCode()); + System.out.println(dumpCount + " FAILED:Target website responded Code " + e.getStatusCode()); } catch (NullPointerException e) { System.out.println(dumpCount + " FAILED:It doesn't seem to be a novel. REDUMPING IN 10 SECONDS..."); Thread.sleep(10000); diff --git a/src/main/java/lau/NovelDL/UpdateCheck.java b/src/main/java/lau/NovelDL/UpdateCheck.java new file mode 100644 index 0000000..e80f710 --- /dev/null +++ b/src/main/java/lau/NovelDL/UpdateCheck.java @@ -0,0 +1,30 @@ +package lau.NovelDL; + +import java.io.IOException; + +/** + * NovelDL, the best application for Chinese readers + * + * @author layou233 + */ + +public class UpdateCheck { + public static String version = "1.0"; + + public static void updateCheck() { + System.out.println("Version " + version); + String remoteVersion; + try { + remoteVersion = Weber.getWebString("https://raw.githubusercontent.com/layou233/NovelDL/master/src/main/resources/version.txt", "UTF-8"); + } catch ( + IOException e) { + System.out.println("FAILED TO CHECK FOR UPDATES.\n" + + "You can check it yourself at https://github.com/layou233/NovelDL/releases\n"); + return; + } + if (version.equals(remoteVersion)) + System.out.println("Nice! You are using the latest version!\n"); + else System.out.println("THE NovelDL YOU ARE CURRENTLY USING HAS BEEN OUTDATED!\n" + + "Check out newer versions at https://github.com/layou233/NovelDL/releases\n"); + } +} diff --git a/src/main/java/lau/NovelDL/Weber.java b/src/main/java/lau/NovelDL/Weber.java index 5b1c2ed..dc70c9c 100644 --- a/src/main/java/lau/NovelDL/Weber.java +++ b/src/main/java/lau/NovelDL/Weber.java @@ -33,7 +33,7 @@ public static String getWebString(String url, String charset) throws IOException InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[10485760]; // 10MiB Buffer - int len = 0; + int len; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } diff --git a/src/main/kotlin/lau/NovelDL/KotlinFunctions.kt b/src/main/kotlin/lau/NovelDL/KotlinFunctions.kt index 977e344..3d578cd 100644 --- a/src/main/kotlin/lau/NovelDL/KotlinFunctions.kt +++ b/src/main/kotlin/lau/NovelDL/KotlinFunctions.kt @@ -1,8 +1,5 @@ package lau.NovelDL -import java.net.URL -import java.nio.charset.Charset - /** * NovelDL, the best application for Chinese readers *