2009-08-05 08:59:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/11/16
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
import logging
|
|
|
|
from appscript import app, k, CommandError
|
|
|
|
import time
|
|
|
|
|
2010-07-13 06:08:18 +00:00
|
|
|
from hscommon.cocoa import as_fetch
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-12-30 10:37:57 +00:00
|
|
|
from core.app_cocoa import JOBID2TITLE, DupeGuru as DupeGuruBase
|
2009-10-23 14:35:51 +00:00
|
|
|
|
|
|
|
from . import data, scanner, fs
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
JOB_REMOVE_DEAD_TRACKS = 'jobRemoveDeadTracks'
|
|
|
|
JOB_SCAN_DEAD_TRACKS = 'jobScanDeadTracks'
|
|
|
|
|
2009-10-23 14:35:51 +00:00
|
|
|
JOBID2TITLE.update({
|
2009-06-01 09:55:11 +00:00
|
|
|
JOB_REMOVE_DEAD_TRACKS: "Removing dead tracks from your iTunes Library",
|
|
|
|
JOB_SCAN_DEAD_TRACKS: "Scanning the iTunes Library",
|
|
|
|
})
|
|
|
|
|
2009-10-23 14:35:51 +00:00
|
|
|
class DupeGuruME(DupeGuruBase):
|
2009-06-01 09:55:11 +00:00
|
|
|
def __init__(self):
|
2010-09-29 14:49:50 +00:00
|
|
|
DupeGuruBase.__init__(self, data, 'dupeGuru Music Edition')
|
2009-06-01 09:55:11 +00:00
|
|
|
self.scanner = scanner.ScannerME()
|
2009-10-23 14:35:51 +00:00
|
|
|
self.directories.fileclasses = [fs.Mp3File, fs.Mp4File, fs.WmaFile, fs.OggFile, fs.FlacFile, fs.AiffFile]
|
2009-06-01 09:55:11 +00:00
|
|
|
self.dead_tracks = []
|
|
|
|
|
|
|
|
def remove_dead_tracks(self):
|
|
|
|
def do(j):
|
|
|
|
a = app('iTunes')
|
2009-06-10 13:54:24 +00:00
|
|
|
a.activate(timeout=0)
|
2009-06-01 09:55:11 +00:00
|
|
|
for index, track in enumerate(j.iter_with_progress(self.dead_tracks)):
|
|
|
|
if index % 100 == 0:
|
|
|
|
time.sleep(.1)
|
|
|
|
try:
|
2009-06-10 13:54:24 +00:00
|
|
|
track.delete(timeout=0)
|
2009-06-01 09:55:11 +00:00
|
|
|
except CommandError as e:
|
2010-08-11 14:39:06 +00:00
|
|
|
logging.warning('Error while trying to remove a track from iTunes: %s' % str(e))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
self._start_job(JOB_REMOVE_DEAD_TRACKS, do)
|
|
|
|
|
|
|
|
def scan_dead_tracks(self):
|
|
|
|
def do(j):
|
|
|
|
a = app('iTunes')
|
2009-06-10 13:54:24 +00:00
|
|
|
a.activate(timeout=0)
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
2009-06-10 13:54:24 +00:00
|
|
|
[source] = [s for s in a.sources(timeout=0) if s.kind(timeout=0) == k.library]
|
|
|
|
[library] = source.library_playlists(timeout=0)
|
2009-06-01 09:55:11 +00:00
|
|
|
except ValueError:
|
|
|
|
logging.warning('Some unexpected iTunes configuration encountered')
|
|
|
|
return
|
|
|
|
self.dead_tracks = []
|
|
|
|
tracks = as_fetch(library.file_tracks, k.file_track)
|
|
|
|
for index, track in enumerate(j.iter_with_progress(tracks)):
|
|
|
|
if index % 100 == 0:
|
|
|
|
time.sleep(.1)
|
2009-06-10 13:54:24 +00:00
|
|
|
if track.location(timeout=0) == k.missing_value:
|
2009-06-01 09:55:11 +00:00
|
|
|
self.dead_tracks.append(track)
|
|
|
|
logging.info('Found %d dead tracks' % len(self.dead_tracks))
|
|
|
|
|
|
|
|
self._start_job(JOB_SCAN_DEAD_TRACKS, do)
|
|
|
|
|