Skip to content

Commit

Permalink
feat: downloading extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed Apr 19, 2024
1 parent 44178b2 commit 3c46c21
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import tachiyomi.core.util.lang.launchIO
import uy.kohesive.injekt.Injekt
Expand Down Expand Up @@ -266,7 +267,7 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
it.server.name == ep.selectedExtractor
}?.videos?.getOrNull(ep.selectedVideo)
video?.file?.url?.let { url ->
if (video.file.url.startsWith("magnet:") || video.file.url.endsWith(".torrent")) {
if (url.startsWith("magnet:") || url.endsWith(".torrent")) {
val torrentExtension = Injekt.get<TorrentAddonManager>()
if (torrentExtension.isAvailable()) {
val activity = currActivity() ?: requireActivity()
Expand All @@ -275,12 +276,12 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
torrentExtension.torrentHash?.let {
extension.removeTorrent(it)
}
val index = if (video.file.url.contains("index=")) {
video.file.url.substringAfter("index=").toIntOrNull() ?: 0
val index = if (url.contains("index=")) {
url.substringAfter("index=").toIntOrNull() ?: 0
} else 0
Logger.log("Sending: ${video.file.url}, ${video.quality}, $index")
Logger.log("Sending: ${url}, ${video.quality}, $index")
val currentTorrent = extension.addTorrent(
video.file.url, video.quality.toString(), "", "", false
url, video.quality.toString(), "", "", false
)
torrentExtension.torrentHash = currentTorrent.hash
video.file.url = extension.getLink(currentTorrent, index)
Expand Down Expand Up @@ -483,6 +484,34 @@ class SelectorDialogFragment : BottomSheetDialogFragment() {
val subtitleNames = subtitles.map { it.language }
var subtitleToDownload: Subtitle? = null
val activity = currActivity() ?: requireActivity()
selectedVideo?.file?.url?.let { url ->
if (url.startsWith("magnet:") || url.endsWith(".torrent")) {
val torrentExtension = Injekt.get<TorrentAddonManager>()
if (!torrentExtension.isAvailable()) {
snackString("Torrent Extension not available")
return@setSafeOnClickListener
}
runBlocking {
withContext(Dispatchers.IO) {
val extension = torrentExtension.extension!!.extension
torrentExtension.torrentHash?.let {
extension.removeTorrent(it)
}
val index = if (url.contains("index=")) {
url.substringAfter("index=").toIntOrNull() ?: 0
} else 0
Logger.log("Sending: ${url}, ${selectedVideo.quality}, $index")
val currentTorrent = extension.addTorrent(
url, selectedVideo.quality.toString(), "", "", false
)
torrentExtension.torrentHash = currentTorrent.hash
selectedVideo.file.url =
extension.getLink(currentTorrent, index)
Logger.log("Received: ${selectedVideo.file.url}")
}
}
}
}
if (subtitles.isNotEmpty()) {
val alertDialog = AlertDialog.Builder(context, R.style.MyPopup)
.setTitle("Download Subtitle")
Expand Down

1 comment on commit 3c46c21

@rebelonion
Copy link
Owner Author

Choose a reason for hiding this comment

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

torrent*

Please sign in to comment.