Skip to content

Commit

Permalink
fixed issues with magnet links
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Oct 30, 2024
1 parent 620d006 commit 0b1399f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ dependencies {
Level 25 or Less. */

// torrent support
implementation("com.github.recloudstream:Aria2cStream:0.0.3")
implementation("com.github.recloudstream:Aria2cStream:0.0.6")

// Downloading & Networking
implementation("androidx.work:work-runtime:2.9.0")
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@

<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.OPEN_DOCUMENT" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="magnet" />
</intent-filter>
<!--<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/x-bittorrent" />
</intent-filter>-->
</activity>
<!--
android:launchMode="singleTask"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ class CS3IPlayer : IPlayer {
eventLooperIndex += 1
val currentIndex = eventLooperIndex
while (currentIndex == eventLooperIndex) {
Aria2Starter.refresh()
DownloadListener.sessionIdToGid[activeTorrentRequest?.requestId]?.let { gid ->
val metadata = DownloadListener.getInfo(gid)
event(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DownloadedPlayerActivity : AppCompatActivity() {

val data = intent.data

if (intent?.action == Intent.ACTION_SEND) {
if (intent?.action == Intent.ACTION_SEND || intent?.action == Intent.ACTION_OPEN_DOCUMENT || intent?.action == Intent.ACTION_VIEW) {
val extraText = normalSafeApiCall { // I dont trust android
intent.getStringExtra(Intent.EXTRA_TEXT)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ object OfflinePlaybackHelper {
}

fun playUri(activity: Activity, uri: Uri) {
if (uri.scheme == "magnet") {
playLink(activity, uri.toString())
return
}
val name = SafeFile.fromUri(activity, uri)?.name()
activity.navigate(
R.id.global_to_navigation_player, GeneratorPlayer.newInstance(
Expand All @@ -33,7 +37,8 @@ object OfflinePlaybackHelper {
name = name ?: getString(activity, R.string.downloaded_file),
// well not the same as a normal id, but we take it as users may want to
// play downloaded files and save the location
id = kotlin.runCatching { ContentUris.parseId(uri) }.getOrNull()?.hashCode()
id = kotlin.runCatching { ContentUris.parseId(uri) }.getOrNull()
?.hashCode()
)
)
)
Expand Down
82 changes: 50 additions & 32 deletions app/src/main/java/com/lagradost/cloudstream3/ui/player/Torrent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ object Torrent {
): ExtractorUri {
val minimumBytes: Long = 30 shl 20
var hasFileChecked = false
val defaultWait = 10
var extraWait = defaultWait
while (true) {
val gid = DownloadListener.sessionIdToGid[requestId]

Expand All @@ -115,15 +117,43 @@ object Torrent {
connections = metadata.items.sumOf { it.connections }
)
)
if(metadata.status != DownloadStatusTell.Complete) {
extraWait = defaultWait
}
when (metadata.status) {
// if completed/error/removed then we don't have to wait anymore
DownloadStatusTell.Complete,
DownloadStatusTell.Error,
DownloadStatusTell.Removed -> {
Log.i(TAG, "awaitAria2c, Completed with status = $metadata")
break
}

// some things are downloaded in multiple parts, and is therefore important
// to wait a bit extra
DownloadStatusTell.Complete -> {
// we have waited extra, but no extra data is found
if (extraWait < 0) {
break
}
val gids = metadata.items.map { it.gid }

// if any follower is not found in the gids,
// then Complete is invalid, and we wait a bit extra
if (metadata.items.any { item ->
item.followedBy.any { follow ->
!gids.contains(
follow
)
}
}) {
extraWait -= 1
delay(500)
continue
} else {
break
}
}

// if waiting to be added, wait more
DownloadStatusTell.Waiting -> {
delay(1000)
Expand Down Expand Up @@ -159,15 +189,6 @@ object Torrent {
continue
}

// if downloading then check if we have reached a stable file length
/*DownloadStatusTell.Active -> {
if (getPlayableFile(metadata, minimumBytes = 50 shl 20) != null) {
break
}
delay(1000)
continue
}*/

// unpause any pending files
DownloadStatusTell.Paused -> {
Aria2Starter.unpause(gid)
Expand All @@ -190,9 +211,15 @@ object Torrent {
when (metadata.status) {
DownloadStatusTell.Active, DownloadStatusTell.Complete -> {
val uri = getPlayableFile(metadata, minimumBytes = minimumBytes)
?: throw Exception("Not downloaded enough")
?: throw Exception(
if (metadata.status == DownloadStatusTell.Active) {
"No playable file found, this should never happened"
} else {
"Completed, but no playable file of ${minimumBytes shr 20}MB found"
}
)
return ExtractorUri(
// we require at least 10MB to play the file
// we require at least x MB to play the file
uri = uri,
name = link.name,
tvType = TvType.Torrent
Expand Down Expand Up @@ -221,7 +248,7 @@ object Torrent {
}
}

fun release() = ioSafe {
fun release() {
pauseAll()
// TODO move this into Aria2Starter
/*normalSafeApiCall { (Aria2Starter.client as? WebsocketClient)?.close() }
Expand All @@ -231,14 +258,8 @@ object Torrent {
Aria2Starter.aria2 = null*/
}

private suspend fun pauseAll() {
if (Aria2Starter.client == null) return
for ((_, gid) in DownloadListener.sessionIdToGid) {
val current = DownloadListener.getInfo(gid)
if (current.status == null || current.status == DownloadStatusTell.Active) {
Aria2Starter.client?.pauseAsync(gid, true)
}
}
private fun pauseAll() {
Aria2Starter.pauseAll()
}

@Throws
Expand Down Expand Up @@ -361,18 +382,15 @@ object Torrent {
val defaultDirectory = "${act.cacheDir.path}/torrent_tmp"

// start the client if not active, lazy init
if (Aria2Starter.aria2 == null) {
Aria2Starter.start(
activity = act,
Aria2Settings(
UUID.randomUUID().toString(),
6800, // https://github.com/devgianlu/aria2lib/blob/d34fd083835775cdf65f170437575604e96b602e/src/main/java/com/gianlu/aria2lib/internal/Aria2.java#L382
defaultDirectory,
)
Aria2Starter.start(
activity = act,
Aria2Settings(
UUID.randomUUID().toString(),
6800, // https://github.com/devgianlu/aria2lib/blob/d34fd083835775cdf65f170437575604e96b602e/src/main/java/com/gianlu/aria2lib/internal/Aria2.java#L382
defaultDirectory,
)
// remove all the cache
//File(defaultDirectory).deleteRecursively()
}
)

return playAria2c(link, event)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ import com.lagradost.cloudstream3.extractors.VidHidePro2
import com.lagradost.cloudstream3.extractors.VidHidePro3
import com.lagradost.cloudstream3.extractors.Voe1
import com.lagradost.cloudstream3.extractors.Wishonly
import com.lagradost.cloudstream3.extractors.Beastx
import com.lagradost.cloudstream3.extractors.Beastx
import com.lagradost.cloudstream3.extractors.Playerx
import com.lagradost.cloudstream3.extractors.AnimesagaStream
import com.lagradost.cloudstream3.extractors.Anplay
Expand Down Expand Up @@ -361,12 +361,16 @@ data class ExtractorLinkPlayList(
enum class ExtractorLinkType {
/** Single stream of bytes no matter the actual file type */
VIDEO,

/** Split into several .ts files, has support for encrypted m3u8s */
M3U8,

/** Like m3u8 but uses xml, currently no download support */
DASH,

/** No support at the moment */
TORRENT,

/** No support at the moment */
MAGNET;

Expand All @@ -383,7 +387,12 @@ enum class ExtractorLinkType {
}

private fun inferTypeFromUrl(url: String): ExtractorLinkType {
val path = normalSafeApiCall { URL(url).path }
val path = try {
URL(url).path
} catch (_: Throwable) {
// don't log magnet links as errors
null
}
return when {
path?.endsWith(".m3u8") == true -> ExtractorLinkType.M3U8
path?.endsWith(".mpd") == true -> ExtractorLinkType.DASH
Expand All @@ -392,7 +401,8 @@ private fun inferTypeFromUrl(url: String): ExtractorLinkType {
else -> ExtractorLinkType.VIDEO
}
}
val INFER_TYPE : ExtractorLinkType? = null

val INFER_TYPE: ExtractorLinkType? = null

/**
* UUID for the ClearKey DRM scheme.
Expand Down Expand Up @@ -429,12 +439,12 @@ open class DrmExtractorLink private constructor(
/** Used for getExtractorVerifierJob() */
override val extractorData: String? = null,
override val type: ExtractorLinkType,
open val kid : String,
open val key : String,
open val uuid : UUID,
open val kty : String,
open val kid: String,
open val key: String,
open val uuid: UUID,
open val kty: String,

open val keyRequestParameters : HashMap<String, String>
open val keyRequestParameters: HashMap<String, String>
) : ExtractorLink(
source, name, url, referer, quality, type, headers, extractorData
) {
Expand All @@ -449,11 +459,11 @@ open class DrmExtractorLink private constructor(
headers: Map<String, String> = mapOf(),
/** Used for getExtractorVerifierJob() */
extractorData: String? = null,
kid : String,
key : String,
uuid : UUID = CLEARKEY_UUID,
kty : String = "oct",
keyRequestParameters : HashMap<String, String> = hashMapOf(),
kid: String,
key: String,
uuid: UUID = CLEARKEY_UUID,
kty: String = "oct",
keyRequestParameters: HashMap<String, String> = hashMapOf(),
) : this(
source = source,
name = name,
Expand Down Expand Up @@ -506,7 +516,7 @@ open class ExtractorLink constructor(
}

@JsonIgnore
fun getAllHeaders() : Map<String, String> {
fun getAllHeaders(): Map<String, String> {
if (referer.isBlank()) {
return headers
} else if (headers.keys.none { it.equals("referer", ignoreCase = true) }) {
Expand Down Expand Up @@ -1013,8 +1023,8 @@ val extractorApis: MutableList<ExtractorApi> = arrayListOf(
Ds2play(),
Ds2video(),
Filegram(),
)

)


fun getExtractorApiFromName(name: String): ExtractorApi {
Expand Down

4 comments on commit 0b1399f

@DZFOORT
Copy link

Choose a reason for hiding this comment

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

Image

@DZFOORT
Copy link

Choose a reason for hiding this comment

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

Image

@LagradOst
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DZFOORT That is not a torrent, that is a regular stream, and as such is not even remotely connected to the content of this commit.

@digitalus00
Copy link

Choose a reason for hiding this comment

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

I tried with magnet links it keeps on loading but not loaded and i am using latest one version , Image
But the big buck bunny works. Is there any mistake from my side or what?

Please sign in to comment.