test_youtube_signature.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import contextlib
  8. import re
  9. import string
  10. import urllib.request
  11. from test.helper import FakeYDL, is_download_test
  12. from yt_dlp.extractor import YoutubeIE
  13. from yt_dlp.jsinterp import JSInterpreter
  14. _SIG_TESTS = [
  15. (
  16. 'https://s.ytimg.com/yts/jsbin/html5player-vflHOr_nV.js',
  17. 86,
  18. '>=<;:/.-[+*)(\'&%$#"!ZYX0VUTSRQPONMLKJIHGFEDCBA\\yxwvutsrqponmlkjihgfedcba987654321',
  19. ),
  20. (
  21. 'https://s.ytimg.com/yts/jsbin/html5player-vfldJ8xgI.js',
  22. 85,
  23. '3456789a0cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS[UVWXYZ!"#$%&\'()*+,-./:;<=>?@',
  24. ),
  25. (
  26. 'https://s.ytimg.com/yts/jsbin/html5player-vfle-mVwz.js',
  27. 90,
  28. ']\\[@?>=<;:/.-,+*)(\'&%$#"hZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjiagfedcb39876',
  29. ),
  30. (
  31. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vfl0Cbn9e.js',
  32. 84,
  33. 'O1I3456789abcde0ghijklmnopqrstuvwxyzABCDEFGHfJKLMN2PQRSTUVW@YZ!"#$%&\'()*+,-./:;<=',
  34. ),
  35. (
  36. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflXGBaUN.js',
  37. '2ACFC7A61CA478CD21425E5A57EBD73DDC78E22A.2094302436B2D377D14A3BBA23022D023B8BC25AA',
  38. 'A52CB8B320D22032ABB3A41D773D2B6342034902.A22E87CDD37DBE75A5E52412DC874AC16A7CFCA2',
  39. ),
  40. (
  41. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflBb0OQx.js',
  42. 84,
  43. '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ0STUVWXYZ!"#$%&\'()*+,@./:;<=>',
  44. ),
  45. (
  46. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vfl9FYC6l.js',
  47. 83,
  48. '123456789abcdefghijklmnopqr0tuvwxyzABCDETGHIJKLMNOPQRS>UVWXYZ!"#$%&\'()*+,-./:;<=F',
  49. ),
  50. (
  51. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflCGk6yw/html5player.js',
  52. '4646B5181C6C3020DF1D9C7FCFEA.AD80ABF70C39BD369CCCAE780AFBB98FA6B6CB42766249D9488C288',
  53. '82C8849D94266724DC6B6AF89BBFA087EACCD963.B93C07FBA084ACAEFCF7C9D1FD0203C6C1815B6B',
  54. ),
  55. (
  56. 'https://s.ytimg.com/yts/jsbin/html5player-en_US-vflKjOTVq/html5player.js',
  57. '312AA52209E3623129A412D56A40F11CB0AF14AE.3EE09501CB14E3BCDC3B2AE808BF3F1D14E7FBF12',
  58. '112AA5220913623229A412D56A40F11CB0AF14AE.3EE0950FCB14EEBCDC3B2AE808BF331D14E7FBF3',
  59. ),
  60. (
  61. 'https://www.youtube.com/s/player/6ed0d907/player_ias.vflset/en_US/base.js',
  62. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  63. 'AOq0QJ8wRAIgXmPlOPSBkkUs1bYFYlJCfe29xx8j7v1pDL2QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0',
  64. ),
  65. (
  66. 'https://www.youtube.com/s/player/3bb1f723/player_ias.vflset/en_US/base.js',
  67. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  68. 'MyOSJXtKI3m-uME_jv7-pT12gOFC02RFkGoqWpzE0Cs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  69. ),
  70. (
  71. 'https://www.youtube.com/s/player/2f1832d2/player_ias.vflset/en_US/base.js',
  72. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  73. '0QJ8wRAIgXmPlOPSBkkUs1bYFYlJCfe29xxAj7v1pDL0QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJ2OySqa0q',
  74. ),
  75. (
  76. 'https://www.youtube.com/s/player/643afba4/tv-player-ias.vflset/tv-player-ias.js',
  77. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  78. 'AAOAOq0QJ8wRAIgXmPlOPSBkkUs1bYFYlJCfe29xx8j7vgpDL0QwbdV06sCIEzpWqMGkFR20CFOS21Tp-7vj_EMu-m37KtXJoOy1',
  79. ),
  80. (
  81. 'https://www.youtube.com/s/player/363db69b/player_ias.vflset/en_US/base.js',
  82. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  83. '0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpz2ICs6EVdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  84. ),
  85. (
  86. 'https://www.youtube.com/s/player/363db69b/player_ias_tce.vflset/en_US/base.js',
  87. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  88. '0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpz2ICs6EVdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  89. ),
  90. (
  91. 'https://www.youtube.com/s/player/4fcd6e4a/player_ias.vflset/en_US/base.js',
  92. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  93. 'wAOAOq0QJ8ARAIgXmPlOPSBkkUs1bYFYlJCfe29xx8q7v1pDL0QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0',
  94. ),
  95. (
  96. 'https://www.youtube.com/s/player/4fcd6e4a/player_ias_tce.vflset/en_US/base.js',
  97. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  98. 'wAOAOq0QJ8ARAIgXmPlOPSBkkUs1bYFYlJCfe29xx8q7v1pDL0QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0',
  99. ),
  100. (
  101. 'https://www.youtube.com/s/player/20830619/player_ias.vflset/en_US/base.js',
  102. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  103. '7AOq0QJ8wRAIgXmPlOPSBkkAs1bYFYlJCfe29xx8jOv1pDL0Q2bdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0qaw',
  104. ),
  105. (
  106. 'https://www.youtube.com/s/player/20830619/player_ias_tce.vflset/en_US/base.js',
  107. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  108. '7AOq0QJ8wRAIgXmPlOPSBkkAs1bYFYlJCfe29xx8jOv1pDL0Q2bdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0qaw',
  109. ),
  110. (
  111. 'https://www.youtube.com/s/player/20830619/player-plasma-ias-phone-en_US.vflset/base.js',
  112. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  113. '7AOq0QJ8wRAIgXmPlOPSBkkAs1bYFYlJCfe29xx8jOv1pDL0Q2bdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0qaw',
  114. ),
  115. (
  116. 'https://www.youtube.com/s/player/20830619/player-plasma-ias-tablet-en_US.vflset/base.js',
  117. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  118. '7AOq0QJ8wRAIgXmPlOPSBkkAs1bYFYlJCfe29xx8jOv1pDL0Q2bdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_EMu-m37KtXJoOySqa0qaw',
  119. ),
  120. (
  121. 'https://www.youtube.com/s/player/8a8ac953/player_ias_tce.vflset/en_US/base.js',
  122. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  123. 'IAOAOq0QJ8wRAAgXmPlOPSBkkUs1bYFYlJCfe29xx8j7v1pDL0QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_E2u-m37KtXJoOySqa0',
  124. ),
  125. (
  126. 'https://www.youtube.com/s/player/8a8ac953/tv-player-es6.vflset/tv-player-es6.js',
  127. '2aq0aqSyOoJXtK73m-uME_jv7-pT15gOFC02RFkGMqWpzEICs69VdbwQ0LDp1v7j8xx92efCJlYFYb1sUkkBSPOlPmXgIARw8JQ0qOAOAA',
  128. 'IAOAOq0QJ8wRAAgXmPlOPSBkkUs1bYFYlJCfe29xx8j7v1pDL0QwbdV96sCIEzpWqMGkFR20CFOg51Tp-7vj_E2u-m37KtXJoOySqa0',
  129. ),
  130. ]
  131. _NSIG_TESTS = [
  132. (
  133. 'https://www.youtube.com/s/player/7862ca1f/player_ias.vflset/en_US/base.js',
  134. 'X_LCxVDjAavgE5t', 'yxJ1dM6iz5ogUg',
  135. ),
  136. (
  137. 'https://www.youtube.com/s/player/9216d1f7/player_ias.vflset/en_US/base.js',
  138. 'SLp9F5bwjAdhE9F-', 'gWnb9IK2DJ8Q1w',
  139. ),
  140. (
  141. 'https://www.youtube.com/s/player/f8cb7a3b/player_ias.vflset/en_US/base.js',
  142. 'oBo2h5euWy6osrUt', 'ivXHpm7qJjJN',
  143. ),
  144. (
  145. 'https://www.youtube.com/s/player/2dfe380c/player_ias.vflset/en_US/base.js',
  146. 'oBo2h5euWy6osrUt', '3DIBbn3qdQ',
  147. ),
  148. (
  149. 'https://www.youtube.com/s/player/f1ca6900/player_ias.vflset/en_US/base.js',
  150. 'cu3wyu6LQn2hse', 'jvxetvmlI9AN9Q',
  151. ),
  152. (
  153. 'https://www.youtube.com/s/player/8040e515/player_ias.vflset/en_US/base.js',
  154. 'wvOFaY-yjgDuIEg5', 'HkfBFDHmgw4rsw',
  155. ),
  156. (
  157. 'https://www.youtube.com/s/player/e06dea74/player_ias.vflset/en_US/base.js',
  158. 'AiuodmaDDYw8d3y4bf', 'ankd8eza2T6Qmw',
  159. ),
  160. (
  161. 'https://www.youtube.com/s/player/5dd88d1d/player-plasma-ias-phone-en_US.vflset/base.js',
  162. 'kSxKFLeqzv_ZyHSAt', 'n8gS8oRlHOxPFA',
  163. ),
  164. (
  165. 'https://www.youtube.com/s/player/324f67b9/player_ias.vflset/en_US/base.js',
  166. 'xdftNy7dh9QGnhW', '22qLGxrmX8F1rA',
  167. ),
  168. (
  169. 'https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js',
  170. 'TDCstCG66tEAO5pR9o', 'dbxNtZ14c-yWyw',
  171. ),
  172. (
  173. 'https://www.youtube.com/s/player/c81bbb4a/player_ias.vflset/en_US/base.js',
  174. 'gre3EcLurNY2vqp94', 'Z9DfGxWP115WTg',
  175. ),
  176. (
  177. 'https://www.youtube.com/s/player/1f7d5369/player_ias.vflset/en_US/base.js',
  178. 'batNX7sYqIJdkJ', 'IhOkL_zxbkOZBw',
  179. ),
  180. (
  181. 'https://www.youtube.com/s/player/009f1d77/player_ias.vflset/en_US/base.js',
  182. '5dwFHw8aFWQUQtffRq', 'audescmLUzI3jw',
  183. ),
  184. (
  185. 'https://www.youtube.com/s/player/dc0c6770/player_ias.vflset/en_US/base.js',
  186. '5EHDMgYLV6HPGk_Mu-kk', 'n9lUJLHbxUI0GQ',
  187. ),
  188. (
  189. 'https://www.youtube.com/s/player/113ca41c/player_ias.vflset/en_US/base.js',
  190. 'cgYl-tlYkhjT7A', 'hI7BBr2zUgcmMg',
  191. ),
  192. (
  193. 'https://www.youtube.com/s/player/c57c113c/player_ias.vflset/en_US/base.js',
  194. 'M92UUMHa8PdvPd3wyM', '3hPqLJsiNZx7yA',
  195. ),
  196. (
  197. 'https://www.youtube.com/s/player/5a3b6271/player_ias.vflset/en_US/base.js',
  198. 'B2j7f_UPT4rfje85Lu_e', 'm5DmNymaGQ5RdQ',
  199. ),
  200. (
  201. 'https://www.youtube.com/s/player/7a062b77/player_ias.vflset/en_US/base.js',
  202. 'NRcE3y3mVtm_cV-W', 'VbsCYUATvqlt5w',
  203. ),
  204. (
  205. 'https://www.youtube.com/s/player/dac945fd/player_ias.vflset/en_US/base.js',
  206. 'o8BkRxXhuYsBCWi6RplPdP', '3Lx32v_hmzTm6A',
  207. ),
  208. (
  209. 'https://www.youtube.com/s/player/6f20102c/player_ias.vflset/en_US/base.js',
  210. 'lE8DhoDmKqnmJJ', 'pJTTX6XyJP2BYw',
  211. ),
  212. (
  213. 'https://www.youtube.com/s/player/cfa9e7cb/player_ias.vflset/en_US/base.js',
  214. 'aCi3iElgd2kq0bxVbQ', 'QX1y8jGb2IbZ0w',
  215. ),
  216. (
  217. 'https://www.youtube.com/s/player/8c7583ff/player_ias.vflset/en_US/base.js',
  218. '1wWCVpRR96eAmMI87L', 'KSkWAVv1ZQxC3A',
  219. ),
  220. (
  221. 'https://www.youtube.com/s/player/b7910ca8/player_ias.vflset/en_US/base.js',
  222. '_hXMCwMt9qE310D', 'LoZMgkkofRMCZQ',
  223. ),
  224. (
  225. 'https://www.youtube.com/s/player/590f65a6/player_ias.vflset/en_US/base.js',
  226. '1tm7-g_A9zsI8_Lay_', 'xI4Vem4Put_rOg',
  227. ),
  228. (
  229. 'https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js',
  230. 'b6HcntHGkvBLk_FRf', 'kNPW6A7FyP2l8A',
  231. ),
  232. (
  233. 'https://www.youtube.com/s/player/3400486c/player_ias.vflset/en_US/base.js',
  234. 'lL46g3XifCKUZn1Xfw', 'z767lhet6V2Skl',
  235. ),
  236. (
  237. 'https://www.youtube.com/s/player/20dfca59/player_ias.vflset/en_US/base.js',
  238. '-fLCxedkAk4LUTK2', 'O8kfRq1y1eyHGw',
  239. ),
  240. (
  241. 'https://www.youtube.com/s/player/b12cc44b/player_ias.vflset/en_US/base.js',
  242. 'keLa5R2U00sR9SQK', 'N1OGyujjEwMnLw',
  243. ),
  244. (
  245. 'https://www.youtube.com/s/player/3bb1f723/player_ias.vflset/en_US/base.js',
  246. 'gK15nzVyaXE9RsMP3z', 'ZFFWFLPWx9DEgQ',
  247. ),
  248. (
  249. 'https://www.youtube.com/s/player/2f1832d2/player_ias.vflset/en_US/base.js',
  250. 'YWt1qdbe8SAfkoPHW5d', 'RrRjWQOJmBiP',
  251. ),
  252. (
  253. 'https://www.youtube.com/s/player/9c6dfc4a/player_ias.vflset/en_US/base.js',
  254. 'jbu7ylIosQHyJyJV', 'uwI0ESiynAmhNg',
  255. ),
  256. (
  257. 'https://www.youtube.com/s/player/e7567ecf/player_ias_tce.vflset/en_US/base.js',
  258. 'Sy4aDGc0VpYRR9ew_', '5UPOT1VhoZxNLQ',
  259. ),
  260. (
  261. 'https://www.youtube.com/s/player/d50f54ef/player_ias_tce.vflset/en_US/base.js',
  262. 'Ha7507LzRmH3Utygtj', 'XFTb2HoeOE5MHg',
  263. ),
  264. (
  265. 'https://www.youtube.com/s/player/074a8365/player_ias_tce.vflset/en_US/base.js',
  266. 'Ha7507LzRmH3Utygtj', 'ufTsrE0IVYrkl8v',
  267. ),
  268. (
  269. 'https://www.youtube.com/s/player/643afba4/player_ias.vflset/en_US/base.js',
  270. 'N5uAlLqm0eg1GyHO', 'dCBQOejdq5s-ww',
  271. ),
  272. (
  273. 'https://www.youtube.com/s/player/69f581a5/tv-player-ias.vflset/tv-player-ias.js',
  274. '-qIP447rVlTTwaZjY', 'KNcGOksBAvwqQg',
  275. ),
  276. (
  277. 'https://www.youtube.com/s/player/643afba4/tv-player-ias.vflset/tv-player-ias.js',
  278. 'ir9-V6cdbCiyKxhr', '2PL7ZDYAALMfmA',
  279. ),
  280. (
  281. 'https://www.youtube.com/s/player/363db69b/player_ias.vflset/en_US/base.js',
  282. 'eWYu5d5YeY_4LyEDc', 'XJQqf-N7Xra3gg',
  283. ),
  284. (
  285. 'https://www.youtube.com/s/player/4fcd6e4a/player_ias.vflset/en_US/base.js',
  286. 'o_L251jm8yhZkWtBW', 'lXoxI3XvToqn6A',
  287. ),
  288. (
  289. 'https://www.youtube.com/s/player/4fcd6e4a/player_ias_tce.vflset/en_US/base.js',
  290. 'o_L251jm8yhZkWtBW', 'lXoxI3XvToqn6A',
  291. ),
  292. (
  293. 'https://www.youtube.com/s/player/20830619/tv-player-ias.vflset/tv-player-ias.js',
  294. 'ir9-V6cdbCiyKxhr', '9YE85kNjZiS4',
  295. ),
  296. (
  297. 'https://www.youtube.com/s/player/20830619/player-plasma-ias-phone-en_US.vflset/base.js',
  298. 'ir9-V6cdbCiyKxhr', '9YE85kNjZiS4',
  299. ),
  300. (
  301. 'https://www.youtube.com/s/player/20830619/player-plasma-ias-tablet-en_US.vflset/base.js',
  302. 'ir9-V6cdbCiyKxhr', '9YE85kNjZiS4',
  303. ),
  304. (
  305. 'https://www.youtube.com/s/player/8a8ac953/player_ias_tce.vflset/en_US/base.js',
  306. 'MiBYeXx_vRREbiCCmh', 'RtZYMVvmkE0JE',
  307. ),
  308. (
  309. 'https://www.youtube.com/s/player/8a8ac953/tv-player-es6.vflset/tv-player-es6.js',
  310. 'MiBYeXx_vRREbiCCmh', 'RtZYMVvmkE0JE',
  311. ),
  312. ]
  313. @is_download_test
  314. class TestPlayerInfo(unittest.TestCase):
  315. def test_youtube_extract_player_info(self):
  316. PLAYER_URLS = (
  317. ('https://www.youtube.com/s/player/4c3f79c5/player_ias.vflset/en_US/base.js', '4c3f79c5'),
  318. ('https://www.youtube.com/s/player/64dddad9/player_ias.vflset/en_US/base.js', '64dddad9'),
  319. ('https://www.youtube.com/s/player/64dddad9/player_ias.vflset/fr_FR/base.js', '64dddad9'),
  320. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-phone-en_US.vflset/base.js', '64dddad9'),
  321. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-phone-de_DE.vflset/base.js', '64dddad9'),
  322. ('https://www.youtube.com/s/player/64dddad9/player-plasma-ias-tablet-en_US.vflset/base.js', '64dddad9'),
  323. ('https://www.youtube.com/s/player/e7567ecf/player_ias_tce.vflset/en_US/base.js', 'e7567ecf'),
  324. ('https://www.youtube.com/s/player/643afba4/tv-player-ias.vflset/tv-player-ias.js', '643afba4'),
  325. # obsolete
  326. ('https://www.youtube.com/yts/jsbin/player_ias-vfle4-e03/en_US/base.js', 'vfle4-e03'),
  327. ('https://www.youtube.com/yts/jsbin/player_ias-vfl49f_g4/en_US/base.js', 'vfl49f_g4'),
  328. ('https://www.youtube.com/yts/jsbin/player_ias-vflCPQUIL/en_US/base.js', 'vflCPQUIL'),
  329. ('https://www.youtube.com/yts/jsbin/player-vflzQZbt7/en_US/base.js', 'vflzQZbt7'),
  330. ('https://www.youtube.com/yts/jsbin/player-en_US-vflaxXRn1/base.js', 'vflaxXRn1'),
  331. ('https://s.ytimg.com/yts/jsbin/html5player-en_US-vflXGBaUN.js', 'vflXGBaUN'),
  332. ('https://s.ytimg.com/yts/jsbin/html5player-en_US-vflKjOTVq/html5player.js', 'vflKjOTVq'),
  333. )
  334. for player_url, expected_player_id in PLAYER_URLS:
  335. player_id = YoutubeIE._extract_player_info(player_url)
  336. self.assertEqual(player_id, expected_player_id)
  337. @is_download_test
  338. class TestSignature(unittest.TestCase):
  339. def setUp(self):
  340. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  341. self.TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata/sigs')
  342. if not os.path.exists(self.TESTDATA_DIR):
  343. os.mkdir(self.TESTDATA_DIR)
  344. def tearDown(self):
  345. with contextlib.suppress(OSError):
  346. for f in os.listdir(self.TESTDATA_DIR):
  347. os.remove(f)
  348. def t_factory(name, sig_func, url_pattern):
  349. def make_tfunc(url, sig_input, expected_sig):
  350. m = url_pattern.match(url)
  351. assert m, f'{url!r} should follow URL format'
  352. test_id = re.sub(r'[/.-]', '_', m.group('id') or m.group('compat_id'))
  353. def test_func(self):
  354. basename = f'player-{test_id}.js'
  355. fn = os.path.join(self.TESTDATA_DIR, basename)
  356. if not os.path.exists(fn):
  357. urllib.request.urlretrieve(url, fn)
  358. with open(fn, encoding='utf-8') as testf:
  359. jscode = testf.read()
  360. self.assertEqual(sig_func(jscode, sig_input, url), expected_sig)
  361. test_func.__name__ = f'test_{name}_js_{test_id}'
  362. setattr(TestSignature, test_func.__name__, test_func)
  363. return make_tfunc
  364. def signature(jscode, sig_input, player_url):
  365. func = YoutubeIE(FakeYDL())._parse_sig_js(jscode, player_url)
  366. src_sig = (
  367. str(string.printable[:sig_input])
  368. if isinstance(sig_input, int) else sig_input)
  369. return func(src_sig)
  370. def n_sig(jscode, sig_input, player_url):
  371. ie = YoutubeIE(FakeYDL())
  372. funcname = ie._extract_n_function_name(jscode, player_url=player_url)
  373. jsi = JSInterpreter(jscode)
  374. func = jsi.extract_function_from_code(*ie._fixup_n_function_code(*jsi.extract_function_code(funcname), jscode, player_url))
  375. return func([sig_input])
  376. make_sig_test = t_factory(
  377. 'signature', signature,
  378. re.compile(r'''(?x)
  379. .+(?:
  380. /player/(?P<id>[a-zA-Z0-9_/.-]+)|
  381. /html5player-(?:en_US-)?(?P<compat_id>[a-zA-Z0-9_-]+)(?:/watch_as3|/html5player)?
  382. )\.js$'''))
  383. for test_spec in _SIG_TESTS:
  384. make_sig_test(*test_spec)
  385. make_nsig_test = t_factory(
  386. 'nsig', n_sig, re.compile(r'.+/player/(?P<id>[a-zA-Z0-9_/.-]+)\.js$'))
  387. for test_spec in _NSIG_TESTS:
  388. make_nsig_test(*test_spec)
  389. if __name__ == '__main__':
  390. unittest.main()