-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpod_dl.py
More file actions
118 lines (96 loc) · 3.29 KB
/
pod_dl.py
File metadata and controls
118 lines (96 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Podcast Downloader
Attempts to download every episode of a podcast in a given
RSS feed.
Note that this downloads the most recent episodes first, and you can
just cancel the script at any time if you don't want all the episodes.
Usage:
python pod_dl.py -u [URL]
Optionally, you can also provide the -t argument to return only the podcast
titles without actually downloading anything.
"""
import sys
import argparse
import feedparser
import requests
def dl_with_requests(link, filename):
r2 = requests.get(link)
# print(type(r2.content))
# print(sys.getsizeof(r2.content))
with open(filename, "wb") as f:
f.write(r2.content)
# print(sys.getsizeof(f))
def pod_dl(url, titles):
# Check to see if RSS URL can be accessed and, if so, get the feed
# using requests. If not, return the http error code and end the program.
try:
r = requests.get(url)
except Exception as e:
print(
f"There was an error: {e}.\n\nPlease ensure that a valid URL"
" was provided."
)
sys.exit()
status = r.status_code
if status == 200:
pass
else:
print(
"There was a problem accessing the RSS feed. HTTP Status "
f"code {status} was returned. Please ensure that the RSS URL"
" provided is accessable from a web browser."
)
sys.exit()
# Parse the feed with feedparser
d = feedparser.parse(r.text)
# check to see if titles argument is True. If so, print a list of titles
# and end the program
if titles is True:
for item in d.entries:
print(item.title)
sys.exit()
else:
pass
# Download all episodes
for item in d.entries:
for link in item.enclosures:
pod_url = link.get("href")
#Obviously should refactor below at some point
filename = item.title + ".mp3"
filename = filename.replace(":", "-")
filename = filename.replace(" ", "-")
filename = filename.replace("/", "-")
filename = filename.replace("?", "-")
filename = filename.replace("\\", "-")
filename = filename.replace("&", "and")
filename = filename.replace("?", "-")
filename = filename.replace("%", "-")
filename = filename.replace(">", "-")
filename = filename.replace("<", "-")
filename = filename.replace("*", "-")
filename = filename.replace("|", "-")
filename = filename.replace("`", "-")
filename = filename.replace("$", "-")
# print(filename)
print(f"Now downloading: {filename}")
dl_with_requests(pod_url, filename)
# argument parser
parser = argparse.ArgumentParser(
prog="podcast downloader",
description="A password generator using the Python secrets module",
epilog="",
)
parser.add_argument("-u", "--url", help="URL of the RSS feed")
parser.add_argument(
"-t",
"--titles",
action="store_true",
default=False,
help=(
"Will attempt to return the titles of all podcast episodes "
"in the feed without actually downloading the episodes"
),
)
args = parser.parse_args()
# Run pod_dl function with given command line arguments
pod_dl(**vars(args))