Skip to content

Commit

Permalink
banned words (#4)
Browse files Browse the repository at this point in the history
* banned words
  • Loading branch information
th0rn0 authored Sep 27, 2024
1 parent 7a07411 commit b075ce5
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 1 deletion.
49 changes: 49 additions & 0 deletions api/banned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import "github.com/zmb3/spotify/v2"

func containsBannedWord(word string) bool {
var bannedWords []BannedWord

result := db.Where("word LIKE ?", "%"+word+"%").Find(&bannedWords)

if result.Error != nil {
return false
}
if len(bannedWords) == 0 {
return false
}
return true
}

func addBannedWord(word string) (BannedWord, error) {
bannedWord := BannedWord{Word: word}
result := db.Create(&bannedWord)
if result.Error != nil {
return bannedWord, result.Error
}
return bannedWord, nil
}

func isBannedTrack(trackUri spotify.URI) bool {
var bannedTracks []BannedTrack

result := db.Where("track_uri LIKE ?", "%"+trackUri+"%").Find(&bannedTracks)

if result.Error != nil {
return false
}
if len(bannedTracks) == 0 {
return false
}
return true
}

func addBannedTrack(trackUri spotify.URI) (BannedTrack, error) {
bannedTrack := BannedTrack{TrackURI: trackUri}
result := db.Create(&bannedTrack)
if result.Error != nil {
return bannedTrack, result.Error
}
return bannedTrack, nil
}
47 changes: 47 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"bufio"
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -66,6 +69,50 @@ func init() {
db.AutoMigrate(&TrackImage{})
db.AutoMigrate(&Device{})
db.AutoMigrate(&LoginToken{})
db.AutoMigrate(&BannedWord{})
db.AutoMigrate(&BannedTrack{})

// Load Banned Words
bannedWordsFile, err := os.Open("../resources/banned/words.txt")
if err != nil {
panic(err)
}
defer bannedWordsFile.Close()

scannerBannedWords := bufio.NewScanner(bannedWordsFile)
for scannerBannedWords.Scan() {
if !containsBannedWord(scannerBannedWords.Text()) {
_, err := addBannedWord(fmt.Sprintf("%v", scannerBannedWords.Text()))
if err != nil {
logger.Fatal().Err(err).Msg("Cannot Set Banned Words")
}
}
}

if err := scannerBannedWords.Err(); err != nil {
log.Fatal(err)
}

// Load Banned Tracks
bannedTracksFile, err := os.Open("../resources/banned/tracks.txt")
if err != nil {
panic(err)
}
defer bannedTracksFile.Close()

scannerBannedTracks := bufio.NewScanner(bannedTracksFile)
for scannerBannedTracks.Scan() {
if !isBannedTrack(spotify.URI(fmt.Sprintf("%v", scannerBannedTracks.Text()))) {
_, err := addBannedTrack(spotify.URI(fmt.Sprintf("%v", scannerBannedTracks.Text())))
if err != nil {
logger.Fatal().Err(err).Msg("Cannot Set Banned Words")
}
}
}

if err := scannerBannedTracks.Err(); err != nil {
log.Fatal(err)
}

// Load Spotify API
auth = spotifyauth.New(
Expand Down
8 changes: 8 additions & 0 deletions api/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func handlePlayer(c *gin.Context) {
return
}
}
banQuery := c.Query("ban")
if banQuery == "true" {
_, err := addBannedTrack(currentTrackURI)
if err != nil {
c.JSON(http.StatusInternalServerError, err)
return
}
}
}

c.JSON(http.StatusAccepted, "Ok: "+action)
Expand Down
18 changes: 18 additions & 0 deletions api/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
)

func handleTrack(c *gin.Context) {
// var bannedWords = [2]string{"ram ranch", "fart"}
var bannedWords []BannedWord
var handleTrackInput HandleTrackInput
// var playerState *spotify.PlayerState
// var track Track

db.Find(&bannedWords)

ctx := c.Request.Context()
action := c.Param("action")

Expand All @@ -34,6 +38,20 @@ func handleTrack(c *gin.Context) {
c.JSON(http.StatusBadRequest, err)
return
}

// Check for Banned Filter
for _, artist := range track.Artists {
if containsBannedWord(artist.Name) {
c.JSON(http.StatusBadRequest, "Fuck off")
return
}
}

if containsBannedWord(track.Name) || isBannedTrack(track.URI) || track.Duration > 600000 {
c.JSON(http.StatusBadRequest, "Fuck off")
return
}

// Get Track Images
trackImages := []TrackImage{}
for _, image := range track.Album.Images {
Expand Down
13 changes: 12 additions & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ type HandlePlayerVolumeInput struct {
}

type HandleTrackInput struct {
URI spotify.URI `json:"uri"`
URI spotify.URI `json:"uri"`
Banned bool `json:"banned"`
}

type HandleVoteInput struct {
Expand Down Expand Up @@ -93,6 +94,16 @@ func (t *Track) BeforeDelete(tx *gorm.DB) (err error) {
return
}

type BannedWord struct {
ID uint `gorm:"primarykey"`
Word string `json:"word"`
}

type BannedTrack struct {
ID uint `gorm:"primarykey"`
TrackURI spotify.URI
}

type LoginToken struct {
AccessToken string `gorm:"primaryKey" json:"access_token"`
TokenType string `json:"token_type"`
Expand Down
1 change: 1 addition & 0 deletions resources/banned/tracks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spotify:track:6rsoBvxrlxdmqJyGPPciyq
5 changes: 5 additions & 0 deletions resources/banned/words.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fart
cum
ram ranch
orgasm
beats by talent

0 comments on commit b075ce5

Please sign in to comment.