Skip to content

Instantly share code, notes, and snippets.

@GISmd
Last active February 20, 2018 16:21
Show Gist options
  • Save GISmd/c39e46d4e70beaf71ba28c7d2adafc46 to your computer and use it in GitHub Desktop.
Save GISmd/c39e46d4e70beaf71ba28c7d2adafc46 to your computer and use it in GitHub Desktop.
A little script that attempts to fix the awful naming scheme of the Brains On! podcast.
'''A little script that attempts to fix the awful naming scheme of the Brains
On! podcast.'''
import os
import glob
import eyed3
import re
def new_name(filename, mp3_date, title):
"""Gives a new filename starting with the eyed3 date and then the title
(if there is one)."""
reserved = '<>::"/\|?*'
if title:
title = re.sub('[{}]'.format(reserved), '', title)
if not title:
return '{}_no_title.mp3'.format(mp3_date)
else:
return '{}_{}.mp3'.format(mp3_date,title)
def rename_files(filenames, new_names):
for f, n in zip(filenames, new_names):
os.rename(f, n)
def redun_name_check(name2check, names):
"""Returns an int of the number of occurences of the name within
names"""
i = 0
for n in names:
if n == name2check:
i += 1
return i
d = r'I:\Podcasts\[brains]brains on! science podcast for kid\temp'
os.chdir(d)
r = re.compile(r'(?<=[^0-9][_|~])[0-9]{8}')
fs = glob.glob('*.mp3')
dates = [m.group(0) for l in fs for m in [r.search(l)] if m]
titles = []
for f, m in zip(fs, dates):
titles.append(eyed3.load(f).tag.title)
newnames = []
for i, (f, d, t) in enumerate(zip(fs, dates, titles)):
newname = new_name(f, d, t)
occurances = redun_name_check(newname, newnames)
if occurances:
newname = newname[:8] + '_{}'.format(occurances) + newname[8:]
newnames.append(newname)
#with open('names.csv', 'w') as myfile:
# for f, n in zip(fs, newnames):
# myfile.write('{},{}\n'.format(f, n))
rename_files(fs, newnames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment