Video Url Parser

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

As of 2014-10-22. See the latest version.

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