Upload tiktok.go
Browse files
tiktok.go
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package media
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"encoding/json"
|
| 6 |
+
"fmt"
|
| 7 |
+
"io"
|
| 8 |
+
"net/http"
|
| 9 |
+
"net/url"
|
| 10 |
+
"strings"
|
| 11 |
+
"time"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
const (
|
| 15 |
+
TikWMAPI = "https://www.tikwm.com/api/"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
// tikwmResponse represents the tikwm.com API response.
|
| 19 |
+
type tikwmResponse struct {
|
| 20 |
+
Code int `json:"code"`
|
| 21 |
+
Msg string `json:"msg"`
|
| 22 |
+
ProcessedTime float64 `json:"processed_time"`
|
| 23 |
+
Data tikwmData `json:"data"`
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
type tikwmData struct {
|
| 27 |
+
ID string `json:"id"`
|
| 28 |
+
Title string `json:"title"`
|
| 29 |
+
Play string `json:"play"` // video download URL (no watermark)
|
| 30 |
+
Hdplay string `json:"hdplay"` // HD video URL
|
| 31 |
+
Images []string `json:"images"` // slideshow image URLs
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
func GetTikTokMedia(ctx context.Context, rawURL string) (MediaResult, error) {
|
| 35 |
+
var lastErr error
|
| 36 |
+
for i := 0; i < 3; i++ {
|
| 37 |
+
if i > 0 {
|
| 38 |
+
backoff := time.Duration(i) * 500 * time.Millisecond
|
| 39 |
+
select {
|
| 40 |
+
case <-time.After(backoff):
|
| 41 |
+
case <-ctx.Done():
|
| 42 |
+
return MediaResult{}, ctx.Err()
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
items, err := doTikWMRequest(ctx, rawURL)
|
| 46 |
+
if err != nil {
|
| 47 |
+
lastErr = err
|
| 48 |
+
continue
|
| 49 |
+
}
|
| 50 |
+
return MediaResult{Items: items}, nil
|
| 51 |
+
}
|
| 52 |
+
return MediaResult{}, fmt.Errorf("tikwm api failed after 3 retries: %w", lastErr)
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
func doTikWMRequest(ctx context.Context, tiktokURL string) ([]MediaItem, error) {
|
| 56 |
+
// Build form body
|
| 57 |
+
form := url.Values{}
|
| 58 |
+
form.Set("url", tiktokURL)
|
| 59 |
+
form.Set("hd", "1")
|
| 60 |
+
|
| 61 |
+
apiReq, err := http.NewRequestWithContext(ctx, "POST", TikWMAPI, strings.NewReader(form.Encode()))
|
| 62 |
+
if err != nil {
|
| 63 |
+
return nil, fmt.Errorf("failed to create tikwm request: %w", err)
|
| 64 |
+
}
|
| 65 |
+
apiReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
| 66 |
+
apiReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
| 67 |
+
|
| 68 |
+
apiResp, err := httpClient.Do(apiReq)
|
| 69 |
+
if err != nil {
|
| 70 |
+
return nil, fmt.Errorf("failed to call tikwm api: %w", err)
|
| 71 |
+
}
|
| 72 |
+
defer apiResp.Body.Close()
|
| 73 |
+
|
| 74 |
+
if apiResp.StatusCode != http.StatusOK {
|
| 75 |
+
snippet, _ := io.ReadAll(io.LimitReader(apiResp.Body, 200))
|
| 76 |
+
return nil, fmt.Errorf("tikwm api returned status %d: %s", apiResp.StatusCode, string(snippet))
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// Stream-decode JSON directly from the response body — avoids buffering
|
| 80 |
+
// the full 2 MB response into a []byte before parsing.
|
| 81 |
+
var data tikwmResponse
|
| 82 |
+
if err := json.NewDecoder(io.LimitReader(apiResp.Body, 2*1024*1024)).Decode(&data); err != nil {
|
| 83 |
+
return nil, fmt.Errorf("failed to decode tikwm response: %w", err)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
if data.Code != 0 {
|
| 87 |
+
return nil, fmt.Errorf("tikwm api error: %s", data.Msg)
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
var items []MediaItem
|
| 91 |
+
|
| 92 |
+
// Slideshow (images)
|
| 93 |
+
if len(data.Data.Images) > 0 {
|
| 94 |
+
for _, imgURL := range data.Data.Images {
|
| 95 |
+
if imgURL != "" {
|
| 96 |
+
items = append(items, MediaItem{Type: Image, URL: imgURL})
|
| 97 |
+
}
|
| 98 |
+
}
|
| 99 |
+
if len(items) > 0 {
|
| 100 |
+
return items, nil
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Video – prefer HD
|
| 105 |
+
videoURL := data.Data.Hdplay
|
| 106 |
+
if videoURL == "" {
|
| 107 |
+
videoURL = data.Data.Play
|
| 108 |
+
}
|
| 109 |
+
if videoURL != "" {
|
| 110 |
+
return []MediaItem{{Type: Video, URL: videoURL}}, nil
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
return nil, fmt.Errorf("no video or images found in tikwm response")
|
| 114 |
+
}
|