mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
back end done for video archiving
This commit is contained in:
15
server/webserver/routes/config.go
Normal file
15
server/webserver/routes/config.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
)
|
||||
|
||||
// AddConfigRoutes -
|
||||
func AddConfigRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/config/client_id", getClientIDHandler)
|
||||
}
|
||||
|
||||
func getClientIDHandler(c *gin.Context) {
|
||||
c.JSON(200, map[string]string{"id": config.Config.ClientID})
|
||||
}
|
||||
75
server/webserver/routes/downloader.go
Normal file
75
server/webserver/routes/downloader.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddDownloaderRoutes -
|
||||
func AddDownloaderRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/ytdownloader", downloaderHandler)
|
||||
}
|
||||
|
||||
func downloaderHandler(c *gin.Context) {
|
||||
url := c.Query("url")
|
||||
fileType := c.Query("fileType")
|
||||
|
||||
// create youtube folder if it does not exist
|
||||
if _, err := os.Stat(config.Config.YoutubePath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.YoutubePath, os.ModePerm)
|
||||
}
|
||||
|
||||
// get the video title
|
||||
titleCmd := exec.Command("youtube-dl", "--get-title", url)
|
||||
var titleOut bytes.Buffer
|
||||
titleCmd.Stdout = &titleOut
|
||||
|
||||
err := titleCmd.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(400, err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO add video id to tile to not get collisions
|
||||
|
||||
// ------------------------------------------------
|
||||
|
||||
// remove all special characters from title
|
||||
cleanTitle := cleanseTitle(titleOut.String())
|
||||
log.Debug(cleanTitle)
|
||||
|
||||
cmd := exec.Command("youtube-dl", "-x", "--audio-format", "mp3", "-o", config.Config.YoutubePath+"/"+cleanTitle+".%(ext)s", url)
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
|
||||
err = cmd.Run()
|
||||
|
||||
if err != nil {
|
||||
log.Error(out.String())
|
||||
log.Error(err)
|
||||
c.JSON(400, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, map[string]interface{}{"fileName": cleanTitle + "." + fileType})
|
||||
}
|
||||
|
||||
func cleanseTitle(title string) string {
|
||||
|
||||
// Make a Regex to say we only want
|
||||
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
return reg.ReplaceAllString(title, "")
|
||||
}
|
||||
42
server/webserver/routes/logger.go
Normal file
42
server/webserver/routes/logger.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/logger"
|
||||
)
|
||||
|
||||
// AddLoggerRoutes -
|
||||
func AddLoggerRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/logger/messages", getMessagesHandler)
|
||||
group.GET("/logger/linkedmessages", getLinkedMessagesHandler)
|
||||
}
|
||||
|
||||
func getMessagesHandler(c *gin.Context) {
|
||||
page, err := strconv.Atoi(c.Query("page"))
|
||||
|
||||
if err != nil {
|
||||
page = 0
|
||||
}
|
||||
|
||||
messages, err := logger.GetMessages(page)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, messages)
|
||||
}
|
||||
|
||||
func getLinkedMessagesHandler(c *gin.Context) {
|
||||
posts, err := logger.GetLinkedMessages()
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, posts)
|
||||
}
|
||||
61
server/webserver/routes/oauth.go
Normal file
61
server/webserver/routes/oauth.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const cashGuildID = "101198129352691712"
|
||||
|
||||
type oauthReq struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// AddOauthRoutes -
|
||||
func AddOauthRoutes(group *gin.RouterGroup) {
|
||||
group.POST("/oauth", oauthHandler)
|
||||
}
|
||||
|
||||
func oauthHandler(c *gin.Context) {
|
||||
|
||||
var json oauthReq
|
||||
|
||||
err := c.ShouldBindJSON(&json)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// get users oauth code
|
||||
oauth, err := discord.Oauth(json.Code)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// verify and grab user information
|
||||
user, err := discord.GetUserInfo(oauth.AccessToken)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// generate json web token
|
||||
token, err := middleware.GetJWT(user)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, token)
|
||||
}
|
||||
76
server/webserver/routes/soundlist.go
Normal file
76
server/webserver/routes/soundlist.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type sound struct {
|
||||
Prefix string `json:"prefix"`
|
||||
Name string `json:"name"`
|
||||
Extension string `json:"extension"`
|
||||
}
|
||||
|
||||
// AddSoundListRoutes -
|
||||
func AddSoundListRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/soundlist", soundListHandler)
|
||||
group.GET("/cliplist", clipListHandler)
|
||||
}
|
||||
|
||||
func soundListHandler(c *gin.Context) {
|
||||
|
||||
soundList, err := readSoundsDir(config.Config.SoundsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, soundList)
|
||||
}
|
||||
|
||||
func clipListHandler(c *gin.Context) {
|
||||
|
||||
clipList, err := readSoundsDir(config.Config.ClipsPath)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, clipList)
|
||||
}
|
||||
|
||||
func readSoundsDir(dir string) ([]sound, error) {
|
||||
|
||||
soundList := []sound{}
|
||||
|
||||
files, err := ioutil.ReadDir(dir)
|
||||
|
||||
if err != nil {
|
||||
return soundList, err
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileName := strings.Split(f.Name(), ".")[0]
|
||||
extension := strings.Split(f.Name(), ".")[1]
|
||||
|
||||
listItem := sound{
|
||||
Name: fileName,
|
||||
Extension: extension,
|
||||
Prefix: config.Config.BotPrefix,
|
||||
}
|
||||
|
||||
soundList = append(soundList, listItem)
|
||||
}
|
||||
|
||||
return soundList, nil
|
||||
}
|
||||
58
server/webserver/routes/upload.go
Normal file
58
server/webserver/routes/upload.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AddUploadRoutes - add file upload routes
|
||||
func AddUploadRoutes(group *gin.RouterGroup) {
|
||||
group.POST("/upload", middleware.AuthorizedJWT(), fileUploadHandler)
|
||||
}
|
||||
|
||||
func fileUploadHandler(c *gin.Context) {
|
||||
|
||||
// originalClaims, _ := c.Get("claims")
|
||||
// claims, _ := originalClaims.(*middleware.CustomClaims)
|
||||
// TODO: verify user for upload
|
||||
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error reading file.")
|
||||
return
|
||||
}
|
||||
|
||||
// create uploads folder if it does not exist
|
||||
if _, err := os.Stat(config.Config.SoundsPath); os.IsNotExist(err) {
|
||||
os.Mkdir(config.Config.SoundsPath, os.ModePerm)
|
||||
}
|
||||
|
||||
// convert file name to lower case and trim spaces
|
||||
file.Filename = strings.ToLower(file.Filename)
|
||||
file.Filename = strings.Replace(file.Filename, " ", "", -1)
|
||||
|
||||
// check if file already exists
|
||||
if _, err := os.Stat(config.Config.SoundsPath + "/" + file.Filename); err == nil {
|
||||
c.JSON(http.StatusInternalServerError, "File already exists.")
|
||||
return
|
||||
}
|
||||
|
||||
err = c.SaveUploadedFile(file, config.Config.SoundsPath+"/"+file.Filename)
|
||||
log.Debug("Saving file", config.Config.SoundsPath+"/"+file.Filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(http.StatusInternalServerError, "Error creating file.")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "Success")
|
||||
}
|
||||
104
server/webserver/routes/video_archive.go
Normal file
104
server/webserver/routes/video_archive.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/db"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/model"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/response"
|
||||
"github.com/rylio/ytdl"
|
||||
)
|
||||
|
||||
// AddVideoArchiveRoutes -
|
||||
func AddVideoArchiveRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/video-archives", listVideoArchivesHandler)
|
||||
|
||||
authGroup := group.Group("", middleware.AuthorizedJWT())
|
||||
authGroup.POST("/video-archives", middleware.AuthPermissions(middleware.PermMod), postVideoArchivesHandler)
|
||||
authGroup.DELETE("/video-archives/:id", middleware.AuthPermissions(middleware.PermAdmin), deleteVideoArchivesHandler)
|
||||
}
|
||||
|
||||
func listVideoArchivesHandler(c *gin.Context) {
|
||||
archives, err := model.VideoArchiveList(db.GetConn())
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, archives)
|
||||
}
|
||||
|
||||
func deleteVideoArchivesHandler(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
response.BadRequest(c, "Invalid ID")
|
||||
return
|
||||
}
|
||||
|
||||
err := model.VideoArchiveDelete(db.GetConn(), id)
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, "deleted")
|
||||
}
|
||||
|
||||
func postVideoArchivesHandler(c *gin.Context) {
|
||||
params := struct {
|
||||
URL string `json:"url"`
|
||||
}{}
|
||||
|
||||
c.BindJSON(¶ms)
|
||||
|
||||
if params.URL == "" {
|
||||
response.BadRequest(c, "URL Required")
|
||||
return
|
||||
}
|
||||
|
||||
cl, _ := c.Get("claims")
|
||||
claims, ok := cl.(*middleware.CustomClaims)
|
||||
|
||||
if !ok {
|
||||
response.InternalError(c, errors.New("Claims error"))
|
||||
return
|
||||
}
|
||||
|
||||
info, err := ytdl.GetVideoInfo(params.URL)
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// if title and author are blank
|
||||
if info.Title == "" && info.Author == "" {
|
||||
response.BadRequest(c, "Invalid URL")
|
||||
return
|
||||
}
|
||||
|
||||
videoArchive := model.VideoArchive{
|
||||
Author: info.Author,
|
||||
DatePublished: info.DatePublished,
|
||||
Description: info.Description,
|
||||
Duration: int(info.Duration.Seconds()),
|
||||
Title: info.Title,
|
||||
URL: params.URL,
|
||||
YoutubeID: info.ID,
|
||||
UploadedBy: claims.Username,
|
||||
}
|
||||
|
||||
err = model.VideoArchiveSave(db.GetConn(), &videoArchive)
|
||||
|
||||
if err != nil {
|
||||
response.InternalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, "saved")
|
||||
}
|
||||
Reference in New Issue
Block a user