Last active
January 15, 2019 09:27
-
-
Save loon3/6730c3187d5b84a6cbbb to your computer and use it in GitHub Desktop.
Convert json data to webtorrent and seed, then download json from webtorrent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jQuery and Webtorrent (https://webtorrent.io/) dependencies | |
var json_source_url = ""; //json data source | |
var filename = ""; //filename.json | |
$.getJSON(json_source_url, function( data ) { | |
var jsonstring = JSON.stringify(data); | |
var blob = new Blob([jsonstring], {type: "application/json"}); | |
var client = new WebTorrent() | |
client.seed(blob, {name: filename}, function (torrent) { | |
console.log('Client is seeding ' + torrent.infoHash); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jQuery and Webtorrent dependencies, also requires decodeUtf8 function | |
var infohash = ""; //json torrent infohash | |
var client = new WebTorrent() | |
var magnetUri = 'magnet:?xt=urn:btih:'+infohash; | |
client.add(magnetUri, function (torrent) { | |
torrent.files.forEach(function (file) { | |
file.getBuffer(function (err, buffer) { | |
if (err) throw err | |
var jsonstring = decodeUtf8(buffer); | |
var data = JSON.parse(jsonstring); | |
console.log(data); //json data | |
client.destroy(); | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://gist.github.com/boushley/5471599 | |
function decodeUtf8(arrayBuffer) { | |
var result = ""; | |
var i = 0; | |
var c = 0; | |
var c1 = 0; | |
var c2 = 0; | |
var data = new Uint8Array(arrayBuffer); | |
// If we have a BOM skip it | |
if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { | |
i = 3; | |
} | |
while (i < data.length) { | |
c = data[i]; | |
if (c < 128) { | |
result += String.fromCharCode(c); | |
i++; | |
} else if (c > 191 && c < 224) { | |
if( i+1 >= data.length ) { | |
throw "UTF-8 Decode failed. Two byte character was truncated."; | |
} | |
c2 = data[i+1]; | |
result += String.fromCharCode( ((c&31)<<6) | (c2&63) ); | |
i += 2; | |
} else { | |
if (i+2 >= data.length) { | |
throw "UTF-8 Decode failed. Multi byte character was truncated."; | |
} | |
c2 = data[i+1]; | |
c3 = data[i+2]; | |
result += String.fromCharCode( ((c&15)<<12) | ((c2&63)<<6) | (c3&63) ); | |
i += 3; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment