From 6a62e97aa4e79a8341c3ba1ef511f23903c6f952 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Sun, 28 Jul 2024 11:27:11 +0200 Subject: [PATCH] added PostPhotoInit --- tiktok/content.go | 39 ++++++++++++++++++++++++++++++++++++++- tiktok/errors.go | 1 + tiktok/post_mode.go | 18 ++++++++++++++++++ tiktok/request_content.go | 28 +++++++++++++++++++++++++++- tiktok/tiktok.go | 1 + 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tiktok/post_mode.go diff --git a/tiktok/content.go b/tiktok/content.go index 4553199..14594d5 100644 --- a/tiktok/content.go +++ b/tiktok/content.go @@ -151,4 +151,41 @@ curl --location 'https://open.tiktokapis.com/v2/post/publish/content/init/' \ "post_mode": "MEDIA_UPLOAD", "media_type": "PHOTO" }' -*/ \ No newline at end of file +*/ + +func (o *tiktok) PostPhotoInit(title, description, privacyLevel string, photoUrls []string, photoMode string) (*PublishStatusFetchResponse, error) { + if !CheckPrivacyLevel(privacyLevel){ + return nil, PrivacyLevelWrong + } + if !CheckPostMode(photoMode){ + return nil, PhotoModeWrong + } + request := &PublishPhotoRequest{ + PostInfo: PostPhotoInfo{ + Title: title, + PrivacyLevel: privacyLevel, + DisableComment: false, + AutoAddMusic: true, + }, + SourceInfo: PhotoSourceInfo{ + Source: "PULL_FROM_URL", + PhotoCoverIndex: 1, + PhotoImages: photoUrls, + }, + PostMode: photoMode, + MediaType: "PHOTO", + } + resp, err := o.restyPost(POST_PUBLISH_CONTENT_INIT, request) + if err != nil { + return nil, err + } + if resp.IsError() { + return nil, fmt.Errorf("post video init error %s", resp.String()) + } + var obj PublishStatusFetchResponse + if err := json.Unmarshal(resp.Body(), &obj); err != nil { + return nil, err + } + o.debugPrint(obj) + return &obj, nil +} \ No newline at end of file diff --git a/tiktok/errors.go b/tiktok/errors.go index 5c19b0b..02ebd40 100644 --- a/tiktok/errors.go +++ b/tiktok/errors.go @@ -4,4 +4,5 @@ import "errors" var ( PrivacyLevelWrong = errors.New("Privacy Level is not correct!") + PhotoModeWrong = errors.New("Photo mode is not correct!") ) diff --git a/tiktok/post_mode.go b/tiktok/post_mode.go new file mode 100644 index 0000000..9c74519 --- /dev/null +++ b/tiktok/post_mode.go @@ -0,0 +1,18 @@ +package tiktok + +type PostModeOptions string + +const ( + DIRECT_POST PostModeOptions = "DIRECT_POST" + MEDIA_UPLOAD PostModeOptions = "MEDIA_UPLOAD" +) + +// CheckPostMode checks if the given string matches any PostModeOptions +func CheckPostMode(mode string) bool { + switch PostModeOptions(mode) { + case DIRECT_POST, MEDIA_UPLOAD: + return true + default: + return false + } +} \ No newline at end of file diff --git a/tiktok/request_content.go b/tiktok/request_content.go index f6f1dba..ce2a769 100644 --- a/tiktok/request_content.go +++ b/tiktok/request_content.go @@ -16,6 +16,9 @@ package tiktok } } */ +/*type BasePostInfo struct { +}*/ + type PublishVideoRequest struct { PostInfo PostInfo `json:"post_info"` SourceInfo SourceInfo `json:"source_info"` @@ -37,4 +40,27 @@ type SourceInfo struct { type PublishStatusFetchRequest struct { PublishId string `json:"publish_id"` -} \ No newline at end of file +} + + +type PublishPhotoRequest struct { + PostInfo PostPhotoInfo `json:"post_info"` + SourceInfo PhotoSourceInfo `json:"source_info"` + PostMode string `json:"post_mode"` // DIRECT_POST | MEDIA_UPLAOD + MediaType string `json:"media_type"` // PHOTO +} + +type PostPhotoInfo struct { + Title string `json:"title"` + Description string `json:"description"` + PrivacyLevel string `json:"privacy_level"` + DisableComment bool `json:"disable_comment"` + AutoAddMusic bool `json:"auto_add_music"` +} + +type PhotoSourceInfo struct { + Source string `json:"source"` + PhotoCoverIndex int `json:"photo_cover_index"` + PhotoImages []string `json:"photo_images"` +} + diff --git a/tiktok/tiktok.go b/tiktok/tiktok.go index 233ba0e..29c514c 100644 --- a/tiktok/tiktok.go +++ b/tiktok/tiktok.go @@ -13,6 +13,7 @@ type ITiktok interface { CreatorInfo() (*QueryCreatorInfoResponse, error) PostVideoInit(title, videoUrl string, privacyLevel string) (*PublishVideoResponse, error) PublishVideo(publishId string) (*PublishStatusFetchResponse, error) + PostPhotoInit(title, description, privacyLevel string, photoUrls []string, photoMode string) (*PublishStatusFetchResponse, error) UserInfo() (*UserInfoResponse, error) // }