#! /usr/bin/env python # Find podcasts recently downloaded by podget -- # everything up to the second-to-the-latest .m3u file. # Copy the new files to an mp3 player. # Copyright 2011 by Akkana, feel free to use or adapt under the GPLv2. import os, time, shutil new_podcast_dir = os.path.expanduser("~/POD") player_dir = "/mp3" class PodcastFile : """A podcast .mp3 file with its last-modified date""" def __init__(self, path) : self.pathname = path self.mtime = os.path.getmtime(path) def __str__(self) : return os.path.basename(self.pathname) + "\t [" + \ time.strftime("%b %d %H:%M:%S %Y", time.localtime(self.mtime)) \ + "]" def __lt__(self, other) : return self.mtime < other.mtime def __gt__(self, other) : return self.mtime > other.mtime def filename(self) : return os.path.basename(self.pathname) def find_podcasts(dir) : """Return a list of PodcastFiles in dir -- but only up to the second file that starts with "New". """ podcasts = [] cmd = "find %s -type f" % dir fp = os.popen(cmd) for file in fp : file = file.strip() if file.endswith(".mp3") or file.endswith(".m3u") : podcasts.append(PodcastFile(file)) fp.close() # Now podcasts is a list of all PodcastFiles. Sort by date: podcasts.sort() # If the last entry is an m3u, delete it: # podget creates a .m3u AFTER each set, so it's newer than # any of the downloaded files. if podcasts[-1].pathname.endswith(".m3u") : podcasts.pop(-1) #podcasts = podcasts[:-1] # Now find the previous most recent (latest in the list) .m3u file: # loop backward from the end of the list. for i in range(len(podcasts)-1, -1, -1) : if podcasts[i].pathname.endswith(".m3u") : print "Stopping at", podcasts[i] return podcasts[i+1:] return podcasts if __name__ == '__main__' : podcasts = find_podcasts(new_podcast_dir) for podcast in podcasts : new_pathname = os.path.join(player_dir, podcast.filename()) if os.path.exists(new_pathname) : print "Oops!", new_pathname, "already exists!" else : #print "cp %s %s" % (podcast.pathname, new_pathname) shutil.copy2(podcast.pathname, new_pathname) print "Copied", podcast