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 6 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
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
*.woff binary
*.pyc binary
*.swp binary

# Libs
*.aar binary
Diegopyl1209 marked this conversation as resolved.
Show resolved Hide resolved
54 changes: 54 additions & 0 deletions .github/workflows/build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,60 @@ jobs:
steps:
- name: Clone repo
uses: actions/checkout@v4

- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
gofiles:
- 'go/**'

- name: Set up Go
if: steps.filter.outputs.gofiles == 'true'
uses: actions/setup-go@v2
with:
go-version: '1.21'

- name: Install Android SDK
if: steps.filter.outputs.gofiles == 'true'
uses: android-actions/setup-android@v2
with:
sdk-version: '30'

- name: Install Android NDK
if: steps.filter.outputs.gofiles == 'true'
uses: nttld/setup-ndk@v1
with:
ndk-version: r25b

- name: Install GoMobile
if: steps.filter.outputs.gofiles == 'true'
run: go install golang.org/x/mobile/cmd/gomobile@latest

- name: Build GoMobile app
if: steps.filter.outputs.gofiles == 'true'
run: |
cd go/torrserver
go get golang.org/x/mobile/bind
gomobile init
cd bindings
gomobile bind -target=android -androidapi 23 -ldflags "-s -w" -o ../../../app/libs/server.aar

- name: Commit build library
if: steps.filter.outputs.gofiles == 'true'
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add app/libs/server.aar
git add app/libs/server-sources.jar
git commit -m "Add compiled torrserver library"

- name: Push changes
if: steps.filter.outputs.gofiles == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1
Expand Down
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(files("libs/server.aar"))
}

androidComponents {
Expand Down
Binary file added app/libs/server-sources.jar
Binary file not shown.
Binary file added app/libs/server.aar
Binary file not shown.
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 @@ -18,10 +18,14 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
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 All @@ -38,6 +42,7 @@ import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.collections.immutable.toPersistentMap
import tachiyomi.core.i18n.stringResource
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.components.WheelTextPicker
import tachiyomi.presentation.core.i18n.stringResource
Expand All @@ -55,6 +60,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 +90,7 @@ object SettingsPlayerScreen : SearchableSettings {
playerPreferences = playerPreferences,
basePreferences = basePreferences,
),
getTorrentServerGroup(torrentServerPreferences),
)
}

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

@Composable
private fun getTorrentServerGroup(
torrentServerPreferences: TorrentServerPreferences,
): Preference.PreferenceGroup {
val scope = rememberCoroutineScope()
val context = LocalContext.current
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 = context.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()
context.stringResource(MR.strings.requires_app_restart)
},
),
),
)
}

@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 (isTor(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 (isTor(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,15 @@ class AnimeDownloader(
}
}

private fun isTor(video: Video): Boolean {
return when {
video.videoUrl?.startsWith("magnet") == true -> true
video.videoUrl?.endsWith(".torrent") == true -> true
video.videoUrl?.startsWith(TorrentServerUtils.hostUrl) == true -> true
else -> false
}
}
Diegopyl1209 marked this conversation as resolved.
Show resolved Hide resolved

private fun isMpd(video: Video): Boolean {
return video.videoUrl?.toHttpUrl()?.encodedPath?.endsWith(".mpd") ?: false
}
Expand All @@ -525,6 +541,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
Loading
Loading