Skip to content

Instantly share code, notes, and snippets.

@dardevelin
Created April 9, 2014 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dardevelin/10228555 to your computer and use it in GitHub Desktop.
Save dardevelin/10228555 to your computer and use it in GitHub Desktop.
evidence of me fixing dave's code
#!/usr/bin/env python3
#############################################################################
## readcue.py
## This file is part of readcue
## Copyright (C) 2014 Dhaval Anjaria
## Copyright (C) 2014 Darcy Bras da Silva
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/
#############################################################################
## this script reads the cue file from a folder, reads the name of the
## flac file and from that separates it into it's individual songs
#############################################################################
import argparse
def ReadTrack(filepath):
"""
Generates a list of tracks from a given file
"""
## some tracks don't have 00 and 01 index.
## keep track of the previous index to use as start
previous = None
## keep track if we are in a track collecting metadata ?
inTrack = False
## which part of the collection are we ?
stage = 0
## track data
title = ""
performer = ""
s_index = 0
e_index = 0
with open(filepath, 'r') as f:
for line in f:
## check if we are currently collecting
if inTrack == False:
## we are not collecting
## see if we need to be
if 'TRACK' in line:
inTrack = True
continue
## we are currently collecting
if 'TITLE' in line:
s = line.find('"')+1
e = line.rfind('"')
title = line[s:e]
stage = stage + 1
continue
if 'PERFORMER' in line:
s = line.find('"')+1
e = line.rfind('"')
performer = line[s:e]
stage = stage + 1
continue
if 'INDEX' in line:
if '01 ' in line:
## we don't have 2 indexes
t = line.find('01 ')+3
e_index = line[t:].strip()
stage = 0
#yield data
inTrack = False
if previous == None:
s_index = '00:00:00'
previous = e_index
else:
s_index = previous
previous = e_index
yield title,performer,s_index,e_index
## we have two indexes
elif '00 ' in line and 3 == stage + 1:
## we have 2 indexes
t = line.find('00 ')+3
s_index = line[t:].strip()
stage = stage + 1
## read second index
line = f.readline()
if '01 ' in line and 4 == stage + 1:
t = line.find('01 ')+3
e_index = line[t:].strip()
stage = 0
inTrack = False
yield title,performer,s_index,e_index
def create_argparser():
"""
creates and configures the argparser
for use
"""
# configure the argument parser
parser = argparse.ArgumentParser(
description=
"""
Converts the tracks listed in a cue file
to individual ['alac','flac'] files
if no arguments are provided it will attempt to
use 'readcue.txt' file with codec configured to alac
"""
)
parser.add_argument('-f', '--file',
help="takes a cue file as extra argument")
# don't allow verbose with quiet at the same time
group_vq = parser.add_mutually_exclusive_group()
group_vq.add_argument('-v','--verbose', action='store_true',
help='increase output verbosity')
group_vq.add_argument('-q', '--quiet', action='store_true',
help='no output messages will be displayed')
parser.add_argument('-c', '--codec',
choices=['alac','flac'],
help='enables conversion to alac or flac file format')
return parser
if __name__ == '__main__':
parser = create_argparser()
# parse the arguments
args = parser.parse_args()
cue_file = 'readcue.txt'
if args.file != None:
cue_file = args.file
for _title,_performer,_idx,__idx in ReadTrack(cue_file):
print("we got\n\t{}\n\t{}\n\t{}\n\t{}\n".format(_title,_performer,_idx,__idx))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment