Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torrent support #1620

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ dependencies {
implementation(libs.seeker)
// true type parser
implementation(libs.truetypeparser)
// torrserver
implementation(libs.torrentserver)
}

androidComponents {
Expand Down
1 change: 1 addition & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
-keep,allowoptimization class eu.kanade.tachiyomi.network.OkHttpExtensionsKt { public protected *; }
-keep,allowoptimization class eu.kanade.tachiyomi.network.RequestsKt { public protected *; }
-keep,allowoptimization class eu.kanade.tachiyomi.AppInfo { public protected *; }
-keep,allowoptimization class eu.kanade.tachiyomi.torrentutils.** { public protected *; }

##---------------Begin: proguard configuration for RxJava 1.x ----------
-dontwarn sun.misc.**
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@
android:foregroundServiceType="dataSync"
tools:node="merge" />

<service
android:name=".data.torrentServer.service.TorrentServerService"
android:foregroundServiceType="dataSync"
/>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import eu.kanade.core.preference.asState
import eu.kanade.domain.base.BasePreferences
import eu.kanade.presentation.more.settings.Preference
import eu.kanade.tachiyomi.data.torrentServer.TorrentServerPreferences
import eu.kanade.tachiyomi.data.torrentServer.service.TorrentServerService
import eu.kanade.tachiyomi.ui.player.JUST_PLAYER
import eu.kanade.tachiyomi.ui.player.MPV_PLAYER
import eu.kanade.tachiyomi.ui.player.MPV_REMOTE
Expand Down Expand Up @@ -55,6 +58,7 @@ object SettingsPlayerScreen : SearchableSettings {
override fun getPreferences(): List<Preference> {
val playerPreferences = remember { Injekt.get<PlayerPreferences>() }
val basePreferences = remember { Injekt.get<BasePreferences>() }
val torrentServerPreferences = remember { Injekt.get<TorrentServerPreferences>() }
val deviceSupportsPip = basePreferences.deviceHasPip()

return listOfNotNull(
Expand Down Expand Up @@ -84,6 +88,7 @@ object SettingsPlayerScreen : SearchableSettings {
playerPreferences = playerPreferences,
basePreferences = basePreferences,
),
getTorrentServerGroup(torrentServerPreferences),
)
}

Expand Down Expand Up @@ -394,6 +399,55 @@ object SettingsPlayerScreen : SearchableSettings {
)
}

@Composable
private fun getTorrentServerGroup(
torrentServerPreferences: TorrentServerPreferences,
): Preference.PreferenceGroup {
val scope = rememberCoroutineScope()
val trackersPref = torrentServerPreferences.trackers()
val trackers by trackersPref.collectAsState()

return Preference.PreferenceGroup(
title = stringResource(MR.strings.pref_category_torrentserver),
preferenceItems = persistentListOf(
Preference.PreferenceItem.EditTextPreference(
pref = torrentServerPreferences.port(),
title = stringResource(MR.strings.pref_torrentserver_port),
onValueChanged = {
try {
Integer.parseInt(it)
TorrentServerService.stop()
true
} catch (e: Exception) {
false
}
},
),
Preference.PreferenceItem.MultiLineEditTextPreference(
pref = torrentServerPreferences.trackers(),
title = stringResource(MR.strings.pref_torrent_trackers),
subtitle = trackersPref.asState(scope).value
.lines().take(2)
.joinToString(
separator = "\n",
postfix = if (trackersPref.asState(scope).value.lines().size > 2) "\n..." else "",
),
onValueChanged = {
TorrentServerService.stop()
true
},
),
Preference.PreferenceItem.TextPreference(
title = stringResource(MR.strings.pref_reset_torrent_trackers_string),
enabled = remember(trackers) { trackers != trackersPref.defaultValue() },
onClick = {
trackersPref.delete()
},
),
),
)
}

@Composable
private fun SkipIntroLengthDialog(
initialSkipIntroLength: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import eu.kanade.tachiyomi.data.download.anime.model.AnimeDownload
import eu.kanade.tachiyomi.data.download.anime.model.AnimeDownloadPart
import eu.kanade.tachiyomi.data.library.anime.AnimeLibraryUpdateNotifier
import eu.kanade.tachiyomi.data.notification.NotificationHandler
import eu.kanade.tachiyomi.data.torrentServer.TorrentServerApi
import eu.kanade.tachiyomi.data.torrentServer.TorrentServerUtils
import eu.kanade.tachiyomi.data.torrentServer.service.TorrentServerService
import eu.kanade.tachiyomi.network.ProgressListener
import eu.kanade.tachiyomi.util.size
import eu.kanade.tachiyomi.util.storage.DiskUtil
Expand Down Expand Up @@ -483,7 +486,9 @@ class AnimeDownloader(

if (downloadScope.isActive) {
file = try {
if (isHls(download.video!!) || isMpd(download.video!!)) {
if (isTorrent(download.video!!)) {
torrentDownload(download, tmpDir, filename)
} else if (isHls(download.video!!) || isMpd(download.video!!)) {
ffmpegDownload(download, tmpDir, filename)
} else {
httpDownload(download, tmpDir, filename, newThreads, safe)
Expand All @@ -503,7 +508,9 @@ class AnimeDownloader(
// otherwise we attempt a final try forcing safe mode
return if (downloadScope.isActive) {
file ?: try {
if (isHls(download.video!!) || isMpd(download.video!!)) {
if (isTorrent(download.video!!)) {
torrentDownload(download, tmpDir, filename)
} else if (isHls(download.video!!) || isMpd(download.video!!)) {
ffmpegDownload(download, tmpDir, filename)
} else {
httpDownload(download, tmpDir, filename, 1, true)
Expand All @@ -517,6 +524,11 @@ class AnimeDownloader(
}
}

private fun isTorrent(video: Video): Boolean {
val url = video.videoUrl ?: return false
return url.startsWith("magnet") || url.endsWith(".torrent") || url.startsWith(TorrentServerUtils.hostUrl)
}

private fun isMpd(video: Video): Boolean {
return video.videoUrl?.toHttpUrl()?.encodedPath?.endsWith(".mpd") ?: false
}
Expand All @@ -525,6 +537,35 @@ class AnimeDownloader(
return video.videoUrl?.toHttpUrl()?.encodedPath?.endsWith(".m3u8") ?: false
}

// this start the torrent server and get the url to download the video
private suspend fun torrentDownload(
download: AnimeDownload,
tmpDir: UniFile,
filename: String,
): UniFile {
val video = download.video!!
TorrentServerService.start()
if (video.videoUrl!!.startsWith(TorrentServerUtils.hostUrl)) {
val hash = video.videoUrl!!.substringAfter("link=").substringBefore("&")
val index = video.videoUrl!!.substringAfter("index=").substringBefore("&").toInt()
val magnet = "magnet:?xt=urn:btih:$hash&index=$index"
video.videoUrl = magnet
}
val currentTorrent = TorrentServerApi.addTorrent(video.videoUrl!!, video.quality, "", "", false)
var index = 0
if (video.videoUrl!!.contains("index=")) {
index = try {
video.videoUrl?.substringAfter("index=")
?.substringBefore("&")?.toInt() ?: 0
} catch (_: Exception) {
0
}
}
val torrentUrl = TorrentServerUtils.getTorrentPlayLink(currentTorrent, index)
video.videoUrl = torrentUrl
return ffmpegDownload(download, tmpDir, filename)
}

// ffmpeg is always on safe mode
private fun ffmpegDownload(
download: AnimeDownload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ object Notifications {
const val CHANNEL_INCOGNITO_MODE = "incognito_mode_channel"
const val ID_INCOGNITO_MODE = -701

/**
* Notification channel and ids used for torrent server
*/
const val CHANNEL_TORRENT_SERVER = "torrent_server_channel"
const val ID_TORRENT_SERVER = -801

/**
* Notification channel and ids used for app and extension updates.
*/
Expand Down Expand Up @@ -162,6 +168,10 @@ object Notifications {
buildNotificationChannel(CHANNEL_INCOGNITO_MODE, IMPORTANCE_LOW) {
setName(context.stringResource(MR.strings.pref_incognito_mode))
},
buildNotificationChannel(CHANNEL_TORRENT_SERVER, IMPORTANCE_LOW) {
setName(context.stringResource(MR.strings.pref_category_torrentserver))
setShowBadge(false)
},
buildNotificationChannel(CHANNEL_APP_UPDATE, IMPORTANCE_DEFAULT) {
setGroup(GROUP_APK_UPDATES)
setName(context.stringResource(MR.strings.channel_app_updates))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package eu.kanade.tachiyomi.data.torrentServer

import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.data.torrentServer.model.Torrent
import eu.kanade.tachiyomi.data.torrentServer.model.TorrentRequest
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.network.awaitSuccess
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.jsoup.Jsoup
import uy.kohesive.injekt.injectLazy
import java.io.InputStream

object TorrentServerApi {
private val network: NetworkHelper by injectLazy()
private val hostUrl = TorrentServerUtils.hostUrl

suspend fun echo(): String {
return try {
network.client.newCall(GET("$hostUrl/echo")).awaitSuccess().body.string()
} catch (e: Exception) {
if (BuildConfig.DEBUG) println(e.message)
""
}
}

suspend fun shutdown(): String {
return try {
network.client.newCall(GET("$hostUrl/shutdown")).awaitSuccess().body.string()
} catch (e: Exception) {
if (BuildConfig.DEBUG) println(e.message)
""
}
}

// / Torrents
suspend fun addTorrent(
link: String,
title: String,
poster: String = "",
data: String = "",
save: Boolean,
): Torrent {
val req =
TorrentRequest(
"add",
link = link,
title = title,
poster = poster,
data = data,
save_to_db = save,
).toString()
val resp =
network.client.newCall(
POST("$hostUrl/torrents", body = req.toRequestBody("application/json".toMediaTypeOrNull())),
).awaitSuccess()
return Json.decodeFromString(Torrent.serializer(), resp.body.string())
}

suspend fun getTorrent(hash: String): Torrent {
val req = TorrentRequest("get", hash).toString()
val resp =
network.client.newCall(
POST("$hostUrl/torrents", body = req.toRequestBody("application/json".toMediaTypeOrNull())),
).awaitSuccess()
return Json.decodeFromString(Torrent.serializer(), resp.body.string())
}

suspend fun remTorrent(hash: String) {
val req = TorrentRequest("rem", hash).toString()
network.client.newCall(
POST("$hostUrl/torrents", body = req.toRequestBody("application/json".toMediaTypeOrNull())),
).awaitSuccess()
}

suspend fun listTorrent(): List<Torrent> {
val req = TorrentRequest("list").toString()
val resp =
network.client.newCall(
POST("$hostUrl/torrents", body = req.toRequestBody("application/json".toMediaTypeOrNull())),
).awaitSuccess()
return Json.decodeFromString<List<Torrent>>(resp.body.string())
}

fun uploadTorrent(
file: InputStream,
title: String,
poster: String,
data: String,
save: Boolean,
): Torrent {
val resp =
Jsoup.connect("$hostUrl/torrent/upload")
.data("title", title)
.data("poster", poster)
.data("data", data)
.data("save", save.toString())
.data("file1", "filename", file)
.ignoreContentType(true)
.ignoreHttpErrors(true)
.post()
return Json.decodeFromString(Torrent.serializer(), resp.body().text())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package eu.kanade.tachiyomi.data.torrentServer

import tachiyomi.core.common.preference.PreferenceStore

class TorrentServerPreferences(
private val preferenceStore: PreferenceStore,
) {
fun port() = preferenceStore.getString("pref_torrent_port", "8090")

fun trackers() = preferenceStore.getString(
abdallahmehiz marked this conversation as resolved.
Show resolved Hide resolved
"pref_torrent_trackers",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmir1 will have to check this...

"""http://nyaa.tracker.wf:7777/announce
http://anidex.moe:6969/announce
http://tracker.anirena.com:80/announce
udp://tracker.uw0.xyz:6969/announce
http://share.camoe.cn:8080/announce
http://t.nyaatracker.com:80/announce
udp://47.ip-51-68-199.eu:6969/announce
udp://9.rarbg.me:2940
udp://9.rarbg.to:2820
udp://exodus.desync.com:6969/announce
udp://explodie.org:6969/announce
udp://ipv4.tracker.harry.lu:80/announce
udp://open.stealth.si:80/announce
udp://opentor.org:2710/announce
udp://opentracker.i2p.rocks:6969/announce
udp://retracker.lanta-net.ru:2710/announce
udp://tracker.cyberia.is:6969/announce
udp://tracker.dler.org:6969/announce
udp://tracker.ds.is:6969/announce
udp://tracker.internetwarriors.net:1337
udp://tracker.openbittorrent.com:6969/announce
udp://tracker.opentrackr.org:1337/announce
udp://tracker.tiny-vps.com:6969/announce
udp://tracker.torrent.eu.org:451/announce
udp://valakas.rollo.dnsabr.com:2710/announce
udp://www.torrent.eu.org:451/announce""".replace(" ", ""),
)
}
Loading