本文整理汇总了TypeScript中parse-torrent类的典型用法代码示例。如果您正苦于以下问题:TypeScript parse-torrent类的具体用法?TypeScript parse-torrent怎么用?TypeScript parse-torrent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了parse-torrent类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Buffer
import * as parseTorrent from 'parse-torrent';
import * as fs from 'fs';
// info hash (as a hex string)
parseTorrent('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36');
// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
// info hash (as a Buffer)
parseTorrent(new Buffer('d2474e86c95b19b8bcfdb92bc12c9d44667cfa36', 'hex'));
// { infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
// magnet uri (as a utf8 string)
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36');
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36' }
// magnet uri with torrent name
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves%20of%20Grass%20by%20Walt%20Whitman.epub');
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// dn: 'Leaves of Grass by Walt Whitman.epub',
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// name: 'Leaves of Grass by Walt Whitman.epub' }
// magnet uri with trackers
parseTorrent('magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&tr=http%3A%2F%2Ftracker.example.com%2Fannounce');
// { xt: 'urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// tr: 'http://tracker.example.com/announce',
// infoHash: 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36',
// announce: [ 'http://tracker.example.com/announce' ] }
// .torrent file (as a Buffer)
示例2: ParseTorrent
const tabUpdated = (tabId: number, url: string, state: TorrentsState) => {
const { torrentStateMap, torrentObjMap } = state
const origTorrentState: TorrentState = torrentStateMap[tabId]
const origInfoHash = origTorrentState ? origTorrentState.infoHash : undefined
let newTorrentState: TorrentState | undefined
let newInfoHash: string | undefined
// create new torrent state
const parsedURL = new window.URL(url)
const torrentId = parsedURL.href
if (parsedURL.protocol === 'magnet:') { // parse torrent
try {
const { name, infoHash, ix } = ParseTorrent(torrentId)
newInfoHash = infoHash
newTorrentState = { tabId, torrentId, name, infoHash, ix }
} catch (error) {
newTorrentState = { tabId, torrentId, errorMsg: error.message }
}
} else if (parsedURL.protocol === 'https:' || parsedURL.protocol === 'http:') {
const name = parsedURL.pathname.substr(parsedURL.pathname.lastIndexOf('/') + 1)
// for .torrent case, ix (index of file) for selecting a specific file in
// the file list is given in url like #ix=5
let ix: number | undefined = Number(parse(parsedURL.hash.slice(1)).ix)
ix = Number.isNaN(ix) ? undefined : ix
// Use an existing infoHash if it's the same torrentId
const torrentUrl = parsedURL.origin + parsedURL.pathname
const key = Object.keys(torrentStateMap).find(
key => torrentStateMap[key].infoHash &&
torrentStateMap[key].torrentId === torrentUrl)
newInfoHash = key
? torrentStateMap[key].infoHash
: undefined
newTorrentState = { tabId, torrentId, name, ix, infoHash: newInfoHash }
}
// delete old torrent state
delete torrentStateMap[tabId]
// unsubscribe old torrent if not the same
const isSameTorrent = newInfoHash && origInfoHash === newInfoHash
if (origInfoHash && torrentObjMap[origInfoHash] && !isSameTorrent) {
torrentObjMap[origInfoHash].tabClients.delete(tabId)
if (!torrentObjMap[origInfoHash].tabClients.size) {
delete torrentObjMap[origInfoHash]
delTorrent(origInfoHash)
}
}
// save new torrent state and subscribe directly if torrent already existed
if (newTorrentState) {
torrentStateMap[tabId] = newTorrentState
if (newInfoHash && torrentObjMap[newInfoHash]) {
torrentObjMap[newInfoHash].tabClients.add(tabId)
}
}
return { ...state, torrentStateMap, torrentObjMap }
}
示例3: createTorrentAndSetInfoHash
async createTorrentAndSetInfoHash (videoFile: VideoFileModel) {
const options = {
// Keep the extname, it's used by the client to stream the file inside a web browser
name: `${this.name} ${videoFile.resolution}p${videoFile.extname}`,
createdBy: 'PeerTube',
announceList: [
[ CONFIG.WEBSERVER.WS + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT + '/tracker/socket' ],
[ CONFIG.WEBSERVER.URL + '/tracker/announce' ]
],
urlList: [
CONFIG.WEBSERVER.URL + STATIC_PATHS.WEBSEED + this.getVideoFilename(videoFile)
]
}
const torrent = await createTorrentPromise(this.getVideoFilePath(videoFile), options)
const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile))
logger.info('Creating torrent %s.', filePath)
await writeFilePromise(filePath, torrent)
const parsedTorrent = parseTorrent(torrent)
videoFile.infoHash = parsedTorrent.infoHash
}
示例4: res
readFile(torrentPath, (err, data) => {
if (err) return rej(err)
return res(parseTorrent(data))
})