infohash转换磁力 php,磁力链接或 infohash 如何转换成 torrent

想要开发一个磁力搜索引擎,已经有 DHT 爬虫可以收集 infohash,可是这些 infohash 如何转成 torrent 成了一道难关立在面前。

先考虑用 bittorrent 官方扩展协议获取 metadata,从 github 上找到了 ih2torrent 安装使用后发现运行很久也无法获得数据(难道是因为没有独立 ip 的原因吗?),然后又参考了网上提供的第三方种子平台,如torcache,发现资源也不齐全,随便从收集到的 infohash 中选了一个下载失败。

看到网上各式各样的磁力搜索引擎,感觉自己实在是能有有限,无法找到更好的种子获取方法,想问下他们是怎么及时获取最新种子资源的?

我收藏了一个dht爬虫的源码。你可以看看。我当时没来得及尝试。这个应该是把infohash转换成种子的代码。

https://github.com/78/ssbc/blob/master/workers/ltMetadata.py

两种方式。

去迅雷种子http://bt.box.n0808.com库看有没有,有就索引之。这方法最简单快速。

走bep_009协议,去peer端请求metainfo,索引之。。我的engiy.com磁力搜索,BT搜索就是这样干的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java通过magnet连接下载torrent文件需要用到第三方库,比如BT种子下载器Transmission的Java API。 以下是一个简单的示例代码: ```java import java.net.URL; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import org.json.JSONTokener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.github.axet.wget.WGet; import com.github.axet.wget.info.URLInfo; import com.github.axet.wget.info.ex.DownloadError; import com.github.axet.wget.info.ex.DownloadInterruptedError; import com.github.axet.wget.info.ex.DownloadMultipartError; import com.github.axet.wget.info.ex.DownloadRetry; import com.github.axet.wget.info.ex.DownloadSSLException; import com.github.axet.wget.info.ex.DownloadTimeoutException; import com.github.axet.wget.info.ex.DownloadVerificationException; @RestController public class TorrentDownloadController { private static final Logger LOGGER = LoggerFactory.getLogger(TorrentDownloadController.class); private static final String TRANSMISSION_API_VERSION = "v2.94"; private static final String TRANSMISSION_RPC_URI = "/transmission/rpc"; private static final String TRANSMISSION_RPC_URL = "http://localhost:9091" + TRANSMISSION_RPC_URI; private static final String MAGNET_URI_PREFIX = "magnet:?xt=urn:btih:"; private static final int CONNECT_TIMEOUT = 10000; private static final int SOCKET_TIMEOUT = 30000; private static final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); static { connectionManager.setMaxTotal(100); connectionManager.setDefaultMaxPerRoute(20); } @GetMapping(value = "/download/{magnetUri}", produces = MediaType.APPLICATION_JSON_VALUE) public JSONObject download(@PathVariable String magnetUri) throws Exception { LOGGER.info("Downloading torrent from magnet URI: {}", magnetUri); String torrentUrl = getTorrentUrlFromMagnetUri(magnetUri); LOGGER.info("Torrent URL: {}", torrentUrl); // Download torrent file String torrentFilePath = downloadTorrentFile(torrentUrl); LOGGER.info("Torrent file downloaded to: {}", torrentFilePath); // Add torrent to Transmission JSONObject addTorrentResponse = addTorrentToTransmission(torrentFilePath); LOGGER.info("Add torrent response: {}", addTorrentResponse); return addTorrentResponse; } private String getTorrentUrlFromMagnetUri(String magnetUri) throws Exception { if (!magnetUri.startsWith(MAGNET_URI_PREFIX)) { throw new IllegalArgumentException("Invalid magnet URI: " + magnetUri); } String hash = magnetUri.substring(MAGNET_URI_PREFIX.length()); String infoHash = hash.split("&")[0]; String infoUrl = "https://itorrents.org/torrent/" + infoHash + ".torrent"; HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setConnectionManagerShared(true) .build(); HttpGet httpGet = new HttpGet(infoUrl); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed to get torrent file URL, HTTP error code: " + response.getStatusLine().getStatusCode()); } String responseBody = EntityUtils.toString(response.getEntity()); JSONObject jsonResponse = new JSONObject(new JSONTokener(responseBody)); String torrentUrl = jsonResponse.getString("url"); return torrentUrl; } private String downloadTorrentFile(String torrentUrl) throws Exception { URL url = new URL(torrentUrl); URLInfo urlInfo = new URLInfo(url); String fileName = urlInfo.getFile().substring(urlInfo.getFile().lastIndexOf('/') + 1); WGet wGet = new WGet(url, fileName); wGet.setConnectTimeout(CONNECT_TIMEOUT); wGet.setReadTimeout(SOCKET_TIMEOUT); try { wGet.download(); } catch (DownloadRetry e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadTimeoutException e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadMultipartError e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadInterruptedError e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadVerificationException e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadSSLException e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } catch (DownloadError e) { LOGGER.error("Failed to download torrent file: {}", e.getMessage()); throw new RuntimeException(e); } return fileName; } private JSONObject addTorrentToTransmission(String torrentFilePath) throws Exception { String addTorrentUrl = TRANSMISSION_RPC_URL + "/" + TRANSMISSION_API_VERSION + "/torrent-add"; HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setConnectionManagerShared(true) .build(); HttpGet httpGet = new HttpGet(addTorrentUrl); httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); String jsonRequest = "{\"method\":\"torrent-add\",\"arguments\":{\"paused\":false,\"download-dir\":\"/downloads\",\"filename\":\"" + torrentFilePath + "\"}}"; httpGet.setHeader("Content-Type", "application/json"); httpGet.setHeader("X-Transmission-Session-Id", "null"); httpGet.setEntity(IOUtils.toInputStream(jsonRequest)); HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 409) { String sessionHeader = response.getFirstHeader("X-Transmission-Session-Id").getValue(); httpGet.setHeader("X-Transmission-Session-Id", sessionHeader); httpGet.setEntity(IOUtils.toInputStream(jsonRequest)); response = httpClient.execute(httpGet); } String responseBody = EntityUtils.toString(response.getEntity()); JSONObject jsonResponse = new JSONObject(new JSONTokener(responseBody)); return jsonResponse; } } ``` 这个示例代码使用了Transmission的Java API来添加torrent文件到Transmission,如果你想使用其他BT下载工具或者其他BT下载库,你需要修改相应的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值