Skip to content

Commit

Permalink
updated aria2c lib + added all aria2c options
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Oct 25, 2023
1 parent 766529d commit 3c92cde
Show file tree
Hide file tree
Showing 10 changed files with 1,359 additions and 168 deletions.
115 changes: 16 additions & 99 deletions app/src/main/java/com/lagradost/fetchbutton/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu
import com.lagradost.fetchbutton.NotificationMetaData
import com.lagradost.fetchbutton.aria2c.Aria2Settings
import com.lagradost.fetchbutton.aria2c.Aria2Starter
import com.lagradost.fetchbutton.aria2c.DownloadStatusTell
import com.lagradost.fetchbutton.aria2c.newUriRequest
import com.lagradost.fetchbutton.ui.PieFetchButton
import java.io.File
import java.util.*
import kotlin.concurrent.thread

Expand Down Expand Up @@ -66,114 +64,33 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val downloadButton = findViewById<PieFetchButton>(R.id.download_button)
val dir = "${filesDir.path}/session"

thread {
Aria2Starter.start(
this,
Aria2Settings(
UUID.randomUUID().toString(),
4337,
filesDir.path,
//"${filesDir.path}/session"
dir,
)
)
}

val pId = 1L
downloadButton.setPersistentId(pId)

//val image = BitmapFactory.decodeResource(resources, R.drawable.example_poster)

val uriReq = newUriRequest(
pId,
"https://speed.hetzner.de/100MB.bin",
"Hello World",
"/storage/emulated/0/Download",
notificationMetaData = NotificationMetaData(
pId.toInt(),
0xFF0000,
"contentTitle",
"Subtext",
"https://www.royalroadcdn.com/public/covers-full/36735-the-perfect-run.jpg",
"SpeedTest",
"secondRow"
val tmpdir = "${cacheDir.absolutePath}/torrent_tmp"
File(tmpdir).deleteRecursively()
val id = 0L

val uriReq = newUriRequest(
id = id,
uri = "",
fileName = "Hello World",
directory = tmpdir,
seed = false
)
)

//downloadButton.setDefaultClickListener {
// listOf(uriReq)
//}

downloadButton.setOnClickListener { view ->
if (view !is PieFetchButton) return@setOnClickListener
val id = view.persistentId
when (view.currentStatus) {
null, DownloadStatusTell.Removed -> {
view.setStatus(DownloadStatusTell.Waiting)
Aria2Starter.download(
uriReq
)
}
DownloadStatusTell.Paused -> {
view.popupMenuNoIcons(listOf(1 to "Resume", 2 to "Play Files", 3 to "Delete")) {
when (itemId) {
1 -> if (!view.resumeDownload()) {
Aria2Starter.download(
uriReq
)
}
2 -> {
val files = view.getFiles()
println("FILES:$files")
}
3 -> {
view.deleteAllFiles()
}
}
}

}
DownloadStatusTell.Complete -> {
view.popupMenuNoIcons(listOf(2 to "Play Files", 3 to "Delete")) {
when (itemId) {
2 -> {
val files = view.getFiles()
println("FILES:$files")
}
3 -> {
view.deleteAllFiles()
}
}
}
}
DownloadStatusTell.Active -> {
view.pauseDownload()
}
DownloadStatusTell.Error -> {
view.redownload()
}
DownloadStatusTell.Waiting -> {
println("REMOVED")
}
else -> {}
}
Aria2Starter.download(
uriReq
)
}
//downloadButton.setDefaultClickListener {
// val id = downloadButton.persistentId ?: return@setDefaultClickListener listOf()
// listOf(
// //newUriRequest(id,"magnet:?xt=urn:btih:f7a543c036b06ba973beea56e98543c9e315314d&dn=%5BSubsPlease%5D%20Yofukashi%20no%20Uta%20-%2010%20%281080p%29%20%5B52314177%5D.mkv&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce")
// newUriRequest(
// id, "https://speed.hetzner.de/1010MB.bin", "Hello World",
// ),
// newUriRequest(
// id, "https://speed.hetzner.de/100MB.bin", "Hello World2",
// ),
// //newUriRequest(
// // id, "https://speed.hetzner.de/100MB.bin", "Hello World",
// //)
// )
//}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -364,33 +364,39 @@ abstract class AbstractClient(
).map { async { send<JsonTellParent>(it) } }.map { it.await() }
}

private fun Any.toJson(): String {
if (this is String) return this
return mapper.writeValueAsString(this)
}

private fun createUriRequest(data: UriRequest) = createRequest(
Method.ADD_URI,
JSONArray().apply { data.uris.forEach { uri -> put(uri) } }, // uris
JSONObject().apply {
if (data.headers.isNotEmpty()) {
if (data.args.headers.isNotEmpty()) {
val array = JSONArray()
for ((key, value) in data.headers) {
for ((key, value) in data.args.headers) {
array.put("$key:$value")
}
put("header", array)
}
if (data.fileName != null)
put("out", data.fileName)
if (data.directory != null)
put("dir", data.directory)
if (data.checkIntegrity != null)
put("check-integrity", data.checkIntegrity)
if (data.continueDownload != null)
put("continue ", data.continueDownload)
if (data.userAgent != null)
put("user-agent ", data.userAgent)
if (data.seedRatio != null)
put("seed-ratio", data.seedRatio)
if (data.seedTime != null)
put("seed-time", data.seedTime)
if (data.referer != null)
put("referer", data.referer)

if (data.args.btExcludeTracker.isNotEmpty()) {
put("bt-exclude-tracker", data.args.btExcludeTracker.joinToString(separator = ","))
}
if (data.args.btTracker.isNotEmpty()) {
put("bt-tracker", data.args.btTracker.joinToString(separator = ","))
}
if (data.args.noProxy.isNotEmpty()) {
put("no-proxy", data.args.noProxy.joinToString(separator = ","))
}

// kinda shitty solution, but it is the easiest. skip all _ as those are lists
val jsonList = parseJson<Map<String, Any>>(data.args.toJson())
for ((k, v) in jsonList) {
if(k.startsWith('_')) continue
put(k,v.toString())
}
}, // options
Int.MAX_VALUE // position, max to push to the last position within the queue
)
Expand Down
Loading

0 comments on commit 3c92cde

Please sign in to comment.