Skip to content

Commit 6855771

Browse files
committed
Fix the out-of-bounds crash and add QQ music search api.
1 parent af0aeca commit 6855771

File tree

9 files changed

+372
-389
lines changed

9 files changed

+372
-389
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ let package = Package(
2121
.package(url: "https://github.com/ddddxxx/Regex", from: "1.0.1"),
2222
.package(url: "https://github.com/Mx-Iris/SwiftCF", .branchItem("master")),
2323
.package(name: "Gzip", url: "https://github.com/1024jp/GzipSwift", from: "5.0.0"),
24+
.package(url: "https://github.com/drmohundro/SWXMLHash", .upToNextMajor(from: "7.0.0")),
2425
],
2526
targets: [
2627
.target(
2728
name: "LyricsCore",
2829
dependencies: ["Regex", "SwiftCF"]),
2930
.target(
3031
name: "LyricsService",
31-
dependencies: ["LyricsCore", "CXShim", "CXExtensions", "Regex", "Gzip"]),
32+
dependencies: [
33+
"LyricsCore", "CXShim", "CXExtensions", "Regex", "Gzip",
34+
.product(name: "SWXMLHash", package: "SWXMLHash")
35+
]
36+
),
3237
.testTarget(
3338
name: "LyricsKitTests",
3439
dependencies: ["LyricsCore", "LyricsService"]),

Sources/LyricsService/JSONModel/QQ/QQResponseSearchResult.swift

Lines changed: 58 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,125 +9,83 @@
99

1010
import Foundation
1111

12+
public protocol QQMusicSongSearchResult {
13+
var id: String { get }
14+
var mid: String { get }
15+
var name: String { get }
16+
var singers: [String] { get }
17+
}
18+
1219
struct QQResponseSearchResult: Decodable {
1320
let data: Data
1421
let code: Int
15-
16-
/*
17-
let message: String
18-
let notice: String
19-
let subcode: Int
20-
let time: Int // time stamp
21-
let tips: String
22-
*/
23-
22+
2423
struct Data: Decodable {
2524
let song: Song
26-
27-
/*
28-
let keyword: String
29-
...
30-
*/
31-
25+
3226
struct Song: Decodable {
3327
let list: [Item]
3428
enum CodingKeys: String, CodingKey {
3529
case list = "itemlist"
3630
}
37-
/*
38-
let curnum: Int
39-
let curpage: Int
40-
let totalnum: Int
41-
*/
42-
43-
struct Item: Decodable {
44-
let songmid: String
45-
let songname: String
31+
32+
struct Item: Decodable, QQMusicSongSearchResult {
33+
var singers: [String] { [singer] }
34+
let mid: String
35+
let name: String
4636
let singer: String
4737
let id: String
48-
/*
49-
let albumname: String
50-
let interval: Int
51-
*/
52-
enum CodingKeys: String, CodingKey {
53-
case songmid = "mid"
54-
case songname = "name"
55-
case singer
56-
case id
57-
}
58-
/*
59-
let albummid: String
60-
let albumname_hilight: String
61-
let alertid: Int
62-
let belongCD: Int
63-
let cdIdx: Int
64-
let chinesesinger: Int
65-
let docid: String
66-
let format: String
67-
let isonly: Int
68-
let lyric: String
69-
let lyric_hilight: String
70-
let media_mid: String
71-
let msgid: Int
72-
let newStatus: Int
73-
let nt: Int
74-
let pay: Pay
75-
let preview: Preview
76-
let pubtime: Int
77-
let pure: Int
78-
let size128: Int
79-
let size320: Int
80-
let sizeape: Int
81-
let sizeflac: Int
82-
let sizeogg: Int
83-
let songid: Int
84-
let songname_hilight: String
85-
let songurl: URL?
86-
let strMediaMid: String
87-
let stream: Int
88-
let `switch`: Int
89-
let t: Int
90-
let tag: Int
91-
let type: Int
92-
let ver: Int
93-
let vid: String
94-
*/
95-
96-
// let grp: [Any]
97-
/*
98-
struct Pay: Decodable {
99-
let payalbum: Int
100-
let payalbumprice: Int
101-
let paydownload: Int
102-
let payinfo: Int
103-
let payplay: Int
104-
let paytrackmouth: Int
105-
let paytrackprice: Int
106-
}
107-
108-
struct Preview: Decodable {
109-
let trybegin: Int
110-
let tryend: Int
111-
let trysize: Int
112-
}
113-
114-
struct Singer: Decodable {
115-
let name: String
116-
117-
118-
let id: Int
119-
let mid: String
120-
let name_hilight: String
121-
}
122-
*/
12338
}
12439
}
12540
}
12641
}
12742

12843
extension QQResponseSearchResult {
129-
13044
var songs: [Data.Song.Item] {
13145
return data.song.list
13246
}
13347
}
48+
49+
struct QQResponseSearchResult2: Decodable {
50+
struct Request: Decodable {
51+
struct Data: Decodable {
52+
struct Body: Decodable {
53+
struct Song: Decodable {
54+
struct Item: Decodable, QQMusicSongSearchResult {
55+
struct Singer: Decodable {
56+
let name: String
57+
}
58+
59+
let mid: String
60+
let name: String
61+
let _id: Int
62+
let singer: [Singer]
63+
var singers: [String] { singer.map(\.name) }
64+
var id: String { .init(_id) }
65+
enum CodingKeys: String, CodingKey {
66+
case mid
67+
case name
68+
case _id = "id"
69+
case singer
70+
}
71+
}
72+
73+
let list: [Item]
74+
}
75+
76+
let song: Song
77+
}
78+
79+
let body: Body
80+
}
81+
82+
let data: Data
83+
let code: Int
84+
}
85+
86+
let request: Request
87+
88+
enum CodingKeys: String, CodingKey {
89+
case request = "req_1"
90+
}
91+
}

Sources/LyricsService/LyricsSearchRequest.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ extension LyricsSearchRequest.SearchTerm: CustomStringConvertible {
3939
return title + " " + artist
4040
}
4141
}
42+
43+
public var titleOnly: String {
44+
switch self {
45+
case .keyword(let string):
46+
return string
47+
case .info(let title, let artist):
48+
return title
49+
}
50+
}
4251
}

Sources/LyricsService/Parser/KugouKrcParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extension Lyrics {
8888
idTags[.init(key)] = value
8989
}
9090

91-
let lines: [LyricsLine] = krcLineRegex.matches(in: content).map { match in
91+
let lines: [LyricsLine] = qrcLineRegex.matches(in: content).map { match in
9292
let timeTagStr = match[1]!.content
9393
let timeTag = TimeInterval(timeTagStr)! / 1000
9494

0 commit comments

Comments
 (0)