写一个导出酷狗音乐列表的程序

今天想从酷狗中导出播放列表的歌曲到U盘,发现酷狗只支持导出列表,无奈自己写一个程序来导出,顺便温习一下digester的使用,哈哈。
apache的digester已经升到3.2的版本,3以后的版本比较爽,可以直接使用annotation来定义rule。

1.酷狗播放列表长的什么样呢?
他导出的是以 .kgl后缀的文件,其实就是xml格式的,哈哈,如下:
<?xml version="1.0" encoding="UTF-8"?>
<List ListName="赵传">
	<File>
		<MediaFileType>0</MediaFileType>
		<FileName>赵传 - 爱要怎么说出口.mp3</FileName>
		<FilePath>H:\01-娱乐\01-音乐\KuGou\</FilePath>
		<FileSize>6748736</FileSize>
		<Duration>281208</Duration>
		<Hash>359db1787952c1d65803c3936796402c</Hash>
		<Lyric></Lyric>
		<Bitrate>191992</Bitrate>
	</File>
	<File>
		<MediaFileType>0</MediaFileType>
		<FileName>赵传 - 我终于失去了你.mp3</FileName>
		<FilePath>H:\01-娱乐\01-音乐\KuGou\</FilePath>
		<FileSize>6833544</FileSize>
		<Duration>285152</Duration>
		<Hash>cb4c43fe3d214b48be2228040babcaed</Hash>
		<Lyric></Lyric>
		<Bitrate>191716</Bitrate>
	</File>
</list>


2.怎么定义播放列表呢?
2.1 定义播放列表实体
@ObjectCreate(pattern = "List")
public class MusicPlayList implements Serializable {

	private static final long serialVersionUID = -5345631686784548242L;

	@SetProperty(pattern = "List", attributeName = "ListName")
	private String name;

	private List<MusicFile> fileList = new ArrayList<MusicFile>();

	@SetNext
	public void addFile(MusicFile file) {
		fileList.add(file);
	}

	// Getter and Setter

}

2.2 定义关联音乐文件实体
public class MusicFile implements Serializable {

	private static final long serialVersionUID = 1428265224588267115L;

	@BeanPropertySetter(pattern="List/File/MediaFileType")
	private String mediaFileType;
	@BeanPropertySetter(pattern="List/File/FileName")
	private String fileName;
	@BeanPropertySetter(pattern="List/File/FilePath")
	private String filePath;
	@BeanPropertySetter(pattern="List/File/FileSize")
	private long fileSize;
	@BeanPropertySetter(pattern="List/File/Duration")
	private String duration;
	@BeanPropertySetter(pattern="List/File/Hash")
	private String hash;
	@BeanPropertySetter(pattern="List/File/Lyric")
	private String lyric;
	@BeanPropertySetter(pattern="List/File/Bitrate")
	private long bitrate;

	public File getFile() {
		return new File(getFilePath(), getFileName());
	}

	// Getter and Setter
}

3. 然后如何让定义的annotation生效?
		DigesterLoader digesterLoader = DigesterLoader.newLoader(new FromAnnotationsRuleModule() {
			@Override
			protected void configureRules() {
				bindRulesFrom(MusicPlayList.class);
			}
		});
		Digester digester = digesterLoader.newDigester();
		URL dataUrl = Thread.currentThread().getClass().getResource(playListFile);
		try {
			return digester.parse(dataUrl);
		}
		catch (Exception e) {
			throw new RuntimeException("Play list parse failuer.", e);
		}


4. 好了,应该可以拷贝文件了吧
是的,可以看看我们的工具类了
public class ExportTool {

	public static void exportFileFromPlayList(String playListFile, String toDir) {
		MusicPlayList playList = digesterPlayList(playListFile);
		File tofolder = new File(toDir, playList.getName());
		if (!tofolder.exists()) {
			tofolder.mkdirs();
		}
		Iterator<MusicFile> musicFilesInterator = playList.list();
		while (musicFilesInterator.hasNext()) {
			MusicFile file = musicFilesInterator.next();
			if (!file.getFile().exists()) {
				continue;
			}
			copy(file.getFile(), tofolder);
		}
	}

	private static MusicPlayList digesterPlayList(String playListFile) {
		DigesterLoader digesterLoader = DigesterLoader.newLoader(new FromAnnotationsRuleModule() {
			@Override
			protected void configureRules() {
				bindRulesFrom(MusicPlayList.class);
			}
		});
		Digester digester = digesterLoader.newDigester();
		URL dataUrl = Thread.currentThread().getClass().getResource(playListFile);
		try {
			return digester.parse(dataUrl);
		}
		catch (Exception e) {
			throw new RuntimeException("Play list parse failuer.", e);
		}
	}

	private static void copy(File from, File toFolder) {
		try {

			File toFile = new File(toFolder, from.getName());
			if (toFile.exists()) {
				return;
			}
			
			InputStream input = new FileInputStream(from);
			OutputStream output = new FileOutputStream(toFile);
			IOUtils.copy(input, output);
		}
		catch (Exception e) {
			throw new RuntimeException("Copy file from [" + from.getAbsolutePath() + "] to [" + toFolder.getAbsolutePath() + "] failure.");
		}
	}
}


5. 这个功能还不错,但是我们依赖那些工具呢?
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-digester3</artifactId>
			<version>3.2</version>
		</dependency>


6. 具体的代码在附件,可以参考一下哦

你可能感兴趣的:(apache,Digester)