frostwire / frostwire-jlibtorrent

A swig Java interface for libtorrent by the makers of FrostWire. Develop libtorrent based apps with the joy of coding in Java.
http://www.frostwire.com
MIT License
444 stars 137 forks source link

Is it possible to add a listener to get Alerts for a single torrent? #233

Closed abbasshah17 closed 4 years ago

abbasshah17 commented 4 years ago

While attempting to listen to various alerts on a torrent via sessionManager.addListener() you get Alerts for all the torrents added through the session manager. Is there a way to simply listen to Alerts from selected torrents?

Currently I'm matching the name from each Alert to map to the existing torrents and dispatching these events accordingly. But I'm not happy about it, it adds too much verbosity to the code and feels kinda bad/hacky approach.

If currently there is no way to do that, would it be possible to add it to a future release? And meanwhile would you point me in the correct direction on how to achieve the same effect in a better and more compact way?

gubatron commented 4 years ago

Seems like what you ask is very particular to what you need, but we could try to implement an AlertListener that's associated to one or more Sha1Hash objects.


interface OnTorrentAlertHandler {
  void onAlert(Alert<?> alert);
}

public static final class TorrentFilteredAlertListener implements AlertListener {
    private final int[] alertTypes;
    private final HashSet<sha1_hash> hashes = new HashSet<>();
    private final OnTorrentAlertHandler alertHandler;
    public TorrentFilteredAlertListener(int[] alertTypes, OnTorrentAlertHandler alertHandler) {
      this.alertTypes = alertTypes; //so that alerts can be filtered by alert type, ideally you pass 
      this.alertHandler = alertHandler;
    }

   public void trackTorrent(Sha1Hash torrentSha1) {
     hashes.add(torrentSha1.swig());
   }

   public void stopTrackingTorrent(Sha1Hash torrentSha1) {
    hashes.remove(torrentSha1.swig());
   }

   public void alert(Alert<?> alert) {
     torrent_handle th = ((torrent_alert)((TorrentAlert)alert).swig()).getHandle();
     if (th != null && th.is_valid() && alertHandler != null && hashes.contains(th.info_hash()) { 
       try {
         alertHandler.onAlert(alert);  
     } catch (Throwable t) { /* handle exception */  }
   }   
}

You'd then define what to do with the alert by implementing a OnTorrentAlertHandler.

You'd then add a new TorrentFilteredAlertListener as a parameter of the

TorrentFilteredAlertListener torrentListener = new TorrentFilteredAlertListner(types, myOnTorrentAlertHandler);
sessionManager.addListener(torrentListener);

// then if you want to listen to alerts of only one torrent you'd do this:
torrentListener.trackTorrent(torrentHandle.infoHash());

// to stop tracking that torrent's alerts
torrentListener.stopTrackingTorrent(torrentHandle.infoHash());
abbasshah17 commented 4 years ago
torrentHandle.infoHash()

That is exactly the kind of thing I was looking for, I was reluctant to use names of the torrents since they can match with different torrents with same name. I can't believe I didn't think of that, it was right there! Thanks a lot.

Seems like what you ask is very particular to what you need

Really? I'd have imagined this being very useful. Any client downloading more than a single torrent would need similar functionality to update their UI with updates for each torrent separately and not have to go through the entire list of torrents to update a single one.

gubatron commented 4 years ago

yes, there are so many kinds of alerts being fired and as you can see it's trivial to implement your own alert listener.

Most likely this github post will serve as a template for people wanting to do the same as you.

I wouldn't like to introduce this to the API as it then burdens the user into managing the HashSet contents (tracking/untracking), other programmers might have a different idea on how to implement this.

Closing ticket.

Out of curiosity, it'd be awesome to know the product you're using jlibtorrent with, perhaps we could put a link to it on the read me under a section of projects using jlibtorrent.

abbasshah17 commented 4 years ago

Sure, I'd really like to have my app added, but it's not live yet. Have been working on and off for the past couple years, hopefully it'll be live in the next couple of months, atleast with my current dev speed with COVID-19. I'll send you a request for it as soon as I move to production.

gubatron commented 4 years ago

Awesome! Good luck with your release

abbasshah17 commented 3 years ago

@gubatron I finally published the app: YIFY BROWSER if you wanna include it in the ReadMe.

Regarding other issues where my responses are pending, I just wanna say that I haven't forgotten about them, have just been real busy. But now that the app is finally live I would be getting around to them during the weekend.

gubatron commented 3 years ago

Congrats, however 2 issues.

  1. Your app says it's not compatible with any of my devices. (Android phones running Android 10)
  2. Your app will be taken down the moment they review your screenshots, it seems like you have a bunch of copyrighted content showing there. Be careful.
abbasshah17 commented 3 years ago
  1. There are quiet a few Android 10s showing up in my Firebase Console, you might've been logged into playstore using a different account than your phone? If that is not the case can you provide your device's model so I can look it up?
  2. Aargh... I know but that can't be helped, I'm using the open YTS Rest API which rips these copyrighted contents... Hopefully Playstore won't take it down, there already are several apps which use the same API, none that stream the torrents which is where Frostwire comes in. Which could also be the reason for its take down.

But I'm planning on putting the app on other available stores as well.

gubatron commented 3 years ago

OnePlus 7T

gubatron commented 3 years ago
  1. You need to figure that out, that opens you up for a lawsuit as well. A lot of small new torrent clients have ended up in court for dismissing what happened to Grokster. It might even be a lawyer that's not even representing the labels that try to scare you into settling for money. beware, there're thousands of copyright sharks out there.