-
Notifications
You must be signed in to change notification settings - Fork 141
/
webtorrent.js
127 lines (103 loc) · 3.22 KB
/
webtorrent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var moment = require('moment')
var prettyBytes = require('pretty-bytes')
// HTML elements
var $body = $('body')
var $progressBar = $('#progressBar')
var $streamedFileName = $('#streamedFileName')
var $numPeers = $('#numPeers')
var $downloaded = $('#downloaded')
var $total = $('#total')
var $remaining = $('#remaining')
var $uploadSpeed = $('#uploadSpeed')
var $downloadSpeed = $('#downloadSpeed')
var announceList = [
['udp://tracker.openbittorrent.com:80'],
['udp://tracker.internetwarriors.net:1337'],
['udp://tracker.leechers-paradise.org:6969'],
['udp://tracker.coppersurfer.tk:6969'],
['udp://exodus.desync.com:6969'],
['wss://tracker.webtorrent.io'],
['wss://tracker.btorrent.xyz'],
['wss://tracker.openwebtorrent.com'],
['wss://tracker.fastcast.nz']
]
global.WEBTORRENT_ANNOUNCE = announceList
.map(function (arr) {
return arr[0]
})
.filter(function (url) {
return url.indexOf('wss://') === 0 || url.indexOf('ws://') === 0
})
var client = new WebTorrent()
client.on('error', function(err) {
console.error('ERROR: ' + err.message)
})
// Download by form input
$('form').submit(function(e) {
e.preventDefault() // Prevent page refresh
var torrentId = $('form input[name=torrentId]').val()
if (torrentId.length > 0)
downloadTorrent(torrentId)
})
// Download by URL hash
onHashChange()
window.addEventListener('hashchange', onHashChange)
function onHashChange () {
var hash = decodeURIComponent(window.location.hash.substring(1)).trim()
if (hash !== '') downloadTorrent(hash)
}
function downloadTorrent(torrentId) {
console.log('Downloading torrent from ' + torrentId)
client.add(torrentId, onTorrent)
}
function onTorrent(torrent) {
torrent.on('warning', console.log)
torrent.on('error', console.log)
console.log('Got torrent metadata!')
// Find largest file
var largestFile = torrent.files[0]
for (var i = 1; i < torrent.files.length; i++) {
if (torrent.files[i].length > largestFile.length)
largestFile = torrent.files[i]
}
// Display name of the file being streamed
$streamedFileName.html(largestFile.name)
// Update clipboard share url
$('#share-url').val('https://ferrolho.github.io/magnet-player/#' + torrent.infoHash);
// Stream the file in the browser
largestFile.appendTo('#output')
// hide magnet input
$('#magnet-input').slideUp()
// show player
$('#hero').slideDown()
// Trigger statistics refresh
torrent.on('done', onDone)
setInterval(onProgress, 500)
onProgress()
// Statistics
function onProgress () {
// Peers
$numPeers.html(torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers'))
// Progress
var percent = Math.round(torrent.progress * 100 * 100) / 100
$progressBar.width(percent + '%')
$downloaded.html(prettyBytes(torrent.downloaded))
$total.html(prettyBytes(torrent.length))
// Remaining time
var remaining
if (torrent.done) {
remaining = 'Done'
} else {
remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining'
}
$remaining.html(remaining)
// Speed rates
$downloadSpeed.html(prettyBytes(torrent.downloadSpeed) + '/s')
$uploadSpeed.html(prettyBytes(torrent.uploadSpeed) + '/s')
}
function onDone () {
$body.addClass('is-seed')
onProgress()
}
}