From 7a3f2ea99ab896a8b282536b10d046d6c98885df Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Mon, 15 Mar 2021 19:23:51 +0100 Subject: [PATCH 1/5] Add tagging section to extras_requires for optional dependencies --- setup.py | 3 +++ 1 file changed, 3 insertions(+) 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', From 5179bdca0f142deb6d00400f85d752681a010d21 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Mon, 15 Mar 2021 21:28:17 +0100 Subject: [PATCH 2/5] Add get_fullpath method to Placeholders class and use it. --- greg/aux_functions.py | 12 +++--------- greg/classes.py | 4 +++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/greg/aux_functions.py b/greg/aux_functions.py index 026c12b..e7bb455 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 @@ -233,12 +229,10 @@ def download_handler(feed, placeholders): if fin.getcode() != 200: raise URLError # check if fullpath allready exists - while os.path.isfile(placeholders.fullpath): + while os.path.isfile(placeholders.get_fullpath()): placeholders.filename = placeholders.filename + '_' - placeholders.fullpath = os.path.join( - placeholders.directory, placeholders.filename) # 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 +316,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(), diff --git a/greg/classes.py b/greg/classes.py index 8802351..cad76bf 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,9 @@ 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): + return os.path.join(self.directory, self.filename) def date_string(self): date_format = self.feed.retrieve_config("date_format", "%Y-%m-%d") From 067cf0ca62bf9da3380b26d8948dc4db5181964a Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Tue, 16 Mar 2021 11:34:48 +0100 Subject: [PATCH 3/5] Add filename_template option --- greg/aux_functions.py | 8 ++++++-- greg/classes.py | 1 + greg/data/greg.conf | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/greg/aux_functions.py b/greg/aux_functions.py index e7bb455..5b90a96 100755 --- a/greg/aux_functions.py +++ b/greg/aux_functions.py @@ -170,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 @@ -224,6 +224,9 @@ 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: @@ -325,5 +328,6 @@ def substitute_placeholders(inputstring, placeholders): placeholders.filename_podcasttitle, name=placeholders.name, subtitle=placeholders.sanitizedsubtitle, - entrysummary=placeholders.entrysummary) + entrysummary=placeholders.entrysummary, + extension=placeholders.extension) return newst diff --git a/greg/classes.py b/greg/classes.py index cad76bf..760baf7 100644 --- a/greg/classes.py +++ b/greg/classes.py @@ -368,6 +368,7 @@ 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) + self.extension = filename.split(".")[-1] def get_fullpath(self): return os.path.join(self.directory, self.filename) 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}. From c2b279c31533502c521c94f64d66300b145771ef Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Tue, 16 Mar 2021 12:51:32 +0100 Subject: [PATCH 4/5] Improve renaming when there are duplicate filenames --- greg/aux_functions.py | 5 +++-- greg/classes.py | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/greg/aux_functions.py b/greg/aux_functions.py index 5b90a96..7ab18c9 100755 --- a/greg/aux_functions.py +++ b/greg/aux_functions.py @@ -233,7 +233,7 @@ def download_handler(feed, placeholders): raise URLError # check if fullpath allready exists while os.path.isfile(placeholders.get_fullpath()): - placeholders.filename = placeholders.filename + '_' + placeholders.filename = placeholders.get_file_basename() + '_.' + placeholders.get_extension() # write content to file with open(placeholders.get_fullpath(),'wb') as fout: fout.write(fin.read()) @@ -329,5 +329,6 @@ def substitute_placeholders(inputstring, placeholders): name=placeholders.name, subtitle=placeholders.sanitizedsubtitle, entrysummary=placeholders.entrysummary, - extension=placeholders.extension) + extension=placeholders.get_extension(), + file_basename=placeholders.get_file_basename()) return newst diff --git a/greg/classes.py b/greg/classes.py index 760baf7..6bc6e25 100644 --- a/greg/classes.py +++ b/greg/classes.py @@ -368,11 +368,16 @@ 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) - self.extension = filename.split(".")[-1] def get_fullpath(self): return os.path.join(self.directory, self.filename) + def get_extension(self): + return self.filename.split(".")[-1] + + def get_file_basename(self): + return ".".join(self.filename.split(".")[0:-1]) + def date_string(self): date_format = self.feed.retrieve_config("date_format", "%Y-%m-%d") return time.strftime(date_format, self.date) From a4ef6ecb524bb9c66b8f7e4c011758c62a775fa8 Mon Sep 17 00:00:00 2001 From: Eugene Davis Date: Tue, 16 Mar 2021 13:14:09 +0100 Subject: [PATCH 5/5] Add function comments --- greg/classes.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/greg/classes.py b/greg/classes.py index 6bc6e25..6084d09 100644 --- a/greg/classes.py +++ b/greg/classes.py @@ -370,12 +370,22 @@ def __init__(self, feed, entry, link, filename, title, summary): 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):