Changed to using extended tweets.
This commit is contained in:
parent
687e1105f0
commit
2d61798457
|
@ -12,34 +12,20 @@ struct Tweet: Codable {
|
||||||
|
|
||||||
let createdAt: Date?
|
let createdAt: Date?
|
||||||
let idStr: String?
|
let idStr: String?
|
||||||
let text: String?
|
let fullText: String?
|
||||||
let displayTextRange: [Int]?
|
let displayTextRange: [Int]?
|
||||||
let user: TwitterUser
|
let user: TwitterUser
|
||||||
let truncated: Bool
|
let truncated: Bool
|
||||||
let retweeted: Bool
|
let retweeted: Bool
|
||||||
let extendedTweet: ExtendedTweet?
|
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case createdAt = "created_at"
|
case createdAt = "created_at"
|
||||||
case idStr = "id_str"
|
case idStr = "id_str"
|
||||||
case text = "text"
|
case fullText = "full_text"
|
||||||
case displayTextRange = "display_text_range"
|
case displayTextRange = "display_text_range"
|
||||||
case user = "user"
|
case user = "user"
|
||||||
case truncated = "truncated"
|
case truncated = "truncated"
|
||||||
case retweeted = "retweeted"
|
case retweeted = "retweeted"
|
||||||
case extendedTweet = "extended_tweet"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ExtendedTweet: Codable {
|
|
||||||
|
|
||||||
let fullText: String?
|
|
||||||
let displayTextRange: [Int]?
|
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
|
||||||
case fullText = "full_text"
|
|
||||||
case displayTextRange = "display_text_range"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -216,7 +216,7 @@ private extension TwitterFeedProvider {
|
||||||
|
|
||||||
func retrieveTweets(api: String, completion: @escaping (Result<[Tweet], Error>) -> Void) {
|
func retrieveTweets(api: String, completion: @escaping (Result<[Tweet], Error>) -> Void) {
|
||||||
let url = "\(Self.apiBase)\(api)"
|
let url = "\(Self.apiBase)\(api)"
|
||||||
let parameters = [String: Any]()
|
let parameters = ["tweet_mode": "extended"]
|
||||||
|
|
||||||
client.get(url, parameters: parameters) { result in
|
client.get(url, parameters: parameters) { result in
|
||||||
switch result {
|
switch result {
|
||||||
|
@ -225,6 +225,12 @@ private extension TwitterFeedProvider {
|
||||||
let dateFormatter = DateFormatter()
|
let dateFormatter = DateFormatter()
|
||||||
dateFormatter.dateFormat = Self.dateFormat
|
dateFormatter.dateFormat = Self.dateFormat
|
||||||
decoder.dateDecodingStrategy = .formatted(dateFormatter)
|
decoder.dateDecodingStrategy = .formatted(dateFormatter)
|
||||||
|
let jsonString = String(data: response.data, encoding: .utf8)
|
||||||
|
|
||||||
|
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("twitter.json")
|
||||||
|
print("******** writing to: \(url.path)")
|
||||||
|
try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let tweets = try decoder.decode([Tweet].self, from: response.data)
|
let tweets = try decoder.decode([Tweet].self, from: response.data)
|
||||||
completion(.success(tweets))
|
completion(.success(tweets))
|
||||||
|
@ -277,22 +283,12 @@ private extension TwitterFeedProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTweetText(_ tweet: Tweet) -> String? {
|
func makeTweetText(_ tweet: Tweet) -> String? {
|
||||||
if tweet.truncated, let extendedText = tweet.extendedTweet?.fullText {
|
if let text = tweet.fullText, let displayRange = tweet.displayTextRange, displayRange.count > 1,
|
||||||
if let displayRange = tweet.extendedTweet?.displayTextRange, displayRange.count > 1 {
|
let startIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex),
|
||||||
let startIndex = extendedText.index(extendedText.startIndex, offsetBy: displayRange[0])
|
let endIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) {
|
||||||
let endIndex = extendedText.index(extendedText.startIndex, offsetBy: displayRange[1])
|
return String(text[startIndex..<endIndex])
|
||||||
return String(extendedText[startIndex...endIndex])
|
|
||||||
} else {
|
|
||||||
return extendedText
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if let text = tweet.text, let displayRange = tweet.displayTextRange {
|
return tweet.fullText
|
||||||
let startIndex = text.index(text.startIndex, offsetBy: displayRange[0])
|
|
||||||
let endIndex = text.index(text.startIndex, offsetBy: displayRange[1])
|
|
||||||
return String(text[startIndex...endIndex])
|
|
||||||
} else {
|
|
||||||
return tweet.text
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue