Video Url Parser

urlParser to get extract information like provider, videoId and other from urls

Verze ze dne 31. 12. 2014. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/2859/30009/Video%20Url%20Parser.js

  1. // ==UserScript==
  2. // @name Video Url Parser
  3. // @namespace Zod-
  4. // @description urlParser to get extract information like provider, videoId and other from urls
  5. // @source https://github.com/Zod-/jsVideoUrlParser
  6. // @copyright Zod-
  7. // @version 0.4
  8. // @license MIT
  9. // ==/UserScript==
  10. function URLParser() {
  11. "use strict";
  12. this.plugins = {};
  13. }
  14. URLParser.prototype.parse = function(url) {
  15. "use strict";
  16. var th = this,
  17. match = url.match(/(?:https?:\/\/)?(?:[^\.]+\.)?(\w+)\./i),
  18. provider = match ? match[1] : undefined,
  19. result,
  20. createdUrl;
  21. if (match && provider && th.plugins[provider]) {
  22. result = th.plugins[provider].parse.call(this, url);
  23. if (result) {
  24. result.provider = th.plugins[provider].provider;
  25. return result;
  26. }
  27. }
  28. return undefined;
  29. };
  30. URLParser.prototype.bind = function(plugin) {
  31. var th = this;
  32. th.plugins[plugin.provider] = plugin;
  33. if (plugin.alternatives) {
  34. var i;
  35. for (i = 0; i < plugin.alternatives.length; i += 1) {
  36. th.plugins[plugin.alternatives[i]] = plugin;
  37. }
  38. }
  39. };
  40. URLParser.prototype.create = function(op) {
  41. var th = this,
  42. vi = op.videoInfo;
  43. op.format = op.format || 'short';
  44. if (th.plugins[vi.provider].create) {
  45. return th.plugins[vi.provider].create.call(this, op);
  46. }
  47. return undefined;
  48. };
  49.  
  50. window.urlParser = new URLParser();
  51.  
  52. //parses strings like 1h30m20s to seconds
  53. window.getTime(timeString) {
  54. "use strict";
  55. var totalSeconds = 0,
  56. timeValues = {
  57. 's': 1,
  58. 'm': 1 * 60,
  59. 'h': 1 * 60 * 60,
  60. 'd': 1 * 60 * 60 * 24,
  61. 'w': 1 * 60 * 60 * 24 * 7
  62. },
  63. timePairs;
  64. //is the format 1h30m20s etc
  65. if (!timeString.match(/^(\d+[smhdw]?)+$/)) {
  66. return 0;
  67. }
  68. //expand to "1 h 30 m 20 s" and split
  69. timeString = timeString.replace(/([smhdw])/g, ' $1 ').trim();
  70. timePairs = timeString.split(' ');
  71. for (var i = 0; i < timePairs.length; i += 2) {
  72. totalSeconds += parseInt(timePairs[i], 10) * timeValues[timePairs[i + 1] || 's'];
  73. }
  74. return totalSeconds;
  75. }
  76. //http://joquery.com/2012/string-format-for-javascript
  77. if (typeof String.prototype.format !== 'function') {
  78. String.prototype.format = function() {
  79. // The string containing the format items (e.g. "{0}")
  80. // will and always has to be the first argument.
  81. var theString = this,
  82. i,
  83. regEx;
  84.  
  85. // start with the second argument (i = 1)
  86. for (i = 0; i < arguments.length; i += 1) {
  87. // "gm" = RegEx options for Global search (more than one instance)
  88. // and for Multiline search
  89. regEx = new RegExp("\\{" + (i) + "\\}", "gm");
  90. theString = theString.replace(regEx, arguments[i]);
  91. }
  92. return theString;
  93. };
  94. }
  95. urlParser.bind({
  96. 'provider': 'dailymotion',
  97. 'alternatives': ['dai'],
  98. 'parse': function(url) {
  99. "use strict";
  100. var match,
  101. id,
  102. startTime,
  103. result = {};
  104.  
  105. match = url.match(/(?:\/video|ly)\/([A-Za-z0-9]+)/i);
  106. id = match ? match[1] : undefined;
  107.  
  108. match = url.match(/[#\?&]start=([A-Za-z0-9]+)/i);
  109. startTime = match ? getTime(match[1]) : undefined;
  110.  
  111. if (!id) {
  112. return undefined;
  113. }
  114. result.mediaType = 'video';
  115. result.id = id;
  116. if (startTime) {
  117. result.startTime = startTime;
  118. }
  119. return result;
  120. },
  121. 'create': function(op) {
  122. "use strict";
  123. var vi = op.videoInfo;
  124. if (vi.startTime) {
  125. return 'https://www.dailymotion.com/video/{0}?start={1}'.format(vi.id, vi.startTime);
  126. }
  127.  
  128. if (op.format === 'short') {
  129. return 'https://dai.ly/{0}'.format(vi.id);
  130. }
  131.  
  132. return 'https://www.dailymotion.com/video/{0}'.format(vi.id);
  133. }
  134. });
  135.  
  136. //not finished
  137. urlParser.bind({
  138. 'provider': 'livestream',
  139. 'parse': function(url) {
  140. "use strict";
  141. var match,
  142. channel;
  143. match = url.match(/livestream\.com\/(\w+)/i);
  144. channel = match ? match[1] : undefined;
  145. if (!channel) {
  146. return undefined;
  147. }
  148.  
  149. return {
  150. 'mediaType': 'stream',
  151. 'channel': channel
  152. };
  153. }
  154. });
  155. urlParser.bind({
  156. 'provider': 'twitch',
  157. 'parse': function(url) {
  158. "use strict";
  159. var match,
  160. id,
  161. channel,
  162. idPrefix,
  163. result = {};
  164.  
  165. match = url.match(/twitch\.tv\/(\w+)(?:\/(.)\/(\d+))?/i);
  166. channel = match ? match[1] : undefined;
  167. idPrefix = match ? match[2] : undefined;
  168. id = match ? match[3] : undefined;
  169.  
  170. match = url.match(/(?:\?channel|\&utm_content)=(\w+)/i);
  171. channel = match ? match[1] : channel;
  172.  
  173. if (!channel) {
  174. return undefined;
  175. }
  176. if (id) {
  177. result.mediaType = 'video';
  178. result.id = id;
  179. result.idPrefix = idPrefix;
  180. } else {
  181. result.mediaType = 'stream';
  182. }
  183. result.channel = channel;
  184.  
  185. return result;
  186. },
  187. 'create': function(videoInfo) {
  188. "use strict";
  189. var url,
  190. vi = op.videoInfo;
  191. if (vi.mediaType === 'stream') {
  192. return 'https://twitch.tv/{0}'.format(vi.channel);
  193. }
  194.  
  195. return 'https://twitch.tv/{0}/{1}/{2}'.format(vi.channel, vi.idPrefix, vi.id);
  196. }
  197. });
  198. urlParser.bind({
  199. 'provider': 'vimeo',
  200. 'alternatives': ['vimeopro'],
  201. 'parse': function(url) {
  202. "use strict";
  203. var match,
  204. id;
  205. match = url.match(/(?:\/(?:channels\/[\w]+|(?:album\/\d+\/)?videos?))?\/(\d+)/i);
  206. id = match ? match[1] : undefined;
  207. if (!id) {
  208. return undefined;
  209. }
  210. return {
  211. 'mediaType': 'video',
  212. 'id': id
  213. };
  214. },
  215. 'create': function(videoInfo) {
  216. "use strict";
  217. return 'https://vimeo.com/{0}'.format(op.videoInfo.id);
  218. }
  219. });
  220. urlParser.bind({
  221. 'provider': 'youtube',
  222. 'alternatives': ['youtu'],
  223. 'parse': function(url) {
  224. "use strict";
  225. var match,
  226. id,
  227. playlistId,
  228. playlistIndex,
  229. startTime,
  230. result = {};
  231.  
  232. match = url.match(/(?:(?:v|be|videos)\/|v=)([\w\-]{11})/i);
  233. id = match ? match[1] : undefined;
  234.  
  235. match = url.match(/list=([\w\-]+)/i);
  236. playlistId = match ? match[1] : undefined;
  237.  
  238. match = url.match(/index=(\d+)/i);
  239. playlistIndex = match ? Number(match[1]) : undefined;
  240.  
  241. match = url.match(/[#\?&](?:star)?t=([A-Za-z0-9]+)/i);
  242. startTime = match ? getTime(match[1]) : undefined;
  243.  
  244. if (id) {
  245. result.mediaType = 'video';
  246. result.id = id;
  247. if (playlistId) {
  248. result.playlistId = playlistId;
  249. if (playlistIndex) {
  250. result.playlistIndex = playlistIndex;
  251. }
  252. }
  253. if (startTime) {
  254. result.startTime = startTime;
  255. }
  256. } else if (playlistId) {
  257. result.mediaType = 'playlist';
  258. result.playlistId = playlistId;
  259. } else {
  260. return undefined;
  261. }
  262.  
  263. return result;
  264. },
  265. 'create': function(op) {
  266. "use strict";
  267. var url,
  268. vi = op.videoInfo;
  269. if (vi.mediaType === 'playlist') {
  270. return 'https://www.youtube.com/playlist?feature=share&list={0}'.format(vi.playlistId);
  271. }
  272.  
  273. if (vi.playlistId) {
  274. url = 'https://www.youtube.com/watch?v={0}&list={1}'.format(vi.id, vi.playlistId);
  275. if (vi.playlistIndex) {
  276. url += '&index={0}'.format(vi.playlistIndex);
  277. }
  278. } else {
  279. if (op.format === 'short') {
  280. url = 'https://youtu.be/{0}'.format(vi.id);
  281. } else {
  282. url = 'https://www.youtube.com/watch?v={0}'.format(vi.id);
  283. }
  284. }
  285.  
  286. if (vi.startTime) {
  287. url += '#t={0}'.format(vi.startTime);
  288. }
  289. return url;
  290. }
  291. });