Sweetening

vaughnsoft

Nov 8th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. pip install websockets requests --break-system
  2. ------------------------------------------------------------------------------------------
  3. ----------------------Vaughnsoft API------------------------------------------------------
  4. ------------------------------------------------------------------------------------------
  5. import websockets
  6. import asyncio
  7. import requests
  8. import base64
  9. import json
  10. import time
  11.  
  12. # Base URL for the VaughnSoft API
  13. API_BASE_URL = "https://api.vaughnsoft.net/v1/"
  14.  
  15. # Function to fetch stream data from REST API
  16. def fetch_stream_data(stream_name):
  17. endpoint = f"stream/vl/{stream_name}"
  18. response = requests.get(f"{API_BASE_URL}{endpoint}")
  19. if response.status_code == 200:
  20. return response.json()
  21. else:
  22. return {"error": "Failed to fetch stream data"}
  23.  
  24. # Function to fetch server data (Ingest servers, sApi servers)
  25. def fetch_server_data():
  26. endpoints = {
  27. "ingest_servers": "ingest_servers",
  28. "sapi_servers": "sapi_servers"
  29. }
  30. server_data = {}
  31. for key, endpoint in endpoints.items():
  32. response = requests.get(f"{API_BASE_URL}{endpoint}")
  33. if response.status_code == 200:
  34. server_data[key] = response.json()
  35. else:
  36. server_data[key] = {"error": f"Failed to fetch {key} data"}
  37. return server_data
  38.  
  39. # WebSocket connection to stream and handle server API responses
  40. async def handle_stream_data():
  41. url = "wss://sapi-ws-1x01.vaughnsoft.net/mvn"
  42. async with websockets.connect(url) as websocket:
  43. print("Connected to the WebSocket server")
  44.  
  45. # Send an initial connection request
  46. await websocket.send("MVN LOAD3 #vl-mark guest guest\n\0")
  47.  
  48. while True:
  49. try:
  50. response = await websocket.recv()
  51. process_server_api_response(response)
  52.  
  53. # Poll for fresh stream data every 5 seconds (or another interval)
  54. await asyncio.sleep(5)
  55. await websocket.send("MVN STREAM3 #vl-mark\n\0")
  56.  
  57. except websockets.exceptions.ConnectionClosed:
  58. print("Lost connection to WebSocket. Reconnecting...")
  59. await asyncio.sleep(2)
  60. await handle_stream_data()
  61.  
  62. # Process the response from the server API
  63. def process_server_api_response(response):
  64. response = response.replace("\n", "").replace("\0", "")
  65.  
  66. if response.startswith("ACK3 "):
  67. print("Acknowledgment received from server")
  68. elif response.startswith("STREAM3 "):
  69. data = response.split(" ")[1].split(";")
  70.  
  71. # Parsing the response
  72. stream_name = data[0]
  73. live_viewers = data[2]
  74. is_live = "Yes" if data[6] == "1" else "No"
  75. stream_status = "Yes" if data[10] == "2" else "No"
  76.  
  77. # Displaying stream information
  78. print("---")
  79. print(f"Stream Name: {stream_name}")
  80. print(f"Live Viewers: {live_viewers}")
  81. print(f"Is {stream_name} Live?: {is_live}")
  82. print(f"Is {stream_name} Banned?: {stream_status}")
  83.  
  84. # WebSocket connection for follower notifications
  85. async def handle_follower_notifications():
  86. url = "wss://chat-ws-1x01.vaughnsoft.net/mvn"
  87. async with websockets.connect(url) as websocket:
  88. print("Connected to Follower WebSocket")
  89.  
  90. # Send authentication request
  91. await websocket.send("MVN AUTH guest guest")
  92.  
  93. while True:
  94. try:
  95. response = await websocket.recv()
  96. process_follower_notification(response)
  97.  
  98. except websockets.exceptions.ConnectionClosed:
  99. print("Lost connection to Follower WebSocket. Reconnecting...")
  100. await asyncio.sleep(2)
  101. await handle_follower_notifications()
  102.  
  103. # Process follower notification message
  104. def process_follower_notification(response):
  105. response = response.replace("\n", "").replace("\0", "")
  106.  
  107. if response.startswith("MVN FOLLOW "):
  108. parts = response.split(" ")
  109. if len(parts) > 2:
  110. follower_username = parts[2]
  111. print(f"{follower_username} Followed!")
  112.  
  113. elif response == "PING":
  114. print("Ping received, sending Pong...")
  115. return "PONG"
  116.  
  117. # Main function to orchestrate WebSocket connections
  118. async def main():
  119. # Fetch initial server data (Ingest servers, sApi servers)
  120. server_data = fetch_server_data()
  121. print("Fetched Server Data: ", json.dumps(server_data, indent=4))
  122.  
  123. # Example of fetching a specific stream data
  124. stream_name = "mark"
  125. stream_data = fetch_stream_data(stream_name)
  126. print("Stream Data for 'mark': ", json.dumps(stream_data, indent=4))
  127.  
  128. # Run WebSocket handlers concurrently
  129. await asyncio.gather(
  130. handle_stream_data(), # Stream data (sApi)
  131. handle_follower_notifications() # Follower notifications
  132. )
  133.  
  134. if __name__ == "__main__":
  135. asyncio.run(main())
  136.  
  137. ------------------------------------------------------------------------------------------
  138. ------------------------------------------------------------------------------------------
  139. ------------------------------------------------------------------------------------------
  140. Explanation
  141. REST API Calls
  142.  
  143. fetch_stream_data retrieves stream data (like viewers, status, etc.) for a given stream.
  144. fetch_server_data fetches server details like ingest and sApi servers from the VaughnSoft API.
  145. WebSocket Connections
  146.  
  147. handle_stream_data establishes a WebSocket connection to the sApi server, sends commands, and processes responses. It retrieves and prints live stream data (viewers, status).
  148. handle_follower_notifications connects to the chat WebSocket to listen for new followers and outputs their username when a follow event occurs.
  149. Reconnection Logic
  150.  
  151. If the WebSocket connection is closed, the script will attempt to reconnect after a brief delay.
  152. Polling and Real-time Updates
  153.  
  154. The script continuously polls the server for fresh data (via WebSocket) at intervals, ensuring the data is always up-to-date.
  155. Base64 Encoded Messages
  156.  
  157. Base64-encoded strings like status_msg and about_msg can be decoded to provide more human-readable information. You can add logic to decode these messages if needed.
  158. Concurrency with Asyncio
  159.  
  160. asyncio is used to handle WebSocket connections concurrently, so the stream data and follower notifications can be processed in real-time.
  161. How It Works
  162.  
  163. Stream Data: The WebSocket connection listens for stream data and displays important information such as viewers, status, and stream health.
  164. Follower Alerts: The script listens to the WebSocket for follower notifications, printing out the usernames of new followers.
  165. Polling: Every 5 seconds, the script requests fresh stream data and processes responses to keep information up to date.
  166. Future Enhancements
  167.  
  168. Error Handling: Add more robust error handling for edge cases.
  169. Decoding Base64: Decode any Base64-encoded messages (like status and about messages) for better readability.
  170. Support for Other Streams: Extend the functionality to handle additional stream types.
  171. ------------------------------------------------------------------------------------------
  172. ------------------------------------------------------------------------------------------
  173. ------------------------------------------------------------------------------------------
  174. For that guy that made jtv big mad that one time
Add Comment
Please, Sign In to add comment