forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcopy
executable file
·97 lines (79 loc) · 2.98 KB
/
podcopy
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
#!/usr/bin/env python
import sys, os
import datetime
import shutil
# 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-')
# 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", 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.
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)
# Now try to copy the input files.
fp = open(playlist)
for f in fp:
f = f.strip()
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. 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&e=378885682&d=257&ft=pod&f=344098539.mp3
src_size = os.stat(f).st_size
try:
i = existing_sizes.index(src_size)
print "\n**** Duplicate!", existing_files[i], \
"is the same size as", f
# Skip this file. XXX Should probably check for similar filenames.
continue
except ValueError:
pass
# add today's date.
base = today + base
while os.path.exists(os.path.join(outdir, base + ext)):
# Otherwise, we have a name collision. Add an X.
base += 'X'
if base:
print "cp '" + f + "'", os.path.join(outdir, base + ext)
shutil.copyfile(f, os.path.join(outdir, base + ext))
if len(sys.argv) > 3:
playlist = sys.argv[3]
else:
playlist = None
copy_pods(sys.argv[1], sys.argv[2], playlist)