iwara download

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

目前為 2021-05-04 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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://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);
    });
  }
 
})();