Home > utils > read_itunes_playlist.m

read_itunes_playlist

PURPOSE ^

Reads a playlist text file generated by iTunes and returns an

SYNOPSIS ^

function data_st = read_itunes_playlist(fname)

DESCRIPTION ^

 Reads a playlist text file generated by iTunes and returns an
 Ensemble-style data struct

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function data_st = read_itunes_playlist(fname)
0002 % Reads a playlist text file generated by iTunes and returns an
0003 % Ensemble-style data struct
0004 
0005 % 25Dec2013 Petr Janata
0006 logfid = 1;
0007 
0008 if nargin < 1
0009   error('Filename containing playlist not specified');
0010 elseif ~exist(fname,'file')
0011   error('File not found: %s', fname);
0012 end
0013 
0014 % Initialize the output data struct
0015 data_st = ensemble_init_data_struct;
0016 data_st.type = 'playlist';
0017 
0018 data_st.meta.filename = fname;
0019 
0020 % Open the file
0021 fid = fopen(fname,'rt');
0022 
0023 fprintf(logfid,'Reading playlist from: %s\n', fname);
0024 
0025 % Read the headerline
0026 hdrline = fgetl(fid);
0027 vars = regexp(hdrline,'\t','split'); % Parse the variable names
0028 vars = lower(regexprep(vars,'\s','_')); % Regularlize the varibale names
0029 nvars = length(vars);
0030 
0031 data_st.vars = vars; % copy variable names to output structure
0032 cols = set_var_col_const(vars);
0033 
0034 % Initialize the cell array
0035 data_st.data = cell(1,nvars);
0036 
0037 % Read in the rest of the playlist
0038 [data_st.data(:)] = textscan(fid, repmat('%s',1,nvars), 'delimiter','\t');
0039 
0040 % Convert variables to desired types. Keep them as strings by default
0041 vartypes.numeric = {'size','disc_number','disc_count','track_number','track_count', ...
0042   'bit_rate','sample_rate','plays','year','skips'};
0043 vartypes.date = {'date_modified','date_added','last_played','last_skipped'};
0044 vartypes.time = {'time'};
0045 
0046 for ivar = 1:nvars
0047   currVar = vars{ivar};
0048   
0049   % Get rows with missing values
0050   emptyMask = cellfun('isempty',data_st.data{ivar});
0051   switch currVar
0052     case vartypes.date
0053       tmpdatenum = nan(size(data_st.data{ivar}));
0054       
0055       % Replace missing values with bogus datenum
0056       [data_st.data{ivar}{emptyMask}] = deal(datestr(0));
0057       
0058       % Convert the date string to a datenum. Have to do this in two steps
0059       % because of datenum method
0060       if any(emptyMask)
0061         tmpdatenum(emptyMask) = datenum(data_st.data{ivar}(emptyMask));
0062       end
0063       if any(~emptyMask)
0064         tmpdatenum(~emptyMask) = datenum(data_st.data{ivar}(~emptyMask));
0065       end
0066       
0067       data_st.data{ivar} = tmpdatenum;
0068       
0069     case vartypes.time
0070       % Check to see if we have a number in seconds or in MM:SS format
0071       val = data_st.data{ivar}{1};
0072       
0073       % Replace empty values with NaNs
0074       [data_st.data{ivar}{emptyMask}] = deal('NaN');
0075 
0076       if ~isempty(regexp(val,':'))
0077         data_st.data{ivar} = cellfun(@convert_datetime, data_st.data{ivar});
0078       else
0079         data_st.data{ivar} = cellfun(@str2num, data_st.data{ivar});
0080       end
0081       
0082     case vartypes.numeric
0083       % Replace empty values with NaNs
0084       [data_st.data{ivar}{emptyMask}] = deal('NaN');
0085       
0086       % Convert to numeric
0087       data_st.data{ivar} = cellfun(@str2num, data_st.data{ivar});
0088       
0089       
0090   end % switch
0091 end % for ivar=
0092 
0093 % Close the file
0094 fclose(fid);
0095 
0096 return
0097 end
0098 
0099 function seconds = convert_datetime(dstr)
0100 seconds = etime(datevec(datenum(dstr,'MM:SS')),datevec(datenum('0:0','MM:SS')));
0101 end

Generated on Sat 25-May-2019 04:00:51 by m2html © 2003