iwara 下载

下载 iwara 视频。 注意:可能需要给 tampermonkey 等插件设置下载权限。

当前为 2021-05-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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://sleazyfork.org/zh-CN/scripts/425903-iwara-download
// @version      0.1.1
// @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,'¥')
      .replace(/\//g,'/')
      .replace(/:/g,':')
      .replace(/\*/g,'*')
      .replace(/\?/g,'?')
      .replace(/"/g,'”')
      .replace(/</g,'<')
      .replace(/>/g,'>')
      .replace(/\|/g,'|')
      .replace(/\t/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);
    });
  }

})();