From dd94212c9f615db25d7bb9ca2dfd7c2d27e2a0c5 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 18 Apr 2020 14:48:12 -0500 Subject: [PATCH] Add inline video support to Tweets --- .../Account/Account.xcodeproj/project.pbxproj | 4 ++ .../FeedProvider/Twitter/TwitterMedia.swift | 40 ++++++++++++++++++- .../FeedProvider/Twitter/TwitterVideo.swift | 34 ++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 Frameworks/Account/FeedProvider/Twitter/TwitterVideo.swift diff --git a/Frameworks/Account/Account.xcodeproj/project.pbxproj b/Frameworks/Account/Account.xcodeproj/project.pbxproj index a4347fbb9..70f38d9b3 100644 --- a/Frameworks/Account/Account.xcodeproj/project.pbxproj +++ b/Frameworks/Account/Account.xcodeproj/project.pbxproj @@ -69,6 +69,7 @@ 51B3630D244B6428000DEF2A /* TwitterSymbol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B3630C244B6428000DEF2A /* TwitterSymbol.swift */; }; 51B3630F244B6CB9000DEF2A /* TwitterExtendedEntities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B3630E244B6CB9000DEF2A /* TwitterExtendedEntities.swift */; }; 51B36311244B6CFB000DEF2A /* TwitterMedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B36310244B6CFA000DEF2A /* TwitterMedia.swift */; }; + 51B36313244B8B5E000DEF2A /* TwitterVideo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51B36312244B8B5E000DEF2A /* TwitterVideo.swift */; }; 51BB7B84233531BC008E8144 /* AccountBehaviors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51BB7B83233531BC008E8144 /* AccountBehaviors.swift */; }; 51BC8FCC237EC055004F8B56 /* Feed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51BC8FCB237EC055004F8B56 /* Feed.swift */; }; 51BFDECE238B508D00216323 /* ContainerIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51BFDECD238B508D00216323 /* ContainerIdentifier.swift */; }; @@ -317,6 +318,7 @@ 51B3630C244B6428000DEF2A /* TwitterSymbol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitterSymbol.swift; sourceTree = ""; }; 51B3630E244B6CB9000DEF2A /* TwitterExtendedEntities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitterExtendedEntities.swift; sourceTree = ""; }; 51B36310244B6CFA000DEF2A /* TwitterMedia.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitterMedia.swift; sourceTree = ""; }; + 51B36312244B8B5E000DEF2A /* TwitterVideo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitterVideo.swift; sourceTree = ""; }; 51BB7B83233531BC008E8144 /* AccountBehaviors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountBehaviors.swift; sourceTree = ""; }; 51BC8FCB237EC055004F8B56 /* Feed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Feed.swift; sourceTree = ""; }; 51BFDECD238B508D00216323 /* ContainerIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerIdentifier.swift; sourceTree = ""; }; @@ -590,6 +592,7 @@ 51B3630C244B6428000DEF2A /* TwitterSymbol.swift */, 51B36308244B62A5000DEF2A /* TwitterURL.swift */, 5132DE802449159100806ADE /* TwitterUser.swift */, + 51B36312244B8B5E000DEF2A /* TwitterVideo.swift */, ); path = Twitter; sourceTree = ""; @@ -1215,6 +1218,7 @@ 51E59599228C77BC00FCC42B /* FeedbinUnreadEntry.swift in Sources */, 552032F8229D5D5A009559E0 /* ReaderAPIEntry.swift in Sources */, 552032FB229D5D5A009559E0 /* ReaderAPITag.swift in Sources */, + 51B36313244B8B5E000DEF2A /* TwitterVideo.swift in Sources */, 5165D72822835F7800D9D53D /* FeedFinder.swift in Sources */, 9EBD49C023C67602005AD5CD /* FeedlyDownloadArticlesOperation.swift in Sources */, 51D58755227F53BE00900287 /* FeedbinTag.swift in Sources */, diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterMedia.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterMedia.swift index 1f83311a7..ff10b5034 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterMedia.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterMedia.swift @@ -17,6 +17,7 @@ struct TwitterMedia: Codable { let url: String? let displayURL: String? let type: String? + let video: TwitterVideo? enum CodingKeys: String, CodingKey { case idStr = "idStr" @@ -26,6 +27,7 @@ struct TwitterMedia: Codable { case url = "url" case displayURL = "display_url" case type = "type" + case video = "video_info" } func renderAsHTML() -> String { @@ -37,9 +39,13 @@ struct TwitterMedia: Codable { html += "" html += renderPhotoAsHTML() html += "" + } else { + html += renderPhotoAsHTML() } + case "video": + html += renderVideoAsHTML() default: - return "" + break } return html @@ -58,5 +64,37 @@ private extension TwitterMedia { } return "" } + + func renderVideoAsHTML() -> String { + guard let bestVariantURL = findBestVariant()?.url else { return "" } + + var html = "" + return html + } + func findBestVariant() -> TwitterVideo.Variant? { + var best: TwitterVideo.Variant? = nil + if let variants = video?.variants { + for variant in variants { + if let currentBest = best { + if variant.bitrate ?? 0 > currentBest.bitrate ?? 0 { + best = variant + } + } else { + best = variant + } + } + } + return best + } + +// } diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterVideo.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterVideo.swift new file mode 100644 index 000000000..a6df96292 --- /dev/null +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterVideo.swift @@ -0,0 +1,34 @@ +// +// TwitterVideoInfo.swift +// Account +// +// Created by Maurice Parker on 4/18/20. +// Copyright © 2020 Ranchero Software, LLC. All rights reserved. +// + +import Foundation + + +struct TwitterVideo: Codable { + + let variants: [Variant]? + + enum CodingKeys: String, CodingKey { + case variants = "variants" + } + + struct Variant: Codable { + + let bitrate: Int? + let contentType: String? + let url: String? + + enum CodingKeys: String, CodingKey { + case bitrate = "bitrate" + case contentType = "content_type" + case url = "url" + } + + } + +}