Skip to content

Instantly share code, notes, and snippets.

@Matir
Created September 20, 2018 03:33
Show Gist options
  • Save Matir/9e3c75d407aa2659091e02c08489ef7e to your computer and use it in GitHub Desktop.
Save Matir/9e3c75d407aa2659091e02c08489ef7e to your computer and use it in GitHub Desktop.
Rename videos from SoK with Title of Talk
import sys
from lxml import html
import os
import os.path
import re
run = True
buf = open(sys.argv[1]).read()
doc = html.fromstring(buf[:])
for e in doc.xpath('//a[@id]'):
title = e.text
inname = e.attrib['href']
fname, ext = os.path.splitext(os.path.basename(inname))
newname = re.sub(r'[^A-Za-z0-9_-]+', '_', fname+'-'+title).strip('_')[:200] + ext
#newname = os.path.join(os.path.dirname(inname), newname).encode('utf-8')
newname = newname.encode('utf-8')
sys.stderr.write('{} -> {}\n'.format(inname, newname))
try:
buf = buf.replace(inname, newname)
except:
print(repr(inname))
print(repr(newname))
raise
try:
if run:
os.rename(inname, newname)
except Exception as ex:
sys.stderr.write(str(ex)+'\n')
if run:
open(sys.argv[1], 'w').write(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment