forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebLoader.swift
More file actions
151 lines (131 loc) · 4.87 KB
/
WebLoader.swift
File metadata and controls
151 lines (131 loc) · 4.87 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Foundation
import Logger
import SwiftSoup
import WebKit
public struct WebLoader: DocumentLoader {
enum MetadataKeys {
static let title = "title"
static let url = "url"
static let date = "date"
}
var downloadHTML: (_ url: URL) async throws -> (url: URL, html: String) = { url in
let html = try await WebScrapper().fetch(url: url)
return (url, html)
}
public var urls: [URL]
public init(urls: [URL]) {
self.urls = urls
}
public init(url: URL) {
urls = [url]
}
public func load() async throws -> [Document] {
try await withThrowingTaskGroup(of: (url: URL, html: String).self) { group in
for url in urls {
group.addTask {
try await downloadHTML(url)
}
}
var documents: [Document] = []
for try await result in group {
do {
let parsed = try SwiftSoup.parse(result.html, result.url.path)
let title = (try? parsed.title()) ?? "Untitled"
let body = try DefaultLoadContentStrategy().load(parsed)
if let body = body {
let doc = Document(pageContent: body, metadata: [
MetadataKeys.title: .string(title),
MetadataKeys.url: .string(result.url.absoluteString),
MetadataKeys.date: .number(Date().timeIntervalSince1970),
])
documents.append(doc)
}
} catch let Exception.Error(_, message) {
Logger.langchain.error(message)
} catch {
Logger.langchain.error(error.localizedDescription)
}
}
return documents
}
}
}
protocol LoadWebPageMainContentStrategy {
func load(_ document: SwiftSoup.Document) throws -> String?
}
extension LoadWebPageMainContentStrategy {
func text(inFirstTag tagName: String, from document: SwiftSoup.Document) -> String? {
if let tag = try? document.getElementsByTag(tagName).first(),
let text = try? tag.text()
{
return text
}
return nil
}
}
extension WebLoader {
struct DefaultLoadContentStrategy: LoadWebPageMainContentStrategy {
func load(_ document: SwiftSoup.Document) throws -> String? {
if let article = text(inFirstTag: "article", from: document) { return article }
if let main = text(inFirstTag: "main", from: document) { return main }
let body = try document.body()?.text()
return body
}
}
}
@MainActor
final class WebScrapper: NSObject, WKNavigationDelegate {
var webView: WKWebView
let retryLimit: Int
var webViewDidFinishLoading = false
var navigationError: (any Error)?
init(retryLimit: Int = 10) {
self.retryLimit = retryLimit
let configuration = WKWebViewConfiguration()
configuration.defaultWebpagePreferences.preferredContentMode = .desktop
configuration.defaultWebpagePreferences.allowsContentJavaScript = true
configuration.mediaTypesRequiringUserActionForPlayback = []
configuration.applicationNameForUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15"
// The web page need the web view to have a size to load correctly.
let webView = WKWebView(
frame: .init(x: 0, y: 0, width: 500, height: 500),
configuration: configuration
)
self.webView = webView
super.init()
webView.navigationDelegate = self
}
func fetch(url: URL) async throws -> String {
webViewDidFinishLoading = false
navigationError = nil
var retryCount = 0
_ = webView.load(.init(url: url))
while !webViewDidFinishLoading {
try await Task.sleep(nanoseconds: 1_000_000)
}
if let navigationError { throw navigationError }
while retryCount < retryLimit {
let html = try await getHTML()
if !html.isEmpty { return html }
retryCount += 1
}
throw CancellationError()
}
nonisolated func webView(_: WKWebView, didFinish _: WKNavigation!) {
Task { @MainActor in
self.webViewDidFinishLoading = true
}
}
nonisolated func webView(_: WKWebView, didFail _: WKNavigation!, withError error: Error) {
Task { @MainActor in
self.navigationError = error
self.webViewDidFinishLoading = true
}
}
func getHTML() async throws -> String {
return try await webView.evaluateJavaScript(getHTMLText) as? String ?? ""
}
}
private let getHTMLText = """
document.documentElement.outerHTML;
"""