forked from CatchChat/Yep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGraph.swift
181 lines (132 loc) · 5.16 KB
/
OpenGraph.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// OpenGraph.swift
// Yep
//
// Created by NIX on 16/5/20.
// Copyright © 2016年 Catch Inc. All rights reserved.
//
import Foundation
import Kanna
public struct OpenGraph {
enum Kind {
case Default
case AppleMusic
case AppleMovie
case AppleEBook
}
var kind: Kind = .Default
public var URL: NSURL
public var siteName: String?
public var title: String?
public var description: String?
public var previewImageURLString: String?
public var previewVideoURLString: String?
public var previewAudioURLString: String?
public var isValid: Bool {
guard
let siteName = siteName where !siteName.isEmpty,
let title = title where !title.isEmpty,
let description = description where !description.isEmpty,
let _ = previewImageURLString
else {
return false
}
return true
}
struct AppleMusic {
var artistName: String?
var artworkURLString30: String?
var artworkURLString60: String?
var artworkURLString100: String?
var artworkURLString160: String?
var collectionType: String?
var collectionName: String?
var collectionViewURLString: String?
var trackTimeMillis: Int?
var trackViewURLString: String?
}
var appleMusic: AppleMusic?
struct AppleMovie {
var artistName: String?
var artworkURLString30: String?
var artworkURLString60: String?
var artworkURLString100: String?
var shortDescription: String?
var longDescription: String?
var trackTimeMillis: Int?
var trackViewURLString: String?
}
var appleMovie: AppleMovie?
struct AppleEBook {
var artistName: String?
var artworkURLString60: String?
var artworkURLString100: String?
var description: String?
var trackName: String? // book name
var trackViewURLString: String?
}
var appleEBook: AppleEBook?
init(URL: NSURL) {
self.URL = URL.opengraph_appleAllianceURL
}
static func fromHTMLString(HTMLString: String, forURL URL: NSURL) -> OpenGraph? {
if let doc = Kanna.HTML(html: HTMLString, encoding: NSUTF8StringEncoding) {
var openGraph = OpenGraph(URL: URL)
if let metaSet = doc.head?.css("meta") {
var openGraphInfo = [String: String]()
for meta in metaSet {
if let property = meta["property"]?.lowercaseString {
if property.hasPrefix("og:") {
if let content = meta["content"] {
openGraphInfo[property] = content
}
}
}
}
openGraph.siteName = openGraphInfo["og:site_name"]
openGraph.title = openGraphInfo["og:title"]
openGraph.description = openGraphInfo["og:description"]
openGraph.previewImageURLString = openGraphInfo["og:image"]
// 若缺失某些`og:`标签,再做补救
if openGraph.siteName == nil {
openGraph.siteName = URL.host
}
if openGraph.title == nil {
if let title = doc.head?.css("title").first?.text where !title.isEmpty {
openGraph.title = title
}
}
if openGraph.description == nil {
for meta in metaSet {
if let name = meta["name"]?.lowercaseString {
if name == "description" {
if let description = meta["content"] where !description.isEmpty {
openGraph.description = description
break
}
}
}
}
}
if openGraph.previewImageURLString == nil {
openGraph.previewImageURLString = HTMLString.opengraph_firstImageURL?.absoluteString
}
// 特别再补救一次 description
if openGraph.description == nil {
let firstParagraph = doc.body?.css("p").first?.text
openGraph.description = firstParagraph
}
// 再去除字符串中的换行
openGraph.siteName = openGraph.siteName?.opengraph_removeAllNewLines
openGraph.title = openGraph.title?.opengraph_removeAllNewLines
openGraph.description = openGraph.description?.opengraph_removeAllNewLines
// 以及行首行尾的空白
openGraph.siteName = openGraph.siteName?.opengraph_trimming(.WhitespaceAndNewline)
openGraph.title = openGraph.title?.opengraph_trimming(.WhitespaceAndNewline)
openGraph.description = openGraph.description?.opengraph_trimming(.WhitespaceAndNewline)
}
return openGraph
}
return nil
}
}