Replace nginx with flask server #49

This commit is contained in:
Mungai Geoffrey 2022-05-04 02:44:43 +03:00 committed by GitHub
commit e05c4602b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 80 additions and 33 deletions

View File

@ -44,6 +44,9 @@ def get_album_tracks():
index = albumslib.find_album(album, artist)
album = api.ALBUMS[index]
album.count = len(songs)
album.duration = albumslib.get_album_duration(songs)
return {"songs": songs, "info": album}

View File

@ -1,4 +1,4 @@
import os
from os import path
from typing import Tuple
from flask import Flask
@ -8,14 +8,15 @@ app = Flask(__name__)
def join(*args: Tuple[str]) -> str:
return os.path.join(*args)
return path.join(*args)
HOME = os.path.expanduser("~")
ROOT_PATH = os.path.join(HOME, ".alice", "images")
HOME = path.expanduser("~")
ROOT_PATH = path.join(HOME, ".alice", "images")
THUMB_PATH = join(ROOT_PATH, "thumbnails")
ARTIST_PATH = join(ROOT_PATH, "artists")
PLAYLIST_PATH = join(ROOT_PATH, "playlists")
@app.route("/")
@ -23,28 +24,42 @@ def hello():
return "Hello mf"
@app.route("/thumb/<path>")
def send_thumbnail(path: str):
fpath = join(THUMB_PATH, path)
exists = os.path.exists(fpath)
@app.route("/t/<imgpath>")
def send_thumbnail(imgpath: str):
fpath = join(THUMB_PATH, imgpath)
exists = path.exists(fpath)
if exists:
return send_from_directory(THUMB_PATH, path)
return send_from_directory(THUMB_PATH, imgpath)
return {"msg": "Not found"}, 404
@app.route("/artist/<path>")
def send_artist_image(path: str):
@app.route("/a/<imgpath>")
def send_artist_image(imgpath: str):
print(ARTIST_PATH)
fpath = join(ARTIST_PATH, path)
exists = os.path.exists(fpath)
fpath = join(ARTIST_PATH, imgpath)
exists = path.exists(fpath)
if exists:
return send_from_directory(ARTIST_PATH, path)
return send_from_directory(ARTIST_PATH, imgpath)
return {"msg": "Not found"}, 404
@app.route("/p/<imgpath>")
def send_playlist_image(imgpath: str):
fpath = join(PLAYLIST_PATH, imgpath)
exists = path.exists(fpath)
if exists:
return send_from_directory(PLAYLIST_PATH, imgpath)
return {"msg": "Not found"}, 404
# TODO
# Return Fallback images instead of JSON
if __name__ == "__main__":
app.run(threaded=True, port=9877)

View File

@ -77,7 +77,7 @@ def find_album(albumtitle: str, artist: str) -> int or None:
return None
def get_album_duration(album: list) -> int:
def get_album_duration(album: List[models.Track]) -> int:
"""
Gets the duration of an album.
"""
@ -85,7 +85,7 @@ def get_album_duration(album: list) -> int:
album_duration = 0
for track in album:
album_duration += track["length"]
album_duration += track.length
return album_duration
@ -150,8 +150,6 @@ def create_album(track) -> models.Album:
album_tracks = get_album_tracks(album["album"], album["artist"])
album["count"] = len(album_tracks)
album["duration"] = get_album_duration(album_tracks)
album["date"] = album_tracks[0]["date"]
album["artistimage"] = urllib.parse.quote_plus(

View File

@ -56,17 +56,15 @@ class Album:
title: str
artist: str
count: int
duration: int
date: int
artistimage: str
image: str
count: int = 0
duration: int = 0
def __init__(self, tags):
self.title = tags["album"]
self.artist = tags["artist"]
self.count = tags["count"]
self.duration = tags["duration"]
self.date = tags["date"]
self.artistimage = tags["artistimage"]
self.image = tags["image"]
@ -128,9 +126,9 @@ class Playlist:
def create_img_link(self, image: str):
if image:
return settings.IMG_PLAYLIST_URI + image
return image
return settings.IMG_PLAYLIST_URI + "default.webp"
return "default.webp"
def update_count(self):
self.count = len(self._pre_tracks)

View File

@ -5,7 +5,7 @@
<div
class="image shadow-lg"
:style="{
backgroundImage: `url(&quot;${props.album.image}&quot;)`,
backgroundImage: `url(&quot;${imguri + props.album.image}&quot;)`,
}"
></div>
</div>
@ -32,7 +32,9 @@ import perks from "../../composables/perks.js";
import { AlbumInfo } from "../../interfaces.js";
import PlayBtnRect from "../shared/PlayBtnRect.vue";
import { playSources } from "../../composables/enums";
import { paths } from "../../config";
const imguri = paths.images.thumb
const props = defineProps<{
album: AlbumInfo;
}>();

View File

@ -6,7 +6,7 @@
<div
class="l-image image rounded"
:style="{
backgroundImage: `url(&quot;${track.image}&quot;)`,
backgroundImage: `url(&quot;${imguri + track.image}&quot;)`,
}"
></div>
</div>
@ -36,6 +36,9 @@
<script setup lang="ts">
import perks from "../../composables/perks";
import { Track } from "../../interfaces";
import { paths } from "../../config";
const imguri = paths.images.thumb
const putCommas = perks.putCommas;
const props = defineProps<{

View File

@ -3,7 +3,7 @@
class="p-header image"
:style="[
{
backgroundImage: `url(${props.info.image})`,
backgroundImage: `url(${imguri + props.info.image})`,
},
]"
>
@ -45,7 +45,9 @@ import useModalStore from "../../stores/modal";
import Option from "../shared/Option.vue";
import pContext from "../../contexts/playlist";
import useContextStore from "../../stores/context";
import { paths } from "../../config";
const imguri = paths.images.playlist
const context = useContextStore();
const modal = useModalStore();

View File

@ -1,11 +1,11 @@
<template>
<div class="main-item border" @click="playNext">
<div class="h">Up Next</div>
<div class="itemx shadow">
<div class="itemx shadow">
<div
class="album-art image"
:style="{
backgroundImage: `url(&quot;${next.image}&quot;)`,
backgroundImage: `url(&quot;${imguri + next.image}&quot;)`,
}"
></div>
<div class="tags">
@ -24,6 +24,8 @@
<script setup lang="ts">
import { Track } from "../../../interfaces";
import perks from "../../../composables/perks";
import { paths } from "../../../config";
const imguri = paths.images.thumb;
const props = defineProps<{
next: Track;
@ -38,7 +40,7 @@ const props = defineProps<{
&:hover {
background-color: $accent;
border: 1px solid transparent;
border: 1px solid transparent;
.h {
background-color: $black;

View File

@ -10,7 +10,7 @@
<div
class="image p-image rounded shadow-sm"
:style="{
backgroundImage: `url(${props.playlist.thumb})`,
backgroundImage: `url(${imguri + props.playlist.thumb})`,
}"
></div>
<div class="pbtn">
@ -33,6 +33,10 @@
import { Playlist } from "../../interfaces";
import PlayBtn from "../shared/PlayBtn.vue";
import Option from "../shared/Option.vue";
import { paths } from "../../config";
const imguri = paths.images.playlist
const props = defineProps<{
playlist: Playlist;

View File

@ -9,7 +9,11 @@
<div class="flex">
<div
class="album-art image rounded"
:style="{ backgroundImage: `url(&quot;${props.song.image}&quot;` }"
:style="{
backgroundImage: `url(&quot;${
imguri + props.song.image
}&quot;`,
}"
@click="emitUpdate(props.song)"
>
<div
@ -64,10 +68,12 @@ import { ContextSrc } from "../../composables/enums";
import { ref } from "vue";
import trackContext from "../../contexts/track_context";
import { Track } from "../../interfaces.js";
import { paths } from "../../config";
const contextStore = useContextStore();
const modalStore = useModalStore();
const context_on = ref(false);
const imguri = paths.images.thumb
const showContextMenu = (e: Event) => {
e.preventDefault();
@ -118,6 +124,7 @@ function emitUpdate(track: Track) {
width: 45px;
background-color: red;
}
@include tablet-landscape {
grid-template-columns: 1.5rem 1.5fr 1fr 1.5fr;
}

View File

@ -13,7 +13,7 @@
<div
class="album-art image rounded"
:style="{
backgroundImage: `url(&quot;${props.track.image}&quot;)`,
backgroundImage: `url(&quot;${imguri + props.track.image}&quot;)`,
}"
>
<div
@ -43,9 +43,12 @@ import { ContextSrc } from "../../composables/enums";
import useContextStore from "../../stores/context";
import useModalStore from "../../stores/modal";
import { paths } from "../../config";
const contextStore = useContextStore();
const modalStore = useModalStore();
const imguri = paths.images.thumb
const props = defineProps<{
track: Track;

10
src/config.ts Normal file
View File

@ -0,0 +1,10 @@
const paths = {
api: "",
images: {
thumb: "http://0.0.0.0:9877/t/",
artist: "http://0.0.0.0:9877/a/",
playlist: "http://0.0.0.0:9877/p/"
},
};
export { paths };