Skip to content

Instantly share code, notes, and snippets.

@tdeebswihart
Created August 22, 2015 23:15
Show Gist options
  • Save tdeebswihart/29b0737cff6a9eaa98cf to your computer and use it in GitHub Desktop.
Save tdeebswihart/29b0737cff6a9eaa98cf to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import click
from collections import defaultdict
import os
import re
import subprocess
NETWORK = 'Udacity'
def call_ap(mp4path, show, season_num, episode_num, title):
''' Call AtomicParsley to tag the episode.
:param mp4path str: The mp4 file to tag
:param show str: The name of the show to use
:param season_num int: What season to tag the file as
:param episode_num int: The episode number to use
:param title str: The show title
'''
invoc = ['AtomicParsley',
mp4path,
'--TVShowName', show,
'--TVSeasonNum', str(season_num),
'--TVEpisodeNum', str(episode_num),
'--title', title]
try:
subprocess.check_output(invoc, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print("Error: {}".format(e.output))
raise
@click.command()
@click.argument('showname')
@click.argument('season_rgx')
@click.argument('path', type=click.Path(exists=True, file_okay=False, dir_okay=True))
@click.option('-e', '--episode-name-rgx', default=None,
help='The regex that will extract an episode\'s name from the file name')
def main(showname, season_rgx, path, episode_name_rgx):
rgx = re.compile(season_rgx)
episode_nums = defaultdict(lambda: 0)
for season in filter(None, map(rgx.match, os.listdir(path))):
season_no = int(season.group(1))
print('Tagging season {}...'.format(season_no))
try:
episode_pfx = season.group(2).replace('_', ' ').strip()
except IndexError:
episode_pfx = ''
for episode in os.listdir(os.path.join(path, season.string)):
episode_nums[season_no] += 1 # set to 1
ep = os.path.join(path, season.string, episode)
en = episode
if episode_name_rgx:
en = re.match(episode_name_rgx, episode).group(1)
name = '{}: {}'.format(episode_pfx, en.replace('_', ' ').strip())
call_ap(ep, showname, season_no, episode_nums[season_no], name)
#print('{} = {}'.format(episode_nums[season_no], name))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment