Skip to content

Commit

Permalink
Merge pull request #122 from Diegopyl1209/master
Browse files Browse the repository at this point in the history
torrent changes from aniyomi
  • Loading branch information
LuftVerbot authored Jul 19, 2024
2 parents d2cc26d + a77b673 commit 64672fe
Show file tree
Hide file tree
Showing 62 changed files with 35 additions and 5,749 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@
*.woff binary
*.pyc binary
*.swp binary

*.aar binary
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ dependencies {
// true type parser
implementation(libs.truetypeparser)
// torrserver
implementation(files("libs/server.aar"))
implementation(libs.torrentserver)
}

androidComponents {
Expand Down
Binary file removed app/libs/server-sources.jar
Binary file not shown.
Binary file removed app/libs/server.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import android.os.Environment
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.vector.ImageVector
import eu.kanade.tachiyomi.data.connections.ConnectionsService
import eu.kanade.core.preference.asState
import eu.kanade.tachiyomi.data.connections.ConnectionsService
import eu.kanade.tachiyomi.data.track.Tracker
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.ImmutableMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.IBinder
import android.util.Log
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.torrentServer.TorrentServerApi
Expand Down Expand Up @@ -58,7 +56,6 @@ class TorrentServerService : Service() {
private fun startServer() {
serviceScope.launch {
if (TorrentServerApi.echo() == "") {
if (BuildConfig.DEBUG) Log.d("TorrentService", "startServer()")
torrServer.TorrServer.startTorrentServer(filesDir.absolutePath)
wait(10)
TorrentServerUtils.setTrackersList()
Expand All @@ -68,7 +65,6 @@ class TorrentServerService : Service() {

private fun stopServer() {
serviceScope.launch {
if (BuildConfig.DEBUG) Log.d("TorrentService", "stopServer()")
torrServer.TorrServer.stopTorrentServer()
TorrentServerApi.shutdown()
applicationContext.cancelNotification(Notifications.ID_TORRENT_SERVER)
Expand Down Expand Up @@ -96,7 +92,7 @@ class TorrentServerService : Service() {
)
val builder = context.notificationBuilder(Notifications.CHANNEL_TORRENT_SERVER) {
setSmallIcon(R.drawable.ic_sua)
setContentText(stringResource(MR.strings.torrentserver_isrunning))
setContentText(stringResource(MR.strings.torrentserver_is_running))
setContentTitle(stringResource(MR.strings.app_name))
setAutoCancel(false)
setOngoing(true)
Expand Down Expand Up @@ -134,7 +130,6 @@ class TorrentServerService : Service() {
}
applicationContext.startService(intent)
} catch (e: Exception) {
if (BuildConfig.DEBUG) Log.d("TorrentService", "start() error: ${e.message}")
println(e.stackTrace)
}
}
Expand All @@ -148,7 +143,6 @@ class TorrentServerService : Service() {
}
applicationContext.startService(intent)
} catch (e: Exception) {
if (BuildConfig.DEBUG) Log.d("TorrentService", "stop() error: ${e.message}")
println(e.stackTrace)
}
}
Expand Down
36 changes: 18 additions & 18 deletions app/src/main/java/eu/kanade/tachiyomi/torrentutils/TorrentUtils.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package eu.kanade.tachiyomi.torrentutils

import eu.kanade.tachiyomi.data.torrentServer.TorrentServerApi
import eu.kanade.tachiyomi.torrentutils.model.DeadTorrentException
import eu.kanade.tachiyomi.torrentutils.model.TorrentFile
import eu.kanade.tachiyomi.torrentutils.model.TorrentInfo
import java.net.SocketTimeoutException

/**
* Used by extensions.
*/
@Suppress("UNUSED")
object TorrentUtils {
fun getTorrentInfo(
suspend fun getTorrentInfo(
url: String,
title: String,
): TorrentInfo {
val torrent = TorrentServerApi.addTorrent(url, title, "", "", false)
return TorrentInfo(
torrent.title,
torrent.file_stats?.map { file ->
TorrentFile(file.path, file.id ?: 0, file.length, torrent.hash!!)
} ?: emptyList(),
torrent.hash!!,
torrent.torrent_size!!,
torrent.trackers ?: emptyList(),
)
}

fun getTorrentPlayUrl(
torrent: TorrentInfo,
indexFile: Int = 0,
): String {
return torrent.files[indexFile].toVideoUrl()
@Suppress("SwallowedException")
try {
val torrent = TorrentServerApi.addTorrent(url, title, "", "", false)
return TorrentInfo(
torrent.title,
torrent.file_stats?.map { file ->
TorrentFile(file.path, file.id ?: 0, file.length, torrent.hash!!, torrent.trackers ?: emptyList())
} ?: emptyList(),
torrent.hash!!,
torrent.torrent_size!!,
torrent.trackers ?: emptyList(),
)
} catch (e: SocketTimeoutException) {
throw DeadTorrentException()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
package eu.kanade.tachiyomi.torrentutils.model

import eu.kanade.tachiyomi.data.torrentServer.TorrentServerUtils
import java.io.File
import java.net.URLEncoder

data class TorrentFile(
val path: String,
val indexFile: Int,
val size: Long,
private val torrentHash: String,
private val trackers: List<String> = emptyList(),
) {
override fun toString(): String {
return path
}

private fun getFileName(): String {
return File(path).name
}

fun toVideoUrl(): String {
val encodedName = URLEncoder.encode(getFileName(), "utf8")
return "${TorrentServerUtils.hostUrl}/stream/$encodedName?link=$torrentHash&index=$indexFile&play"
fun toMagnetURI(): String {
val trackers = trackers.joinToString("&tr=") { URLEncoder.encode(it, "UTF-8") }
return "magnet:?xt=urn:btih:$torrentHash${if (trackers.isNotEmpty()) "&tr=$trackers" else ""}&index=$indexFile"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ data class TorrentInfo(
val hash: String,
val size: Long,
val trackers: List<String> = emptyList(),
)
) {
fun setTrackers(trackers: List<String>): TorrentInfo {
return TorrentInfo(title, files, hash, size, trackers)
}
}

class DeadTorrentException : Exception()
21 changes: 0 additions & 21 deletions go/torrserver/bindings/main.go

This file was deleted.

78 changes: 0 additions & 78 deletions go/torrserver/go.mod

This file was deleted.

Loading

0 comments on commit 64672fe

Please sign in to comment.