code cleanup

This commit is contained in:
stefanodvx 2025-04-24 12:20:35 +02:00
parent 917be1687f
commit 5336968e05
34 changed files with 193 additions and 115 deletions

View file

@ -8,6 +8,8 @@ import (
"io"
"net/http"
"regexp"
"github.com/pkg/errors"
)
var instagramHost = []string{"instagram.com"}
@ -43,7 +45,7 @@ var Extractor = &models.Extractor{
MediaList: mediaList,
}, nil
}
return nil, fmt.Errorf("failed to extract media: all methods failed")
return nil, errors.New("failed to extract media: all methods failed")
},
}
@ -196,7 +198,7 @@ func GetFromIGram(
if err != nil {
return nil, fmt.Errorf("failed to build signed payload: %w", err)
}
req, err := http.NewRequest("POST", apiURL, payload)
req, err := http.NewRequest(http.MethodPost, apiURL, payload)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

View file

@ -5,9 +5,6 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"govd/enums"
"govd/models"
"govd/util"
"io"
"math/big"
"net/http"
@ -17,7 +14,12 @@ import (
"strings"
"time"
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
"github.com/pkg/errors"
"github.com/titanous/json5"
)
@ -63,11 +65,10 @@ func ParseGQLMedia(
caption = data.EdgeMediaToCaption.Edges[0].Node.Text
}
mediaType := data.Typename
contentID := ctx.MatchedContentID
contentURL := ctx.MatchedContentURL
switch mediaType {
switch data.Typename {
case "GraphVideo", "XDTGraphVideo":
media := ctx.Extractor.NewMedia(contentID, contentURL)
media.SetCaption(caption)
@ -139,17 +140,17 @@ func ParseEmbedGQL(
) (*Media, error) {
match := embedPattern.FindStringSubmatch(string(body))
if len(match) < 2 {
return nil, fmt.Errorf("failed to find JSON in response")
return nil, errors.New("failed to find JSON in response")
}
jsonData := match[1]
var data map[string]interface{}
var data map[string]any
if err := json5.Unmarshal([]byte(jsonData), &data); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
igCtx := util.TraverseJSON(data, "contextJSON")
if igCtx == nil {
return nil, fmt.Errorf("contextJSON not found in data")
return nil, errors.New("contextJSON not found in data")
}
var ctxJSON ContextJSON
switch v := igCtx.(type) {
@ -158,13 +159,13 @@ func ParseEmbedGQL(
return nil, fmt.Errorf("failed to unmarshal contextJSON: %w", err)
}
default:
return nil, fmt.Errorf("contextJSON is not a string")
return nil, errors.New("contextJSON is not a string")
}
if ctxJSON.GqlData == nil {
return nil, fmt.Errorf("gql_data is nil")
return nil, errors.New("gql_data is nil")
}
if ctxJSON.GqlData.ShortcodeMedia == nil {
return nil, fmt.Errorf("media is nil")
return nil, errors.New("media is nil")
}
return ctxJSON.GqlData.ShortcodeMedia, nil
}
@ -297,13 +298,13 @@ func GetGQLData(
return nil, fmt.Errorf("failed to parse response: %w", err)
}
if response.Data == nil {
return nil, fmt.Errorf("data is nil")
return nil, errors.New("data is nil")
}
if response.Status != "ok" {
return nil, fmt.Errorf("status is not ok: %s", response.Status)
}
if response.Data.ShortcodeMedia == nil {
return nil, fmt.Errorf("media is nil")
return nil, errors.New("media is nil")
}
return response.Data, nil
}

View file

@ -10,6 +10,7 @@ import (
"govd/util"
"github.com/bytedance/sonic"
"github.com/pkg/errors"
)
const (
@ -125,7 +126,7 @@ func GetPostData(postID string) (*Post, error) {
}
if response.Data == nil || response.Data.Post == nil {
return nil, fmt.Errorf("no post data found")
return nil, errors.New("no post data found")
}
return response.Data.Post, nil

View file

@ -25,17 +25,13 @@ type Media struct {
}
type Post struct {
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Type string `json:"type"`
Nsfw int `json:"nsfw"`
CreationTs int `json:"creationTs"`
GamFlagged bool `json:"gamFlagged"`
IsVoteMasked int `json:"isVoteMasked"`
HasLongPostCover int `json:"hasLongPostCover"`
Images map[string]*Media `json:"images"`
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Type string `json:"type"`
Nsfw int `json:"nsfw"`
Images map[string]*Media `json:"images"`
}
type Data struct {

View file

@ -6,6 +6,8 @@ import (
"govd/enums"
"govd/models"
"github.com/pkg/errors"
)
func FindBestPhoto(
@ -25,7 +27,7 @@ func FindBestPhoto(
}
if bestPhoto == nil {
return nil, fmt.Errorf("no photo found in post")
return nil, errors.New("no photo found in post")
}
return bestPhoto, nil
@ -47,7 +49,7 @@ func ParseVideoFormats(
}
}
if video == nil {
return nil, fmt.Errorf("no video found in post")
return nil, errors.New("no video found in post")
}
codecMapping := map[string]struct {

View file

@ -2,6 +2,7 @@ package pinterest
import (
"fmt"
"govd/enums"
"govd/models"
"govd/util/parser"

View file

@ -10,6 +10,7 @@ import (
"govd/util"
"github.com/bytedance/sonic"
"github.com/pkg/errors"
)
var (
@ -94,7 +95,7 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
}
if len(manifest) == 0 || len(manifest[0].Data.Children) == 0 {
return nil, fmt.Errorf("no data found in response")
return nil, errors.New("no data found in response")
}
data := manifest[0].Data.Children[0].Data
@ -115,11 +116,11 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
image := data.Preview.Images[0]
// check for video preview (GIF)
if data.Preview.RedditVideoPreview != nil {
if data.Preview.VideoPreview != nil {
formats, err := GetHLSFormats(
data.Preview.RedditVideoPreview.FallbackURL,
data.Preview.VideoPreview.FallbackURL,
image.Source.URL,
data.Preview.RedditVideoPreview.Duration,
data.Preview.VideoPreview.Duration,
)
if err != nil {
return nil, err
@ -189,12 +190,12 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
media.NSFW = true
}
var redditVideo *RedditVideo
var redditVideo *Video
if data.Media != nil && data.Media.RedditVideo != nil {
redditVideo = data.Media.RedditVideo
} else if data.SecureMedia != nil && data.SecureMedia.RedditVideo != nil {
redditVideo = data.SecureMedia.RedditVideo
if data.Media != nil && data.Media.Video != nil {
redditVideo = data.Media.Video
} else if data.SecureMedia != nil && data.SecureMedia.Video != nil {
redditVideo = data.SecureMedia.Video
}
if redditVideo != nil {
@ -230,7 +231,7 @@ func GetRedditData(
host string,
slug string,
raise bool,
) (RedditResponse, error) {
) (Response, error) {
url := fmt.Sprintf("https://%s/%s/.json", host, slug)
req, err := http.NewRequest(http.MethodGet, url, nil)
@ -266,7 +267,7 @@ func GetRedditData(
return GetRedditData(client, altHost, slug, true)
}
var response RedditResponse
var response Response
decoder := sonic.ConfigFastest.NewDecoder(res.Body)
err = decoder.Decode(&response)
if err != nil {

View file

@ -12,7 +12,7 @@ type ResponseItem struct {
Data *Data `json:"data"`
}
type RedditResponse []*ResponseItem
type Response []*ResponseItem
type PostData struct {
ID string `json:"id"`
@ -27,10 +27,10 @@ type PostData struct {
}
type Media struct {
RedditVideo *RedditVideo `json:"reddit_video"`
Video *Video `json:"reddit_video"`
}
type RedditVideo struct {
type Video struct {
FallbackURL string `json:"fallback_url"`
HLSURL string `json:"hls_url"`
DashURL string `json:"dash_url"`
@ -41,8 +41,8 @@ type RedditVideo struct {
}
type Preview struct {
Images []Image `json:"images"`
RedditVideoPreview *RedditVideoPreview `json:"reddit_video_preview"`
Images []Image `json:"images"`
VideoPreview *VideoPreview `json:"reddit_video_preview"`
}
type Image struct {
@ -64,7 +64,7 @@ type MP4Variant struct {
Source ImageSource `json:"source"`
}
type RedditVideoPreview struct {
type VideoPreview struct {
FallbackURL string `json:"fallback_url"`
Duration int64 `json:"duration"`
}

View file

@ -18,7 +18,7 @@ const (
)
var (
baseApiHeaders = map[string]string{
baseAPIHeaders = map[string]string{
"referer": "https://www.redgifs.com/",
"origin": "https://www.redgifs.com",
"content-type": "application/json",
@ -132,7 +132,7 @@ func GetVideo(
req.Header.Set("authorization", "Bearer "+token.AccessToken)
req.Header.Set("user-agent", token.Agent)
req.Header.Set("x-customheader", "https://www.redgifs.com/watch/"+videoID)
for k, v := range baseApiHeaders {
for k, v := range baseAPIHeaders {
req.Header.Set(k, v)
}
res, err := client.Do(req)

View file

@ -11,6 +11,7 @@ import (
"govd/util"
"github.com/bytedance/sonic"
"github.com/pkg/errors"
)
const (
@ -45,7 +46,7 @@ var ShortExtractor = &models.Extractor{
}
matchedURL := Extractor.URLPattern.FindStringSubmatch(string(body))
if matchedURL == nil {
return nil, fmt.Errorf("failed to find url in body")
return nil, errors.New("failed to find url in body")
}
return &models.ExtractorResponse{
URL: matchedURL[0],
@ -62,7 +63,6 @@ var Extractor = &models.Extractor{
Host: []string{
"twitter.com",
"x.com",
"vxx.com",
"vxtwitter.com",
},
@ -95,7 +95,7 @@ func MediaListFromAPI(ctx *models.DownloadContext) ([]*models.Media, error) {
} else if tweetData.Entities != nil && len(tweetData.Entities.Media) > 0 {
mediaEntities = tweetData.Entities.Media
} else {
return nil, fmt.Errorf("no media found in tweet")
return nil, nil
}
for _, mediaEntity := range mediaEntities {
@ -140,7 +140,7 @@ func GetTweetAPI(
}
headers := BuildAPIHeaders(cookies)
if headers == nil {
return nil, fmt.Errorf("failed to build headers. check cookies")
return nil, errors.New("failed to build headers. check cookies")
}
query := BuildAPIQuery(tweetID)

View file

@ -2,15 +2,17 @@ package twitter
import (
"fmt"
"govd/enums"
"govd/models"
"govd/util"
"net/http"
"regexp"
"strconv"
"strings"
"govd/enums"
"govd/models"
"govd/util"
"github.com/bytedance/sonic"
"github.com/pkg/errors"
)
const authToken = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
@ -137,7 +139,7 @@ func extractResolution(url string) (int64, int64) {
func FindTweetData(resp *APIResponse, tweetID string) (*Tweet, error) {
instructions := resp.Data.ThreadedConversationWithInjectionsV2.Instructions
if len(instructions) == 0 {
return nil, fmt.Errorf("tweet data missing")
return nil, errors.New("tweet data missing")
}
entries := instructions[0].Entries
@ -155,9 +157,9 @@ func FindTweetData(resp *APIResponse, tweetID string) (*Tweet, error) {
return result.Legacy, nil
}
return nil, fmt.Errorf("invalid tweet data")
return nil, errors.New("invalid tweet data")
}
}
return nil, fmt.Errorf("tweet not found")
return nil, errors.New("tweet not found")
}

View file

@ -2,10 +2,13 @@ package ext
import (
"fmt"
"govd/models"
"net/url"
"strings"
"sync"
"govd/models"
"github.com/pkg/errors"
)
var (
@ -37,7 +40,7 @@ func CtxByURL(urlStr string) (*models.DownloadContext, error) {
for redirectCount <= maxRedirects {
parsedURL, err := url.Parse(currentURL)
if err != nil {
return nil, fmt.Errorf("invalid URL: %v", err)
return nil, fmt.Errorf("invalid URL: %w", err)
}
host := strings.TrimPrefix(parsedURL.Host, "www.")
@ -87,7 +90,7 @@ func CtxByURL(urlStr string) (*models.DownloadContext, error) {
return nil, err
}
if response.URL == "" {
return nil, fmt.Errorf("no URL found in response")
return nil, errors.New("no URL found in response")
}
currentURL = response.URL