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

Allow Greg Built-In Handler to Use a Filename Template #102

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 11 additions & 12 deletions greg/aux_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand All @@ -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
20 changes: 19 additions & 1 deletion greg/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down
20 changes: 12 additions & 8 deletions greg/data/greg.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
#
###############################################################################
#
Expand Down Expand Up @@ -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}.
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down