mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-05 04:53:01 +00:00
🔶 fix passed time converter function
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
Contains all the playlist routes.
|
Contains all the playlist routes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
from flask import Blueprint, request
|
from flask import Blueprint, request
|
||||||
from app import instances, api
|
from app import instances, api
|
||||||
from app.lib import playlistlib
|
from app.lib import playlistlib
|
||||||
@@ -17,7 +18,14 @@ TrackExistsInPlaylist = exceptions.TrackExistsInPlaylist
|
|||||||
|
|
||||||
@playlist_bp.route("/playlists", methods=["GET"])
|
@playlist_bp.route("/playlists", methods=["GET"])
|
||||||
def get_all_playlists():
|
def get_all_playlists():
|
||||||
return {"data": [serializer.Playlist(p) for p in api.PLAYLISTS]}
|
playlists = [
|
||||||
|
serializer.Playlist(p, construct_last_updated=False) for p in api.PLAYLISTS
|
||||||
|
]
|
||||||
|
playlists.sort(
|
||||||
|
key=lambda p: datetime.strptime(p.lastUpdated, "%Y-%m-%d %H:%M:%S"),
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
return {"data": playlists}
|
||||||
|
|
||||||
|
|
||||||
@playlist_bp.route("/playlist/new", methods=["POST"])
|
@playlist_bp.route("/playlist/new", methods=["POST"])
|
||||||
@@ -28,7 +36,7 @@ def create_playlist():
|
|||||||
"name": data["name"],
|
"name": data["name"],
|
||||||
"description": "",
|
"description": "",
|
||||||
"pre_tracks": [],
|
"pre_tracks": [],
|
||||||
"lastUpdated": 0,
|
"lastUpdated": data["lastUpdated"],
|
||||||
"image": "",
|
"image": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +76,10 @@ def get_single_p_info(playlistid: str):
|
|||||||
for p in api.PLAYLISTS:
|
for p in api.PLAYLISTS:
|
||||||
if p.playlistid == playlistid:
|
if p.playlistid == playlistid:
|
||||||
tracks = p.get_tracks()
|
tracks = p.get_tracks()
|
||||||
return {"info": serializer.Playlist(p), "tracks": tracks}
|
return {
|
||||||
|
"info": serializer.Playlist(p),
|
||||||
|
"tracks": tracks,
|
||||||
|
}
|
||||||
|
|
||||||
return {"info": {}, "tracks": []}
|
return {"info": {}, "tracks": []}
|
||||||
|
|
||||||
@@ -82,9 +93,6 @@ def update_playlist(playlistid: str):
|
|||||||
|
|
||||||
data = request.form
|
data = request.form
|
||||||
|
|
||||||
print(type(image))
|
|
||||||
print(image.content_type)
|
|
||||||
|
|
||||||
playlist = {
|
playlist = {
|
||||||
"name": str(data.get("name")).strip(),
|
"name": str(data.get("name")).strip(),
|
||||||
"description": str(data.get("description").strip()),
|
"description": str(data.get("description").strip()),
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
This library contains all the functions related to playlists.
|
This library contains all the functions related to playlists.
|
||||||
"""
|
"""
|
||||||
import base64
|
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
@@ -53,6 +52,7 @@ def create_all_playlists():
|
|||||||
_bar = Bar("Creating playlists", max=len(playlists))
|
_bar = Bar("Creating playlists", max=len(playlists))
|
||||||
for playlist in playlists:
|
for playlist in playlists:
|
||||||
api.PLAYLISTS.append(models.Playlist(playlist))
|
api.PLAYLISTS.append(models.Playlist(playlist))
|
||||||
|
|
||||||
_bar.next()
|
_bar.next()
|
||||||
_bar.finish()
|
_bar.finish()
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ Contains all the models for objects generation and typing.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import date
|
|
||||||
from typing import List
|
from typing import List
|
||||||
from app import api
|
from app import api
|
||||||
from app import settings
|
from app import settings
|
||||||
|
|||||||
@@ -1,7 +1,57 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime
|
||||||
from app import models
|
from app import models
|
||||||
|
|
||||||
|
|
||||||
|
def date_string_to_time_passed(dstring: str) -> str:
|
||||||
|
"""
|
||||||
|
Converts a date string to time passed. eg. 2 minutes ago, 1 hour ago, yesterday, 2 days ago, 2 weeks ago, etc.
|
||||||
|
"""
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
then = datetime.strptime(dstring, "%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
diff = now - then
|
||||||
|
days = diff.days
|
||||||
|
|
||||||
|
if days < 0:
|
||||||
|
return "in the future"
|
||||||
|
|
||||||
|
elif days == 0:
|
||||||
|
seconds = diff.seconds
|
||||||
|
if seconds < 15:
|
||||||
|
return "now"
|
||||||
|
elif seconds < 60:
|
||||||
|
return str(seconds) + " seconds ago"
|
||||||
|
elif seconds < 3600:
|
||||||
|
return str(seconds // 60) + " minutes ago"
|
||||||
|
else:
|
||||||
|
return str(seconds // 3600) + " hours ago"
|
||||||
|
|
||||||
|
elif days == 1:
|
||||||
|
return "yesterday"
|
||||||
|
elif days < 7:
|
||||||
|
if days == 1:
|
||||||
|
return "1 day ago"
|
||||||
|
|
||||||
|
return str(days) + " days ago"
|
||||||
|
elif days < 30:
|
||||||
|
if days == 7:
|
||||||
|
return "1 week ago"
|
||||||
|
|
||||||
|
return str(days // 7) + " weeks ago"
|
||||||
|
elif days < 365:
|
||||||
|
if days == 30:
|
||||||
|
return "1 month ago"
|
||||||
|
|
||||||
|
return str(days // 30) + " months ago"
|
||||||
|
elif days > 365:
|
||||||
|
if days == 365:
|
||||||
|
return "1 year ago"
|
||||||
|
|
||||||
|
return str(days // 365) + " years ago"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Playlist:
|
class Playlist:
|
||||||
playlistid: str
|
playlistid: str
|
||||||
@@ -11,10 +61,16 @@ class Playlist:
|
|||||||
description: str
|
description: str
|
||||||
count: int = 0
|
count: int = 0
|
||||||
|
|
||||||
def __init__(self, p: models.Playlist) -> None:
|
def __init__(self, p: models.Playlist, construct_last_updated: bool = True) -> None:
|
||||||
self.playlistid = p.playlistid
|
self.playlistid = p.playlistid
|
||||||
self.name = p.name
|
self.name = p.name
|
||||||
self.image = p.image
|
self.image = p.image
|
||||||
self.lastUpdated = p.lastUpdated
|
self.lastUpdated = p.lastUpdated
|
||||||
self.description = p.description
|
self.description = p.description
|
||||||
self.count = p.count
|
self.count = p.count
|
||||||
|
|
||||||
|
if construct_last_updated:
|
||||||
|
self.lastUpdated = self.l_updated(p.lastUpdated)
|
||||||
|
|
||||||
|
def l_updated(self, date: str) -> str:
|
||||||
|
return date_string_to_time_passed(date)
|
||||||
|
|||||||
Reference in New Issue
Block a user