diff --git a/greg/aux_functions.py b/greg/aux_functions.py index 026c12b..7ab18c9 100755 --- a/greg/aux_functions.py +++ b/greg/aux_functions.py @@ -122,8 +122,6 @@ def check_directory(placeholders): feed = placeholders.feed args = feed.args placeholders.directory = "This very directory" # wink, wink - placeholders.fullpath = os.path.join( - placeholders.directory, placeholders.filename) try: if args["downloaddirectory"]: ensure_dir(args["downloaddirectory"]) @@ -143,8 +141,6 @@ def check_directory(placeholders): subdnametemplate, placeholders) placeholders.directory = os.path.join(download_path, subdname) ensure_dir(placeholders.directory) - placeholders.fullpath = os.path.join( - placeholders.directory, placeholders.filename) return placeholders @@ -174,7 +170,7 @@ def tag(placeholders): Tag the file at podpath with the information in podcast and entry """ # We first recover the name of the file to be tagged... - template = placeholders.feed.retrieve_config("file_to_tag", "{filename}") + template = placeholders.feed.retrieve_config("filename_template", "{filename}") filename = substitute_placeholders(template, placeholders) podpath = os.path.join(placeholders.directory, filename) # ... and this is it @@ -228,17 +224,18 @@ def download_handler(feed, placeholders): """ value = feed.retrieve_config('downloadhandler', 'greg') if value == 'greg': + # Get the name of the output file and set in placeholders + template = placeholders.feed.retrieve_config("filename_template", "{filename}") + placeholders.filename = substitute_placeholders(template, placeholders) with urlopen(placeholders.link) as fin: # check if request went ok if fin.getcode() != 200: raise URLError # check if fullpath allready exists - while os.path.isfile(placeholders.fullpath): - placeholders.filename = placeholders.filename + '_' - placeholders.fullpath = os.path.join( - placeholders.directory, placeholders.filename) + while os.path.isfile(placeholders.get_fullpath()): + placeholders.filename = placeholders.get_file_basename() + '_.' + placeholders.get_extension() # write content to file - with open(placeholders.fullpath,'wb') as fout: + with open(placeholders.get_fullpath(),'wb') as fout: fout.write(fin.read()) else: value_list = shlex.split(value) @@ -322,7 +319,7 @@ def substitute_placeholders(inputstring, placeholders): newst = inputstring.format(link=placeholders.link, filename=placeholders.filename, directory=placeholders.directory, - fullpath=placeholders.fullpath, + fullpath=placeholders.get_fullpath(), title=placeholders.title, filename_title=placeholders.filename_title, date=placeholders.date_string(), @@ -331,5 +328,7 @@ def substitute_placeholders(inputstring, placeholders): placeholders.filename_podcasttitle, name=placeholders.name, subtitle=placeholders.sanitizedsubtitle, - entrysummary=placeholders.entrysummary) + entrysummary=placeholders.entrysummary, + extension=placeholders.get_extension(), + file_basename=placeholders.get_file_basename()) return newst diff --git a/greg/classes.py b/greg/classes.py index 8802351..6084d09 100644 --- a/greg/classes.py +++ b/greg/classes.py @@ -351,7 +351,6 @@ def __init__(self, feed, entry, link, filename, title, summary): self.feed = feed self.link = link self.filename = filename - # self.fullpath = os.path.join(self.directory, self.filename) self.title = title.replace("\"", "'") self.filename_title = aux.sanitize(title) try: @@ -369,6 +368,25 @@ def __init__(self, feed, entry, link, filename, title, summary): self.filename_podcasttitle = aux.sanitize(self.podcasttitle) self.name = feed.name self.date = tuple(entry.linkdate) + + def get_fullpath(self): + """ + Returns the current directory path + filename using `os.path.join` + """ + return os.path.join(self.directory, self.filename) + + def get_extension(self): + """ + Returns the current extension using a best-guess method (taking everything after the last dot). + """ + return self.filename.split(".")[-1] + + def get_file_basename(self): + """ + Returns the current basename (minus extension) of the current filename using a best-guess method + (stripping everything after the last dot) + """ + return ".".join(self.filename.split(".")[0:-1]) def date_string(self): date_format = self.feed.retrieve_config("date_format", "%Y-%m-%d") diff --git a/greg/data/greg.conf b/greg/data/greg.conf index b00f7e7..a08d920 100644 --- a/greg/data/greg.conf +++ b/greg/data/greg.conf @@ -36,6 +36,7 @@ # {name} the name you use to refer to the podcast in greg # {subtitle} a description of the feed # {entrysummary} a summary of the podcast entry +# {extension} best effort guess of the extension by taking the suffix (after the dot) # # If you have chosen to install BeautifulSoup, which is an optional dependency, # {subtitle} and {summary} will be converted from html to text. Otherwise, they @@ -142,14 +143,6 @@ tag_genre = Podcast # leave it blank, like so: # # tag_comment = - -# Finally, if you are using a custom download handler (see below), you need to tell -# greg how to figure out the name of the podcast files, using the file_to_tag -# option. For example if your -# download handler saves podcasts under a name such as "entry title.ogg", you -# will need to add the following to the podcast's config section: -# -# file_to_tag = {entry}.ogg # ############################################################################### # @@ -196,10 +189,21 @@ mime = audio # The following special case simply instructs greg to download the podcast # itself. # +# If you are using an external hander and wish to use tagging you must set +# the filename_template option (see below). +# downloadhandler = greg # ############################################################################### # +# The filename_template option pairs with the downloadhandler in two ways: +# 1. If using greg to download, use this option to set custom names. +# 2. If using an external handler to download, set this to match the output +# filenames if using tagging. +# +# filename_template = {entry}.{extension} +############################################################################### +# # Some feeds are abnormal in that they don't use enclosures. The following # option, when set to "yes", instructs greg to ignore enclosures and simply # return the entry link as {link}. diff --git a/setup.py b/setup.py index ab30549..4fe0bfe 100755 --- a/setup.py +++ b/setup.py @@ -5,6 +5,9 @@ name='Greg', version='0.4.7', install_requires=['feedparser'], + extras_require={ + 'tagging': ['beautifulsoup4', 'stagger', 'lxml'], + }, description='A command-line podcast aggregator', author='Manolo Martínez', author_email='manolo@austrohungaro.com',