Browse Source

[Anilist] Fix date parsing error (#1805)

fix #1804
DitFranXX 6 years ago
parent
commit
f8e121ee06

+ 11 - 2
app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistApi.kt

@@ -12,6 +12,7 @@ import okhttp3.OkHttpClient
 import okhttp3.Request
 import okhttp3.RequestBody
 import rx.Observable
+import java.util.Calendar
 
 
 class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
@@ -246,10 +247,18 @@ class AnilistApi(val client: OkHttpClient, interceptor: AnilistInterceptor) {
     }
 
     fun jsonToALManga(struct: JsonObject): ALManga{
+        val date = try {
+            val date = Calendar.getInstance()
+            date.set(struct["startDate"]["year"].nullInt ?: 0, (struct["startDate"]["month"].nullInt ?: 0) - 1,
+                    struct["startDate"]["day"].nullInt ?: 0)
+            date.timeInMillis
+        } catch (_: Exception) {
+            0L
+        }
+
         return ALManga(struct["id"].asInt, struct["title"]["romaji"].asString, struct["coverImage"]["large"].asString,
                 struct["description"].nullString.orEmpty(), struct["type"].asString, struct["status"].asString,
-                struct["startDate"]["year"].nullString.orEmpty() + struct["startDate"]["month"].nullString.orEmpty()
-                        + struct["startDate"]["day"].nullString.orEmpty(), struct["chapters"].nullInt ?: 0)
+                date, struct["chapters"].nullInt ?: 0)
     }
 
     fun jsonToALUserManga(struct: JsonObject): ALUserManga{

+ 4 - 7
app/src/main/java/eu/kanade/tachiyomi/data/track/anilist/AnilistModels.kt

@@ -1,6 +1,5 @@
 package eu.kanade.tachiyomi.data.track.anilist
 
-import eu.kanade.tachiyomi.BuildConfig
 import eu.kanade.tachiyomi.data.database.models.Track
 import eu.kanade.tachiyomi.data.preference.PreferencesHelper
 import eu.kanade.tachiyomi.data.preference.getOrDefault
@@ -17,7 +16,7 @@ data class ALManga(
         val description: String?,
         val type: String,
         val publishing_status: String,
-        val start_date_fuzzy: String,
+        val start_date_fuzzy: Long,
         val total_chapters: Int) {
 
     fun toTrack() = TrackSearch.create(TrackManager.ANILIST).apply {
@@ -29,14 +28,12 @@ data class ALManga(
         tracking_url = AnilistApi.mangaUrl(media_id)
         publishing_status = [email protected]_status
         publishing_type = type
-        if (!start_date_fuzzy.isNullOrBlank()) {
+        if (start_date_fuzzy != 0L) {
             start_date = try {
-                val inputDf = SimpleDateFormat("yyyyMMdd", Locale.US)
                 val outputDf = SimpleDateFormat("yyyy-MM-dd", Locale.US)
-                val date = inputDf.parse(BuildConfig.BUILD_TIME)
-                outputDf.format(date)
+                outputDf.format(start_date_fuzzy)
             } catch (e: Exception) {
-                start_date_fuzzy.orEmpty()
+                ""
             }
         }
     }