Skip to content

Commit

Permalink
filtering out music files
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoz committed Nov 6, 2011
1 parent 3f36d9c commit e4c3702
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions muname.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
# TODO: need to validate source and destination directories
# TODO: Define the output format string specification.

import mimetypes
from optparse import OptionParser
from os import path
import os
import sys

DEFAULT_FORMAT=''
SUPPORTED_TYPES = ['audio/mpeg', 'audio/ogg']
DEFAULT_FORMAT = ''



def IsSong(path):
return mimetypes.guess_type(path)[0] in SUPPORTED_TYPES


class Song(object):
"""Song Class."""

def __init__(self, path):
self._path = path

def GetType(self):
return mimetypes.guess_type(self._path)[0]

def __str__(self):
return self._path

def __repr__(self):
return str(self)


class MuName(object):
"""MuName Class.
Expand All @@ -16,7 +40,7 @@ class MuName(object):
depending on given output_format.
"""

def __init__(self, destination=None, source=None,
def __init__(self, destination=None, source=None, no_action=True,
output_format=DEFAULT_FORMAT):
"""MuName __init__
Expand All @@ -27,16 +51,37 @@ def __init__(self, destination=None, source=None,
souce: Source directory.
output_format: String format for output.
"""
self._destination = destination
self._destination = os.path.abspath(destination)
self._output_format = output_format

if path.isdir(source):
self._source = source
if os.path.isdir(source):
self._source = os.path.abspath(source)
else:
raise IOError('Given source directory does not exist')

def ExamineSource(self):
"""Build a representation of files in source.
Look at the files in the source directory and extract information for each
of the files.
Returns:
A representation of the music in the directory.
"""
songs = []

for root, dirs, files in os.walk(self._source, followlinks=True):
for f in files:
path = os.path.join(root, f)
if IsSong(path):
songs.append(Song(path))

print(songs)



def _GetParser():
"""Create the parser with defaults."""
parser = OptionParser()
parser.add_option('-s', '--source',
dest='source',
Expand All @@ -54,13 +99,23 @@ def _GetParser():
return parser


def main():
def _GetOptions():
"""Fetch command line args."""
parser = _GetParser()

(options, args) = parser.parse_args()
opt_dict = vars(options)

# TODO at some point, positional args for src and dest would be nice.

return opt_dict


def main():
options = _GetOptions()
muname = MuName(**options)

print(options)
print(args)
muname.ExamineSource()


if __name__ == "__main__":
Expand Down

0 comments on commit e4c3702

Please sign in to comment.