forked from artxfm/spinget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinget.py
executable file
·330 lines (270 loc) · 9.43 KB
/
spinget.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
"""
This script captures a radio show and saves it as an mp3 file.
It fetches the stream using m3u8 and concatenates the downloaded segments.
Usage:
python3 spinget.py MM/DD/YYYY HH:MM hours
Example:
python3 spinget.py 11/20/2021 10:00 1
Capture 1 hour show starting at 10:00am on November 20, 2021.
Requires ffmpeg and appropriate Python packages (m3u8, requests, dotenv).
Version 2.1 by Mathias for WXOX
Version 2.2 by Mason Daugherty for WBOR
"""
import argparse
from datetime import datetime, timezone, timedelta
import os
import subprocess
import sys
from urllib.error import HTTPError
import concurrent.futures
import m3u8
import requests
from dotenv import load_dotenv
load_dotenv()
STATION_SHORTCODE = os.getenv("STATION_SHORTCODE")
INDEX_URL = (
"https://ark3.spinitron.com/ark2/{0}-{1}/index.m3u8"
# Pass in the station shortcode and a UTC timestamp
)
def seg_to_file(n, seguri):
"""
Given a segment URI, return a unique file name for the segment.
Parameters:
- n: The segment number.
- seguri: The segment URI.
Returns:
- A file name string.
"""
chunk_id = seguri.split("/")[-1]
return f"{STATION_SHORTCODE}_{n:05d}_{chunk_id}.tmp.mpeg"
def concat(segment_list, output, rm):
"""
Concatenate the segments in `segment_list` into a single file named `output`.
If `rm` is set True then also delete the downloaded segments if concatenation succeeds.
Parameters:
- segment_list: A list of segment URIs.
- output: The output file name.
- rm: Whether to remove the downloaded segments after concatenation.
Returns:
- True on success.
"""
print(f"Creating index file for {len(segment_list)} segments...")
index_file = f"{output}.index"
with open(index_file, "w", encoding="utf-8") as fdout:
for n, seguri in enumerate(segment_list, start=1):
file_name = seg_to_file(n, seguri)
fdout.write(f"file {file_name}\n")
print("Concatenating with ffmpeg...")
# Arguments for ffmpeg:
# -f concat: Use the concat demuxer
# -safe 0: Allow unsafe file names
# -i index_file: Read the list of files to concatenate from the index file
# -c copy: Copy the streams directly without re-encoding
# output: The output file name
ffproc = subprocess.run(
[
"ffmpeg",
"-f",
"concat",
"-safe",
"0",
"-i",
index_file,
"-c",
"copy",
output,
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
check=True,
)
if ffproc.returncode != 0:
print("ffmpeg run failed! Output:")
print(ffproc.stdout)
return False
if rm:
print("Cleaning up segment files...")
for n, seguri in enumerate(segment_list, start=1):
os.remove(seg_to_file(n, seguri))
os.remove(index_file)
return True
def download_segment(seguri, n, total_segments):
"""
Download a single segment given its URI.
Parameters:
- seguri: The segment URI.
- n: The segment number.
- total_segments: The total number of segments.
Returns:
- True on success.
"""
print(f"Fetching segment {n}/{total_segments} from {seguri}")
chunk_file = seg_to_file(n, seguri)
if os.path.exists(chunk_file):
print(f"--> used cached: {chunk_file}")
return True
try:
r = requests.get(seguri, stream=True, timeout=(5, 30))
if r.status_code != 200:
print(f" * Request failed: {r.status_code}")
return False
with open(chunk_file, "wb") as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
return True
except requests.exceptions.Timeout:
print(f" * Request timed out for segment {n}")
return False
except requests.exceptions.RequestException as e:
print(f" * Failed to download segment {n}: {e}")
return False
def download(segment_list):
"""
Download all segments in `segment_list` using parallel execution.
Parameters:
- segment_list: A list of segment URIs.
Returns:
- True if all downloads were successful.
"""
total_segments = len(segment_list)
with concurrent.futures.ThreadPoolExecutor() as executor:
# Create a list of futures for each segment download
futures = [
executor.submit(download_segment, seguri, n, total_segments)
for n, seguri in enumerate(segment_list, start=1)
]
# Wait for all downloads to complete
results = [
future.result() for future in concurrent.futures.as_completed(futures)
]
# Return True only if all downloads were successful
return all(results)
def make_ts(t):
"""
Parse the given time string and return a datetime object in UTC timezone.
UTC is necessary for retrieving from the Spinitron server.
Parameters:
- t: A time string in the format "MM/DD/YYYY HH:MM".
Returns:
- A datetime object in UTC timezone.
"""
try:
# Parse the input time string
localstamp = datetime.strptime(t, "%m/%d/%Y %H:%M")
# TODO: check if we get results from the server for the given time?
if localstamp.minute % 5 != 0:
print("ERROR: time must be a multiple of 5 minutes")
sys.exit(1)
# Check if the input date is more than two weeks ago
now = datetime.now()
two_weeks_ago = now - timedelta(weeks=2)
if localstamp < two_weeks_ago:
print("ERROR: Provided date is greater than two weeks ago.")
sys.exit(1)
return localstamp.astimezone(timezone.utc)
except ValueError as e:
print(f"ERROR: Invalid time format - {e}")
sys.exit(1)
def load_segs(stamp, duration_hours):
"""
Load the segments for the given timestamp and number of hours.
Parameters:
- stamp: A datetime object in UTC timezone.
- duration_hours: The number of hours to capture.
Returns:
- A list of segment URIs.
"""
current_ts = stamp
segs = []
accum = 0 # seconds
required = duration_hours * 60 * 60 # seconds
# Fetch segments until we have enough content
while accum < required:
showtime = current_ts.strftime("%Y%m%dT%H%M00Z")
print(f"Fetching index for {showtime}")
try:
playlist = m3u8.load(INDEX_URL.format(STATION_SHORTCODE, showtime))
if len(playlist.segments) == 0:
print("No playlist data found!")
return []
except HTTPError as e:
if e.code == 404:
print(
f"404 Error: Playlist for {showtime} not found. Try waiting an hour..."
)
return []
print(f"HTTPError occurred: {e}")
return []
total_secs = 0
for seg in playlist.segments:
if total_secs + seg.duration > 30 * 60:
break
segs.append(seg.uri)
total_secs += seg.duration
accum += seg.duration
if accum >= required:
break
if total_secs == 0:
print("Playlist has no content!")
return []
if accum >= required:
break
print(f" --> has {total_secs} seconds (need {required - accum} more)")
current_ts += timedelta(minutes=30)
return segs
def generate_new_file_name(output):
"""
Generate a new file name by appending a number if the file already exists.
Parameters:
- output: The original file name.
Returns:
- A new file name string with a number appended.
"""
base, ext = os.path.splitext(output) # Split the file name and extension
counter = 1
new_output = f"{base}_{counter}{ext}"
# Increment the counter until we find a file name that doesn't exist
while os.path.exists(new_output):
counter += 1
new_output = f"{base}_{counter}{ext}"
return new_output
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("date", metavar="MM/DD/YYYY", help="The show date")
parser.add_argument("time", metavar="HH:MM", help="Starting time")
parser.add_argument("count", type=int, metavar="N", help="hours (1 or 2)")
parser.add_argument(
"--keep",
dest="keep",
action="store_const",
const=True,
help="keep intermediate files around for debugging",
)
args = parser.parse_args()
# Validate the hours argument
# TODO: check if this is needed?
hours = args.count
if hours > 2 or hours < 1:
print("Hours must be 1 or 2")
sys.exit(1)
# Parse the date and time arguments
TIMESTAMP = f"{args.date} {args.time}"
utc_ts = make_ts(TIMESTAMP)
# Generate the show ID
show_id = utc_ts.astimezone(timezone(timedelta(hours=-5))).strftime("%Y-%m-%d-%H-%M")
print(f"Show start is {show_id}")
OUTFILE = f"{STATION_SHORTCODE}_{show_id}_{hours}h.mp4"
# Automatically generate a new file name if the output file already exists
if os.path.exists(OUTFILE):
OUTFILE = generate_new_file_name(OUTFILE)
print(f"File already exists. Using new file name: {OUTFILE}")
seglist = load_segs(utc_ts, hours)
if seglist:
print(f"Downloading {len(seglist)} segments...")
if download(seglist):
if concat(seglist, OUTFILE, not args.keep):
print(
f"Done! The file has been output as {OUTFILE} in the current working directory"
)