From 41bb2afbde98a15d96e1ddda9688a0ecd95d281e Mon Sep 17 00:00:00 2001
From: Allan Nava <allannava95@gmail.com>
Date: Fri, 26 Jul 2024 09:37:48 +0200
Subject: [PATCH] added postvideo init

---
 tiktok/constants.go     | 11 +++++++++--
 tiktok/content.go       | 27 ++++++++++++++++++++++++---
 tiktok/model_content.go | 13 +++++++++++--
 tiktok/tiktok.go        |  1 +
 4 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/tiktok/constants.go b/tiktok/constants.go
index 9cd9e97..6677920 100644
--- a/tiktok/constants.go
+++ b/tiktok/constants.go
@@ -1,6 +1,13 @@
 package tiktok
 
 const (
-    BASE_URL = "https://open.tiktokapis.com"
-    QUERY_CREATOR_INFO = "v2/post/publish/creator_info/query"
+    BASE_URL                = "https://open.tiktokapis.com/"
+    QUERY_CREATOR_INFO      = "v2/post/publish/creator_info/query"
+    POST_PUBLISH_VIDEO_INIT = "v2/post/publish/video/init"
 )
+
+
+var (
+    API_QUERY_CREATOR_INFO      = fmt.Sprintf("%s%s", BASE_URL, QUERY_CREATOR_INFO)
+    API_POST_PUBLISH_VIDEO_INIT = fmt.Sprintf("%s%s",BASE_URL,  POST_PUBLISH_VIDEO_INIT)
+)
\ No newline at end of file
diff --git a/tiktok/content.go b/tiktok/content.go
index 564214c..0aad7c7 100644
--- a/tiktok/content.go
+++ b/tiktok/content.go
@@ -16,7 +16,7 @@ curl --location --request POST 'https://open.tiktokapis.com/v2/post/publish/crea
 --header 'Content-Type: application/json; charset=UTF-8'
 */
 func (o *tiktok) CreatorInfo() (*QueryCreatorInfoResponse, error) {
-	resp, err := o.restyPost("/", nil)
+	resp, err := o.restyPost(API_QUERY_CREATOR_INFO, nil)
 	if err != nil {
 		return nil, err
 	}
@@ -61,6 +61,27 @@ curl --location 'https://open.tiktokapis.com/v2/post/publish/video/init/' \
 	  }
 	}'
 */
-func (o *tiktok) PostVideo() {
-
+func (o *tiktok) PostVideoInit(title, videoUrl string, privacyLevel string) (*PublishVideoResponse, error) {
+	request := &PublishVideoRequest{
+		PostInfo: PostInfo{
+			Title: title,
+		},
+		SourceInfo: SourceInfo{
+			Source: "PULL_FROM_URL",
+			VideoUrl: videoUrl
+		}
+	}
+	resp, err := o.restyPost(API_POST_PUBLISH_VIDEO_INIT, request)
+	if err != nil {
+		return nil, err
+	}
+	if resp.IsError() {
+		return nil, fmt.Errorf("post video init error %s", resp.String())
+	}
+	var obj PublishVideoResponse
+	if err := json.Unmarshal(resp.Body(), &obj); err != nil {
+		return nil, err
+	}
+	o.debugPrint(obj)
+	return &obj, nil
 }
diff --git a/tiktok/model_content.go b/tiktok/model_content.go
index ec5e955..69ab662 100644
--- a/tiktok/model_content.go
+++ b/tiktok/model_content.go
@@ -22,7 +22,7 @@ package tiktok
 
 type QueryCreatorInfoResponse struct {
 	Data  DataQueryCreatorInfo  `json:"data"`
-	Error ErrorQueryCreatorInfo `json:"error"`
+	Error ErrorObject `json:"error"`
 }
 
 type DataQueryCreatorInfo struct {
@@ -35,8 +35,17 @@ type DataQueryCreatorInfo struct {
 	StitchDisabled      bool     `json:"stitch_disabled"`
 }
 
-type ErrorQueryCreatorInfo struct {
+type ErrorObject struct {
 	Code    string `json:"code"`
 	Message string `json:"message"`
 	LogId   string `json:"log_id"`
 }
+
+type PublishVideoResponse struct {
+   Data  DataPublishVideo `json:"data"`
+	Error ErrorObject `json:"error"`
+}
+
+type DataPublishVideo struct {
+	PubblishId    string   `json:"publish_id"`
+}
\ No newline at end of file
diff --git a/tiktok/tiktok.go b/tiktok/tiktok.go
index e77bbac..2879c34 100644
--- a/tiktok/tiktok.go
+++ b/tiktok/tiktok.go
@@ -7,6 +7,7 @@ type ITiktok interface {
 	HealthCheck() error
 	IsDebug() bool
 	CreatorInfo() (*QueryCreatorInfoResponse, error)
+	PostVideoInit(title, videoUrl string, privacyLevel string) (*PublishVideoResponse, error)
 	//
 }