-
Notifications
You must be signed in to change notification settings - Fork 1
/
Provider.swift
49 lines (42 loc) · 1.38 KB
/
Provider.swift
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
//
// Provider.swift
// App
//
import Async
import Vapor
import NIO
import Kanna
protocol Provider {
typealias Details = (
id: String,
title: String,
price: String,
image: String
)
static var baseURL: String { get }
static var searchURL: String { get }
static var listx: String { get }
static var detailsx: Details { get }
static func resolve(page: Int, in app: Application) -> Future<[House]>?
static func resolve(element: Kanna.XMLElement) -> House?
}
extension Provider {
static func resolve(page: Int = 0, in app: Application) -> Future<[House]>? {
return try? app.client().get(searchURL).map { response in
// return try response.content.decode(String.self)
return response.http.body
}.map { content -> HTMLDocument? in
guard let data = content.data else { return nil }
let doc = try? HTML(html: data, encoding: .utf8)
print(doc?.title ?? "")
return doc
}.map { document -> [House] in
typealias Item = (title: String, href: String)
guard let articles = document?.xpath(listx) else { return [] }
print("articles \(articles.count)")
let houses: [House] = articles.compactMap { self.resolve(element: $0) }
print("houses \(articles)")
return houses
}
}
}