-
Notifications
You must be signed in to change notification settings - Fork 0
/
WMP_Read_PL.py
281 lines (248 loc) · 9.77 KB
/
WMP_Read_PL.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import win32com.client
import pandas as pd
# ORDER OF THE COLS. IN THE DF (BUT THEY CAN BE SPECIFIED ANY WAY)
# THE BELOW IS JUST SO THE RIGHT HEADERS GO WITH THE RIGHT COLS.
order_list_wmp = ["PL_nbr","PL_name","Pos","ID","Arq","Art","Title","AA","Album","Genre","Year","Group","Bitrate","Len","Plays","Skips","Added"]
tag_dict = {
"Art" : "Artist",
"AA" : "WM/AlbumArtist",
"Album" : "WM/AlbumTitle", # OR Album
"Genre" : "WM/Genre", # OR Genre
"Year" : "ReleaseDateYear", #WM/Year
"Group" : "WM/ContentGroupDescription",
"Bitrate" : "Bitrate",
"Plays": "UserPlayCount",
"Added": "AcquisitionTime" #AcquisitionTimeYearMonthDay
}
# ORDERS A SUBLIST BASED ON THE ORDER OF THE SUPERLIST order_list=order_list_itunes
def order_list(smaller_list,order_list=None):
# Create a dictionary to store the index of each element in the larger list
index_dict = {element: index for index, element in enumerate(order_list)}
# Define a custom key function that returns the index of each element in the larger list
key_func = lambda element: index_dict[element]
# Sort the smaller list based on the custom key function
smaller_list.sort(key=key_func)
return smaller_list
# OS OBJETOS ABAIXO SAO RECONHECIDOS POR QQ FUNCAO DESSE MODULO
# OBJECT wmp IS THE Player
def Init_wmp():
global wmp
global library
global playlists
wmp = win32com.client.Dispatch('WMPlayer.OCX')
# library = wmp.mediaCollection.getAll()
library = wmp.mediaCollection.getByAttribute("MediaType", "audio")
# get the playlist collection
playlists = wmp.playlistCollection.getAll()
# create a dictionary to store attribute names
# THERE'S A CODE TO OBTAIN ALL PROPERTIES OF TRACK (COMMENTED OUT)
def WMP_tag_dict(item,cols):
dict = {}
# for i in range(item.attributeCount):
# k = item.getAttributeName(i)
# print("Attrib:",k,"Value:",item.getItemInfo(k))
# PROPERTIES
for key in cols:
if key in tag_dict.keys():
dict[key] = item.getItemInfo(tag_dict[key])
if "Plays" in cols:
dict["Plays"] = int(dict["Plays"])
if "Skips" in cols:
dict["Skips"] = 0
if "Bitrate" in cols:
dict["Bitrate"] = int(dict["Bitrate"])/1000
if "Added" in cols:
dict["Added"] = pd.to_datetime(dict["Added"], format="%d/%b/%Y %I:%M:%S %p")
if "Arq" in cols:
dict["Arq"] = item.sourceURL
if "Title" in cols:
dict["Title"] = item.name
if "Len" in cols:
dict["Len"] = item.durationString
if "ID" in cols:
dict["ID"] = 0
return dict
# FINDS A PLAYLIST
def Get_WMP_PL_by_nbr(srch_PL):
dict = {}
Achou = False
PL_nbr = 0
for PL in playlists:
PL_name = PL.name
if PL_name == srch_PL:
Achou = True
break
PL_nbr = PL_nbr+1
dict["res"] = Achou
dict["PL_nbr"] = PL_nbr
return dict
# PLAYLISTS
def Read_WMP_PL(col_names,PL_name=None,PL_nbr=None,Do_lib=False,rows=None,Modify_cols=True):
# CREATES A COPY OF THE COL. LIST SO IT'S NOT MODIFIED OUTSIDE OF THIS FUNCTION
if not Modify_cols:
col_names = col_names[:]
Init_wmp()
# LISTA A SER PROCESSADA
By_name = False
if not Do_lib and PL_name is not None:
#print("\nReading WMP playlists...this may take a while")
read_PL = wmp.playlistCollection.getByName(PL_name).Item(0)
PL_nbr = 0
user_inp = "0"
By_name = True
elif not Do_lib and PL_nbr == None:
nbr_PLs = playlists.Count
# READS A PL
print("\nReading WMP playlists...this may take a while")
print("Select from the following WMP playlists")
print("Number of playlists:",nbr_PLs)
for j in range(nbr_PLs):
playlist = playlists[j]
# PL_name = playlist.Name
print(j, ":", PL_name)
user_inp = input("\nEnter comma-separated lists to process: ")
elif not Do_lib and PL_nbr != None:
user_inp = str(PL_nbr)
elif Do_lib:
# THIS IS THE LIBRARY
user_inp = "0"
read_PL = library
PL_name = "library"
PL_nbr = 0
# data IS A LIST OF LISTS
data = []
# A PL SELECIONADA
res_list = user_inp.split(",")
nbr_PLs = len(res_list)
for k in range(nbr_PLs):
if not Do_lib and By_name:
print("\nProcessing playlist",k+1,"of",nbr_PLs,":",PL_name)
PL_nbr = 0
elif not Do_lib:
# PL_name = playlists[k].name
PL_nbr = int(res_list[k])
read_PL = playlists[PL_nbr]
PL_name = read_PL.Name
print("\nProcessing WMP playlist",k+1,"of",nbr_PLs,":",PL_name)
elif Do_lib:
print("\nProcessing WMP music library")
# PROCESS SPECIFIED NUMBER OF ROWS
if rows is None:
if not Do_lib:
numtracks = read_PL.count
else:
numtracks = len(library)
else:
numtracks = min(rows, read_PL.count if not Do_lib else len(library))
# DISPLAY MESSAGE
print("\ntracks: ",read_PL.Count,"(processing",numtracks,")\n")
# LOGIC TO DISPLAY IN THE LOG
tam = max(numtracks // 20, 1)
# ORDER LIST SO COLUMN HEADERS ALWAYS MATCH THEIR VALUES
col_names = order_list(col_names,order_list=order_list_wmp)
# THE RANGE FOR ITEMS IN A WMP PL IS 0 TO (N-1)
for m in range(numtracks):
if not Do_lib:
track = read_PL.Item(m)
else:
# IT SEEMS THAT library.Item(m) ALSO WORKS
track = library[m]
# ONLY DOES AUDIO
if track.getiteminfo("MediaType")=="audio":
# THE SOURCE (PLAYLIST/LIBRARY)
tag_list = [PL_nbr,PL_name]
# THE TRACK POSITION
tag_list.append(m)
dict = WMP_tag_dict(track,col_names)
for key in col_names:
value = dict[key]
tag_list.append(value)
#ADD ROW TO LIST, BEFORE CREATING DF
data.append(tag_list)
if (m+1) % tam==0:
print("Row. no: ",m+1)
#print("")
# DATAFRAME
# ADDS COL. PL IF IT WASN'T INCLUDED
if "PL_nbr" not in col_names:
col_names.append("PL_nbr")
if "PL_name" not in col_names:
col_names.append("PL_name")
if "Pos" not in col_names:
col_names.append("Pos")
# ORDER THE LIST SO COLUMN HEADERS MATCH THEIR VALUES
col_names = order_list(col_names,order_list=order_list_wmp)
df = pd.DataFrame(data, columns=col_names)
# SETS YEAR TYPE TO INTEGER
if "Year" in col_names:
df['Year'] = pd.to_numeric(df['Year'], errors="coerce")
df['Year'] = df['Year'].fillna(0)
# ORDERS DF BY ART/TITLE (CONVERTED TO UNICODE)
# df = Order(df, col_names)
# VALUE RETURNED IS A DICT
dict = {"WMP": wmp, "Lib": library, "PLs": playlists, "PL": read_PL, "PL_nbr": PL_nbr, \
"PL_Name": PL_name, "tracks": 1, "DF": df}
return dict
# READS ONLY WMP FILES FROM PROVIDED FILES LIST
# SO IT MATCHES THE FILES FROM THE ITUNES PLAYLIST
def Read_WMP_MC(col_names,files,Modify_cols=True):
# CREATES A COPY OF THE COL. LIST SO IT'S NOT MODIFIED OUTSIDE OF THIS FUNCTION
if not Modify_cols:
col_names = col_names[:]
Init_wmp()
# data IS A LIST OF LISTS
data = []
# PROCESS SPECIFIED NUMBER OF ROWS
numtracks = len(files)
PL_nbr = 0
PL_name = ""
# DISPLAY MESSAGE
print("\nReading",numtracks,"WMP tracks from media collection\n")
# LOGIC TO DISPLAY IN THE LOG
tam = max(numtracks // 20, 1)
# ORDER LIST SO COLUMN HEADERS ALWAYS MATCH THEIR VALUES
col_names = order_list(col_names,order_list=order_list_wmp)
# SEARCH AND ASSIGN TRACK
miss_files = []
for m in range(numtracks):
PL = wmp.mediaCollection.getByAttribute("SourceURL", files[m])
try:
track = PL.Item(0)
except:
print("File",m+1,"of",numtracks,"not found:",files[m],"\n")
miss_files.append(files[m])
track = None
# ONLY DOES AUDIO
if track is not None and track.getiteminfo("MediaType")=="audio":
# THE SOURCE (PLAYLIST/LIBRARY)
tag_list = [PL_nbr,PL_name]
# THE TRACK POSITION
tag_list.append(m)
dict = WMP_tag_dict(track,col_names)
for key in col_names:
value = dict[key]
tag_list.append(value)
#ADD ROW TO LIST, BEFORE CREATING DF
data.append(tag_list)
if (m+1) % tam==0:
print("Row. no: ",m+1)
# DATAFRAME
# ADDS COL. PL IF IT WASN'T INCLUDED
if "PL_nbr" not in col_names:
col_names.append("PL_nbr")
if "PL_name" not in col_names:
col_names.append("PL_name")
if "Pos" not in col_names:
col_names.append("Pos")
# ORDER THE LIST SO COLUMN HEADERS MATCH THEIR VALUES
col_names = order_list(col_names,order_list=order_list_wmp)
df = pd.DataFrame(data, columns=col_names)
# SETS YEAR TYPE TO INTEGER
if "Year" in col_names:
df['Year'] = pd.to_numeric(df['Year'], errors="coerce")
df['Year'] = df['Year'].fillna(0)
# ORDERS DF BY ART/TITLE (CONVERTED TO UNICODE)
# df = Order(df, col_names)
# VALUE RETURNED IS A DICT
dict = {"WMP": wmp, "Lib": library, "DF": df, "PL": None, "Missing": miss_files}
return dict