Created
August 27, 2018 21:46
-
-
Save jjjake/1656a6080d05c591a021de6033441f5f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import datetime | |
import time | |
import pysrt | |
from internetarchive import get_item | |
def download_srt(item): | |
srt = list(item.get_files(formats=['SubRip'])) | |
if not srt: | |
print('error: no srt file found for {}'.format(item.identifier)) | |
sys.exit(1) | |
else: | |
srt_fo = srt[0] | |
r = srt_fo.download(silent=True) | |
return srt_fo | |
def get_description(srt_fp, item): | |
subs = pysrt.open(srt_fp) | |
description = 'Closed captions transcript:\n\n' | |
for x in subs: | |
timestamp = str(x.start).split(',')[0] | |
t = time.strptime(timestamp, '%H:%M:%S') | |
ss = int(datetime.timedelta(hours=t.tm_hour, | |
minutes=t.tm_min, | |
seconds=t.tm_sec).total_seconds()) | |
link = '<a href="/details/{0}?start={1}">{2}</a>'.format(item.identifier, ss, timestamp) | |
_desc = '{0}\n{1}\n\n'.format(link, x.text.encode('utf-8')) | |
description += _desc | |
return description | |
if __name__ == '__main__': | |
item = get_item(sys.argv[-1]) | |
srt_fo = download_srt(item) | |
srt_fp = os.path.join(os.getcwd(), srt_fo.name) | |
description = get_description(srt_fp, item) | |
if item.metadata.get('description'): | |
print('warning: {} - already has a description, skipping.'.format(item.identifier)) | |
sys.exit(0) | |
# Update description | |
r = item.modify_metadata(dict(description=description.strip())) | |
if not r.ok: | |
print('error: {} - failed to update description.'.format(item.identifier)) | |
sys.exit(1) | |
print('success: {}'.format(item.identifier)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment