forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcopy
executable file
·129 lines (106 loc) · 4 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
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
#!/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&e=378885682&d=257&ft=pod&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)