-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c0bcfe
commit b86bb23
Showing
4 changed files
with
163 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package model | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/boggydigital/yt_urls" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Playlist struct { | ||
Id int64 `gorm:"primaryKey" json:"id"` | ||
PlaylistId string `gorm:"-" json:"playlist_id"` | ||
Name string `json:"name"` | ||
Url string `json:"url"` | ||
Downloaded bool `json:"downloaded"` | ||
} | ||
|
||
func (Playlist) TableName() string { | ||
return "tplaylist" | ||
} | ||
|
||
func (Playlist) DefaultFilter(db *gorm.DB) *gorm.DB { | ||
return db | ||
} | ||
|
||
func (Playlist) List(db *gorm.DB, scopes ...func(*gorm.DB) *gorm.DB) (any, error) { | ||
var data []Playlist | ||
rs := db.Scopes(scopes...).Find(&data) | ||
if rs.Error != nil { | ||
return nil, rs.Error | ||
} | ||
|
||
retval := []Playlist{} | ||
for _, item := range data { | ||
playlist_id, err := yt_urls.PlaylistId(item.Url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = os.Stat("/data/icecast/playlists/" + playlist_id) | ||
if !os.IsNotExist(err) { | ||
item.Downloaded = true | ||
} | ||
|
||
item.PlaylistId = playlist_id | ||
retval = append(retval, item) | ||
} | ||
|
||
return retval, nil | ||
} | ||
|
||
func (Playlist) Get(db *gorm.DB, id int64) (any, error) { | ||
var data Playlist | ||
rs := db.First(&data, id) | ||
return data, rs.Error | ||
} | ||
|
||
func (obj Playlist) Update(db *gorm.DB, id int64, body []byte) (any, error) { | ||
model := Playlist{ | ||
Id: id, | ||
} | ||
|
||
var payload Playlist | ||
err := json.Unmarshal(body, &payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Model(&model).Updates(payload) | ||
if rs.Error != nil { | ||
return nil, err | ||
} | ||
|
||
return obj.Get(db, id) | ||
} | ||
|
||
func (Playlist) Create(db *gorm.DB, body []byte) (any, error) { | ||
var payload Playlist | ||
err := json.Unmarshal(body, &payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Create(&payload) | ||
if rs.Error != nil { | ||
return nil, err | ||
} | ||
|
||
return payload, nil | ||
} | ||
|
||
func (obj Playlist) Delete(db *gorm.DB, id int64) (any, error) { | ||
data, err := obj.Get(db, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rs := db.Delete(&Playlist{}, id) | ||
if rs.Error != nil { | ||
return nil, rs.Error | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func M3u(req *Map, res *Map) error { | ||
playlist_id, err := req.GetString("id") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pwd := "/data/icecast/playlists/" + playlist_id | ||
_, err = os.Stat(pwd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entries, err := os.ReadDir(pwd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := os.Create("/data/icecast/playlist.m3u") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
for _, e := range entries { | ||
fmt.Fprintln(f, "/data/icecast/playlists/"+playlist_id+"/"+e.Name()) | ||
} | ||
|
||
return nil | ||
} |