Skip to content

Commit

Permalink
extract metadataConverter from tubeUp
Browse files Browse the repository at this point in the history
  • Loading branch information
orkeilius committed Nov 15, 2024
1 parent ed39ad2 commit cae3c2a
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 313 deletions.
182 changes: 182 additions & 0 deletions tests/helper/test_MetadataConverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import time
import json
import os
import unittest

from tubeup import __version__
from tubeup.Helper.MetadataConverter import MetadataConverter

SCANNER = 'TubeUp Video Stream Mirroring Application {}'.format(__version__)

def get_testfile_path(name):
current_path = os.path.dirname(os.path.realpath(__file__))
return os.path.join(current_path, '../test_tubeup_files', name)

class MetadataConverterTest(unittest.TestCase):
def test_determine_collection_type(self):
soundcloud_colltype = MetadataConverter.determine_collection_type('https://soundcloud.com/testurl')
self.assertEqual(soundcloud_colltype, 'opensource_audio')

another_colltype = MetadataConverter.determine_collection_type('https://www.youtube.com/watch?v=testVideo')
self.assertEqual(another_colltype, 'opensource_movies')



def test_create_archive_org_metadata_from_youtubedl_meta(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
) as f:
vid_meta = json.load(f)

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'Video Background',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': '2015-01-05',
'year': '2015',
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_description_text_null(self):
with open(get_testfile_path(
'description_text_null.json')
) as f:
vid_meta = json.load(f)

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_description = ('')

self.assertEqual(expected_description, result.get('description'))

def test_create_archive_org_metadata_from_youtubedl_meta_no_uploader(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info_no_'
'uploader.json')
) as f:
vid_meta = json.load(f)

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': '2015-01-05',
'year': '2015',
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_no_date(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.'
'info_no_date.json')
) as f:
vid_meta = json.load(f)

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

upload_date = time.strftime("%Y-%m-%d")
upload_year = time.strftime("%Y")

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'Video Background',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': upload_date,
'year': upload_year,
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_twitch_clips(self):
with open(get_testfile_path(
'EA_Play_2016_Live_from_the_Novo_Theatre-42850523.info.json')
) as f:
vid_meta = json.load(f)

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'creator': 'EA',
'collection': 'opensource_movies',
'title': 'EA Play 2016 Live from the Novo Theatre',
'description': (''),
'date': '2016-06-12',
'year': '2016',
'subject': 'TwitchClips;video;',
'originalurl': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
'licenseurl': '',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_mass_of_tags(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
) as f:
vid_meta = json.load(f)

vid_meta['tags'] = [f't{i}' for i in range(0, 300)]

result = MetadataConverter.create_archive_org_metadata_from_youtubedl_meta(vid_meta)

self.assertLessEqual(len(result['subject'].encode(encoding='utf-8')),
255, msg='tags_string not truncated to <= 255 bytes')
173 changes: 1 addition & 172 deletions tests/test_tubeup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,7 @@ def test_tubeup_attribute_logger_when_quiet_mode(self):
def test_tubeup_attribute_logger_when_verbose_mode(self):
tu = TubeUp(verbose=True)
self.assertIsInstance(tu.logger, logging.Logger)

def test_determine_collection_type(self):
soundcloud_colltype = self.tu.determine_collection_type(
'https://soundcloud.com/testurl')
another_colltype = self.tu.determine_collection_type(
'https://www.youtube.com/watch?v=testVideo'
)

self.assertEqual(soundcloud_colltype, 'opensource_audio')
self.assertEqual(another_colltype, 'opensource_movies')


def test_create_basenames_from_ydl_info_dict_video(self):
ydl = YoutubeDL()
result = self.tu.create_basenames_from_ydl_info_dict(
Expand Down Expand Up @@ -106,167 +96,6 @@ def test_create_basenames_from_ydl_info_dict_playlist(self):

self.assertEqual(result, expected_result)

def test_create_archive_org_metadata_from_youtubedl_meta(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
) as f:
vid_meta = json.load(f)

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'Video Background',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': '2015-01-05',
'year': '2015',
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_description_text_null(self):
with open(get_testfile_path(
'description_text_null.json')
) as f:
vid_meta = json.load(f)

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_description = ('')

self.assertEqual(expected_description, result.get('description'))

def test_create_archive_org_metadata_from_youtubedl_meta_no_uploader(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info_no_'
'uploader.json')
) as f:
vid_meta = json.load(f)

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': '2015-01-05',
'year': '2015',
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_no_date(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.'
'info_no_date.json')
) as f:
vid_meta = json.load(f)

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

upload_date = time.strftime("%Y-%m-%d")
upload_year = time.strftime("%Y")

expected_result = {
'mediatype': 'movies',
'channel': 'http://www.youtube.com/channel/UCWpsozCMdAnfI16rZHQ9XDg',
'creator': 'Video Background',
'collection': 'opensource_movies',
'title': 'Mountain 3 - Video Background HD 1080p',
'description': ('Mountain 3 - Video Background HD 1080p<br>'
'If you use this video please put credits to my '
'channel in description:<br>https://www.youtube.com'
'/channel/UCWpsozCMdAnfI16rZHQ9XDg<br>© Don\'t '
'forget to SUBSCRIBE, LIKE, COMMENT and RATE. '
'Hope you all enjoy!'),
'date': upload_date,
'year': upload_year,
'subject': ('Youtube;video;Entertainment;Video Background;Footage;'
'Animation;Cinema;stock video footage;Royalty '
'free videos;Creative Commons videos;free movies '
'online;youtube;HD;1080p;Amazing Nature;Mountain;'),
'originalurl': 'https://www.youtube.com/watch?v=6iRV8liah8A',
'licenseurl': 'https://creativecommons.org/licenses/by/3.0/',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_twitch_clips(self):
with open(get_testfile_path(
'EA_Play_2016_Live_from_the_Novo_Theatre-42850523.info.json')
) as f:
vid_meta = json.load(f)

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

expected_result = {
'mediatype': 'movies',
'creator': 'EA',
'collection': 'opensource_movies',
'title': 'EA Play 2016 Live from the Novo Theatre',
'description': (''),
'date': '2016-06-12',
'year': '2016',
'subject': 'TwitchClips;video;',
'originalurl': 'https://clips.twitch.tv/FaintLightGullWholeWheat',
'licenseurl': '',
'scanner': SCANNER}

self.assertEqual(expected_result, result)

def test_create_archive_org_metadata_from_youtubedl_meta_mass_of_tags(self):
with open(get_testfile_path(
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
) as f:
vid_meta = json.load(f)

vid_meta['tags'] = [f't{i}' for i in range(0, 300)]

result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
vid_meta
)

self.assertLessEqual(len(result['subject'].encode(encoding='utf-8')),
255, msg='tags_string not truncated to <= 255 bytes')

def test_get_resource_basenames(self):
tu = TubeUp(dir_path=os.path.join(current_path,
'test_tubeup_rootdir'))
Expand Down
Loading

4 comments on commit cae3c2a

@vxbinaca
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move away from using youtube as a unit test, youtube has anti-rip tech thats causing the unit tests to fail.

@orkeilius
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to try to make it more clear before changing test bbut why not using something like mockito (in java) to mock youtube related stuff

@vxbinaca
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to try to make it more clear before changing test bbut why not using something like mockito (in java) to mock youtube related stuff

Increases dependencies and code languages and probably won't work. Site specific hack even for unit tests. I wrote about site problems, might save you some effort in picking a new site to pull unit tests from.

@orkeilius
Copy link
Owner Author

@orkeilius orkeilius commented on cae3c2a Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increases dependencies and code languages and probably won't work. Site specific hack even for unit tests. I wrote about bibanon#340, might save you some effort in picking a new site to pull unit tests from.

When I say something like mockito in java, I mean something like it but in python
Also, you already use something similar for request (requests-mock) in test

edit : I found the that unittest already have a mocking system. So if I ever remake the tests, i'm probably going to use that
https://docs.python.org/3/library/unittest.mock.html

Please sign in to comment.