Implemented music page

This commit is contained in:
Eduard Urbach 2017-06-27 16:23:57 +02:00
parent c32166f38c
commit 50cd062140
8 changed files with 45 additions and 159 deletions

View File

@ -1,19 +0,0 @@
package auth
import (
"encoding/json"
"io/ioutil"
"github.com/animenotifier/arn"
)
var apiKeys arn.APIKeys
func init() {
data, _ := ioutil.ReadFile("security/api-keys.json")
err := json.Unmarshal(data, &apiKeys)
if err != nil {
panic(err)
}
}

View File

@ -29,8 +29,8 @@ type GoogleUser struct {
// InstallGoogleAuth enables Google login for the app.
func InstallGoogleAuth(app *aero.Application) {
config := &oauth2.Config{
ClientID: apiKeys.Google.ID,
ClientSecret: apiKeys.Google.Secret,
ClientID: arn.APIKeys.Google.ID,
ClientSecret: arn.APIKeys.Google.Secret,
RedirectURL: "https://" + app.Config.Domain + "/auth/google/callback",
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",

View File

@ -1,13 +1,9 @@
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"syscall"
@ -21,30 +17,8 @@ var discord *discordgo.Session
func main() {
var err error
exe, err := os.Executable()
if err != nil {
panic(err)
}
dir := path.Dir(exe)
var apiKeysPath string
apiKeysPath, err = filepath.Abs(dir + "/../../security/api-keys.json")
if err != nil {
panic(err)
}
var apiKeys arn.APIKeys
data, _ := ioutil.ReadFile(apiKeysPath)
err = json.Unmarshal(data, &apiKeys)
if err != nil {
panic(err)
}
discord, _ = discordgo.New()
discord.Token = "Bot " + apiKeys.Discord.Token
discord.Token = "Bot " + arn.APIKeys.Discord.Token
// Verify a Token was provided
if discord.Token == "" {

View File

@ -46,9 +46,15 @@ func sync(data *kitsu.Anime) {
anime.EpisodeCount = attr.EpisodeCount
anime.EpisodeLength = attr.EpisodeLength
anime.Status = attr.Status
anime.NSFW = attr.Nsfw
anime.Summary = arn.FixAnimeDescription(attr.Synopsis)
// NSFW
if attr.Nsfw {
anime.NSFW = 1
} else {
anime.NSFW = 0
}
// Rating
overall, convertError := strconv.ParseFloat(attr.AverageRating, 64)
@ -59,10 +65,10 @@ func sync(data *kitsu.Anime) {
anime.Rating.Overall = overall
// Trailers
anime.Trailers = []arn.ExternalMedia{}
anime.Trailers = []*arn.ExternalMedia{}
if attr.YoutubeVideoID != "" {
anime.Trailers = append(anime.Trailers, arn.ExternalMedia{
anime.Trailers = append(anime.Trailers, &arn.ExternalMedia{
Service: "Youtube",
ServiceID: attr.YoutubeVideoID,
})

View File

@ -2,7 +2,6 @@ package middleware
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
@ -15,17 +14,6 @@ import (
"github.com/parnurzeal/gorequest"
)
var apiKeys arn.APIKeys
func init() {
data, _ := ioutil.ReadFile("security/api-keys.json")
err := json.Unmarshal(data, &apiKeys)
if err != nil {
panic(err)
}
}
// UserInfo updates user related information after each request.
func UserInfo() aero.Middleware {
return func(ctx *aero.Context, next func()) {
@ -84,7 +72,7 @@ func updateUserInfo(ctx *aero.Context, user *arn.User) {
// Updates the location of the user.
func updateUserLocation(user *arn.User, newIP string) {
user.IP = newIP
locationAPI := "https://api.ipinfodb.com/v3/ip-city/?key=" + apiKeys.IPInfoDB.ID + "&ip=" + user.IP + "&format=json"
locationAPI := "https://api.ipinfodb.com/v3/ip-city/?key=" + arn.APIKeys.IPInfoDB.ID + "&ip=" + user.IP + "&format=json"
response, data, err := gorequest.New().Get(locationAPI).EndBytes()

View File

@ -1,6 +1,7 @@
package music
import (
"net/http"
"sort"
"github.com/aerogo/aero"
@ -12,105 +13,19 @@ const maxTracks = 10
// Get renders the music page.
func Get(ctx *aero.Context) string {
tracks := []*arn.SoundTrack{}
tracks, err := arn.AllSoundTracks()
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "145918628",
},
},
Tags: []string{
"anime:2357",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "127672476",
},
},
Tags: []string{
"anime:7622",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "270777538",
},
},
Tags: []string{
"anime:11469",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "243839100",
},
},
Tags: []string{
"anime:9962",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "207355237",
},
},
Tags: []string{
"anime:6589",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
tracks = append(tracks, &arn.SoundTrack{
ID: arn.GenerateID("SoundTrack"),
Media: []arn.ExternalMedia{
arn.ExternalMedia{
Service: "Soundcloud",
ServiceID: "242172944",
},
},
Tags: []string{
"anime:10740",
},
Created: arn.DateTimeUTC(),
CreatedBy: "4J6qpK1ve",
})
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Error fetching soundtracks", err)
}
sort.Slice(tracks, func(i, j int) bool {
return tracks[i].Created > tracks[j].Created
})
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
}
return ctx.HTML(components.Music(tracks))
}

View File

@ -1,8 +1,8 @@
component NewSoundTrack(user *arn.User)
.widgets
.widget
input#soundcloud-link.widget-element(type="text", placeholder="Soundcloud link or ID")
input#anime-link.widget-element(type="text", placeholder="Anime link or ID")
input#soundcloud-link.widget-element(type="text", placeholder="Soundcloud link")
input#anime-link.widget-element(type="text", placeholder="Anime link")
input#osu-link.widget-element(type="text", placeholder="Osu beatmap link (optional)")
button.action(data-action="createSoundTrack", data-trigger="click")

View File

@ -103,6 +103,28 @@ export function createThread(arn: AnimeNotifier) {
.catch(console.error)
}
// Create soundtrack
export function createSoundTrack(arn: AnimeNotifier, button: HTMLButtonElement) {
let soundcloud = arn.app.find("soundcloud-link") as HTMLInputElement
let anime = arn.app.find("anime-link") as HTMLInputElement
let osu = arn.app.find("osu-link") as HTMLInputElement
let soundtrack = {
soundcloud: soundcloud.value,
tags: [anime.value, osu.value],
}
button.innerText = "Adding..."
button.disabled = true
arn.post("/api/soundtrack/new", soundtrack)
.then(() => arn.app.load("/music"))
.catch(err => {
console.error(err)
arn.reloadContent()
})
}
// Search
export function search(arn: AnimeNotifier, search: HTMLInputElement, e: KeyboardEvent) {
if(e.ctrlKey || e.altKey) {