-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick-start.html
More file actions
49 lines (46 loc) · 1.93 KB
/
quick-start.html
File metadata and controls
49 lines (46 loc) · 1.93 KB
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
<script src="https://cdn.jsdelivr.net/npm/@swarmcloud/file"></script>
<h3>download info:</h3>
<p id="url"></p>
<p id="mime"></p>
<p id="size"></p>
<p id="progress"></p>
<p id="speed"></p>
<p id="p2p"></p>
<script>
var url = 'https://huya-w20.huya.com/2027/357649831/1300/e0a4cd303b58bab74f809be7f2d09113.mp4';
var downloader = new P2PEngineFile(url, {
// logLevel: 'debug',
// trackerZone: 'hk', // if using Hongkong tracker
// trackerZone: 'us', // if using USA tracker
// token: YOUR_TOKEN
})
.on('metadata', function (source) {
document.querySelector('#url').innerText = `Url: ${source.getUrl()}`;
document.querySelector('#mime').innerText = `Mime: ${source.getMime()}`;
document.querySelector('#size').innerText = `Size: ${source.getFileLength()}`;
})
.on('finished', function (file) {
document.querySelector('#progress').innerText = `Download finished`
file.save('test.mp4')
})
.on('progress', ratio => {
document.querySelector('#progress').innerText = `Progress: ${ratio.toFixed(2)}%`
})
.on('speed', speed => {
document.querySelector('#speed').innerText = `Speed: ${Math.round(speed/1000)}KB/s`
})
.on('failed', function (e) {
// fallback
const a = document.createElement('a');
a.download = "test.mp4";
a.href = url;
document.body.appendChild(a);
a.click();
a.remove();
})
.on('stats', function (stats) {
var total = stats.totalHTTPDownloaded + stats.totalP2PDownloaded;
document.querySelector('#p2p').innerText = `p2p ratio: ${Math.round(stats.totalP2PDownloaded/total*100)}% saved traffic: ${Math.round(stats.totalP2PDownloaded)}KB upload: ${Math.round(stats.totalP2PUploaded)}KB`;
})
downloader.start();
</script>