Skip to content

Commit

Permalink
Add support for saving files using published date and episode name
Browse files Browse the repository at this point in the history
Links to issue brtmr#9
  • Loading branch information
dalepotter committed Oct 2, 2022
1 parent 0ec75cb commit c5ded17
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ In podfox, every podcast is identified with its own `shortname`, which is restri
podfox.py update [<shortname>]
podfox.py feeds
podfox.py episodes <shortname>
podfox.py download [<shortname> --how-many=<n>]
podfox.py download [<shortname> --how-many=<n> --save-file-with-date-and-episode-title]
podfox.py prune [<shortname> --max-age-days=<n>]
```
### Import
Expand Down Expand Up @@ -108,8 +108,8 @@ Extortion Startups | TechSNAP 229 | Not Downloaded

`podfox download ts --how-many=3` will download the 3 newest techsnap podcasts that have not yet been downloaded. (Skipping newer, but already downloaded ones). If the `--how-many` parameter is omitted, the `maxnum` parameter from the configuration file is used instead.

`podfox download --save-file-with-date-and-episode-title` will save download episodes using their published date (in the format YYYY-MM-DD) and the episode title - for example `2020-05-29 430 All Good Things.mp3`.

### Pruning

`podfox prune` will clean up episodes that are too old. If a max age is not set via parameter to command or in the configuration, then no pruning is done.

44 changes: 34 additions & 10 deletions podfox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
podfox.py update [<shortname>] [-c=<path>]
podfox.py feeds [-c=<path>]
podfox.py episodes <shortname> [-c=<path>]
podfox.py download [<shortname> --how-many=<n>] [-c=<path>]
podfox.py download [<shortname> --how-many=<n> --save-file-with-date-and-episode-title] [-c=<path>]
podfox.py rename <shortname> <newname> [-c=<path>]
podfox.py prune [<shortname> --maxage-days=<n>]
Expand All @@ -22,6 +22,8 @@
from docopt import docopt
from os.path import expanduser
from sys import exit
from time import mktime, localtime, strftime
from urllib.parse import urlparse
import colorama
import datetime
import feedparser
Expand Down Expand Up @@ -219,24 +221,44 @@ def episodes_from_feed(d):
return episodes


def download_multiple(feed, maxnum):
def download_multiple(feed, maxnum, save_file_with_date_and_episode_title):
for episode in feed['episodes']:
if maxnum == 0:
break

custom_output_filename = None
if save_file_with_date_and_episode_title:
# Prepare the output filename
title = episode['title']
for char in '<>\"|*%?\\/:;':
title = title.replace(char, "")
title = title.replace("’", "'").replace("—", "-").replace(".", "")
# Shorten the title to max 120 characters
title = title[:120]
extension = os.path.splitext(urlparse(episode['url'])[2])[1]
custom_output_filename = "{} {}{}".format(
strftime('%Y-%m-%d', localtime(episode['published'])),
title,
extension
)

if not episode['downloaded'] and not episode_too_old(episode, CONFIGURATION['maxage-days']):
episode['filename'] = download_single(feed['shortname'], episode['url'])
episode['filename'] = download_single(feed['shortname'], episode['url'], custom_output_filename)
episode['downloaded'] = True
maxnum -= 1
overwrite_config(feed)

def download_single(folder, url):
def download_single(folder, url, custom_output_filename=None):
print(url)
base = CONFIGURATION['podcast-directory']
r = requests.get(url.strip(), stream=True)
try:
filename = re.findall('filename="([^"]+)', r.headers['content-disposition'])[0]
except:
filename = get_filename_from_url(url)
if custom_output_filename:
filename = custom_output_filename
else:
try:
filename = re.findall('filename="([^"]+)', r.headers['content-disposition'])[0]
except:
filename = get_filename_from_url(url)
print_green("{:s} downloading".format(filename))
with open(os.path.join(base, folder, filename), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024**2):
Expand Down Expand Up @@ -393,19 +415,21 @@ def main():
maxnum = int(arguments['--how-many'])
else:
maxnum = CONFIGURATION['maxnum']
save_file_with_date_and_episode_title = bool(arguments['--save-file-with-date-and-episode-title'])

#download episodes for a specific feed
if arguments['<shortname>']:
feed = find_feed(arguments['<shortname>'])
if feed:
download_multiple(feed, maxnum)
download_multiple(feed, maxnum, save_file_with_date_and_episode_title)
exit(0)
else:
print_err("feed {} not found".format(arguments['<shortname>']))
exit(-1)
#download episodes for all feeds.
else:
for feed in available_feeds():
download_multiple(feed, maxnum)
download_multiple(feed, maxnum, save_file_with_date_and_episode_title)
exit(0)
if arguments['rename']:
rename(arguments['<shortname>'], arguments['<newname>'])
Expand Down

0 comments on commit c5ded17

Please sign in to comment.