-
Notifications
You must be signed in to change notification settings - Fork 4
/
Data.java
126 lines (106 loc) · 3.27 KB
/
Data.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public abstract class Data {
/**
* 存储播放列表的数据结构
*
* @author jzj
*/
private static class PlayList {
int id;
String name;
public PlayList(String name, int id) {
this.name = name;
this.id = id;
}
}
public void run(String db_path, String src_dir, String dst_dir)
throws Exception {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + db_path);
Statement stat1 = conn.createStatement();
Statement stat2 = conn.createStatement();
// 读取播放列表
List<PlayList> lists = new ArrayList<Data.PlayList>();
ResultSet rs_list = stat1.executeQuery("select * from playlistsInfo;");
while (rs_list.next()) {
final int id = rs_list.getInt("id");
final String name = rs_list.getString("title");
switch (name) {
// 忽略这几个列表
case "本地歌曲":
case "默认列表":
case "最近播放":
case "我的电台":
break;
case "我喜欢听":
default:
lists.add(new PlayList(name, id));
}
}
// 读取音乐信息
ResultSet rs_res = stat1.executeQuery("select * from musicResource;");
while (rs_res.next()) {
// 源文件路径
String fname = rs_res.getString("file");
if (fname == null || fname.length() == 0) // 如果file字段为空则跳过
continue;
String src_file = src_dir + '\\' + fname;
File src = new File(src_file);
if (!src.exists()) // 如果源文件不存在则跳过
continue;
// 获取音乐rid
int rid = rs_res.getInt("rid");
// 查找该音乐所在播放列表id, 如果没有找到则为-1
ResultSet rs_pl = stat2.executeQuery(new StringBuilder(
"select playlist_id from playlistMusics where rid=")
.append(rid).append(';').toString());
int playlist_id = -1;
while (rs_pl.next()) { // 默认将一首歌放在编号最大的播放列表中(也就是最新创建的列表)
int p_id = rs_pl.getInt("playlist_id");
if (p_id > playlist_id)
playlist_id = p_id;
}
rs_pl.close();
// 目标文件夹路径
StringBuilder b2 = new StringBuilder(dst_dir).append('\\');
if (playlist_id >= 0) {
String playlist_name = getPlaylist(lists, playlist_id);
if (playlist_name != null) {
b2.append(playlist_name).append('\\');
}
}
String dir = b2.toString();
new File(dir).mkdirs();
// 目标文件名: "艺术家 - 歌曲名.扩展名"
StringBuilder b3 = new StringBuilder();
b3.append(rs_res.getString("artist")).append(" - ")
.append(rs_res.getString("title")).append('.')
.append(rs_res.getString("format"));
String dst_path = dir + b3.toString();
// 移动和重命名
File dst = new File(dst_path);
src.renameTo(dst);
// 输出信息
this.output(new StringBuilder(src_file).append(" ---> ")
.append(dst_path).toString());
}
rs_res.close();
conn.close();
this.finish();
}
private static String getPlaylist(List<PlayList> lists, int playlist_id) {
for (PlayList pl : lists) {
if (pl.id == playlist_id)
return pl.name;
}
return null;
}
public abstract void output(String s);
public abstract void finish();
}