Skip to content

Commit

Permalink
add serialization and some info in README
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Dec 12, 2022
1 parent 7b73572 commit e10bb57
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,57 @@

![Build](https://github.com/jmir1/aniparse-android/workflows/Pre%20Merge%20Checks/badge.svg)

## Summary

This is an android wrapper for the python module [Aniparse](https://github.com/MeGaNeKoS/Aniparse)

Huge thanks for the useful repos and inspiration go to:
- [@MeGaNeKoS](https://github.com/MeGaNeKoS) for Aniparse,
- [@erengy](https://github.com/erengy) for [Anitomy](https://github.com/erengy/anitomy) and
- [@joaoventura](https://github.com/joaoventura) for [pybridge](https://github.com/joaoventura/pybridge).

## Usage

build.gradle.kts:
```kotlin
dependencies {
implementation("com.github.jmir1:aniparse-android:0.2")
}
```

```kotlin
// [...]
import com.github.jmir1.aniparseandroid.library.Parser
// [...]
Parser.start(context)
val aniparseResult = Parser.parse("[TaigaSubs]_Toradora!_(2008)_-_01v2_-_Tiger_and_Dragon_[1280x720_H.264_FLAC][1234ABCD].mkv")
val animeTitle = aniparseResult?.animeTitle // "ToraDora!"
val episodeTitle = aniparseResult?.episodeTitle // "Tiger and Dragon"
val releaseGroup = aniparseResult?.releaseGroup // "TaigaSubs"
Parser.stop()
// [...]
```

## Building

The scripts in [library-android/buildscripts](https://github.com/jmir1/aniparse-android/tree/main/library-android/buildscripts) only work on Debian-like systems at the moment.
```shell
cd library-android/buildscripts
./download.sh # This will download the Android SDK and NDK and all other requirements of python-for-android
./build_python.sh # This will run p4a to build the Python distribution and copy all libraries and assets to the src folder
cd ../..
./gradlew library-android:build # You can also use Android Studio if you have successfully run build_python.sh.
```

## Known issues

- The library can only be built on Linux.
I have no idea if it's possible to run python-for-android on Windows
and I don't know enough about macOS to bother with it.
- The library archive is quite large at 20MB.
This is because I haven't yet found a way to split the needed assets
according to the processor architecture.

## License

Copyright (C) 2022 jmir1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MainActivity : AppCompatActivity() {
val input = binding.editTextFactorial.text.toString()
val result = Parser.parse(input)

binding.textResult.text = result
result?.let { binding.textResult.text = it.animeTitle }
binding.textResult.visibility = View.VISIBLE
}
}
Expand Down
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ktlint_gradle = "10.2.1"
min_sdk_version = "21"
target_sdk_version = "33"
benmanesversion = "0.44.0"
aniparse = "0.1"
serialization_json = "1.4.1"

[libraries]
Expand All @@ -45,8 +44,8 @@ agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
kgp = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
dokka_gradle_plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka"}
dokka_core = { module = "org.jetbrains.dokka:dokka-core", version.ref = "dokka"}
aniparse_android = { module = "com.github.jmir1:aniparse-android", version.ref = "aniparse" }
kotlin_serialization_json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization.json" }
kotlin_serialization_core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialization.json" }

[plugins]
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
Expand Down
3 changes: 3 additions & 0 deletions library-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ android {

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
}

buildTypes {
Expand Down Expand Up @@ -64,6 +65,8 @@ android {
dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.core.ktx)
implementation(libs.kotlin.serialization.json)
implementation(libs.kotlin.serialization.core)

testImplementation(libs.junit)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package com.github.jmir1.aniparseandroid.library

import android.content.Context
import android.os.Build
import kotlinx.serialization.json.Json
import kotlinx.serialization.decodeFromString

class Parser {
companion object {
/**
* Json serialization object
*/
private val json = Json { ignoreUnknownKeys = true }

/**
* Whether the python interpreter is started or not
*/
Expand Down Expand Up @@ -56,10 +63,11 @@ class Parser {
* @return The parsed result or null if there was an error
* @throws [InterpreterException] if the interpreter isn't started
*/
fun parse(input: String): String? {
fun parse(input: String): AniparseResult? {
if (!isStarted) throw InterpreterException()
val jsonString = call("json.dumps(aniparse.parse('$input'))")
return jsonString
?: return null
return json.decodeFromString<AniparseResult>(jsonString)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.jmir1.aniparseandroid.library

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonTransformingSerializer
import kotlinx.serialization.serializer

@Serializable
data class AniparseResult(
@SerialName("file_name")
val fileName: String,
@SerialName("file_extension")
val fileExtension: String? = null,
@SerialName("release_group")
val releaseGroup: String? = null,
@SerialName("anime_title")
val animeTitle: String? = null,
@Serializable(with=IntListSerializer::class)
@SerialName("anime_year")
val animeYear: List<Int>? = null,
@SerialName("episode_number")
val episodeNumber: Float? = null,
@SerialName("episode_number_alt")
val episodeNumberAlt: Float? = null,
@SerialName("release_version")
val releaseVersion: Float? = null,
@SerialName("episode_title")
val episodeTitle: String? = null,
@Serializable(with=StringListSerializer::class)
@SerialName("video_resolution")
val videoResolution: List<String>? = null,
@Serializable(with=StringListSerializer::class)
@SerialName("video_term")
val videoTerm: List<String>? = null,
@Serializable(with=StringListSerializer::class)
@SerialName("audio_term")
val audioTerm: List<String>? = null,
@SerialName("file_checksum")
val fileChecksum: String? = null,
@SerialName("episode_prefix")
val episodePrefix: String? = null,
@SerialName("anime_season_prefix")
val animeSeasonPrefix: String? = null,
@Serializable(with=StringListSerializer::class)
@SerialName("other")
val other: List<String>? = null,
)

// If the object is not an array, then it is a single object that should be wrapped into the array
object StringListSerializer :
JsonTransformingSerializer<List<String>>(serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement =
if (element !is JsonArray) JsonArray(listOf(element)) else element
}

object IntListSerializer :
JsonTransformingSerializer<List<Int>>(serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement =
if (element !is JsonArray) JsonArray(listOf(element)) else element
}

0 comments on commit e10bb57

Please sign in to comment.