优酷下载

下载优酷视频

  1. // ==UserScript==
  2. // @name 优酷下载
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.9
  5. // @description 下载优酷视频
  6. // @author silob59998
  7. // @license MIT License
  8. // @run-at document-start
  9. // @grant GM_download
  10. // @include *://*youku.*/*
  11. // @require https://greasyfork.org/scripts/440006-mono/code/mono.js?version=1021983
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. var mono = window['mono-descargar'];
  16. var $ = mono.jQuery;
  17. var md5 = mono.md5;
  18. var filename = mono.filename;
  19. var onRequest = mono.onRequest;
  20. var errCode = mono.FAIL_TO_DEFAULT;
  21. var idKey = 'mono-dsg-id';
  22.  
  23. var metaCache = {}
  24.  
  25. const cookie = document.cookie.split('; ').reduce((prev, current) => {
  26. const [name, ...value] = current.split('=');
  27. prev[name] = value.join('=');
  28. return prev;
  29. }, {});
  30.  
  31. var fetch_cna = () => {
  32. var quote_cna = (val) => {
  33. if (val.indexOf('%') === -1){
  34. return val
  35. }
  36. return encodeURIComponent(val)
  37. }
  38. if (cookie.cna) {
  39. return quote_cna(cookie.cna)
  40. }
  41. return ''
  42. }
  43.  
  44. var get_vid_from_url = (url) => {
  45. var b64p = /([a-zA-Z0-9=]+)/
  46. var p_list = [/youku\.com\/v_show\/id_([a-zA-Z0-9=]+)/,
  47. /player\.youku\.com\/player\.php\/sid\/([a-zA-Z0-9=]+)\/v\.swf/,
  48. /loader\.swf\?VideoIDS=([a-zA-Z0-9=]+)/,
  49. /player\.youku\.com\/embed\/([a-zA-Z0-9=]+)/]
  50. var hit;
  51. for (var p of p_list){
  52. hit = url.match(p)
  53. if (hit[1])
  54. return hit[1]
  55. }
  56. }
  57.  
  58. var ckey = 'DIl58SLFxFNndSV1GFNnMQVYkx1PP5tKe1siZu/86PR1u/Wh1Ptd+WOZsHHWxysSfAOhNJpdVWsdVJNsfJ8Sxd8WKVvNfAS8aS8fAOzYARzPyPc3JvtnPHjTdKfESTdnuTW6ZPvk2pNDh4uFzotgdMEFkzQ5wZVXl2Pf1/Y6hLK0OnCNxBj3+nb0v72gZ6b0td+WOZsHHWxysSo/0y9D2K42SaB8Y/+aD2K42SaB8Y/+ahU+WOZsHcrxysooUeND'
  59. var utid = fetch_cna()
  60. var url = `https://ups.youku.com/ups/get.json?vid=${get_vid_from_url(window.location.href)}&ccode=0532`
  61. url += '&client_ip=192.168.1.1'
  62. url += '&utid=' + utid
  63. url += '&client_ts=' + new Date().getTime()
  64. url += '&ckey=' + encodeURIComponent(ckey)
  65.  
  66. handleCallback = (resp) => {
  67. try {
  68. var streams = resp.data.stream.sort(function(a, b) {
  69. return b.size - a.size;
  70. });
  71. } catch {
  72. if (!url.includes('ccode=0502')) {
  73. url = url.replace('ccode=0532', 'ccode=0502')
  74. $.ajax({
  75. url: url,
  76. type: 'get',
  77. dataType: 'jsonp', // 请求方式为jsonp
  78. jsonpCallback: "handleCallback", // 自定义回调函数名
  79. data: {}
  80. });
  81. }
  82. return
  83. }
  84.  
  85. const stream = streams[0];
  86. var m3u8data;
  87. var videos = []
  88. stream.segs.forEach((item) => {
  89. videos.push(item.cdn_url)
  90. })
  91. m3u8data = videos.join('\n')
  92. metaCache.m3u8Data = m3u8data;
  93. metaCache.url = '';
  94. }
  95. $.ajax({
  96. url: url,
  97. type: 'get',
  98. dataType: 'jsonp', // 请求方式为jsonp
  99. jsonpCallback: "handleCallback", // 自定义回调函数名
  100. data: {}
  101. });
  102.  
  103. var getItemByMeta = (meta, selector='', selClass='') => {
  104. var id = `yk-${md5(meta.m3u8Data)}`
  105. if ($(`[${idKey}=${id}]`).length > 0 || !metaCache.m3u8Data) return null;
  106. var $el = null;
  107. if (selector) $el = $(selector)
  108. if ($el.length > 0 && selClass) {
  109. var ps = $el.parentsUntil(selClass);
  110. if (ps.length > 0) $el = $(ps[ps.length - 1])
  111. }
  112. if ($el.length <= 0) return null;
  113. var container = $el[0];
  114. return { id, url: meta.url, container, meta: meta }
  115. }
  116.  
  117. var cc = () => {
  118. var items = [];
  119. var selector = `#ykPlayer`;
  120. var item = getItemByMeta(metaCache, selector, '#player')
  121.  
  122. if (!item) return items
  123. if (item) items.push(item);
  124. return items
  125. }
  126.  
  127. var parser = async function () {
  128. var url = window.location.href;
  129. if (url.indexOf('v.youku.com/v_show/id_') !== -1 && Object.keys(metaCache).length > 0) {
  130. return cc();
  131. } else {
  132. throw [];
  133. }
  134. }
  135.  
  136. if (mono?.init) mono.init({ parser });
  137. })()