0001 function data_st = read_itunes_playlist(fname)
0002
0003
0004
0005
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
0015 data_st = ensemble_init_data_struct;
0016 data_st.type = 'playlist';
0017
0018 data_st.meta.filename = fname;
0019
0020
0021 fid = fopen(fname,'rt');
0022
0023 fprintf(logfid,'Reading playlist from: %s\n', fname);
0024
0025
0026 hdrline = fgetl(fid);
0027 vars = regexp(hdrline,'\t','split');
0028 vars = lower(regexprep(vars,'\s','_'));
0029 nvars = length(vars);
0030
0031 data_st.vars = vars;
0032 cols = set_var_col_const(vars);
0033
0034
0035 data_st.data = cell(1,nvars);
0036
0037
0038 [data_st.data(:)] = textscan(fid, repmat('%s',1,nvars), 'delimiter','\t');
0039
0040
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
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
0056 [data_st.data{ivar}{emptyMask}] = deal(datestr(0));
0057
0058
0059
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
0071 val = data_st.data{ivar}{1};
0072
0073
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
0084 [data_st.data{ivar}{emptyMask}] = deal('NaN');
0085
0086
0087 data_st.data{ivar} = cellfun(@str2num, data_st.data{ivar});
0088
0089
0090 end
0091 end
0092
0093
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