Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 130 lines (106 sloc) 4 KB
#!/usr/bin/env python
import sys, os
import datetime
import shutil
import androidfiles
# Copy podcasts from podget to an mp3 device (or other directory).
def copy_pods(indir, outdir, playlist=None):
illegal_chars = '?=&;:"\''
today = datetime.datetime.now().strftime('%y-%m-%d-')
is_android = androidfiles.is_android(outdir)
# podget runs are stored in .m3u files.
# We need the one with the most recent last-modified date,
# unless we've been handed one.
if playlist:
playlist = os.path.join(indir, playlist)
else:
mtime = 0
for f in os.listdir(indir):
if not f.endswith('.m3u'):
continue
fpath = os.path.join(indir, f)
statbuf = os.stat(fpath)
if not playlist or statbuf.st_mtime > mtime:
mtime = statbuf.st_mtime
playlist = fpath
if not playlist:
print("No m3u files in %s" % indir)
return
# Get a list of the files already in the output dir.
# We'll compare names and sizes to try to avoid duplicate copies.
if is_android:
ls_out = androidfiles.listdir(outdir, sizes=True)
existing_files = []
existing_sizes = []
for e in ls_out:
existing_files.append(e[0])
existing_sizes.append(e[1])
else:
existing_files = os.listdir(outdir)
existing_sizes = []
for f in existing_files:
statbuf = os.stat(os.path.join(outdir, f))
existing_sizes.append(statbuf.st_size)
files_in_playlist = []
# Read the playlist
# Now try to copy the input files.
fp = open(playlist)
for f in fp:
files_in_playlist.append(f.strip())
# If there are no files there already, most likely we haven't
# mounted the /mp3 device yet. However, it might be handy to
# know what we *would* copy there if it was mounted ...
if len(existing_files) == 0:
print("%s is empty (probably not mounted)" % outdir)
print("\nWould copy:")
for f in files_in_playlist:
print(f)
sys.exit(1)
for f in files_in_playlist:
first_bad = -1
for c in illegal_chars:
if c in f:
i = f.find(c)
if i >= 0 and (first_bad < 0 or i < first_bad):
first_bad = i
# Save the extension:
base, ext = os.path.splitext(f)
f = os.path.join(indir, f)
if first_bad >= 0:
# Try just chopping off everything starting with the
# first bad character.
base = base[0:first_bad]
# Split off the local dir path, e.g. in Science/filename.mp3
base = os.path.split(base)[-1]
# Is the file already there?
# Some sites, NPR in particular, put the same show
# out there several times on different dates and with
# similar but different filenames.
# E.g. npr_378885682.mp3 and npr_378885682?orgId=1&amp;e=378885682&amp;d=257&amp;ft=pod&amp;f=344098539.mp3
# So look for something with the exact same byte size.
src_size = os.stat(f).st_size
try:
i = existing_sizes.index(src_size)
print("\n**** Duplicate! %s is the same size as %s"
% (existing_files[i], f))
# Skip this file. XXX Should probably check for similar filenames.
continue
except ValueError:
pass
# add today's date.
base = today + base
while base + ext in existing_files:
# Otherwise, we have a name collision. Add an X.
base += 'X'
if base:
print("cp '%s %s'" % (f, os.path.join(outdir, base + ext)))
if is_android:
androidfiles.copyto(f, outdir, base+ext)
else:
shutil.copyfile(f, os.path.join(outdir, base + ext))
if __name__ == '__main__':
if len(sys.argv) > 3:
playlist = sys.argv[3]
else:
playlist = None
copy_pods(sys.argv[1], sys.argv[2], playlist)