Skip to content

Commit

Permalink
with play / test-releases
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffy-melli committed Dec 14, 2024
1 parent 7718572 commit f13e086
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 120 deletions.
8 changes: 7 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import src.win.screen
from src.utils import user_setting
import src.discord.client
import locale
import src.with_play
import locale, threading
from pypresence.exceptions import DiscordNotFound

args = sys.argv[1:]

nogui_op = False
once_op = False
playlist_op = False

for arg in args:
if not arg.startswith("--") and playlist_op:
src.win.setting.video_list.append(arg)
Expand All @@ -22,6 +24,10 @@
once_op = True
if arg == "--play":
playlist_op = True
if arg == "--with-play-server":
src.with_play.Start_Server()
if arg == "--with-play-client":
src.with_play.Start_Client('192.168.0.101')

try:
if user_setting.discord_RPC:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pygame == 2.5.2
yt_dlp == 2024.12.13
moviepy == 1.0.3
chardet == 5.2.0
requests == 2.32.3
pytubefix == 7.1rc2
pyvidplayer2 == 0.9.24
opencv-python == 4.5.5.64
Expand Down
3 changes: 3 additions & 0 deletions server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"max-client": 5
}
265 changes: 146 additions & 119 deletions src/gui.py

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/socket/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import socket, json, time
import src.gui

client = None
play = False

url = ''
seek = 0

def playinfo():
try:
data = json.dumps({"type": "req-play-info"})
client.send(data.encode('utf-8'))
except Exception as e:
return None

def start_client(server_ip):
global client, play, url, seek
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((server_ip, 12377))
try:
playinfo()
while True:
time.sleep(2)
response = client.recv(1024)
try:
message_data = json.loads(response.decode('utf-8').split("}{")[0] + "}")
except Exception as e:
message_data = json.loads(response.decode('utf-8'))
type = message_data.get('type')
print(message_data)
if type == "play-info":
play = True
url = message_data.get('playurl')
seek = message_data.get('seek')
if type == "play-wait":
play = False
except KeyboardInterrupt:
pass
finally:
client.close()
58 changes: 58 additions & 0 deletions src/socket/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import socket
import threading
import json
import src.socket.setting as setting
import src.socket.user as user

playurl = ''
seek = 0
clients = []

def handle_client(client_socket, client_address, clients):
try:
while True:
data = client_socket.recv(1024)
if not data:
break

try:
message_data = json.loads(data.decode('utf-8'))
message_type = message_data.get('type')
if message_type == "req-play-info":
if playurl != '':
response = {"type": "play-info", "playurl": playurl, "seek": seek}
else:
response = {"type": "play-wait"}
client_socket.send(json.dumps(response).encode('utf-8'))
except json.JSONDecodeError:
pass

except Exception as e:
pass
finally:
if client_socket in clients:
clients.remove(client_socket)
client_socket.close()

def broadcast_message(message):
for client in clients[:]:
try:
data = json.dumps(message)
client.send(data.encode('utf-8'))
except Exception as e:
print(f"클라이언트에게 메시지 전송 중 오류 발생: {e}")
if client in clients:
clients.remove(client)

def start_server():
global clients
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('0.0.0.0', 12377))
server.listen(setting.max_client)
print(f"SERVER START (ADDRESS) : {user.get_internal_ip()} / {user.get_external_ip()} [NEED PORT FORWARDING -> :12377]")

while True:
client_socket, client_address = server.accept()
clients.append(client_socket)
client_thread = threading.Thread(target=handle_client, args=(client_socket, client_address, clients))
client_thread.start()
23 changes: 23 additions & 0 deletions src/socket/setting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import src.utils.json as json
json_file_path = "./server.json"
max_client = 0
def init_file():
data = {}
data['max-client'] = 5
return data
def change_setting_data(key:str,value):
data = json.read(json_file_path)
if data == None:
data = init_file()
data[key] = value
json.write(json_file_path,data)
reload_setting_file()
def reload_setting_file():
global max_client
data = json.read(json_file_path)
if data == None:
data = init_file()
json.write(json_file_path,data)
max_client = data['max-client']
#####################
reload_setting_file()
18 changes: 18 additions & 0 deletions src/socket/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import socket, requests
def get_external_ip():
try:
response = requests.get("https://api.ipify.org?format=json")
if response.status_code == 200:
return response.json()['ip']
else:
return '?:?:?:?'
except requests.RequestException as e:
return '?:?:?:?'

def get_internal_ip():
try:
hostname = socket.gethostname()
internal_ip = socket.gethostbyname(hostname)
return internal_ip
except socket.error as e:
return '?:?:?:?'
20 changes: 20 additions & 0 deletions src/with_play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import src.socket.server
import src.socket.client
import threading

client = False
server = False

def Start_Server():
global server
server = True
socket_thread = threading.Thread(target=src.socket.server.start_server)
socket_thread.daemon = True
socket_thread.start()

def Start_Client(server_ip):
global client
client = True
socket_thread = threading.Thread(target=src.socket.client.start_client, args=(server_ip,))
socket_thread.daemon = True
socket_thread.start()
1 change: 1 addition & 0 deletions start client.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python ./main.py --with-play-client
1 change: 1 addition & 0 deletions start server.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python ./main.py --with-play-server

0 comments on commit f13e086

Please sign in to comment.