Source code: medi/plugin/MP3File2Db.java
1 /*
2 * MP3File2Db.java
3 *
4 * Created on 19 giugno 2002, 9.25
5 Medi - A media archiver. Archives media files for easy management.
6 Copyright (C) 2002-2003 Antonio Petrelli
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package medi.plugin;
26
27 import java.io.*;
28 import java.text.SimpleDateFormat;
29 import java.util.*;
30 import javatools.io.FileUtils;
31 import javatools.db.*;
32 import javatools.util.plugin.PluginException;
33 import javatools.util.StringUtils;
34 import medi.db.AbstractProvider;
35 import helliker.id3.*;
36
37 /**
38 * Maps an MP3 into the database, storing information about the title, sampling,
39 * author, album.
40 * Thanks go to the creator of jd3lib.
41 * @author Antonio Petrelli
42 * @version 0.3.1
43 */
44 public class MP3File2Db extends AbstractFile2Db {
45
46 /** Creates new MP3File2Db */
47 public MP3File2Db() {
48 extensions = new String[1];
49 extensions[0] = "MP3";
50 author2ID = new HashMap();
51 genre2ID = new HashMap();
52 dataSet2ID = new HashMap();
53 }
54
55 public String getPluginName() {
56 return "MP3File2Db";
57 }
58
59 public String getDisplayName() {
60 return "MP3 analyzer for Medi";
61 }
62
63 public String getLongDescription() {
64 return "Parses MP3 files to put data into the database";
65 }
66
67 public void init() throws PluginException {
68 DbIterator rowIt;
69 DbRow tempRow;
70 String completeName, tempString;
71
72 if (!inited) {
73 try {
74 rowIt = prv.getAuthors(null).iterator();
75 while (rowIt.hasNextRow()) {
76 completeName = null;
77 tempRow = rowIt.nextRow();
78 tempString = (String) tempRow.getValue(1);
79 if (tempString != null)
80 completeName = tempString;
81 tempString = (String) tempRow.getValue(2);
82 if (tempString != null)
83 completeName += (" " + tempString);
84 tempString = (String) tempRow.getValue(3);
85 if (tempString != null)
86 completeName += (" " + tempString);
87 if (completeName != null)
88 completeName = StringUtils.proper(completeName.trim());
89 author2ID.put(completeName, tempRow.getValue(0));
90 }
91 inited = true;
92 }
93 catch (DbException e) {
94 System.out.println(e.getMessage());
95 }
96 }
97 }
98
99 /** Processes a file.
100 * @return The newly created data ID.
101 * @param procFile The file to process.
102 * @param fileTypeID Its file type ID.
103 * @throws DbException If something goes wrong.
104 */
105 public Long process(File procFile, Integer fileTypeID) throws DbException {
106 return process(procFile, fileTypeID, null);
107 }
108
109 /** Processes a file.
110 * @param procFile The file to be processed.
111 * @param fileTypeID The file type ID to use.
112 * @param dataID The data ID to use. If it is null, a new row in DATA table will be created.
113 * Otherwise, an existing row will be used.
114 * @throws DbException If something goes wrong.
115 * @return The passed data ID itself or a newly created data ID.
116 */
117 public Long process(File procFile, Integer fileTypeID, Long dataID) throws DbException {
118 MP3File curFile;
119 String tempFilePath, tempName;
120 SimpleDateFormat formatter;
121 String title, author, genre, dataSet;
122 long duration;
123 Long dataSetID, tempDataID;
124 Integer authorID, genreID;
125
126 tempDataID = dataID;
127 try {
128 curFile = new MP3File(procFile);
129 tempFilePath = procFile.getPath();
130 formatter = prv.getDateFormat();
131 tempName = FileUtils.getFileNameWithNoExtension(tempFilePath);
132 title = curFile.getTitle();
133 if (title != null) {
134 title = title.trim();
135 if (title.equals(""))
136 title = null;
137 }
138 if (title == null)
139 title = guessTitle(tempName);
140 if (title != null)
141 title = StringUtils.proper(title);
142 author = curFile.getArtist();
143 if (author != null) {
144 author = author.trim();
145 if (author.trim().equals(""))
146 author = null;
147 }
148 if (author == null)
149 author = guessAuthor(tempName);
150 if (author != null)
151 author = StringUtils.proper(author);
152 genre = curFile.getGenre();
153 if (genre != null) {
154 genre = genre.trim();
155 if (genre.equals(""))
156 genre = null;
157 else
158 genre = StringUtils.proper(genre);
159 }
160 dataSet = curFile.getAlbum();
161 if (dataSet != null) {
162 dataSet = dataSet.trim();
163 if (dataSet.trim().equals(""))
164 dataSet = null;
165 else
166 dataSet = StringUtils.proper(dataSet);
167 }
168 duration = curFile.getPlayingTime();
169 if (tempDataID == null)
170 tempDataID = prv.createNewData(fileTypeID, title,
171 FileUtils.getFileName(tempFilePath), formatter.format(new Date(procFile.lastModified())),
172 new Long(procFile.length()), null, new Integer(new Long(duration).intValue()),
173 new Integer(16), new Integer(curFile.getSampleRate()), new Integer(1),
174 new Integer(curFile.getBitRate()), null, null);
175 else
176 prv.updateData(tempDataID, fileTypeID, title,
177 FileUtils.getFileName(tempFilePath), formatter.format(new Date(procFile.lastModified())),
178 new Long(procFile.length()), null, new Integer(new Long(duration).intValue()),
179 new Integer(16), new Integer(curFile.getSampleRate()), new Integer(1),
180 new Integer(curFile.getBitRate()), null, null);
181 if (author != null) {
182 authorID = (Integer) author2ID.get(author);
183 if (authorID == null)
184 authorID = prv.createNewAuthor("", null, author,
185 null, null, null, null);
186 prv.linkDataToAuthor(tempDataID, authorID);
187 author2ID.put(author, authorID);
188 }
189 if (genre != null) {
190 genreID = findGenre(genre);
191 prv.linkDataToGenre(tempDataID, genreID);
192 }
193 if (dataSet != null) {
194 dataSetID = findDataSet(dataSet);
195 prv.linkDataToDataSet(tempDataID, dataSetID);
196 }
197 }
198 catch (FileNotFoundException e) {
199 System.out.println(e.getMessage());
200 }
201 catch (IOException e) {
202 System.out.println(e.getMessage());
203 }
204 catch (ID3v2FormatException e) {
205 System.out.println(e.getMessage());
206 }
207 catch (NoMPEGFramesException e) {
208 System.out.println(e.getMessage());
209 }
210 catch (CorruptHeaderException e) {
211 System.out.println(e.getMessage());
212 }
213 return tempDataID;
214 }
215
216 private String guessTitle(String filePath) {
217 String tempString;
218 int pos;
219
220 pos = filePath.indexOf('-');
221 if (pos < 0)
222 tempString = filePath;
223 else {
224 tempString = filePath.substring(pos+1).trim();
225 if (tempString.equals(""))
226 tempString = filePath;
227 }
228 return tempString;
229 }
230
231 private String guessAuthor(String filePath) {
232 String tempString;
233 int pos;
234
235 pos = filePath.indexOf('-');
236 if (pos < 0)
237 tempString = null;
238 else {
239 tempString = filePath.substring(0, pos-1).trim();
240 if (tempString.equals(""))
241 tempString = null;
242 }
243 return tempString;
244 }
245
246 private Integer findGenre(String name) throws DbException {
247 DbIterator rowIt;
248 Integer tempID;
249
250 tempID = (Integer) genre2ID.get(name);
251 if (tempID == null) {
252 rowIt = prv.getGenresByName(name).iterator();
253 if (rowIt.hasNextRow())
254 tempID = (Integer) rowIt.nextRow().getValue(0);
255 else
256 tempID = prv.createNewGenre(null, name, null);
257 genre2ID.put(name, tempID);
258 }
259 return tempID;
260 }
261
262 private Long findDataSet(String name) throws DbException {
263 DbIterator rowIt;
264 Long tempID;
265
266 tempID = (Long) dataSet2ID.get(name);
267 if (tempID == null) {
268 rowIt = prv.getDataSetsByName(name).iterator();
269 if (rowIt.hasNextRow())
270 tempID = (Long) rowIt.nextRow().getValue(0);
271 else
272 tempID = prv.createNewDataSet(name, null);
273 dataSet2ID.put(name, tempID);
274 }
275 return tempID;
276 }
277
278 private HashMap author2ID, genre2ID, dataSet2ID;
279 }