From 24a0a3b96f5ed8a249c9f09d54375f3e4364ae88 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 14 Jan 2016 17:45:32 +0100 Subject: [PATCH] Improve chapter recognition. --- .../data/database/DatabaseHelper.java | 4 +- .../ui/library/LibraryPresenter.java | 4 +- .../mangafeed/util/ChapterRecognition.java | 38 +++++++++++-------- .../mangafeed/ChapterRecognitionTest.java | 14 +++++++ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java index 94f36ec705..a9aa09ee79 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/database/DatabaseHelper.java @@ -269,8 +269,10 @@ public class DatabaseHelper { if (!toAdd.isEmpty()) { // Set the date fetch for new items in reverse order to allow another sorting method. // Sources MUST return the chapters from most to less recent, which is common. + long now = new Date().getTime(); + for (int i = toAdd.size() - 1; i >= 0; i--) { - toAdd.get(i).date_fetch = new Date().getTime(); + toAdd.get(i).date_fetch = now++; } added = insertChapters(toAdd).executeAsBlocking().numberOfInserts(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java index 8e3992a341..8e14e0077b 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryPresenter.java @@ -44,7 +44,9 @@ public class LibraryPresenter extends BasePresenter { this::getLibraryObservable, (view, pair) -> view.onNextLibraryUpdate(pair.first, pair.second)); - start(GET_LIBRARY); + if (savedState == null) { + start(GET_LIBRARY); + } } @Override diff --git a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java index 862b6e2e15..1564eeaad1 100644 --- a/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java +++ b/app/src/main/java/eu/kanade/mangafeed/util/ChapterRecognition.java @@ -10,16 +10,18 @@ import eu.kanade.mangafeed.data.database.models.Manga; public class ChapterRecognition { - private static Pattern p1 = Pattern.compile("Ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); - private static Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); - private static Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); + private static final Pattern p1 = Pattern.compile("ch[^0-9]?\\s*(\\d+[\\.,]?\\d*)"); + private static final Pattern p2 = Pattern.compile("(\\d+[\\.,]?\\d*)"); + private static final Pattern p3 = Pattern.compile("(\\d+[\\.,]?\\d*\\s*:)"); + + private static final Pattern pUnwanted = + Pattern.compile("\\b(v|ver|vol|version|volume)\\.?\\s*\\d+\\b"); public static void parseChapterNumber(Chapter chapter, Manga manga) { if (chapter.chapter_number != -1) return; - // Remove spaces and convert to lower case - String name = chapter.name; + String name = chapter.name.toLowerCase(); Matcher matcher; // Safest option, the chapter has a token prepended @@ -29,34 +31,38 @@ public class ChapterRecognition { return; } - List occurences; + // Remove anything related to the volume or version + name = pUnwanted.matcher(name).replaceAll(""); + + List occurrences; // If there's only one number, use it matcher = p2.matcher(name); - occurences = getAllOccurrences(matcher); - if (occurences.size() == 1) { - chapter.chapter_number = occurences.get(0); + occurrences = getAllOccurrences(matcher); + if (occurrences.size() == 1) { + chapter.chapter_number = occurrences.get(0); return; } // If it has a colon, the chapter number should be that one matcher = p3.matcher(name); - occurences = getAllOccurrences(matcher); - if (occurences.size() == 1) { - chapter.chapter_number = occurences.get(0); + occurrences = getAllOccurrences(matcher); + if (occurrences.size() == 1) { + chapter.chapter_number = occurrences.get(0); return; } - name = replaceIrrelevantCharacters(name); + // This can lead to issues if two numbers are separated by an space + name = name.replaceAll("\\s+", ""); // Try to remove the manga name from the chapter, and try again String mangaName = replaceIrrelevantCharacters(manga.title); String nameWithoutManga = difference(mangaName, name); if (!nameWithoutManga.isEmpty()) { matcher = p2.matcher(nameWithoutManga); - occurences = getAllOccurrences(matcher); - if (occurences.size() == 1) { - chapter.chapter_number = occurences.get(0); + occurrences = getAllOccurrences(matcher); + if (occurrences.size() == 1) { + chapter.chapter_number = occurrences.get(0); return; } } diff --git a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java index 1ea49ce460..e99699dd1f 100644 --- a/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java +++ b/app/src/test/java/eu/kanade/mangafeed/ChapterRecognitionTest.java @@ -121,4 +121,18 @@ public class ChapterRecognitionTest { assertThat(c.chapter_number).isEqualTo(84f); } + @Test + public void testWithVersionBeforeAndAnotherNumber() { + Chapter c = createChapter("Onepunch-Man Punch Ver002 086 : Creeping Darkness [3]"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number).isEqualTo(86f); + } + + @Test + public void testWithVolumeAfterChapter() { + Chapter c = createChapter("Solanin 028 Vol. 2"); + ChapterRecognition.parseChapterNumber(c, randomManga); + assertThat(c.chapter_number).isEqualTo(28f); + } + }