clean code, add golangci config, preallocate some slices and avoid copying body before parsing json (#9)
This commit is contained in:
parent
0a146c515c
commit
c7a2612056
12 changed files with 49 additions and 45 deletions
|
@ -137,12 +137,12 @@ func GetEmbedMediaList(
|
|||
}
|
||||
|
||||
func GetIGramMediaList(ctx *models.DownloadContext) ([]*models.Media, error) {
|
||||
var mediaList []*models.Media
|
||||
postURL := ctx.MatchedContentURL
|
||||
details, err := GetFromIGram(ctx, postURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get post: %w", err)
|
||||
}
|
||||
mediaList := make([]*models.Media, 0, len(details.Items))
|
||||
for _, item := range details.Items {
|
||||
media := ctx.Extractor.NewMedia(
|
||||
ctx.MatchedContentID,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package instagram
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
@ -137,14 +138,14 @@ func ParseGQLMedia(
|
|||
func ParseEmbedGQL(
|
||||
body []byte,
|
||||
) (*Media, error) {
|
||||
match := embedPattern.FindStringSubmatch(string(body))
|
||||
match := embedPattern.FindSubmatch(body)
|
||||
if len(match) < 2 {
|
||||
return nil, errors.New("failed to find JSON in response")
|
||||
}
|
||||
jsonData := match[1]
|
||||
|
||||
var data map[string]any
|
||||
if err := json5.Unmarshal([]byte(jsonData), &data); err != nil {
|
||||
if err := json5.Unmarshal(jsonData, &data); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
|
||||
}
|
||||
igCtx := util.TraverseJSON(data, "contextJSON")
|
||||
|
@ -193,39 +194,27 @@ func BuildIGramPayload(contentURL string) (io.Reader, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshalling payload: %w", err)
|
||||
}
|
||||
reader := strings.NewReader(string(parsedPayload))
|
||||
reader := bytes.NewReader(parsedPayload)
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
func ParseIGramResponse(body []byte) (*IGramResponse, error) {
|
||||
var rawResponse any
|
||||
// try to unmarshal as a single IGramMedia and then as a slice
|
||||
var media IGramMedia
|
||||
|
||||
if err := sonic.ConfigFastest.Unmarshal(body, &rawResponse); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response1: %w", err)
|
||||
}
|
||||
|
||||
switch rawResponse.(type) {
|
||||
case []any:
|
||||
// array of IGramMedia
|
||||
var media []*IGramMedia
|
||||
if err := sonic.ConfigFastest.Unmarshal(body, &media); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response2: %w", err)
|
||||
if err := sonic.ConfigFastest.Unmarshal(body, &media); err != nil {
|
||||
// try with slice
|
||||
var mediaList []*IGramMedia
|
||||
if err := sonic.ConfigFastest.Unmarshal(body, &mediaList); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
return &IGramResponse{
|
||||
Items: media,
|
||||
Items: mediaList,
|
||||
}, nil
|
||||
case map[string]any:
|
||||
// single IGramMedia
|
||||
var media IGramMedia
|
||||
if err := sonic.ConfigFastest.Unmarshal(body, &media); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response3: %w", err)
|
||||
}
|
||||
return &IGramResponse{
|
||||
Items: []*IGramMedia{&media},
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected response type: %T", rawResponse)
|
||||
}
|
||||
return &IGramResponse{
|
||||
Items: []*IGramMedia{&media},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func GetCDNURL(contentURL string) (string, error) {
|
||||
|
|
|
@ -36,7 +36,6 @@ func FindBestPhoto(
|
|||
func ParseVideoFormats(
|
||||
images map[string]*Media,
|
||||
) ([]*models.MediaFormat, error) {
|
||||
var formats []*models.MediaFormat
|
||||
var video *Media
|
||||
var thumbnailURL string
|
||||
|
||||
|
@ -63,6 +62,8 @@ func ParseVideoFormats(
|
|||
"av1Url": {"Av1URL", enums.MediaCodecAV1},
|
||||
}
|
||||
|
||||
formats := make([]*models.MediaFormat, 0, len(codecMapping))
|
||||
|
||||
for _, mapping := range codecMapping {
|
||||
url := getField(video, mapping.Field)
|
||||
if url == "" {
|
||||
|
|
|
@ -31,6 +31,7 @@ func ParseVideoObject(videoObj *Videos) ([]*models.MediaFormat, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to extract hls formats: %w", err)
|
||||
}
|
||||
formats = make([]*models.MediaFormat, 0, len(hlsFormats))
|
||||
for _, hlsFormat := range hlsFormats {
|
||||
hlsFormat.Duration = video.Duration / 1000
|
||||
hlsFormat.Thumbnail = []string{video.Thumbnail}
|
||||
|
|
|
@ -176,7 +176,7 @@ func GetVideoAPI(
|
|||
decoder := sonic.ConfigFastest.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(&data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
videoData, err := FindVideoData(data, awemeID)
|
||||
if err != nil {
|
||||
|
|
|
@ -45,12 +45,12 @@ var ShortExtractor = &models.Extractor{
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read body: %w", err)
|
||||
}
|
||||
matchedURL := Extractor.URLPattern.FindStringSubmatch(string(body))
|
||||
matchedURL := Extractor.URLPattern.FindSubmatch(body)
|
||||
if matchedURL == nil {
|
||||
return nil, errors.New("failed to find url in body")
|
||||
}
|
||||
return &models.ExtractorResponse{
|
||||
URL: matchedURL[0],
|
||||
URL: string(matchedURL[0]),
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
@ -174,13 +174,9 @@ func GetTweetAPI(
|
|||
if resp.StatusCode != http.StatusOK {
|
||||
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 = sonic.ConfigFastest.Unmarshal(body, &apiResponse)
|
||||
err = sonic.ConfigFastest.NewDecoder(resp.Body).Decode(&apiResponse)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse response: %w", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue