iwara download

Download videos from iwara.tv. NOTE: may need grant download privilege to browser extension like tampermonkey.

Versione datata 04/05/2021. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         iwara download
// @name:zh-CN   iwara 下载
// @description          Download videos from iwara.tv. NOTE: may need grant download privilege to browser extension like tampermonkey.
// @description:zh-CN    下载 iwara 视频。 注意:可能需要给 tampermonkey 等插件设置下载权限。
// @namespace    https://greasyfork.org/zh-CN/scripts/425898-iwara-download
// @version      0.1.0
// @author       oajsdfk
// @match        https://*.iwara.tv/*
//
// @grant        GM_download
// ==/UserScript==
 
(function() {
  'use strict';
 
  if (!jQuery) { return; }
 
  var $ = jQuery.noConflict();
 
  var view_page = window.location.pathname.startsWith('/videos/')
 
  function fname(str) {
    //return str.replace(/([\\\/:*?"<>|.~])/g, '_');
    return str.replace(/\\/g,'¥')
      .replace(/\//g,'/')
      .replace(/:/g,':')
      .replace(/\*/g,'*')
      .replace(/\?/g,'?')
      .replace(/"/g,'”')
      .replace(/</g,'<')
      .replace(/>/g,'>')
      .replace(/\|/g,'|')
      .replace(/\t/g, ' ')
      .replace(/~/g,'~')
      .replace(/\./g,'.');
  };
 
  var download_clicked;
  $('#user-links').append('<input id="download" type="button" value="download">');
  $('#download').on('click', function() {
    let dl = $(this);
 
    if (view_page) {
      $(dl).val('downloading');
      download();
      return;
    }
 
    if (!download_clicked) {
      $('.node-video').append('<input type="checkbox" class="dl_chk" checked/>');
 
      $(dl).val('download selected');
 
      $('#user-links').append('<span>'+
                              '<input id="sel_dl_all" type="button" value="all">'+
                              '<input id="sel_dl_invert" type="button" value="invert">'+
                              '<input id="sel_dl_none" type="button" value="none">'+
                              '</span>');
 
      $('#sel_dl_all').on('click', function(e) {
        $('.dl_chk:enabled').each(function() {
          this.checked = true;
        });
      });
      $('#sel_dl_invert').on('click', function(e) {
        $('.dl_chk:enabled').each(function() {
          this.checked = !this.checked;
        });
      });
      $('#sel_dl_none').on('click', function(e) {
        $('.dl_chk:enabled').each(function() {
          this.checked = false;
        });
      });
 
      download_clicked = 1;
    } else {
      let checked = $('.node-video').has('.dl_chk:checked:enabled');
 
      if (checked.length === 0) { return; }
 
      $(dl).val('downloading');
 
      checked.each((idx, v) => download($(v)));
    }
  });
 
  function download(video) {
    let like;
    let view;
    let vid;
    let title;
    let user;
    if (!view_page) {
      let t = video.find('.title > a');
      vid = t.attr('href').replace('/videos/', '');
      title = t.text();
      like = video.find('.likes-icon').has('.glyphicon-heart').text().trim();
      view = video.find('.likes-icon').has('.glyphicon-eye-open').text().trim();
      user = video.find('.username').text();
    } else {
      vid = window.location.pathname.replace('/videos/', '');
      title = $('.node-info').find('.title').text();
      let t = $('.node-views').has('.glyphicon-heart').has('.glyphicon-eye-open').text().trim().split(/\s+/);
      like = t[0];
      view = t[1];
      user = $('.node-info').find('.username').text();
    }
 
 
 
    console.log('download:', '♥' + like + ' 👁' + view + ' ' + user + '/' + title + ' [' + vid + ']');
 
    let filename = fname(user) + '/' + fname(title) + ' [' + vid + ']';
 
    $.get('/api/video/' + vid, function(res) {
      if (res[0]) {
        console.log(vid, "urls: ", res[0]);
        let t = res[0].mime.split('/');
        let f = filename +'.' + t[t.length-1];
 
        let url = 'https:'+res[0].uri;
        console.log('downloading file:', f, 'url:', url);
 
        GM_download({
          url: url,
          name: 'iwara/' + f,
          onload: () => {
            console.log('download suc:', f);
 
            if (!view_page) {
              let i = video.find('.dl_chk');
              i.attr('disabled', true);
              i.removeClass('dl_chk');
            } else {
              $('#download').enable(false);
            }
          },
          onprogress: (e) => {
            let v = e.loaded/e.total*100;
            console.log('downloading:', f, e, v+'%');
          },
          onerror : (e) => console.error('download failed:', f, e),
          ontimeout  : (e) => console.error('download timeout:', f, e),
          saveAs: false
        });
 
      } else {
        console.error("no video:", vid);
      }
    }, 'JSON').fail(function(err) {
      console.error("get_url_failed", vid, err);
    });
  }
 
})();