mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-24 03:54:20 +00:00
better discord page
This commit is contained in:
parent
714a5de10f
commit
89b39ba3e8
5 changed files with 148 additions and 58 deletions
120
query_discord_music.py
Normal file
120
query_discord_music.py
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
all_query = """SELECT
|
||||||
|
discord.royal_id,
|
||||||
|
discord.discord_id,
|
||||||
|
discord.name,
|
||||||
|
discord.discriminator,
|
||||||
|
discord.avatar_hex,
|
||||||
|
fav_songs.fav_song,
|
||||||
|
fav_songs.max_plays,
|
||||||
|
last_songs.last_song,
|
||||||
|
last_songs.last_play_time
|
||||||
|
FROM discord
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT DISTINCT ON (ma.discord_id)
|
||||||
|
ma.discord_id discord_id,
|
||||||
|
fs.fav_song fav_song,
|
||||||
|
ma.max_plays max_plays
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
discord_id,
|
||||||
|
max(plays) max_plays
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename fav_song,
|
||||||
|
count(*) plays
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
||||||
|
ORDER BY plays DESC
|
||||||
|
) play_counts
|
||||||
|
GROUP BY discord_id
|
||||||
|
) ma
|
||||||
|
INNER JOIN
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename fav_song,
|
||||||
|
count(*) plays
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
||||||
|
ORDER BY plays DESC
|
||||||
|
) fs ON fs.discord_id = ma.discord_id AND fs.plays = ma.max_plays
|
||||||
|
) fav_songs ON fav_songs.discord_id = discord.discord_id
|
||||||
|
LEFT JOIN
|
||||||
|
(
|
||||||
|
SELECT DISTINCT ON (playedmusic.enqueuer_id)
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename last_song,
|
||||||
|
last_play_times.last_play_time
|
||||||
|
FROM playedmusic
|
||||||
|
JOIN (
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
max(playedmusic.timestamp) last_play_time
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.enqueuer_id
|
||||||
|
) last_play_times ON playedmusic.timestamp = last_play_times.last_play_time
|
||||||
|
) last_songs ON last_songs.discord_id = discord.discord_id;"""
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: can and should be optimized, but I'm too lazy for that
|
||||||
|
one_query = """SELECT
|
||||||
|
discord.royal_id,
|
||||||
|
discord.discord_id,
|
||||||
|
discord.name,
|
||||||
|
discord.discriminator,
|
||||||
|
discord.avatar_hex,
|
||||||
|
fav_songs.fav_song,
|
||||||
|
fav_songs.max_plays,
|
||||||
|
last_songs.last_song,
|
||||||
|
last_songs.last_play_time
|
||||||
|
FROM discord
|
||||||
|
LEFT JOIN (
|
||||||
|
SELECT DISTINCT ON (ma.discord_id)
|
||||||
|
ma.discord_id discord_id,
|
||||||
|
fs.fav_song fav_song,
|
||||||
|
ma.max_plays max_plays
|
||||||
|
FROM (
|
||||||
|
SELECT
|
||||||
|
discord_id,
|
||||||
|
max(plays) max_plays
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename fav_song,
|
||||||
|
count(*) plays
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
||||||
|
ORDER BY plays DESC
|
||||||
|
) play_counts
|
||||||
|
GROUP BY discord_id
|
||||||
|
) ma
|
||||||
|
INNER JOIN
|
||||||
|
(
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename fav_song,
|
||||||
|
count(*) plays
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
||||||
|
ORDER BY plays DESC
|
||||||
|
) fs ON fs.discord_id = ma.discord_id AND fs.plays = ma.max_plays
|
||||||
|
) fav_songs ON fav_songs.discord_id = discord.discord_id
|
||||||
|
LEFT JOIN
|
||||||
|
(
|
||||||
|
SELECT DISTINCT ON (playedmusic.enqueuer_id)
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
playedmusic.filename last_song,
|
||||||
|
last_play_times.last_play_time
|
||||||
|
FROM playedmusic
|
||||||
|
JOIN (
|
||||||
|
SELECT
|
||||||
|
playedmusic.enqueuer_id discord_id,
|
||||||
|
max(playedmusic.timestamp) last_play_time
|
||||||
|
FROM playedmusic
|
||||||
|
GROUP BY playedmusic.enqueuer_id
|
||||||
|
) last_play_times ON playedmusic.timestamp = last_play_times.last_play_time
|
||||||
|
) last_songs ON last_songs.discord_id = discord.discord_id
|
||||||
|
WHERE discord.royal_id = ?;"""
|
|
@ -205,6 +205,11 @@ nav {
|
||||||
grid-row: 3;
|
grid-row: 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.game-extra {
|
||||||
|
grid-row: 4;
|
||||||
|
font-size: smaller;
|
||||||
|
}
|
||||||
|
|
||||||
.player {
|
.player {
|
||||||
grid-row: 1;
|
grid-row: 1;
|
||||||
font-size: large;
|
font-size: large;
|
||||||
|
@ -614,6 +619,7 @@ nav {
|
||||||
padding: 18px;
|
padding: 18px;
|
||||||
color: white;
|
color: white;
|
||||||
font-family: "Whitney", sans-serif;
|
font-family: "Whitney", sans-serif;
|
||||||
|
grid-template-columns: 50% 50%;
|
||||||
|
|
||||||
a, a:hover, a:active, a:visited {
|
a, a:hover, a:active, a:visited {
|
||||||
color: white;
|
color: white;
|
||||||
|
@ -623,6 +629,10 @@ nav {
|
||||||
border-radius: 16px;
|
border-radius: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.game-score {
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
.last-video {
|
.last-video {
|
||||||
grid-column: 1
|
grid-column: 1
|
||||||
}
|
}
|
||||||
|
@ -632,7 +642,7 @@ nav {
|
||||||
}
|
}
|
||||||
|
|
||||||
.none {
|
.none {
|
||||||
opacity: 0.3;
|
color: rgba(255, 255, 255, 0.3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,26 +7,32 @@
|
||||||
<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[0].avatar_url(size=64) }}" class="player-image">
|
<img src="https://cdn.discordapp.com/avatars/{{ record["discord_id"] }}/{{ record["avatar_hex"] }}" class="player-image">
|
||||||
<span class="player-name">{{ record[0] }}</span>
|
<span class="player-name">{{ record["name"] }}#{{ record["discriminator"] }}</span>
|
||||||
</div>
|
</div>
|
||||||
{% if record[0].music_played %}
|
{% if record["last_song"] %}
|
||||||
<div class="game-title last-video">Ultima canzone</div>
|
<div class="game-title last-video">Ultima canzone</div>
|
||||||
<div class="game-score last-video">
|
<div class="game-score last-video" title="{{ record["last_song"] }}">
|
||||||
{{ (record[0].music_played|sort(attribute='timestamp'))[-1].filename }}
|
{{ record["last_song"] }}
|
||||||
</div>
|
</div>
|
||||||
|
<div class="game-extra last-video">{{ record["last_play_time"] }}</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="game-title last-video none">Ultima canzone</div>
|
<div class="game-title last-video none">Ultima canzone</div>
|
||||||
<div class="game-score last-video none">
|
<div class="game-score last-video none">
|
||||||
Nessuna
|
Nessuna
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if record|length > 1 %}
|
{% if record["fav_song"] %}
|
||||||
<div class="game-title fav-video">Canzone preferita</div>
|
<div class="game-title fav-video">Canzone preferita</div>
|
||||||
<div class="game-score fav-video">{{ record[1] }}</div>
|
<div class="game-score fav-video" title="{{ record["fav_song"] }}">
|
||||||
|
{{ record["fav_song"] }}
|
||||||
|
</div>
|
||||||
|
<div class="game-extra fav-video">{{ record["max_plays"] }}x</div>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="game-title fav-video none">Canzone preferita</div>
|
<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>
|
<div class="game-score fav-video none">
|
||||||
|
Nessuna
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
18
webserver.py
18
webserver.py
|
@ -6,6 +6,7 @@ import configparser
|
||||||
import markdown2
|
import markdown2
|
||||||
import datetime
|
import datetime
|
||||||
import telegram
|
import telegram
|
||||||
|
import query_discord_music
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -81,15 +82,7 @@ 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()
|
||||||
fav_song = db_session.query(db.PlayedMusic.enqueuer_id, db.PlayedMusic.filename, db.func.count("*").label("plays")) \
|
discord = [dict(row) for row in db_session.execute(query_discord_music.one_query, user.id)]
|
||||||
.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)
|
||||||
|
@ -183,8 +176,6 @@ 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()
|
||||||
|
@ -211,10 +202,7 @@ 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"
|
||||||
partial_query = db_session.query(db.Discord) \
|
query = [dict(row) for row in db_session.execute(query_discord_music.all_query)]
|
||||||
.options(db.joinedload(db.Discord.music_played)) \
|
|
||||||
.all()
|
|
||||||
query = [[discord] for discord in partial_query]
|
|
||||||
else:
|
else:
|
||||||
abort(404)
|
abort(404)
|
||||||
return
|
return
|
||||||
|
|
34
wow.sql
34
wow.sql
|
@ -1,34 +0,0 @@
|
||||||
SELECT *
|
|
||||||
FROM discord
|
|
||||||
LEFT JOIN (
|
|
||||||
SELECT DISTINCT ON (ma.discord_id)
|
|
||||||
ma.discord_id discord_id2,
|
|
||||||
fs.fav_song fav_song2,
|
|
||||||
ma.max_plays max_plays2
|
|
||||||
FROM (
|
|
||||||
SELECT
|
|
||||||
discord_id,
|
|
||||||
max(plays) max_plays
|
|
||||||
FROM
|
|
||||||
(
|
|
||||||
SELECT
|
|
||||||
playedmusic.enqueuer_id discord_id,
|
|
||||||
playedmusic.filename fav_song,
|
|
||||||
count(*) plays
|
|
||||||
FROM playedmusic
|
|
||||||
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
|
||||||
ORDER BY plays DESC
|
|
||||||
) play_counts
|
|
||||||
GROUP BY discord_id
|
|
||||||
) ma
|
|
||||||
INNER JOIN
|
|
||||||
(
|
|
||||||
SELECT
|
|
||||||
playedmusic.enqueuer_id discord_id,
|
|
||||||
playedmusic.filename fav_song,
|
|
||||||
count(*) plays
|
|
||||||
FROM playedmusic
|
|
||||||
GROUP BY playedmusic.filename, playedmusic.enqueuer_id
|
|
||||||
ORDER BY plays DESC
|
|
||||||
) fs ON fs.discord_id = ma.discord_id AND fs.plays = ma.max_plays
|
|
||||||
) fav_songs ON fav_songs.discord_id2 = discord.discord_id;
|
|
Loading…
Reference in a new issue