window.setInterval(function(){ refreshUpDownTotal(); refreshList(); }, 5000); $("a[data-torrent-id]").click(function() { var hash = $(this).attr("data-torrent-id"); $.ajax({ type: "POST", url: "share", data: { hash: hash } }) .done(function( msg ) { $.notify( "URL : " + msg); }); }); $(document).ready(function() { $.ajaxSetup({ cache: false }); refreshList(); }); function refreshUpDownTotal() { $.getJSON( "api/get_updown_total", function( data ) { $("#up-info").text(bytesToSize(data["up"], true)); $("#down-info").text(bytesToSize(data["down"], true)); }); } function refreshList(){ $.getJSON( "api/get_list", function ( data ) { $.each( data, function( key, val ) { percent = (val['completed_bytes'] / val['size_bytes']) * 100; if(percent == 100) { space = bytesToSize(val['size_bytes'], false); } else { space = bytesToSize(val['completed_bytes'], false) + ' | ' + bytesToSize(val['size_bytes'], false); } html = '' html += val['name']; html += '' + space + ''; html += ''; html += '' + (val['ratio'] / 1000) + ''; html += '' + bytesToSize(val['up'], true) + ''; html += '' + bytesToSize(val['down'], true) + ''; html += 'SHARE'; $("tr[data-torrent-id='" + val['hash'] + "']").html(html); }); }); } function bytesToSize(bytes, speed) { var sizes = ['o', 'Ko', 'Mo', 'Go', 'To']; var speeds = ['o/s', 'Ko/s', 'Mo/s']; if (bytes == 0) return '-'; var i = parseFloat(Math.floor(Math.log(bytes) / Math.log(1024))); if(speed) { return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + ' ' + speeds[i]; } else { return Math.round(bytes / Math.pow(1024, i) * 100) / 100 + ' ' + sizes[i]; } };