Skip to content

Commit

Permalink
refactor: move sponsorblock in a different package
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Jun 3, 2023
1 parent 919e634 commit 01c200d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"github.com/TheTipo01/YADMB/queue"
"github.com/TheTipo01/YADMB/sponsorblock"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/lit"
"github.com/goccy/go-json"
Expand Down Expand Up @@ -86,7 +87,7 @@ func downloadAndPlay(s *discordgo.Session, guildID, link, user string, i *discor
case "youtube":
el.ID = ytdl.ID + "-" + ytdl.Extractor
// SponsorBlock is supported only on YouTube
el.Segments = getSegments(ytdl.ID)
el.Segments = sponsorblock.GetSegments(ytdl.ID)

// If the song is on YouTube, we also add it with its compact url, for faster parsing
db.AddToDb(el, false)
Expand Down
11 changes: 8 additions & 3 deletions sponsorblock.go → sponsorblock/sponsorblock.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sponsorblock

import (
"crypto/sha256"
Expand All @@ -9,8 +9,13 @@ import (
"time"
)

// Returns a map for skipping certain frames of a song
func getSegments(videoID string) map[int]bool {
const (
// How many DCA frames are needed for a second. It's not perfect, but good enough.
frameSeconds = 50.00067787
)

// GetSegments returns a map for skipping certain frames of a song
func GetSegments(videoID string) map[int]bool {
// Gets segments
req, _ := http.NewRequest("GET", "https://sponsor.ajay.app/api/skipSegments/"+hash(videoID)+"?categories=[\"sponsor\",\"music_offtopic\"]", nil) // Sets timeout to one second, as sometime i
client := http.Client{Timeout: time.Second}
Expand Down
4 changes: 2 additions & 2 deletions sponsorblock_test.go → sponsorblock/sponsorblock_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package sponsorblock

import "testing"

Expand All @@ -10,7 +10,7 @@ func TestHash(t *testing.T) {
}

func TestGetSegments(t *testing.T) {
result := getSegments("kJQP7kiw5Fk")
result := GetSegments("kJQP7kiw5Fk")
if result == nil || len(result) == 0 {
t.Error("Getting segments failed. Expected map, got nil")
}
Expand Down
10 changes: 10 additions & 0 deletions sponsorblock/structure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sponsorblock

// SponsorBlock holds data for segments of sponsors in YouTube video
type SponsorBlock []struct {
VideoID string `json:"videoID"`
Segments []struct {
Category string `json:"category"`
Segment []float64 `json:"segment"`
} `json:"segments"`
}
9 changes: 0 additions & 9 deletions structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ type RequestedFormats []struct {
Resolution string `json:"resolution"`
}

// SponsorBlock holds data for segments of sponsors in YouTube video
type SponsorBlock []struct {
VideoID string `json:"videoID"`
Segments []struct {
Category string `json:"category"`
Segment []float64 `json:"segment"`
} `json:"segments"`
}

// Config holds data parsed from the config.yml
type Config struct {
Token string `fig:"token" validate:"required"`
Expand Down

0 comments on commit 01c200d

Please sign in to comment.