mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Added chat support and fixing some timestamp confusion
This commit is contained in:
parent
5e466cbe98
commit
fcdf3ac7e7
@ -23,7 +23,7 @@ def list_indexes():
|
|||||||
ifModifiedSince = request.args.get('ifModifiedSince')
|
ifModifiedSince = request.args.get('ifModifiedSince')
|
||||||
if ifModifiedSince:
|
if ifModifiedSince:
|
||||||
try:
|
try:
|
||||||
ifModifiedSince = int(ifModifiedSince)
|
ifModifiedSince = int(ifModifiedSince) / 1000
|
||||||
except:
|
except:
|
||||||
return request.error_formatter(0, 'Invalid timestamp')
|
return request.error_formatter(0, 'Invalid timestamp')
|
||||||
|
|
||||||
@ -41,10 +41,9 @@ def list_indexes():
|
|||||||
return request.error_formatter(70, 'Folder not found')
|
return request.error_formatter(70, 'Folder not found')
|
||||||
|
|
||||||
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan
|
last_modif = max(map(lambda f: f.last_scan, folder)) if type(folder) is list else folder.last_scan
|
||||||
last_modif_ts = int(time.mktime(last_modif.timetuple()))
|
|
||||||
|
|
||||||
if (not ifModifiedSince is None) and last_modif_ts < ifModifiedSince:
|
if (not ifModifiedSince is None) and last_modif < ifModifiedSince:
|
||||||
return request.formatter({ 'indexes': { 'lastModified': last_modif_ts } })
|
return request.formatter({ 'indexes': { 'lastModified': last_modif * 1000 } })
|
||||||
|
|
||||||
# The XSD lies, we don't return artists but a directory structure
|
# The XSD lies, we don't return artists but a directory structure
|
||||||
if type(folder) is list:
|
if type(folder) is list:
|
||||||
@ -72,7 +71,7 @@ def list_indexes():
|
|||||||
|
|
||||||
return request.formatter({
|
return request.formatter({
|
||||||
'indexes': {
|
'indexes': {
|
||||||
'lastModified': last_modif_ts,
|
'lastModified': last_modif * 1000,
|
||||||
'index': [ {
|
'index': [ {
|
||||||
'name': k,
|
'name': k,
|
||||||
'artist': [ {
|
'artist': [ {
|
||||||
|
30
api/chat.py
Executable file
30
api/chat.py
Executable file
@ -0,0 +1,30 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
|
||||||
|
from flask import request
|
||||||
|
from web import app
|
||||||
|
from db import ChatMessage, session
|
||||||
|
|
||||||
|
@app.route('/rest/getChatMessages.view', methods = [ 'GET', 'POST' ])
|
||||||
|
def get_chat():
|
||||||
|
since = request.args.get('since')
|
||||||
|
try:
|
||||||
|
since = int(since) / 1000 if since else None
|
||||||
|
except:
|
||||||
|
return request.error_formatter(0, 'Invalid parameter')
|
||||||
|
|
||||||
|
query = ChatMessage.query.order_by(ChatMessage.time)
|
||||||
|
if since:
|
||||||
|
query = query.filter(ChatMessage.time > since)
|
||||||
|
|
||||||
|
return request.formatter({ 'chatMessages': { 'chatMessage': [ msg.responsize() for msg in query ] }})
|
||||||
|
|
||||||
|
@app.route('/rest/addChatMessage.view', methods = [ 'GET', 'POST' ])
|
||||||
|
def add_chat_message():
|
||||||
|
msg = request.args.get('message')
|
||||||
|
if not msg:
|
||||||
|
return request.error_formatter(10, 'Missing message')
|
||||||
|
|
||||||
|
session.add(ChatMessage(user = request.user, message = msg))
|
||||||
|
session.commit()
|
||||||
|
return request.formatter({})
|
||||||
|
|
21
db.py
21
db.py
@ -10,7 +10,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
|||||||
from sqlalchemy.types import TypeDecorator
|
from sqlalchemy.types import TypeDecorator
|
||||||
from sqlalchemy.types import BINARY
|
from sqlalchemy.types import BINARY
|
||||||
|
|
||||||
import uuid, datetime
|
import uuid, datetime, time
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
class UUID(TypeDecorator):
|
class UUID(TypeDecorator):
|
||||||
@ -73,7 +73,7 @@ class Folder(Base):
|
|||||||
path = Column(String, unique = True)
|
path = Column(String, unique = True)
|
||||||
created = Column(DateTime, default = now)
|
created = Column(DateTime, default = now)
|
||||||
has_cover_art = Column(Boolean, default = False)
|
has_cover_art = Column(Boolean, default = False)
|
||||||
last_scan = Column(DateTime, default = datetime.datetime.min)
|
last_scan = Column(Integer, default = 0)
|
||||||
|
|
||||||
parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True)
|
parent_id = Column(UUID, ForeignKey('folder.id'), nullable = True)
|
||||||
children = relationship('Folder', backref = backref('parent', remote_side = [ id ]))
|
children = relationship('Folder', backref = backref('parent', remote_side = [ id ]))
|
||||||
@ -297,6 +297,23 @@ class RatingTrack(Base):
|
|||||||
user = relationship('User')
|
user = relationship('User')
|
||||||
rated = relationship('Track')
|
rated = relationship('Track')
|
||||||
|
|
||||||
|
class ChatMessage(Base):
|
||||||
|
__tablename__ = 'chat_message'
|
||||||
|
|
||||||
|
id = UUID.gen_id_column()
|
||||||
|
user_id = Column(UUID, ForeignKey('user.id'))
|
||||||
|
time = Column(Integer, default = lambda: int(time.time()))
|
||||||
|
message = Column(String)
|
||||||
|
|
||||||
|
user = relationship('User')
|
||||||
|
|
||||||
|
def responsize(self):
|
||||||
|
return {
|
||||||
|
'username': self.user.name,
|
||||||
|
'time': self.time * 1000,
|
||||||
|
'message': self.message
|
||||||
|
}
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
Base.metadata.create_all(bind = engine)
|
Base.metadata.create_all(bind = engine)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
import os, os.path
|
import os, os.path
|
||||||
import datetime
|
import time
|
||||||
import eyed3.id3, eyed3.mp3
|
import eyed3.id3, eyed3.mp3
|
||||||
import db
|
import db
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ class Scanner:
|
|||||||
for f in files:
|
for f in files:
|
||||||
if f.endswith('.mp3'):
|
if f.endswith('.mp3'):
|
||||||
self.__scan_file(os.path.join(root, f), folder)
|
self.__scan_file(os.path.join(root, f), folder)
|
||||||
folder.last_scan = datetime.datetime.now()
|
folder.last_scan = int(time.time())
|
||||||
|
|
||||||
def prune(self, folder):
|
def prune(self, folder):
|
||||||
for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not os.path.exists(t.path) ]:
|
for track in [ t for t in self.__tracks if t.root_folder.id == folder.id and not os.path.exists(t.path) ]:
|
||||||
|
Loading…
Reference in New Issue
Block a user