package twitch import ( "safetwitch-backend/extractor" "safetwitch-backend/extractor/structs" "github.com/tidwall/gjson" ) func GetSearchResult(query string) (structs.SearchResult, error) { payload := []TwitchPayload{ { "operationName": "SearchResultsPage_SearchResults", "variables": map[string]interface{}{ "query": query, "options": nil, "requestID": "75948144-d051-4203-8511-57f3ee9b809a", }, "extensions": map[string]interface{}{ "persistedQuery": map[string]interface{}{ "version": 1, "sha256Hash": "6ea6e6f66006485e41dbe3ebd69d5674c5b22896ce7b595d7fce6411a3790138", }, }, }, } _, body, err := parseResponse(payload) if err != nil { return structs.SearchResult{}, err } rawStreamers := gjson.Get(string(body), "0.data.searchFor.channels.edges") parsedStreamers := []structs.Streamer{} for _, streamer := range rawStreamers.Array() { stream := streamer.Get("item.stream").String() parsedStreamers = append(parsedStreamers, structs.Streamer{ Username: streamer.Get("item.login").String(), Followers: int(streamer.Get("item.followers.totalCount").Int()), IsLive: !(stream == ""), About: streamer.Get("item.description").String(), Pfp: extractor.ProxyUrl(streamer.Get("item.profileImageURL").String()), IsPartner: false, ColorHex: nil, Id: streamer.Get("item.channel.id").String(), }) } parsedCategories := []structs.CategoryPreview{} rawCategories := gjson.Get(string(body), "0.data.searchFor.games.edges") for _, category := range rawCategories.Array() { var tags []string for _, tag := range category.Get("item.tags").Array() { tags = append(tags, tag.Get("tagName").String()) } parsedCategories = append(parsedCategories, structs.CategoryPreview{ Name: category.Get("item.name").String(), DisplayName: category.Get("item.displayName").String(), Viewers: int(category.Get("item.viewersCount").Int()), Image: category.Get("item.boxArtURL").String(), Tags: tags, }) } foundRelatedLiveChannels := []structs.Streamer{} streams := gjson.Get(string(body), "0.data.searchFor.relatedLiveChannels.edges") for _, channel := range streams.Array() { name := channel.Get("item.stream.broadcaster.login").String() channel, err := GetStreamerInfo(name) if err != nil { return structs.SearchResult{}, nil } foundRelatedLiveChannels = append(foundRelatedLiveChannels, channel) } foundChannelsWithTag := []structs.Streamer{} streams = gjson.Get(string(body), "0.data.searchFor.channelsWithTag.edges") for _, stream := range streams.Array() { foundChannelsWithTag = append(foundChannelsWithTag, structs.Streamer{ Username: stream.Get("item.login").String(), Followers: int(stream.Get("item.followers.totalCount").Int()), IsLive: !(stream.Get("stream").String() != ""), About: stream.Get("item.description").String(), Pfp: extractor.ProxyUrl(stream.Get("item.profileImageURL").String()), IsPartner: false, ColorHex: nil, Id: stream.Get("item.channel.id").String(), }) } final := structs.SearchResult{ Channels: parsedStreamers, Categories: parsedCategories, RelatedLiveChannels: foundRelatedLiveChannels, ChannelsWithTag: foundChannelsWithTag, } return final, nil }