Changed to using extended tweets.

This commit is contained in:
Maurice Parker 2020-04-17 11:20:22 -05:00
parent 687e1105f0
commit 2d61798457
2 changed files with 14 additions and 32 deletions

View File

@ -12,34 +12,20 @@ struct Tweet: Codable {
let createdAt: Date?
let idStr: String?
let text: String?
let fullText: String?
let displayTextRange: [Int]?
let user: TwitterUser
let truncated: Bool
let retweeted: Bool
let extendedTweet: ExtendedTweet?
enum CodingKeys: String, CodingKey {
case createdAt = "created_at"
case idStr = "id_str"
case text = "text"
case fullText = "full_text"
case displayTextRange = "display_text_range"
case user = "user"
case truncated = "truncated"
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"
}
}

View File

@ -216,7 +216,7 @@ private extension TwitterFeedProvider {
func retrieveTweets(api: String, completion: @escaping (Result<[Tweet], Error>) -> Void) {
let url = "\(Self.apiBase)\(api)"
let parameters = [String: Any]()
let parameters = ["tweet_mode": "extended"]
client.get(url, parameters: parameters) { result in
switch result {
@ -225,6 +225,12 @@ private extension TwitterFeedProvider {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = Self.dateFormat
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 {
let tweets = try decoder.decode([Tweet].self, from: response.data)
completion(.success(tweets))
@ -277,22 +283,12 @@ private extension TwitterFeedProvider {
}
func makeTweetText(_ tweet: Tweet) -> String? {
if tweet.truncated, let extendedText = tweet.extendedTweet?.fullText {
if let displayRange = tweet.extendedTweet?.displayTextRange, displayRange.count > 1 {
let startIndex = extendedText.index(extendedText.startIndex, offsetBy: displayRange[0])
let endIndex = extendedText.index(extendedText.startIndex, offsetBy: displayRange[1])
return String(extendedText[startIndex...endIndex])
} else {
return extendedText
}
if let text = tweet.fullText, let displayRange = tweet.displayTextRange, displayRange.count > 1,
let startIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex),
let endIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) {
return String(text[startIndex..<endIndex])
} else {
if let text = tweet.text, let displayRange = tweet.displayTextRange {
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
}
return tweet.fullText
}
}