Skip to content

Commit 6e7f62e

Browse files
authored
Truncate tags_string until it fits in 255 bytes (#286)
* truncate tags_string until it fits in 255 bytes IA's subject field has a 255 bytes length limit * test: add test for tags_string truncation * style: autopep8 * fix: test: tags_string truncate to 255 bytes (not chars)
1 parent 8c6fd01 commit 6e7f62e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

tests/test_tubeup.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def test_generate_ydl_options_with_verbose_mode(self):
298298
def test_create_archive_org_metadata_from_youtubedl_meta(self):
299299
with open(get_testfile_path(
300300
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
301-
) as f:
301+
) as f:
302302
vid_meta = json.load(f)
303303

304304
result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
@@ -332,7 +332,7 @@ def test_create_archive_org_metadata_from_youtubedl_meta(self):
332332
def test_create_archive_org_metadata_from_youtubedl_meta_description_text_null(self):
333333
with open(get_testfile_path(
334334
'description_text_null.json')
335-
) as f:
335+
) as f:
336336
vid_meta = json.load(f)
337337

338338
result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
@@ -419,7 +419,7 @@ def test_create_archive_org_metadata_from_youtubedl_meta_no_date(self):
419419
def test_create_archive_org_metadata_from_youtubedl_meta_twitch_clips(self):
420420
with open(get_testfile_path(
421421
'EA_Play_2016_Live_from_the_Novo_Theatre-42850523.info.json')
422-
) as f:
422+
) as f:
423423
vid_meta = json.load(f)
424424

425425
result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
@@ -441,6 +441,21 @@ def test_create_archive_org_metadata_from_youtubedl_meta_twitch_clips(self):
441441

442442
self.assertEqual(expected_result, result)
443443

444+
def test_create_archive_org_metadata_from_youtubedl_meta_mass_of_tags(self):
445+
with open(get_testfile_path(
446+
'Mountain_3_-_Video_Background_HD_1080p-6iRV8liah8A.info.json')
447+
) as f:
448+
vid_meta = json.load(f)
449+
450+
vid_meta['tags'] = [f't{i}' for i in range(0, 300)]
451+
452+
result = TubeUp.create_archive_org_metadata_from_youtubedl_meta(
453+
vid_meta
454+
)
455+
456+
self.assertLessEqual(len(result['subject'].encode(encoding='utf-8')),
457+
255, msg='tags_string not truncated to <= 255 bytes')
458+
444459
def test_get_resource_basenames(self):
445460
tu = TubeUp(dir_path=os.path.join(current_path,
446461
'test_tubeup_rootdir'))

tubeup/TubeUp.py

+6
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ def create_archive_org_metadata_from_youtubedl_meta(vid_meta):
517517
except Exception:
518518
print("Unable to process tags successfully.")
519519

520+
# IA's subject field has a 255 bytes length limit, so we need to truncate tags_string
521+
while len(tags_string.encode('utf-8')) > 255:
522+
tags_list = tags_string.split(';')
523+
tags_list.pop()
524+
tags_string = ';'.join(tags_list)
525+
520526
# license
521527
licenseurl = TubeUp.determine_licenseurl(vid_meta)
522528

0 commit comments

Comments
 (0)