Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configurable filenames #115

Merged
merged 5 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions greg/aux_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def check_directory(placeholders):
elif "yes" in subdirectory:
subdnametemplate = feed.retrieve_config(
"subdirectory_name", "{podcasttitle}")
subdname = substitute_placeholders(
subdnametemplate, placeholders)
subdname = placeholders.substitute(subdnametemplate)
placeholders.directory = os.path.join(download_path, subdname)
ensure_dir(placeholders.directory)
placeholders.fullpath = os.path.join(
Expand Down Expand Up @@ -173,7 +172,7 @@ def tag(placeholders):
"""
# We first recover the name of the file to be tagged...
template = placeholders.feed.retrieve_config("file_to_tag", "{filename}")
filename = substitute_placeholders(template, placeholders)
filename = placeholders.substitute(template)
podpath = os.path.join(placeholders.directory, filename)
# ... and this is it

Expand All @@ -191,7 +190,7 @@ def tag(placeholders):
except configparser.NoSectionError:
pass
for tag in tagdict:
metadata = substitute_placeholders(tagdict[tag], placeholders)
metadata = placeholders.substitute(tagdict[tag])
tagdict[tag] = metadata
file_to_tag = eyed3.load(podpath)
if file_to_tag.tag == None:
Expand All @@ -211,7 +210,7 @@ def tag(placeholders):

def filtercond(placeholders):
template = placeholders.feed.retrieve_config("filter", "True")
condition = substitute_placeholders(template, placeholders)
condition = placeholders.substitute(template)
return eval(condition)


Expand Down Expand Up @@ -239,21 +238,25 @@ def download_handler(feed, placeholders):
"""
value = feed.retrieve_config('downloadhandler', 'greg')
if value == 'greg':
filename_placeholders = feed.retrieve_config('downloaded_filename', '{filename}')
filename = placeholders.substitute(filename_placeholders)
with urlopen(placeholders.link) as fin:
# check if request went ok
if fin.getcode() != 200:
raise URLError
# check if fullpath allready exists
# check if fullpath already exists
placeholders.fullpath = os.path.join(
placeholders.directory, filename)
while os.path.isfile(placeholders.fullpath):
placeholders.filename = placeholders.filename + '_'
filename = filename + '_'
placeholders.fullpath = os.path.join(
placeholders.directory, placeholders.filename)
placeholders.directory, filename)
# write content to file
with open(placeholders.fullpath,'wb') as fout:
fout.write(fin.read())
else:
value_list = shlex.split(value)
instruction_list = [substitute_placeholders(part, placeholders) for
instruction_list = [placeholders.substitute(part) for
part in value_list]
returncode = subprocess.call(instruction_list)
if returncode:
Expand Down Expand Up @@ -324,23 +327,3 @@ def pretty_print(session, feed):
else:
print("You don't have a feed called {}.".format(feed), file=sys.stderr,
flush=True)


def substitute_placeholders(inputstring, placeholders):
"""
Take a string with placeholders, and return the strings with substitutions.
"""
newst = inputstring.format(link=placeholders.link,
filename=placeholders.filename,
directory=placeholders.directory,
fullpath=placeholders.fullpath,
title=placeholders.title,
filename_title=placeholders.filename_title,
date=placeholders.date_string(),
podcasttitle=placeholders.podcasttitle,
filename_podcasttitle=
placeholders.filename_podcasttitle,
name=placeholders.name,
subtitle=placeholders.sanitizedsubtitle,
entrysummary=placeholders.entrysummary)
return newst
19 changes: 19 additions & 0 deletions greg/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,22 @@ def __init__(self, feed, entry, link, filename, title, summary):
def date_string(self):
date_format = self.feed.retrieve_config("date_format", "%Y-%m-%d")
return time.strftime(date_format, self.date)

def substitute(self, inputstring):
"""
Take a string with placeholders, and return the strings with substitutions.
"""
newst = inputstring.format(link=self.link,
filename=self.filename,
directory=self.directory,
fullpath=self.fullpath,
title=self.title,
filename_title=self.filename_title,
date=self.date_string(),
podcasttitle=self.podcasttitle,
filename_podcasttitle=
self.filename_podcasttitle,
name=self.name,
subtitle=self.sanitizedsubtitle,
entrysummary=self.entrysummary)
return newst
8 changes: 8 additions & 0 deletions greg/data/greg.conf
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ mime = audio
#
downloadhandler = greg
#
# Greg's own downloader is not very configurable, but you can choose the filename
# for the downloaded file, like so:
#
# downloaded_filename = {title}.mp3
#
# or whatever. The default is
download_filename = {filename}
#
###############################################################################
#
# Some feeds are abnormal in that they don't use enclosures. The following
Expand Down