implement sonic for json + remove some TODOs from readme + fix go version requirement (#5)

This commit is contained in:
dogghi no 2025-04-17 14:16:30 +02:00 committed by GitHub
parent 4a15dd0761
commit e35bd2fe57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 70 additions and 47 deletions

View file

@ -3,7 +3,6 @@ package instagram
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"govd/util"
"html"
@ -14,6 +13,8 @@ import (
"strconv"
"strings"
"time"
"github.com/bytedance/sonic"
)
var captionPattern = regexp.MustCompile(
@ -40,7 +41,7 @@ func BuildSignedPayload(contentURL string) (io.Reader, error) {
"_tsc": "0", // ?
"_s": secretString,
}
parsedPayload, err := json.Marshal(payload)
parsedPayload, err := sonic.ConfigFastest.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("error marshalling payload: %w", err)
}
@ -50,15 +51,20 @@ func BuildSignedPayload(contentURL string) (io.Reader, error) {
func ParseIGramResponse(body []byte) (*IGramResponse, error) {
var rawResponse interface{}
if err := json.Unmarshal(body, &rawResponse); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
//move to the start of the body
// Use sonic's decoder to unmarshal the raw response
if err := sonic.ConfigFastest.Unmarshal(body, &rawResponse); err != nil {
return nil, fmt.Errorf("failed to decode response1: %w", err)
}
switch rawResponse.(type) {
case []interface{}:
// array of IGramMedia
var media []*IGramMedia
if err := json.Unmarshal(body, &media); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
if err := sonic.ConfigFastest.Unmarshal(body, &media); err != nil {
return nil, fmt.Errorf("failed to decode response2: %w", err)
}
return &IGramResponse{
Items: media,
@ -66,8 +72,8 @@ func ParseIGramResponse(body []byte) (*IGramResponse, error) {
case map[string]interface{}:
// single IGramMedia
var media IGramMedia
if err := json.Unmarshal(body, &media); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
if err := sonic.ConfigFastest.Unmarshal(body, &media); err != nil {
return nil, fmt.Errorf("failed to decode response3: %w", err)
}
return &IGramResponse{
Items: []*IGramMedia{&media},

View file

@ -1,9 +1,7 @@
package pinterest
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
@ -11,6 +9,8 @@ import (
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
)
const (
@ -185,13 +185,9 @@ func GetPinData(pinID string) (*PinData, error) {
return nil, fmt.Errorf("bad response: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var pinResponse PinResponse
err = json.Unmarshal(body, &pinResponse)
decoder := sonic.ConfigFastest.NewDecoder(resp.Body)
err = decoder.Decode(&pinResponse)
if err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}

View file

@ -1,11 +1,12 @@
package pinterest
import (
"encoding/json"
"fmt"
"govd/enums"
"govd/models"
"govd/util/parser"
"github.com/bytedance/sonic"
)
func ParseVideoObject(videoObj *Videos) ([]*models.MediaFormat, error) {
@ -48,7 +49,7 @@ func BuildPinRequestParams(pinID string) map[string]string {
},
}
jsonData, _ := json.Marshal(options)
jsonData, _ := sonic.ConfigFastest.Marshal(options)
return map[string]string{
"data": string(jsonData),
}

View file

@ -1,15 +1,15 @@
package reddit
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
)
var (
@ -255,13 +255,9 @@ func GetRedditData(host string, slug string) (RedditResponse, error) {
return GetRedditData(altHost, slug)
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var response RedditResponse
err = json.Unmarshal(body, &response)
decoder := sonic.ConfigFastest.NewDecoder(res.Body)
err = decoder.Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}

View file

@ -1,15 +1,15 @@
package tiktok
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
)
const (
@ -166,13 +166,10 @@ func GetVideoAPI(awemeID string) (*AwemeDetails, error) {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var data *Response
err = json.Unmarshal(body, &data)
decoder := sonic.ConfigFastest.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

View file

@ -1,7 +1,6 @@
package twitter
import (
"encoding/json"
"fmt"
"io"
"net/http"
@ -10,6 +9,8 @@ import (
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
)
const (
@ -168,13 +169,9 @@ func GetTweetAPI(tweetID string) (*Tweet, error) {
return nil, fmt.Errorf("invalid response code: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}
var apiResponse APIResponse
err = json.Unmarshal(body, &apiResponse)
decoder := sonic.ConfigFastest.NewDecoder(resp.Body)
err = decoder.Decode(&apiResponse)
if err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}

View file

@ -1,7 +1,6 @@
package twitter
import (
"encoding/json"
"fmt"
"govd/enums"
"govd/models"
@ -10,6 +9,8 @@ import (
"regexp"
"strconv"
"strings"
"github.com/bytedance/sonic"
)
const authToken = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
@ -75,8 +76,8 @@ func BuildAPIQuery(tweetID string) map[string]string {
"vibe_api_enabled": true,
}
variablesJSON, _ := json.Marshal(variables)
featuresJSON, _ := json.Marshal(features)
variablesJSON, _ := sonic.ConfigFastest.Marshal(variables)
featuresJSON, _ := sonic.ConfigFastest.Marshal(features)
return map[string]string{
"variables": string(variablesJSON),