test_tubeup.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. import unittest
  2. import os
  3. import shutil
  4. import json
  5. import time
  6. import requests_mock
  7. import glob
  8. import logging
  9. from tubeup.TubeUp import TubeUp, DOWNLOAD_DIR_NAME
  10. from tubeup import __version__
  11. from yt_dlp import YoutubeDL
  12. from .constants import info_dict_playlist, info_dict_video
  13. current_path = os.path.dirname(os.path.realpath(__file__))
  14. SCANNER = 'TubeUp Video Stream Mirroring Application {}'.format(__version__)
  15. def get_testfile_path(name):
  16. return os.path.join(current_path, 'test_tubeup_files', name)
  17. def mocked_ydl_progress_hook(d):
  18. pass
  19. def mock_upload_response_by_videobasename(m, ia_id, videobasename):
  20. files_to_upload = glob.glob(videobasename + '*')
  21. for file_path in files_to_upload:
  22. filename = os.path.basename(file_path)
  23. m.put('https://s3.us.archive.org/%s/%s' % (ia_id, filename),
  24. content=b'',
  25. headers={'content-type': 'text/plain'})
  26. def copy_testfiles_to_tubeup_rootdir_test():
  27. # Copy testfiles to rootdir path of TubeUp.
  28. # This method was created because after the uploading done by
  29. # internetarchive library, it deletes the files that has been uploaded.
  30. testfiles_dir = os.path.join(current_path, 'test_tubeup_files',
  31. 'files_for_upload_and_download_tests')
  32. for filepath in os.listdir(testfiles_dir):
  33. shutil.copy(
  34. os.path.join(testfiles_dir, filepath),
  35. os.path.join(current_path, 'test_tubeup_rootdir', 'downloads',
  36. filepath))
  37. class TubeUpTests(unittest.TestCase):
  38. def setUp(self):
  39. self.tu = TubeUp()
  40. self.maxDiff = 999999999
  41. def test_set_dir_path(self):
  42. root_path = os.path.join(
  43. current_path, '.directory_for_tubeup_set_dir_path_test')
  44. dir_paths_dict = dict(root=root_path,
  45. downloads=os.path.join(root_path,
  46. DOWNLOAD_DIR_NAME))
  47. self.tu.dir_path = root_path
  48. self.assertEqual(self.tu.dir_path, dir_paths_dict)
  49. # Make sure that other directories are created as well
  50. self.assertTrue(os.path.exists(dir_paths_dict['downloads']))
  51. # Clean the test directory
  52. shutil.rmtree(root_path, ignore_errors=True)
  53. def test_tubeup_attribute_logger_when_quiet_mode(self):
  54. # self.tu is already `TubeUp` instance with quiet mode, so we don't
  55. # create a new instance here.
  56. self.assertIsInstance(self.tu.logger, logging.Logger)
  57. self.assertEqual(self.tu.logger.level, logging.ERROR)
  58. def test_tubeup_attribute_logger_when_verbose_mode(self):
  59. tu = TubeUp(verbose=True)
  60. self.assertIsInstance(tu.logger, logging.Logger)
  61. def test_determine_collection_type(self):
  62. soundcloud_colltype = self.tu.determine_collection_type(
  63. 'https://soundcloud.com/testurl')
  64. another_colltype = self.tu.determine_collection_type(
  65. 'https://www.youtube.com/watch?v=testVideo'
  66. )
  67. self.assertEqual(soundcloud_colltype, 'opensource_audio')
  68. self.assertEqual(another_colltype, 'opensource_movies')
  69. def test_create_basenames_from_ydl_info_dict_video(self):
  70. ydl = YoutubeDL()
  71. result = self.tu.create_basenames_from_ydl_info_dict(
  72. ydl, info_dict_video)
  73. expected_result = set(
  74. ['Video and Blog Competition 2017 - Bank Indonesia & '
  75. 'NET TV #BIGoesToCampus [hlG3LeFaQwU]'])
  76. self.assertEqual(result, expected_result)
  77. def test_create_basenames_from_ydl_info_dict_playlist(self):
  78. ydl = YoutubeDL()
  79. result = self.tu.create_basenames_from_ydl_info_dict(
  80. ydl, info_dict_playlist)
  81. expected_result = set([
  82. 'Live Streaming Rafid Aslam [7gjgkH5iPaE]',
  83. 'Live Streaming Rafid Aslam [q92kxPm-pqM]',
  84. 'Cara Membuat Laptop Menjadi Hotspot WiFi Dengan CMD [YjFwMSDNphM]',
  85. '[CSO] Defeat Boss in Dead End With Thanatos 7 [EEm6MwXLse0]',
  86. 'Cara Bermain Minecraft Multiplayer Dengan LAN [g2vTZ2ka-tM]',
  87. 'Live Streaming Rafid Aslam [AXhuSS5_9YU]',
  88. 'Cara Membuat Disk Baru di Komputer [KDOygJnK7Sw]',
  89. 'Cara Mendownload Lewat Torrent [cC-9RghkvXs]']
  90. )
  91. self.assertEqual(result, expected_result)
  92. def test_generate_ydl_options_with_download_archive(self):
  93. result = self.tu.generate_ydl_options(mocked_ydl_progress_hook,
  94. use_download_archive=True)
  95. expected_result = {
  96. 'outtmpl': os.path.join(
  97. self.tu.dir_path['downloads'], '%(id)s.%(ext)s'),
  98. 'restrictfilenames': True,
  99. 'verbose': False,
  100. 'quiet': True,
  101. 'download_archive': os.path.join(self.tu.dir_path['root'],
  102. '.ytdlarchive'),
  103. 'progress_with_newline': True,
  104. 'forcetitle': True,
  105. 'continuedl': True,
  106. 'retries': 9001,
  107. 'fragment_retries': 9001,
  108. 'forcejson': False,
  109. 'writeinfojson': True,
  110. 'writedescription': True,
  111. 'getcomments': False,
  112. 'writethumbnail': True,
  113. 'writeannotations': True,
  114. 'writesubtitles': True,
  115. 'allsubtitles': True,
  116. 'ignoreerrors': True,
  117. 'fixup': 'warn',
  118. 'nooverwrites': True,
  119. 'consoletitle': True,
  120. 'prefer_ffmpeg': True,
  121. 'call_home': False,
  122. 'logger': self.tu.logger,
  123. 'progress_hooks': [mocked_ydl_progress_hook]}
  124. self.assertEqual(result, expected_result)
  125. def test_generate_ydl_options(self):
  126. result = self.tu.generate_ydl_options(mocked_ydl_progress_hook)
  127. expected_result = {
  128. 'outtmpl': os.path.join(
  129. self.tu.dir_path['downloads'], '%(id)s.%(ext)s'),
  130. 'restrictfilenames': True,
  131. 'verbose': False,
  132. 'quiet': True,
  133. 'progress_with_newline': True,
  134. 'forcetitle': True,
  135. 'continuedl': True,
  136. 'retries': 9001,
  137. 'fragment_retries': 9001,
  138. 'forcejson': False,
  139. 'writeinfojson': True,
  140. 'writedescription': True,
  141. 'getcomments': False,
  142. 'writethumbnail': True,
  143. 'writeannotations': True,
  144. 'writesubtitles': True,
  145. 'allsubtitles': True,
  146. 'ignoreerrors': True,
  147. 'fixup': 'warn',
  148. 'nooverwrites': True,
  149. 'consoletitle': True,
  150. 'prefer_ffmpeg': True,
  151. 'call_home': False,
  152. 'logger': self.tu.logger,
  153. 'progress_hooks': [mocked_ydl_progress_hook]}
  154. self.assertEqual(result, expected_result)
  155. def test_generate_ydl_options_with_proxy(self):
  156. result = self.tu.generate_ydl_options(
  157. mocked_ydl_progress_hook, proxy_url='http://proxytest.com:8080')
  158. expected_result = {
  159. 'outtmpl': os.path.join(
  160. self.tu.dir_path['downloads'], '%(id)s.%(ext)s'),
  161. 'restrictfilenames': True,
  162. 'verbose': False,
  163. 'quiet': True,
  164. 'progress_with_newline': True,
  165. 'forcetitle': True,
  166. 'continuedl': True,
  167. 'retries': 9001,
  168. 'fragment_retries': 9001,
  169. 'forcejson': False,
  170. 'writeinfojson': True,
  171. 'writedescription': True,
  172. 'getcomments': False,
  173. 'writethumbnail': True,
  174. 'writeannotations': True,
  175. 'writesubtitles': True,
  176. 'allsubtitles': True,
  177. 'ignoreerrors': True,
  178. 'fixup': 'warn',
  179. 'nooverwrites': True,
  180. 'consoletitle': True,
  181. 'prefer_ffmpeg': True,
  182. 'call_home': False,
  183. 'logger': self.tu.logger,
  184. 'progress_hooks': [mocked_ydl_progress_hook],
  185. 'proxy': 'http://proxytest.com:8080'}
  186. self.assertEqual(result, expected_result)
  187. def test_generate_ydl_options_with_ydl_account(self):
  188. result = self.tu.generate_ydl_options(
  189. mocked_ydl_progress_hook, ydl_username='testUsername',
  190. ydl_password='testPassword')
  191. expected_result = {
  192. 'outtmpl': os.path.join(
  193. self.tu.dir_path['downloads'], '%(id)s.%(ext)s'),
  194. 'restrictfilenames': True,
  195. 'verbose': False,
  196. 'quiet': True,
  197. 'progress_with_newline': True,
  198. 'forcetitle': True,
  199. 'continuedl': True,
  200. 'retries': 9001,
  201. 'fragment_retries': 9001,
  202. 'forcejson': False,
  203. 'writeinfojson': True,
  204. 'writedescription': True,
  205. 'getcomments': False,
  206. 'writethumbnail': True,
  207. 'writeannotations': True,
  208. 'writesubtitles': True,
  209. 'allsubtitles': True,
  210. 'ignoreerrors': True,
  211. 'fixup': 'warn',
  212. 'nooverwrites': True,
  213. 'consoletitle': True,
  214. 'prefer_ffmpeg': True,
  215. 'call_home': False,
  216. 'logger': self.tu.logger,
  217. 'progress_hooks': [mocked_ydl_progress_hook],
  218. 'username': 'testUsername',
  219. 'password': 'testPassword'}
  220. self.assertEqual(result, expected_result)
  221. def test_generate_ydl_options_with_verbose_mode(self):
  222. tu = TubeUp(verbose=True)
  223. result = tu.generate_ydl_options(
  224. mocked_ydl_progress_hook, ydl_username='testUsername',
  225. ydl_password='testPassword')
  226. expected_result = {
  227. 'outtmpl': os.path.join(
  228. self.tu.dir_path['downloads'], '%(id)s.%(ext)s'),
  229. 'restrictfilenames': True,
  230. 'verbose': True,
  231. 'quiet': False,
  232. 'progress_with_newline': True,
  233. 'forcetitle': True,
  234. 'continuedl': True,
  235. 'retries': 9001,
  236. 'fragment_retries': 9001,
  237. 'forcejson': False,
  238. 'writeinfojson': True,
  239. 'writedescription': True,
  240. 'getcomments': False,
  241. 'writethumbnail': True,
  242. 'writeannotations': True,
  243. 'writesubtitles': True,
  244. 'allsubtitles': True,
  245. 'ignoreerrors': True,
  246. 'fixup': 'warn',
  247. 'nooverwrites': True,
  248. 'consoletitle': True,
  249. 'prefer_ffmpeg': True,
  250. 'call_home': False,
  251. 'logger': tu.logger,
  252. 'progress_hooks': [mocked_ydl_progress_hook],
  253. 'username': 'testUsername',
  254. 'password': 'testPassword'}
  255. self.assertEqual(result, expected_result)
  256. def test_create_archive_org_metadata_from_youtubedl_meta(self):
  257. with open(get_testfile_path(
  258. 'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
  259. ) as f:
  260. vid_meta = json.load(f)
  261. result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
  262. vid_meta
  263. )
  264. expected_result = {
  265. 'mediatype': 'movies',
  266. 'creator': 'Video Background',
  267. 'collection': 'opensource_movies',
  268. 'title': 'Mountain 3 - Video Background HD 1080p',
  269. 'description': ('Mountain 3 - Video Background HD 1080p<br>'
  270. 'If you use this video please put credits to my '
  271. 'channel in description:<br>https://www.youtube.com'
  272. '/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
  273. 'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
  274. 'Hope you all enjoy! <br/><br/>Source: '
  275. '<a href="https://www.youtube.com/watch?v='
  276. '6iRV8liah8A">https://www.youtube.com/watch?v='
  277. '6iRV8liah8A</a><br/>Uploader: <a href="http://ww'
  278. 'w.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg">'
  279. 'Video Background</a>'),
  280. 'date': '2015-01-05',
  281. 'year': '2015',
  282. 'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
  283. 'Animation;Cinema;stock video footage;Royalty '
  284. 'free videos;Creative Commons videos;free movies '
  285. 'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
  286. 'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
  287. 'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
  288. 'scanner': SCANNER}
  289. self.assertEqual(expected_result, result)
  290. def test_create_archive_org_metadata_from_youtubedl_meta_description_text_null(self):
  291. with open(get_testfile_path(
  292. 'description_text_null.json')
  293. ) as f:
  294. vid_meta = json.load(f)
  295. result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
  296. vid_meta
  297. )
  298. expected_description = (' <br/><br/>Source: <a href="url">url</a><br/>'
  299. 'Uploader: <a href="url">tubeup.py</a>')
  300. self.assertEqual(expected_description, result.get('description'))
  301. def test_create_archive_org_metadata_from_youtubedl_meta_no_uploader(self):
  302. with open(get_testfile_path(
  303. 'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info_no_'
  304. 'uploader.json')
  305. ) as f:
  306. vid_meta = json.load(f)
  307. result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
  308. vid_meta
  309. )
  310. expected_result = {
  311. 'mediatype': 'movies',
  312. 'creator': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
  313. 'collection': 'opensource_movies',
  314. 'title': 'Mountain 3 - Video Background HD 1080p',
  315. 'description': ('Mountain 3 - Video Background HD 1080p<br>'
  316. 'If you use this video please put credits to my '
  317. 'channel in description:<br>https://www.youtube.com'
  318. '/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
  319. 'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
  320. 'Hope you all enjoy! <br/><br/>Source: '
  321. '<a href="https://www.youtube.com/watch?v='
  322. '6iRV8liah8A">https://www.youtube.com/watch?v='
  323. '6iRV8liah8A</a><br/>Uploader: <a href="http://ww'
  324. 'w.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg">'
  325. 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZ'
  326. 'HQ9XDg</a>'),
  327. 'date': '2015-01-05',
  328. 'year': '2015',
  329. 'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
  330. 'Animation;Cinema;stock video footage;Royalty '
  331. 'free videos;Creative Commons videos;free movies '
  332. 'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
  333. 'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
  334. 'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
  335. 'scanner': SCANNER}
  336. self.assertEqual(expected_result, result)
  337. def test_create_archive_org_metadata_from_youtubedl_meta_no_date(self):
  338. with open(get_testfile_path(
  339. 'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.'
  340. 'info_no_date.json')
  341. ) as f:
  342. vid_meta = json.load(f)
  343. result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
  344. vid_meta
  345. )
  346. upload_date = time.strftime("%Y-%m-%d")
  347. upload_year = time.strftime("%Y")
  348. expected_result = {
  349. 'mediatype': 'movies',
  350. 'creator': 'Video Background',
  351. 'collection': 'opensource_movies',
  352. 'title': 'Mountain 3 - Video Background HD 1080p',
  353. 'description': ('Mountain 3 - Video Background HD 1080p<br>'
  354. 'If you use this video please put credits to my '
  355. 'channel in description:<br>https://www.youtube.com'
  356. '/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
  357. 'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
  358. 'Hope you all enjoy! <br/><br/>Source: '
  359. '<a href="https://www.youtube.com/watch?v='
  360. '6iRV8liah8A">https://www.youtube.com/watch?v='
  361. '6iRV8liah8A</a><br/>Uploader: <a href="http://ww'
  362. 'w.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg">'
  363. 'Video Background</a>'),
  364. 'date': upload_date,
  365. 'year': upload_year,
  366. 'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
  367. 'Animation;Cinema;stock video footage;Royalty '
  368. 'free videos;Creative Commons videos;free movies '
  369. 'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
  370. 'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
  371. 'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
  372. 'scanner': SCANNER}
  373. self.assertEqual(expected_result, result)
  374. def test_create_archive_org_metadata_from_youtubedl_meta_twitch_clips(self):
  375. with open(get_testfile_path(
  376. 'EA_Play_2016_Live_from_the_Novo_Theatre-42850523.info.json')
  377. ) as f:
  378. vid_meta = json.load(f)
  379. result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
  380. vid_meta
  381. )
  382. expected_result = {
  383. 'mediatype': 'movies',
  384. 'creator': 'EA',
  385. 'collection': 'opensource_movies',
  386. 'title': 'EA Play 2016 Live from the Novo Theatre',
  387. 'description': (' <br/><br/>Source: <a href="https://clips.twitch.tv/FaintLightGullWholeWheat">'
  388. 'https://clips.twitch.tv/FaintLightGullWholeWheat</a><br/>Uploader: '
  389. '<a href="https://clips.twitch.tv/FaintLightGullWholeWheat">EA</a>'),
  390. 'date': '2016-06-12',
  391. 'year': '2016',
  392. 'subject': 'TwitchClips;video;',
  393. 'originalurl': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
  394. 'licenseurl': '',
  395. 'scanner': SCANNER}
  396. self.assertEqual(expected_result, result)
  397. def test_get_resource_basenames(self):
  398. tu = TubeUp(dir_path=os.path.join(current_path,
  399. 'test_tubeup_rootdir'))
  400. copy_testfiles_to_tubeup_rootdir_test()
  401. result = tu.get_resource_basenames(
  402. ['https://www.youtube.com/watch?v=KdsN9YhkDrY'],
  403. ignore_existing_item=True)
  404. expected_result = {os.path.join(
  405. current_path, 'test_tubeup_rootdir', 'downloads',
  406. 'KdsN9YhkDrY')}
  407. self.assertEqual(expected_result, result)
  408. def test_upload_ia(self):
  409. tu = TubeUp(dir_path=os.path.join(current_path,
  410. 'test_tubeup_rootdir'),
  411. # Use custom ia configuration file so we don't need
  412. # to login with username and password.
  413. ia_config_path=get_testfile_path('ia_config_for_test.ini'))
  414. videobasename = os.path.join(
  415. current_path, 'test_tubeup_rootdir', 'downloads',
  416. 'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A')
  417. copy_testfiles_to_tubeup_rootdir_test()
  418. with requests_mock.Mocker() as m:
  419. # Mock the request to s3.us.archive.org, so it will responds
  420. # a custom json. `internetarchive` library sends GET request to
  421. # that url to check that we don't violate the upload limit.
  422. m.get('https://s3.us.archive.org',
  423. content=b'{"over_limit": 0}',
  424. headers={'content-type': 'application/json'})
  425. m.get('https://archive.org/metadata/youtube-6iRV8liah8A',
  426. content=b'{}',
  427. headers={'content-type': 'application/json'})
  428. # Mock the PUT requests for internetarchive urls that defined
  429. # in mock_upload_response_by_videobasename(), so this test
  430. # doesn't perform upload to the real archive.org server.
  431. mock_upload_response_by_videobasename(
  432. m, 'youtube-6iRV8liah8A', videobasename)
  433. result = tu.upload_ia(videobasename)
  434. expected_result = (
  435. 'youtube-6iRV8liah8A',
  436. {'mediatype': 'movies',
  437. 'creator': 'Video Background',
  438. 'collection': 'opensource_movies',
  439. 'title': 'Mountain 3 - Video Background HD 1080p',
  440. 'description': ('Mountain 3 - Video Background HD 1080p<br>If '
  441. 'you use this video please put credits to my'
  442. ' channel in description:<br>https://www.youtub'
  443. 'e.com/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© D'
  444. 'on\'t forget to SUBSCRIBE, LIKE, COMMENT an'
  445. 'd RATE. Hope you all enjoy! <br/><br/>Sourc'
  446. 'e: <a href="https://www.youtube.com/watch?v'
  447. '=6iRV8liah8A">https://www.youtube.com/watch'
  448. '?v=6iRV8liah8A</a><br/>Uploader: <a href="h'
  449. 'ttp://www.youtube.com/channel/UCWpsozCMdAnf'
  450. 'I16rZHQ9XDg">Video Background</a>'),
  451. 'date': '2015-01-05',
  452. 'year': '2015',
  453. 'subject': ('Youtube;video;Entertainment;Video Background;'
  454. 'Footage;Animation;Cinema;stock video footage;'
  455. 'Royalty free videos;Creative Commons videos;'
  456. 'free movies online;youtube;HD;1080p;Amazing '
  457. 'Nature;Mountain;'),
  458. 'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
  459. 'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
  460. 'scanner': SCANNER})
  461. self.assertEqual(expected_result, result)
  462. def test_archive_urls(self):
  463. tu = TubeUp(dir_path=os.path.join(current_path,
  464. 'test_tubeup_rootdir'),
  465. ia_config_path=get_testfile_path('ia_config_for_test.ini'))
  466. videobasename = os.path.join(
  467. current_path, 'test_tubeup_rootdir', 'downloads',
  468. 'KdsN9YhkDrY')
  469. copy_testfiles_to_tubeup_rootdir_test()
  470. with requests_mock.Mocker() as m:
  471. # Mock the request to s3.us.archive.org, so it will responds
  472. # a custom json. `internetarchive` library sends GET request to
  473. # that url to check that we don't violate the upload limit.
  474. m.get('https://s3.us.archive.org',
  475. content=b'{"over_limit": 0}',
  476. headers={'content-type': 'application/json'})
  477. m.get('https://archive.org/metadata/youtube-KdsN9YhkDrY',
  478. content=b'{}',
  479. headers={'content-type': 'application/json'})
  480. # Mock the PUT requests for internetarchive urls that defined
  481. # in mock_upload_response_by_videobasename(), so this test
  482. # doesn't perform upload to the real archive.org server.
  483. mock_upload_response_by_videobasename(
  484. m, 'youtube-KdsN9YhkDrY', videobasename)
  485. result = list(tu.archive_urls(
  486. ['https://www.youtube.com/watch?v=KdsN9YhkDrY']))
  487. expected_result = [(
  488. 'youtube-KdsN9YhkDrY',
  489. {'mediatype': 'movies',
  490. 'creator': 'RelaxingWorld',
  491. 'collection': 'opensource_movies',
  492. 'title': 'Epic Ramadan - Video Background HD1080p',
  493. 'description': ('If you enjoy my work, please consider Subscribe to my NEW '
  494. 'channel for more videos: <br>'
  495. 'https://www.youtube.com/MusicForRelaxation?sub_confirmation=1 <br>'
  496. '▷ If you use this video, please put credits to my channel '
  497. 'in description: <br>'
  498. 'Source from RelaxingWorld: https://goo.gl/HsW75m<br>'
  499. '<br>'
  500. '▷ Also, do not forget to Subscribe to my channel. Thanks! '
  501. '<br/><br/>Source: <a '
  502. 'href="https://www.youtube.com/watch?v=KdsN9YhkDrY">'
  503. 'https://www.youtube.com/watch?v=KdsN9YhkDrY</a><br/>Uploader: '
  504. '<a '
  505. 'href="http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg">'
  506. 'RelaxingWorld</a>'
  507. ),
  508. 'date': '2016-06-25',
  509. 'year': '2016',
  510. 'subject': ('Youtube;video;Film & Animation;Video Background;'
  511. 'Footage;Animation;Cinema;Royalty Free Videos;'
  512. 'Stock Video Footage;Video Backdrops;'
  513. 'Amazing Nature;youtube;HD;1080p;Creative Commons Videos;'
  514. 'relaxing music;Ramadan;'),
  515. 'originalurl': 'https://www.youtube.com/watch?v=KdsN9YhkDrY',
  516. 'licenseurl': '',
  517. 'scanner': SCANNER})]
  518. self.assertEqual(expected_result, result)