From c55f1acf8533bb851c396f8b9c883337f8afef51 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 11 Mar 2018 20:03:21 +0100 Subject: [PATCH] Music tracking --- db.py | 15 ++++++++++++++- discordbot.py | 27 +++++++++++++++++---------- templates/music.html | 40 ++++++++++++++++++++++++++++++++++++++++ webserver.py | 11 ++++++----- 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 templates/music.html diff --git a/db.py b/db.py index 2b71ae5c..675236a2 100644 --- a/db.py +++ b/db.py @@ -661,6 +661,19 @@ class BaluRage(Base): return f"" +class PlayedMusic(Base): + __tablename__ = "playedmusic" + + id = Column(Integer, primary_key=True) + enqueuer_id = Column(Integer, ForeignKey("royals.id")) + enqueuer = relationship("Royal", lazy="joined") + filename = Column(String) + + def __repr__(self): + return f"" + # If run as script, create all the tables in the db if __name__ == "__main__": - Base.metadata.create_all(bind=engine) \ No newline at end of file + print("Creating new tables...") + Base.metadata.create_all(bind=engine) + print("Done!") \ No newline at end of file diff --git a/discordbot.py b/discordbot.py index 647e7de1..0dab0c86 100644 --- a/discordbot.py +++ b/discordbot.py @@ -27,13 +27,19 @@ loop = asyncio.get_event_loop() config = configparser.ConfigParser() config.read("config.ini") +async def find_user(user: discord.User): + session = await loop.run_in_executor(executor, db.Session) + user = await loop.run_in_executor(executor, session.query(db.Discord).filter_by(discord_id=user.id).join(db.Royal).first) + await loop.run_in_executor(executor, session.close) + return user + class DurationError(Exception): pass class Video: def __init__(self): - self.user = None + self.enqueuer = None self.filename = None self.ytdl_url = None @@ -43,7 +49,7 @@ class Video: raise Exception("Filename or url must be specified") self = Video() discord_user = await find_user(user) - self.user = discord_user.royal if discord_user is not None else None + self.enqueuer = discord_user.royal if discord_user is not None else None self.filename = filename self.ytdl_url = ytdl_url return self @@ -78,6 +84,13 @@ class Video: with youtube_dl.YoutubeDL(ytdl_args) as ytdl: await loop.run_in_executor(executor, functools.partial(ytdl.download, [self.ytdl_url])) + async def add_to_db(self): + session = await loop.run_in_executor(executor, db.Session) + pm = db.PlayedMusic(enqueuer=self.enqueuer, + filename=self.filename) + session.add(pm) + await loop.run_in_executor(executor, session.commit) + await loop.run_in_executor(executor, session.close) if __debug__: version = "Dev" @@ -107,13 +120,6 @@ voice_queue: typing.List[Video] = [] # Init the executor executor = concurrent.futures.ThreadPoolExecutor(max_workers=3) - -async def find_user(user: discord.User): - session = await loop.run_in_executor(executor, db.Session) - user = await loop.run_in_executor(executor, session.query(db.Discord).filter_by(discord_id=user.id).join(db.Royal).first) - return user - - async def on_error(event, *args, **kwargs): type, exception, traceback = sys.exc_info() try: @@ -334,7 +340,7 @@ async def update_music_queue(): video = voice_queue.pop(0) if video.ytdl_url: await client.send_message(client.get_channel(config["Discord"]["main_channel"]), - f"ℹ E' iniziato il download di `{video.ytdl_url}`.") + f"⬇️ E' iniziato il download di `{video.ytdl_url}`.") try: await video.download() except DurationError: @@ -355,6 +361,7 @@ async def update_music_queue(): f"▶ Ora in riproduzione in <#{voice_client.channel.id}>:\n" f"`{video.filename}`") await client.change_presence(game=discord.Game(name=video.filename, type=2)) + await video.add_to_db() def process(users_connection=None): diff --git a/templates/music.html b/templates/music.html new file mode 100644 index 00000000..50e1cbac --- /dev/null +++ b/templates/music.html @@ -0,0 +1,40 @@ + + + + + + Music - RYG + + + + + +
+

+ Royal Music +

+
+

+ File più ascoltati +

+ + + + + + + + + {% for record in music_data %} + + + + + {% endfor %} + +
FileAscoltato
{{ record.filename }}{{ record[1] }} volt{{ 'a' if record[1] == 1 else 'e' }}
+
+
+ + diff --git a/webserver.py b/webserver.py index 0599066d..ea03682d 100644 --- a/webserver.py +++ b/webserver.py @@ -1,5 +1,6 @@ from flask import Flask, render_template -from db import Session, Royal, Steam, RocketLeague, Dota, Osu, Overwatch, LeagueOfLegends, Diario, Telegram +from db import Session, Royal, Steam, RocketLeague, Dota, Osu, Overwatch, LeagueOfLegends, Diario, Telegram, PlayedMusic +from sqlalchemy import func app = Flask(__name__) @@ -28,12 +29,12 @@ def page_leaderboards(): session.close() return render_template("leaderboards.html", dota_data=dota_data, rl_data=rl_data, ow_data=ow_data, osu_data=osu_data, lol_data=lol_data) -@app.route("/challenge/1") -def page_challenge_one(): +@app.route("/music") +def page_music(): session = Session() - result = session.execute(r"SELECT sum(osu.std_pp) + sum(osu.taiko_pp) + sum(osu.catch_pp) + sum(osu.mania_pp) total_pp FROM osu;").first()[0] + music_data = session.query(PlayedMusic.filename, func.count(PlayedMusic.filename)).group_by(PlayedMusic.filename).all() session.close() - return render_template("challenge1.html", starting=4959.518703999999, result=result, target=5200) + return render_template("music.html", music_data=music_data) if __name__ == "__main__":