Skip to content

Instantly share code, notes, and snippets.

@mironal
Last active February 24, 2022 15:19
Show Gist options
  • Save mironal/5a8c6f933ad2ba60b2e23befc316f191 to your computer and use it in GitHub Desktop.
Save mironal/5a8c6f933ad2ba60b2e23befc316f191 to your computer and use it in GitHub Desktop.
Sample code for uploading video and subtitles using TwitterAPIKit.
// Delete subtitle
let exp = expectation(description: "")
client.v1.media.deleteSubtitle(.init(mediaID: "<media id>", mediaCategory: "TweetVideo", subtitleLanguageCodes: ["en"])).responseData { response in
print(response.prettyString)
exp.fulfill()
}
wait(for: [exp], timeout: 100)
// https://github.com/mironal/TwitterAPIKit
// XCTest
func testUploadVideoWithSutitle() throws {
let client = TwitterAPIKit(
consumerKey: "",
consumerSecret: "",
oauthToken: "",
oauthTokenSecret: ""
)
let srt = """
1
00:00:00,000 --> 00:00:05,000
Hello Hello
2
00:00:05,000 --> 00:00:07,000
Hoge Hoge
"""
let exp = expectation(description: "")
let data = try Data(contentsOf: URL(fileURLWithPath: "path/to/video.mp4"))
client.v1.media.uploadMedia(.init(media: data, mediaType: "video/mp4", filename: "video.mp4", mediaCategory: "TWEET_VIDEO")) { response in
print(response.prettyString)
guard let mediaID = response.success else {
XCTFail()
exp.fulfill()
return
}
client.v1.media.uploadMedia(.init(media: srt.data(using: .utf8)!, mediaType: "text/plain", filename: "hoge.str", mediaCategory: "SUBTITLES")) { response in
guard let subtitleMediaID = response.success else {
XCTFail()
exp.fulfill()
return
}
client.v1.media.createSubtitle(.init(mediaID: mediaID, mediaCategory: "tweet_video", subtitles: [.init(mediaID: subtitleMediaID, languageCode: "en", displayName: "English")])).responseData { response in
print(response.prettyString)
if response.isError {
XCTFail()
exp.fulfill()
return
}
client.v1.tweet.postUpdateStatus(.init(status: "Subtitle Text", mediaIDs: [mediaID])).responseObject { response in
print(response.prettyString)
exp.fulfill()
}
}
}
}
wait(for: [exp], timeout: 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment