Added collab
This commit is contained in:
1
gui/modules/collab/__init__.py
Normal file
1
gui/modules/collab/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .handlers import *
|
||||
43
gui/modules/collab/connect.py
Normal file
43
gui/modules/collab/connect.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
import json
|
||||
import pysher
|
||||
from gui.modules.core.popup import popup
|
||||
import base64
|
||||
from modules.player.player import Player
|
||||
|
||||
|
||||
def on_url_music(url: str, p: Player, ui: Ui_MainWindow):
|
||||
ui.collab_connect_logs.append(f"Received {url}")
|
||||
p.set_media(url)
|
||||
p.play(ui)
|
||||
|
||||
|
||||
def handle_connection_to_server(ui: Ui_MainWindow, p: Player):
|
||||
ui.collab_connect_logs.append(f'Connected to {ui.receiver_creds["name"]}')
|
||||
channel = ui.receiver.subscribe(ui.collab_session_key_box.text())
|
||||
channel.bind('sound', lambda _: on_url_music(_, p, ui))
|
||||
channel.bind('stop', lambda _: (p.mediaplayer_preview.stop(),
|
||||
p.mediaplayer_out.stop()))
|
||||
|
||||
|
||||
def on_connect_clicked(ui: Ui_MainWindow, p: Player):
|
||||
try:
|
||||
creds = dict(json.loads(
|
||||
base64.decodebytes(bytes(ui.collab_session_key_box.text().replace('-', '\n'), encoding='utf-8')
|
||||
).decode('utf-8').replace("'", '"')
|
||||
))
|
||||
ui.collab_connect_logs.append(f'Loaded {creds["name"]}')
|
||||
except Exception as e:
|
||||
print(e)
|
||||
popup("Error", 'Invalid connection key')
|
||||
return
|
||||
|
||||
ui.receiver_creds = creds
|
||||
|
||||
ui.receiver = pysher.Pusher(
|
||||
key=creds["key"],
|
||||
cluster=creds["cluster"]
|
||||
)
|
||||
ui.receiver.connection.bind('pusher:connection_established', lambda _: handle_connection_to_server(ui, p))
|
||||
ui.receiver.connect()
|
||||
ui.collab_disconnect_button.setEnabled(True)
|
||||
34
gui/modules/collab/create.py
Normal file
34
gui/modules/collab/create.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
from modules.config.pusher import PusherConfig
|
||||
from gui.modules.core.popup import popup
|
||||
import base64
|
||||
from gui.modules.collab.host import connect_to_host_admin
|
||||
|
||||
|
||||
def on_create_session_clicked(ui: Ui_MainWindow):
|
||||
if ui.create_session_name_box.text() == "":
|
||||
popup("Error", "Specify room name")
|
||||
return
|
||||
|
||||
connection_key = base64.encodebytes(
|
||||
bytes(str(
|
||||
{
|
||||
"name": ui.create_session_name_box.text(),
|
||||
"key": PusherConfig.get().key,
|
||||
"cluster": PusherConfig.get().cluster
|
||||
}
|
||||
), encoding='utf-8')).decode('utf-8').strip().replace('\n', '-')
|
||||
|
||||
ui.new_connection_key_copy_box.setText(connection_key)
|
||||
|
||||
admin_key = base64.encodebytes(
|
||||
bytes(str(
|
||||
{
|
||||
"connection_key": connection_key,
|
||||
"app_id": PusherConfig.get().app_id,
|
||||
"secret": PusherConfig.get().secret
|
||||
}
|
||||
), encoding='utf-8')).decode('utf-8').strip().replace('\n', '-')
|
||||
ui.new_connection_admin_key_copy_box.setText(admin_key)
|
||||
ui.collab_session_admin_key_box.setText(admin_key)
|
||||
connect_to_host_admin(ui)
|
||||
50
gui/modules/collab/handlers.py
Normal file
50
gui/modules/collab/handlers.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
from PyQt5.QtWidgets import QFileDialog
|
||||
from modules.player.player import Player
|
||||
from modules.anonfiles.anonfiles import Anonfiles
|
||||
from ezzthread import threaded
|
||||
from gui.modules.core.qt_updater import call
|
||||
from gui.modules.collab import create, host, connect
|
||||
|
||||
|
||||
def register_handlers(ui: Ui_MainWindow, p: Player):
|
||||
ui.send_to_users_admin_button.setEnabled(False)
|
||||
ui.collab_disconnect_button.setEnabled(False)
|
||||
ui.stop_all_button_admin.setEnabled(False)
|
||||
|
||||
ui.create_session_button.clicked.connect(
|
||||
lambda: create.on_create_session_clicked(ui)
|
||||
)
|
||||
|
||||
|
||||
ui.connect_to_admin_session_button.clicked.connect(
|
||||
lambda: host.connect_to_host_admin(ui)
|
||||
)
|
||||
ui.send_to_users_admin_button.clicked.connect(
|
||||
lambda: host.on_send_sound_button(ui)
|
||||
)
|
||||
ui.stop_all_button_admin.clicked.connect(
|
||||
lambda: host.on_stop_sound_button(ui)
|
||||
)
|
||||
|
||||
|
||||
ui.connect_to_session_button.clicked.connect(
|
||||
lambda: connect.on_connect_clicked(ui, p)
|
||||
)
|
||||
ui.collab_disconnect_button.clicked.connect(
|
||||
lambda: (ui.receiver.disconnect(),
|
||||
ui.collab_disconnect_button.setEnabled(False),
|
||||
ui.collab_connect_logs.append('Disconnected'))
|
||||
)
|
||||
|
||||
|
||||
ui.choose_upload_sound_button.clicked.connect(
|
||||
lambda: ui.filename_to_upload_box.setText(
|
||||
QFileDialog.getOpenFileName(caption='Choose file to upload')[0]
|
||||
)
|
||||
)
|
||||
ui.upload_sound_button.clicked.connect(
|
||||
lambda: threaded(
|
||||
call(ui.anonfiles_uploaded_url_box.setText, Anonfiles.upload(ui.filename_to_upload_box.text()).url)
|
||||
)
|
||||
)
|
||||
45
gui/modules/collab/host.py
Normal file
45
gui/modules/collab/host.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
import pusher
|
||||
import base64
|
||||
import json
|
||||
from gui.modules.core.popup import popup
|
||||
|
||||
|
||||
def connect_to_host_admin(ui: Ui_MainWindow):
|
||||
try:
|
||||
creds = dict(json.loads(
|
||||
base64.decodebytes(bytes(ui.collab_session_admin_key_box.text().replace('-', '\n'), encoding='utf-8')
|
||||
).decode('utf-8').replace("'", '"')
|
||||
))
|
||||
creds_from_connection = dict(json.loads(
|
||||
base64.decodebytes(bytes(creds['connection_key'].replace('-', '\n'), encoding='utf-8')
|
||||
).decode('utf-8').replace("'", '"')
|
||||
))
|
||||
creds |= creds_from_connection
|
||||
ui.control_admin_logs.append(f'Loaded {creds["name"]}')
|
||||
except Exception as e:
|
||||
print(e)
|
||||
popup("Error", 'Invalid admin key')
|
||||
return
|
||||
|
||||
ui.sender_creds = creds
|
||||
|
||||
ui.sender = pusher.Pusher(
|
||||
app_id=creds["app_id"],
|
||||
key=creds["key"],
|
||||
secret=creds["secret"],
|
||||
cluster=creds["cluster"]
|
||||
)
|
||||
ui.send_to_users_admin_button.setEnabled(True)
|
||||
ui.stop_all_button_admin.setEnabled(True)
|
||||
|
||||
|
||||
def on_send_sound_button(ui: Ui_MainWindow):
|
||||
ui.sender.trigger(ui.sender_creds["connection_key"], 'sound',
|
||||
ui.url_to_send_admin_box.text())
|
||||
ui.control_admin_logs.append(f'Sent {ui.url_to_send_admin_box.text()}')
|
||||
|
||||
|
||||
def on_stop_sound_button(ui: Ui_MainWindow):
|
||||
ui.sender.trigger(ui.sender_creds["connection_key"], 'stop', None)
|
||||
ui.control_admin_logs.append(f'Stopped sounds')
|
||||
@@ -13,30 +13,35 @@ import os
|
||||
|
||||
@threaded
|
||||
def download_track(ui: Ui_MainWindow):
|
||||
url = ui.download_url_box.text()
|
||||
try:
|
||||
url = ui.download_url_box.text()
|
||||
|
||||
if not validators.url(url):
|
||||
call(ui.download_track_logs.append, f"{url} is not valid URL, skipping")
|
||||
return
|
||||
if not validators.url(url):
|
||||
call(ui.download_track_logs.append, f"{url} is not valid URL, skipping")
|
||||
return
|
||||
|
||||
call(ui.download_track_logs.append, f"Downloading {url}")
|
||||
call(ui.download_track_logs.append, f"Downloading {url}")
|
||||
|
||||
name = (lambda song: song.artist + " - " + song.name + ".mp3")(Spotify().get_song(url)) \
|
||||
if "spotify" in url or "youtu" in url else url.split('/')[-1]
|
||||
name = (lambda song: song.artist + " - " + song.name + ".mp3")(Spotify().get_song(url)) \
|
||||
if "spotify" in url or "youtu" in url else url.split('/')[-1]
|
||||
|
||||
url = convert.get_raw_link(url)
|
||||
url = convert.get_raw_link(url)
|
||||
|
||||
call(ui.download_track_button.setEnabled, False)
|
||||
call(ui.download_track_button.setEnabled, False)
|
||||
|
||||
response = urlopen(url)
|
||||
call(ui.download_track_progress.setValue, 0)
|
||||
size = int(response.info()["Content-length"])
|
||||
downloaded = 0
|
||||
with open(os.path.join(ui.download_to_path_box.text(), name), "wb") as dest_file:
|
||||
for data in iter(partial(response.read, 4096), b""):
|
||||
downloaded += len(data)
|
||||
dest_file.write(data)
|
||||
call(ui.download_track_progress.setValue, int(downloaded / size * 100))
|
||||
response = urlopen(url)
|
||||
call(ui.download_track_progress.setValue, 0)
|
||||
size = int(response.info()["Content-length"])
|
||||
downloaded = 0
|
||||
with open(os.path.join(ui.download_to_path_box.text(), name), "wb") as dest_file:
|
||||
for data in iter(partial(response.read, 4096), b""):
|
||||
downloaded += len(data)
|
||||
dest_file.write(data)
|
||||
call(ui.download_track_progress.setValue, int(downloaded / size * 100))
|
||||
|
||||
call(ui.download_track_button.setEnabled, True)
|
||||
call(ui.download_track_logs.append, f"Downloaded to {os.path.join(ui.download_to_path_box.text(), name)}")
|
||||
call(ui.download_track_button.setEnabled, True)
|
||||
call(ui.download_track_logs.append, f"Downloaded to {os.path.join(ui.download_to_path_box.text(), name)}")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
call(ui.download_track_button.setEnabled, True)
|
||||
call(ui.download_track_logs.append, f"Failed")
|
||||
|
||||
@@ -9,6 +9,7 @@ from gui.modules import explorer
|
||||
from gui.modules import collections
|
||||
from gui.modules import stream
|
||||
from gui.modules import download
|
||||
from gui.modules import collab
|
||||
from modules.player.player import Player
|
||||
from modules.restream.restream import Restreamer
|
||||
|
||||
@@ -23,3 +24,4 @@ def register_handlers(ui: Ui_MainWindow, MainWindow: QMainWindow, p: Player, rs:
|
||||
collections.register_handlers(ui, p)
|
||||
stream.register_handlers(ui, p)
|
||||
download.register_handlers(ui)
|
||||
collab.register_handlers(ui, p)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
from modules.config import Config
|
||||
from modules.config.pusher import PusherConfig
|
||||
from modules.spotify.config import SpotifyConfig
|
||||
from modules.restream import get_streaming_devices
|
||||
from modules.player.player import get_devices, get_instance, get_player
|
||||
@@ -41,4 +42,9 @@ def fill_settings(ui: Ui_MainWindow):
|
||||
ui.spotify_client_id_box.setText(SpotifyConfig.get().client_id)
|
||||
ui.spotify_client_secret_box.setText(SpotifyConfig.get().client_secret)
|
||||
|
||||
ui.pusher_app_id_box.setText(PusherConfig.get().app_id)
|
||||
ui.pusher_key_box.setText(PusherConfig.get().key)
|
||||
ui.pusher_secret_box.setText(PusherConfig.get().secret)
|
||||
ui.pusher_cluster_box.setText(PusherConfig.get().cluster)
|
||||
|
||||
ui.use_original_streaming_method_check.setChecked(Config.get().direct_stream)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from gui.gui import Ui_MainWindow
|
||||
from modules.config import Config
|
||||
from modules.config.pusher import PusherConfig
|
||||
from modules.spotify.config import SpotifyConfig
|
||||
import shutil
|
||||
|
||||
@@ -30,6 +31,19 @@ def register_handlers(ui: Ui_MainWindow):
|
||||
lambda: SpotifyConfig.update("client_secret", ui.spotify_client_secret_box.text())
|
||||
)
|
||||
|
||||
ui.pusher_app_id_box.textChanged.connect(
|
||||
lambda: PusherConfig.update("app_id", ui.pusher_app_id_box.text())
|
||||
)
|
||||
ui.pusher_key_box.textChanged.connect(
|
||||
lambda: PusherConfig.update("key", ui.pusher_key_box.text())
|
||||
)
|
||||
ui.pusher_secret_box.textChanged.connect(
|
||||
lambda: PusherConfig.update("secret", ui.pusher_secret_box.text())
|
||||
)
|
||||
ui.pusher_cluster_box.textChanged.connect(
|
||||
lambda: PusherConfig.update("cluster", ui.pusher_cluster_box.text())
|
||||
)
|
||||
|
||||
ui.use_original_streaming_method_check.stateChanged.connect(
|
||||
lambda: Config.update("direct_stream", ui.use_original_streaming_method_check.isChecked())
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user