Exemple #1
0
  /** AKA scraping */
  public TorrentStatus getStatus() throws IOException {
    if (!isGetStatusSupported()) {
      throw new IllegalStateException("Get status (scraping) not supported on this tracker");
    }
    String path = announceUrl.getFile();
    int i = path.lastIndexOf('/');
    path = path.substring(0, i + 1) + "scrape" + path.substring(i + ANNOUNCE.length() + 1);

    GetMethod method = new GetMethod(path);
    method.setRequestHeader("User-Agent", "BlackBits/0.1");
    StringBuffer queryString =
        new StringBuffer(announceUrl.getQuery() == null ? "" : announceUrl.getQuery());
    queryString.append("info_hash=" + URLUtils.encode(infoHash.getBytes()));
    method.setQueryString(queryString.toString());
    httpClient.executeMethod(method);

    BDecoder decoder = new BDecoder(method.getResponseBodyAsStream());
    BDictionary dictionary = (BDictionary) decoder.decodeNext();
    BDictionary files = (BDictionary) dictionary.get("files");
    String key = (String) files.keySet().iterator().next();
    BDictionary info = (BDictionary) files.get(key);
    BLong seeders = (BLong) info.get("complete");
    BLong leechers = (BLong) info.get("incomplete");
    BLong numberOfDownloads = (BLong) info.get("downloaded");
    return new TorrentStatus(seeders.intValue(), leechers.intValue(), numberOfDownloads.intValue());
  }
Exemple #2
0
 private TrackerResponse sendEvent(
     long uploadedBytes, long downloadedBytes, long bytesLeftToDownload, String event)
     throws IOException, TrackerCommunicationException {
   GetMethod method = new GetMethod(announceUrl.getFile());
   method.setRequestHeader("User-Agent", "BlackBits/0.1");
   StringBuffer queryString =
       new StringBuffer(announceUrl.getQuery() == null ? "" : announceUrl.getQuery());
   queryString.append("info_hash=" + URLUtils.encode(infoHash.getBytes()));
   queryString.append("&peer_id=" + URLUtils.encode(localPeer.getId()));
   queryString.append("&port=" + localPeer.getPort());
   queryString.append("&uploaded=" + uploadedBytes);
   queryString.append("&downloaded=" + downloadedBytes);
   queryString.append("&left=" + bytesLeftToDownload);
   if (event != null) {
     queryString.append("&event=" + event);
   }
   if (localPeer.getAddress() != null) {
     queryString.append("&ip=" + localPeer.getAddress());
   }
   method.setQueryString(queryString.toString());
   httpClient.executeMethod(method);
   return decodeResponse(method);
 }