1   /**
2    *    Copyright 2009 Webster Smalley
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package com.webstersmalley.iplayer.organiser;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import java.io.FileFilter;
25  
26  import org.apache.log4j.Logger;
27  
28  import com.webstersmalley.iplayer.informer.ProgramInformation;
29  import com.webstersmalley.iplayer.informer.ProgramInformer;
30  import com.webstersmalley.iplayer.mp3.FileSystemUtils;
31  import com.webstersmalley.iplayer.mp3.MP3Utils;
32  
33  /**
34   * @author Matthew Smalley
35   * 
36   */
37  public class PostDownloadProcessor {
38  	private Logger logger = Logger.getLogger(getClass());
39  	private String inputFolder;
40  	private String outputFolder;
41  	private MP3Utils mp3Utils;
42  	private String preset;
43  	private RadioProgramBeanFactory radioProgramBeanFactory;
44  
45  	public void setPreset(String preset) {
46  		this.preset = preset;
47  	}
48  	
49  	public void setMp3Utils(MP3Utils mp3Utils) {
50  		this.mp3Utils = mp3Utils;
51  	}
52  
53  	public void setInputFolder(String inputFolder) {
54  		this.inputFolder = inputFolder;
55  	}
56  
57  	public void setOutputFolder(String outputFolder) {
58  		if (!outputFolder.endsWith(File.separator)) {
59  			outputFolder += File.separator;
60  		}
61  		this.outputFolder = outputFolder;
62  	}
63  	
64  	public void setRadioProgramBeanFactory(RadioProgramBeanFactory radioProgramBeanFactory) {
65  		this.radioProgramBeanFactory = radioProgramBeanFactory;
66  	}
67  
68  	/**
69  	 * For each mp3 in input folder, performs the following process:
70  	 * <ul>
71  	 * <li>Use filename to get program name and program set name</li>
72  	 * <li>Re-encode</li>
73  	 * <li>Retag / move into destination folder</li>
74  	 * </ul>
75  	 */
76  	public void go() {
77  		File inputFolderFile = new File(inputFolder);
78  		File[] children = inputFolderFile.listFiles(new MP3FileFilter());
79  		for (File mp3file : children) {
80  			try {
81  				RadioProgramBean program = radioProgramBeanFactory.getBeanFromFile(mp3file.getName());
82  
83  				String newFilename = outputFolder + program.getFolder() + File.separator + program.getFilename();
84  				
85  				// Re-encode & re-tag
86  				logger.info("Re-encoding: " + mp3file.getName() + " as " + newFilename);
87  				mp3Utils.reencodeMP3(mp3file.getAbsolutePath(), newFilename, preset, false);
88  				
89  				logger.info("Writing tags for: " + newFilename);
90  				mp3Utils.retagMP3(newFilename, program);
91  			} catch (Exception e) {
92  				logger.error("Error processing file: " + mp3file.getName(), e);
93  				throw new RuntimeException ("Error processing file: " + mp3file.getName(), e);
94  			}
95  		}
96  	}
97  
98  	private final class MP3FileFilter implements FileFilter {
99  
100 		@Override
101 		public boolean accept(File arg0) {
102 			return (arg0.getName().toLowerCase().endsWith(".mp3"));
103 		}
104 
105 	}
106 }