1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30

Fix parsing Piped comments

This commit is contained in:
Arkadiusz Fal 2022-12-13 00:38:45 +01:00
parent 1c746bc8e0
commit 02b30394ed

View File

@ -528,11 +528,13 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
} }
private func extractDescription(from content: JSON) -> String? { private func extractDescription(from content: JSON) -> String? {
guard var description = content.dictionaryValue["description"]?.string else { guard let description = content.dictionaryValue["description"]?.string else { return nil }
return nil
return replaceHTML(description)
} }
description = description.replacingOccurrences( private func replaceHTML(_ string: String) -> String {
var string = string.replacingOccurrences(
of: "<br/>|<br />|<br>", of: "<br/>|<br />|<br>",
with: "\n", with: "\n",
options: .regularExpression, options: .regularExpression,
@ -541,9 +543,8 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
let linkRegex = #"(<a\s+(?:[^>]*?\s+)?href=\"[^"]*\">[^<]*<\/a>)"# let linkRegex = #"(<a\s+(?:[^>]*?\s+)?href=\"[^"]*\">[^<]*<\/a>)"#
let hrefRegex = #"href=\"([^"]*)\">"# let hrefRegex = #"href=\"([^"]*)\">"#
guard let hrefRegex = try? NSRegularExpression(pattern: hrefRegex) else { return description } guard let hrefRegex = try? NSRegularExpression(pattern: hrefRegex) else { return string }
string = string.replacingMatches(regex: linkRegex) { matchingGroup in
description = description.replacingMatches(regex: linkRegex) { matchingGroup in
let results = hrefRegex.matches(in: matchingGroup, range: NSRange(matchingGroup.startIndex..., in: matchingGroup)) let results = hrefRegex.matches(in: matchingGroup, range: NSRange(matchingGroup.startIndex..., in: matchingGroup))
if let result = results.first { if let result = results.first {
@ -555,16 +556,17 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
return matchingGroup return matchingGroup
} }
description = description.replacingOccurrences(of: "&amp;", with: "&") string = string
.replacingOccurrences(of: "&amp;", with: "&")
description = description.replacingOccurrences( .replacingOccurrences(of: "&nbsp;", with: " ")
.replacingOccurrences(
of: "<[^>]+>", of: "<[^>]+>",
with: "", with: "",
options: .regularExpression, options: .regularExpression,
range: nil range: nil
) )
return description return string
} }
private func extractVideos(from content: JSON) -> [Video] { private func extractVideos(from content: JSON) -> [Video] {
@ -653,6 +655,7 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
let author = details["author"]?.string ?? "" let author = details["author"]?.string ?? ""
let commentorUrl = details["commentorUrl"]?.string let commentorUrl = details["commentorUrl"]?.string
let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? "" let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? ""
return Comment( return Comment(
id: details["commentId"]?.string ?? UUID().uuidString, id: details["commentId"]?.string ?? UUID().uuidString,
author: author, author: author,
@ -661,12 +664,18 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
pinned: details["pinned"]?.bool ?? false, pinned: details["pinned"]?.bool ?? false,
hearted: details["hearted"]?.bool ?? false, hearted: details["hearted"]?.bool ?? false,
likeCount: details["likeCount"]?.int ?? 0, likeCount: details["likeCount"]?.int ?? 0,
text: details["commentText"]?.string ?? "", text: extractCommentText(from: details["commentText"]?.stringValue),
repliesPage: details["repliesPage"]?.string, repliesPage: details["repliesPage"]?.string,
channel: Channel(id: channelId, name: author) channel: Channel(id: channelId, name: author)
) )
} }
private func extractCommentText(from string: String?) -> String {
guard let string, !string.isEmpty else { return "" }
return replaceHTML(string)
}
private func extractChapters(from content: JSON) -> [Chapter] { private func extractChapters(from content: JSON) -> [Chapter] {
guard let chapters = content.dictionaryValue["chapters"]?.array else { guard let chapters = content.dictionaryValue["chapters"]?.array else {
return .init() return .init()