-
Notifications
You must be signed in to change notification settings - Fork 5
/
dumpituneswin.py
executable file
·88 lines (73 loc) · 2.58 KB
/
dumpituneswin.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#Copyright © 2018 David Perry
#iTunesToRhythm is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3 of the License, or
#(at your option) any later version.
#
#iTunesToRhythm is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with iTunesToRhythm; if not, write to the Free Software Foundation, Inc.,
#51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import win32com.client
from songparser import BaseSong, BaseLibraryParser
# Constants
ITTrackKindFile = 1
class iTunesWinSong(BaseSong):
def __init__(self, track):
self.track = track
self.artist = track.Artist
self.album = track.Album
self.title = track.Name
self.size = track.Size
self.rating = track.Rating
self.playcount = track.PlayedCount
self.filePath = track.Location
def setRating(self, rating):
self.track.Rating = rating
def setPlaycount(self, playcount):
self.track.PlayedCount = playcount
class iTunesWinParser(BaseLibraryParser):
def __init__(self):
self.comInstance = win32com.client.Dispatch("iTunes.Application")
self.library = self.comInstance.LibrarySource.Playlists.ItemByName("Library")
def getSongs(self):
return self.getPlaylistFiles('Library')
def getPlaylistFiles(self, playlistName):
playlist = self.comInstance.LibrarySource.Playlists.ItemByName(playlistName)
if playlist:
return [iTunesWinSong(s) for s in playlist.Tracks if s.Kind == ITTrackKindFile]
return []
def findSongBySize(self, size):
return [iTunesWinSong(s) for s in self.library.Tracks if s.Kind == ITTrackKindFile and s.Size == size]
#@abstractmethod
def findSongByTitle(self, title):
track = self.library.Tracks.ItemByName(title)
if track:
return [iTunesWinSong(track)]
return []
def save(self):
pass
def main(argv):
print("Reading from iTunes running on Windows (win32com)")
parser = iTunesWinParser()
if len(argv) == 2:
print("Using playlist " + argv[1])
allSongs = parser.getPlaylistFiles(argv[1])
else:
allSongs = parser.getSongs()
for song in allSongs:
print("{0} - {1} - {2} - {3}".format(
song.artist.encode("ascii", "replace"),
song.album.encode("ascii", "replace"),
song.title.encode("ascii", "replace"),
song.size))
if __name__ == "__main__":
main(sys.argv)