mirror of
https://github.com/nuttylmao/nutty.gg.git
synced 2026-09-19 04:00:59 -04:00
Python script cleanup
This commit is contained in:
+32
-26
@@ -1,45 +1,39 @@
|
|||||||
import sys
|
###############
|
||||||
import subprocess
|
### IMPORTS ###
|
||||||
|
###############
|
||||||
# --- SELF-HEALING DEPENDENCY CHECK ---
|
|
||||||
def check_dependencies():
|
|
||||||
required = {'flask': 'flask', 'flask_cors': 'flask_cors', 'winsdk': 'winsdk', 'PIL': 'Pillow'}
|
|
||||||
for mod, pkg in required.items():
|
|
||||||
try:
|
|
||||||
__import__(mod)
|
|
||||||
except ImportError:
|
|
||||||
print(f"--- Missing dependency: {pkg}. Installing now... ---")
|
|
||||||
try:
|
|
||||||
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
print(f"--- Failed to install {pkg}. Please install it manually. ---")
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
check_dependencies()
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
import io
|
import io
|
||||||
from PIL import Image
|
import threading
|
||||||
|
import os
|
||||||
|
import pystray # New dependency
|
||||||
|
from PIL import Image, ImageDraw
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as SMTC
|
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as SMTC
|
||||||
from winsdk.windows.storage.streams import DataReader
|
from winsdk.windows.storage.streams import DataReader
|
||||||
|
import winsdk._winrt as winrt
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
######################
|
||||||
|
### INITIALIZATION ###
|
||||||
|
######################
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
|
|
||||||
# --- GLOBAL MEMORY CACHE ---
|
|
||||||
ARTWORK_CACHE = {}
|
ARTWORK_CACHE = {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
######################
|
||||||
|
### CORE FUNCTIONS ###
|
||||||
|
######################
|
||||||
|
|
||||||
async def get_all_media_info():
|
async def get_all_media_info():
|
||||||
global ARTWORK_CACHE
|
global ARTWORK_CACHE
|
||||||
import winsdk._winrt
|
|
||||||
try:
|
|
||||||
winsdk._winrt.init_apartment(1)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Instantiate the SMTC manager -> This allows use to "talk" to the Windows Media API
|
# Instantiate the SMTC manager -> This allows use to "talk" to the Windows Media API
|
||||||
@@ -200,6 +194,12 @@ async def get_all_media_info():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"current_session_id": None, "sessions": [], "error": str(e)}
|
return {"current_session_id": None, "sessions": [], "error": str(e)}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#################
|
||||||
|
### ENDPOINTS ###
|
||||||
|
#################
|
||||||
|
|
||||||
@app.route('/now-playing')
|
@app.route('/now-playing')
|
||||||
def now_playing():
|
def now_playing():
|
||||||
loop = asyncio.new_event_loop()
|
loop = asyncio.new_event_loop()
|
||||||
@@ -243,5 +243,11 @@ def get_sessions():
|
|||||||
finally:
|
finally:
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#################
|
||||||
|
### KICK OFF! ###
|
||||||
|
#################
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(port=5000, threaded=True)
|
app.run(port=5000, threaded=True)
|
||||||
Reference in New Issue
Block a user