1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Display played music

This commit is contained in:
Steffo 2018-07-26 19:26:03 +02:00
parent 5f021fdab2
commit d372e39d05
5 changed files with 55 additions and 15 deletions

5
db.py
View file

@ -1,8 +1,8 @@
import datetime import datetime
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.orm import sessionmaker, relationship, joinedload
from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint, PrimaryKeyConstraint, Boolean, or_, LargeBinary, Text, Date, func from sqlalchemy import Column, BigInteger, Integer, String, DateTime, ForeignKey, Float, Enum, create_engine, UniqueConstraint, PrimaryKeyConstraint, Boolean, or_, LargeBinary, Text, Date, func, desc
import requests import requests
from errors import RequestError, NotFoundError, AlreadyExistingError from errors import RequestError, NotFoundError, AlreadyExistingError
import re import re
@ -741,6 +741,7 @@ class PlayedMusic(Base):
enqueuer_id = Column(BigInteger, ForeignKey("discord.discord_id")) enqueuer_id = Column(BigInteger, ForeignKey("discord.discord_id"))
enqueuer = relationship("Discord", backref="music_played", lazy="joined") enqueuer = relationship("Discord", backref="music_played", lazy="joined")
filename = Column(String) filename = Column(String)
timestamp = Column(DateTime, nullable=False)
def __repr__(self): def __repr__(self):
return f"<PlayedMusic {self.filename}>" return f"<PlayedMusic {self.filename}>"

View file

@ -18,6 +18,7 @@ import async_timeout
import raven import raven
import logging import logging
import errors import errors
import datetime
logging.basicConfig() logging.basicConfig()
@ -555,7 +556,8 @@ async def queue_play_next_video():
if now_playing.enqueuer is not None: if now_playing.enqueuer is not None:
session = await loop.run_in_executor(executor, db.Session) session = await loop.run_in_executor(executor, db.Session)
played_music = db.PlayedMusic(enqueuer=now_playing.enqueuer, played_music = db.PlayedMusic(enqueuer=now_playing.enqueuer,
filename=str(now_playing)) filename=str(now_playing),
timestamp=datetime.datetime.now())
session.add(played_music) session.add(played_music)
await loop.run_in_executor(executor, session.commit) await loop.run_in_executor(executor, session.commit)
await loop.run_in_executor(executor, session.close) await loop.run_in_executor(executor, session.close)

View file

@ -614,7 +614,6 @@ nav {
padding: 18px; padding: 18px;
color: white; color: white;
font-family: "Whitney", sans-serif; font-family: "Whitney", sans-serif;
opacity: 0.8;
a, a:hover, a:active, a:visited { a, a:hover, a:active, a:visited {
color: white; color: white;
@ -624,8 +623,16 @@ nav {
border-radius: 16px; border-radius: 16px;
} }
.useless-stuff { .last-video {
grid-column: 1; grid-column: 1
}
.fav-video {
grid-column: 2;
}
.none {
opacity: 0.3;
} }
} }

View file

@ -7,9 +7,26 @@
<div class="game-panel"> <div class="game-panel">
<div class="game-grid discord"> <div class="game-grid discord">
<div class="player"> <div class="player">
<img src="{{ record.avatar_url(size=64) }}" class="player-image"> <img src="{{ record[0].avatar_url(size=64) }}" class="player-image">
<span class="player-name">{{ record }}</span> <span class="player-name">{{ record[0] }}</span>
</div> </div>
<div class="game-score useless-stuff">è su Discord!</div> {% if record[0].music_played %}
<div class="game-title last-video">Ultima canzone</div>
<div class="game-score last-video">
{{ (record[0].music_played|sort(attribute='timestamp'))[-1].filename }}
</div>
{% else %}
<div class="game-title last-video none">Ultima canzone</div>
<div class="game-score last-video none">
Nessuna
</div>
{% endif %}
{% if record|length > 1 %}
<div class="game-title fav-video">Canzone preferita</div>
<div class="game-score fav-video">{{ record[1] }}</div>
{% else %}
<div class="game-title fav-video none">Canzone preferita</div>
<div class="game-score fav-video none"><a href="/profile/{{ record[0].royal.username }}">Non caricata</a></div>
{% endif %}
</div> </div>
</div> </div>

View file

@ -58,7 +58,8 @@ def page_main():
royals = db_session.query(db.Royal).order_by(db.Royal.username).all() royals = db_session.query(db.Royal).order_by(db.Royal.username).all()
wiki_pages = db_session.query(db.WikiEntry).order_by(db.WikiEntry.key).all() wiki_pages = db_session.query(db.WikiEntry).order_by(db.WikiEntry.key).all()
random_diario = db_session.query(db.Diario).order_by(db.func.random()).first() random_diario = db_session.query(db.Diario).order_by(db.func.random()).first()
next_events = db_session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by(db.Event.time).all() next_events = db_session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by(
db.Event.time).all()
db_session.close() db_session.close()
return render_template("main.html", royals=royals, wiki_pages=wiki_pages, entry=random_diario, return render_template("main.html", royals=royals, wiki_pages=wiki_pages, entry=random_diario,
next_events=next_events, config=config, escape=escape) next_events=next_events, config=config, escape=escape)
@ -80,7 +81,15 @@ def page_profile(name: str):
lol = db_session.query(db.LeagueOfLegends).filter_by(royal=user).one_or_none() lol = db_session.query(db.LeagueOfLegends).filter_by(royal=user).one_or_none()
ow = db_session.query(db.Overwatch).filter_by(royal=user).one_or_none() ow = db_session.query(db.Overwatch).filter_by(royal=user).one_or_none()
tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none() tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none()
discord = db_session.query(db.Discord).filter_by(royal=user).one_or_none() fav_song = db_session.query(db.PlayedMusic.enqueuer_id, db.PlayedMusic.filename, db.func.count("*").label("plays")) \
.group_by(db.PlayedMusic.filename, db.PlayedMusic.enqueuer_id) \
.order_by(db.desc("plays")) \
.subquery()
discord = db_session.query(db.Discord, db.PlayedMusic.filename) \
.options(db.joinedload(db.Discord.music_played)) \
.filter_by(royal=user) \
.outerjoin(fav_song) \
.first()
db_session.close() db_session.close()
return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow, return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow,
tg=tg, discord=discord, config=config) tg=tg, discord=discord, config=config)
@ -174,6 +183,8 @@ def page_setcss():
@app.route("/game/<name>") @app.route("/game/<name>")
def page_game(name: str): def page_game(name: str):
db_session = db.Session() db_session = db.Session()
fav_song = None
last_song = None
if name == "rl": if name == "rl":
game_name = "Rocket League" game_name = "Rocket League"
query = db_session.query(db.RocketLeague).join(db.Steam).all() query = db_session.query(db.RocketLeague).join(db.Steam).all()
@ -200,8 +211,10 @@ def page_game(name: str):
query = db_session.query(db.Telegram).all() query = db_session.query(db.Telegram).all()
elif name == "discord": elif name == "discord":
game_name = "Discord" game_name = "Discord"
# noinspection PyComparisonWithNone partial_query = db_session.query(db.Discord) \
query = db_session.query(db.Discord).filter(db.Discord.royal_id != None).all() .options(db.joinedload(db.Discord.music_played)) \
.all()
query = [[discord] for discord in partial_query]
else: else:
abort(404) abort(404)
return return
@ -215,12 +228,12 @@ def page_wiki(key: str):
wiki_page = db_session.query(db.WikiEntry).filter_by(key=key).one_or_none() wiki_page = db_session.query(db.WikiEntry).filter_by(key=key).one_or_none()
if request.method == "GET": if request.method == "GET":
wiki_latest_edit = db_session.query(db.WikiLog).filter_by(edited_key=key) \ wiki_latest_edit = db_session.query(db.WikiLog).filter_by(edited_key=key) \
.order_by(db.WikiLog.timestamp.desc()).first() .order_by(db.WikiLog.timestamp.desc()).first()
db_session.close() db_session.close()
if wiki_page is None: if wiki_page is None:
return render_template("wiki.html", key=key, config=config) return render_template("wiki.html", key=key, config=config)
converted_md = Markup(markdown2.markdown(wiki_page.content.replace("<", "&lt;"), converted_md = Markup(markdown2.markdown(wiki_page.content.replace("<", "&lt;"),
extras=["spoiler", "tables"])) extras=["spoiler", "tables"]))
return render_template("wiki.html", key=key, wiki_page=wiki_page, converted_md=converted_md, return render_template("wiki.html", key=key, wiki_page=wiki_page, converted_md=converted_md,
wiki_log=wiki_latest_edit, config=config) wiki_log=wiki_latest_edit, config=config)
elif request.method == "POST": elif request.method == "POST":