Giter Site home page Giter Site logo

masterwok / simple-torrent-android Goto Github PK

View Code? Open in Web Editor NEW
94.0 5.0 30.0 1.16 MB

A torrent client library for Android that utilizes frostwire-jlibtorrent. It supports sequential and simultaneous downloads.

License: MIT License

Kotlin 100.00%
android android-library torrent torrent-client bittorrent p2p

simple-torrent-android's Introduction

Release

[deprecated] simple-torrent-android

I'm no longer maintaining this project due to time constraints.

An Android torrent client library powered by frostwire-jlibtorrent. It supports sequential and simultaneous downloads.

Usage

A single torrent download session is represented as the TorrentSession class. An instance of this class can be used to start, stop, pause, and resume a torrent download. When creating a new session, a TorrentSessionOptions instance is a required constructor parameter used to configure the session. The TorrentSessionListener interface can be implemented and set on the session to receive TorrentSessionStatus updates tied to the lifecycle of the torrent. The TorrentSessionBufferState is one of the properties of the status and represents the current state of the torrent piece buffer.

For example, the following code snippet sequentially downloads the largest file of the provided torrent to the downloads directory:

// http, https, magnet, file, and content Uri types are all supported.
val torrentUri = Uri.parse("http://www.frostclick.com/torrents/video/animation/Big_Buck_Bunny_1080p_surround_frostclick.com_frostwire.com.torrent")

val torrentSessionOptions = TorrentSessionOptions(
        downloadLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        , onlyDownloadLargestFile = true
        , enableLogging = false
        , shouldStream = true
)

val torrentSession = TorrentSession(torrentSessionOptions)

torrentSession.listener = object : TorrentSessionListener {
    
    // Omitted for brevity
    
    override fun onPieceFinished(
        torrentHandle: TorrentHandle
        , torrentSessionStatus: TorrentSessionStatus
    ) { ... }
}

torrentSession.start(context, torrentUri) // Invoke on background thread.
// OR torrentSession.start(bencodeByteArray)

For a more detailed example, please see the demo application alongside this library (screenshot below). Please note, that the demo application does not properly show how to keep sessions alive after the activity is killed. This was a quick and dirty way of showing/testing downloads. A more real-world solution, would be to have the torrent sessions exist within a service that enters the foreground while downloading. By doing this, the sessions will retain network connectivity and will not be murdered by Android the destroyer.

Configuration

Add this in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

and add the following in the dependent module:

dependencies {
    implementation 'com.github.masterwok:simple-torrent-android:0.5.0'
}

Projects using simple-torrent-android

Demo Screenshots

simple-torrent-android's People

Contributors

masterwok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

simple-torrent-android's Issues

[Feature Request] Jackett Search Indexer Support

Can you please add support for Jackett Search indexer, it can be implemented by using Torznab.

Thanks

Jackett works as a proxy server: it translates queries from apps (Sonarr, Radarr, SickRage, CouchPotato, Mylar, DuckieTV, etc) into tracker-site-specific http queries, parses the html response, then sends results back to the requesting software. This allows for getting recent uploads (like RSS) and performing searches. Jackett is a single repository of maintained indexer scraping & translation logic - removing the burden from other apps.

hidden_mining

[Feature Request] Remove individual torrent method?

I can't seem to find a method to remove/stop downloaded torrents. I see that libtorrent manual has such a method.

void remove_torrent(torrent_handle const& h, int options = none);

I don't know much about this dependencies or Java in general, but could it be that the method is available on this session instance?

Improvement: TorrentSession.kt

First, It's a very great SDK and easy to use.
However, I notice some fatal errors in our app's firebase log related to
line 303 to line 326:

private fun downloadUsingNetworkUri(
            downloadLocation: File
            , torrentUrl: URL
    ) {
        val connection = (torrentUrl.openConnection() as HttpURLConnection).apply {
            requestMethod = "GET"
            instanceFollowRedirects = true
        }

        connection.connect()

        if (connection.responseCode != 200) {
            Log.w(Tag, "Unexpected response code returned from server: ${connection.responseCode}")
        }

        val data = connection
                .inputStream
                .readBytes()

        sessionManager.download(
                TorrentInfo.bdecode(data)
                , downloadLocation
        )
    }

I'm guessing that when hostname is not available or somehow url not correct, this method will cause uncaught exceptions: net.ConnectException - Failed to connect to ${some url}. Is it better to put a tray-catch block to catch these exceptions?

[Feature Request]

Add Speed and file size support for downloading torrent . And local Data base Support?

Torrent completed event before torrent completed

First some context on my setup:
I use OpenTracker (tracker) and Transmission(seeder) as docker containers on an cloud. I upload video files to that cloud and add them as torrents to Transmission which will start seeding. I get a magnet url of that torrent and sent it to my android app that uses this project.

I am using a singleton instance of this torrent module for a React Native bridge(less important)

I implemented the events interface

init {
        // Init using context argument
        this.reactContext = reactContext
        this.torrentSession = TorrentSession(torrentSessionOptions)

        this.torrentSession.listener = object : TorrentSessionListener {
            override fun onAddTorrent(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "started a torrent ${torrentHandle.name()}")
            }

            override fun onTorrentFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "torrent completed ${torrentHandle.name()}")
            }

            override fun onPieceFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "down: ${torrentSessionStatus.bytesDownloaded} of ${torrentSessionStatus.bytesWanted}")
                Log.i(thorLoggerName, "progress ${torrentSessionStatus.progress}")
            }
        }
    }

When I add a new torrent to download the onAddTorrent fires.

After that I will expect to see some logging from the onPieceFinished event then eventually a onTorrentFinished.

What happens is that:

  • onAddTorrent fires after torrent is started;
  • onTorrentFinished fires as soon as Transmissions sees the peer (soon after onAddTorrent);
  • some time passes;
  • torrent starts downloading and multiple onPieceFinished fire;
  • eventually download completes with another onTorrentFinished;

On first onTorrentFinished file has 0mb. On second onTorrentFinished file was properly downloaded and can be played back.

To overcome this I had to do a double check in the onTorrentFinished event

            override fun onTorrentFinished(torrentHandle: TorrentHandle, torrentSessionStatus: TorrentSessionStatus) {
                Log.i(thorLoggerName, "torrent completed ${torrentHandle.name()}")

                if(torrentSessionStatus.bytesDownloaded == torrentSessionStatus.bytesWanted){
                    // know for sure that torrent downloaded
                    emit("fileName", torrentHandle.name(), "thor_completed")
                }
            }

Is this expected? Note that I am using magnet torrents.

Error

I got this error when download torrent:
08-20 20:20:19.330 9251-9251/com.sharetv.xperiafan13.androidtv I/TORRENT: https://www.elitetorrent.biz/wp-content/uploads/files/1534528102-Todos%20chicos%20que%20enamore%20[1080p][Latino][wWw.EliteTorrent.BiZ].torrent
08-20 20:20:19.480 9251-9251/com.sharetv.xperiafan13.androidtv E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sharetv.xperiafan13.androidtv, PID: 9251
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1460)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
at java.net.InetAddress.getAllByName(InetAddress.java:787)
at com.android.okhttp.Dns$1.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(Unknown Source:0)
at com.masterwok.simpletorrentandroid.TorrentSession.downloadUsingNetworkUri(TorrentSession.kt:310)
at com.masterwok.simpletorrentandroid.TorrentSession.start(TorrentSession.kt:431)
at com.sharetv.xperiafan13.androidtv.utils.TorrentDownloader.download(TorrentDownloader.kt:41)
at com.sharetv.xperiafan13.androidtv.views.TorrentActivity$createDialog$1.onClick(TorrentActivity.kt:91)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1168)
at android.widget.AdapterView.performItemClick(AdapterView.java:321)
at android.widget.AbsListView.performItemClick(AbsListView.java:1217)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)
at android.widget.AbsListView$3.run(AbsListView.java:4151)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

My code:
class TorrentDownloader {

fun download(data: String, directory: File, context: Context) {
    Log.i("TORRENT", data)
    val torrentUri = Uri.parse(data)

    val torrentSessionOptions = TorrentSessionOptions(
            downloadLocation = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            , onlyDownloadLargestFile = true
            , enableLogging = false
            , shouldStream = true
    )
    val torrentSession = TorrentSession(torrentSessionOptions)
    torrentSession.start(context, torrentUri)
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.