Guest User

Untitled

a guest
Feb 26th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. import sys
  2. import time
  3. import os
  4. from datetime import datetime, timedelta
  5. from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QTextBrowser, QSlider, QLabel, QLineEdit
  6. from PyQt5.QtCore import QTimer, Qt
  7.  
  8. class ChatSimulator(QWidget):
  9. def __init__(self, chat_file):
  10. super().__init__()
  11. self.setWindowTitle("Livestream Chat Simulator")
  12. self.setGeometry(100, 100, 500, 450)
  13.  
  14. self.chat_data = self.load_chat(chat_file)
  15. self.start_time = None
  16. self.is_playing = False
  17. self.base_timestamp = self.chat_data[0][0] if self.chat_data else None
  18. self.total_duration = (self.chat_data[-1][0] - self.base_timestamp).total_seconds() if self.chat_data else 0
  19. self.pending_messages = []
  20. self.message_timer = QTimer(self)
  21. self.message_timer.timeout.connect(self.display_next_message)
  22. self.current_messages = []
  23.  
  24. self.init_ui()
  25.  
  26. self.timer = QTimer(self)
  27. self.timer.timeout.connect(self.update_chat)
  28.  
  29. def init_ui(self):
  30. layout = QVBoxLayout()
  31.  
  32. self.chat_display = QTextBrowser(self)
  33. layout.addWidget(self.chat_display)
  34.  
  35. self.play_button = QPushButton("Play", self)
  36. self.play_button.clicked.connect(self.toggle_playback)
  37. layout.addWidget(self.play_button)
  38.  
  39. self.slider = QSlider(Qt.Horizontal, self)
  40. self.slider.setMinimum(0)
  41. self.slider.setMaximum(len(self.chat_data) - 1 if self.chat_data else 0)
  42. self.slider.sliderReleased.connect(self.seek_chat)
  43. layout.addWidget(self.slider)
  44.  
  45. self.time_label = QLabel("0:00 / 0:00", self)
  46. layout.addWidget(self.time_label)
  47.  
  48. self.setLayout(layout)
  49.  
  50. def load_chat(self, chat_file):
  51. chat_entries = []
  52. with open(chat_file, "r", encoding="utf-8") as file:
  53. raw_data = file.read().strip().split("#####\n\n")
  54.  
  55. for entry in raw_data:
  56. lines = [line for line in entry.strip().split("\n") if line.strip()] # Remove empty lines
  57. if len(lines) >= 3 and lines[2].startswith("date: "):
  58. user = lines[0]
  59. try:
  60. timestamp = datetime.strptime(lines[2].replace("date: ", ""), "%Y-%m-%dT%H:%M:%S")
  61. message = "\n".join(lines[3:]) # Start from index 3, avoiding any extra blank lines
  62. chat_entries.append((timestamp, user, message))
  63. except ValueError:
  64. print(f"Skipping malformed timestamp in entry: {entry}")
  65. continue
  66.  
  67. return sorted(chat_entries, key=lambda x: x[0]) # Sort by timestamp
  68.  
  69. def toggle_playback(self):
  70. if not self.chat_data:
  71. return
  72.  
  73. if self.is_playing:
  74. self.timer.stop()
  75. self.message_timer.stop()
  76. self.play_button.setText("Play")
  77. else:
  78. self.start_time = time.time()
  79. self.timer.start(100) # Check every 100ms
  80. self.play_button.setText("Pause")
  81. self.is_playing = not self.is_playing
  82.  
  83. def update_chat(self):
  84. if not self.is_playing or not self.base_timestamp:
  85. return
  86.  
  87. elapsed_time = datetime.utcfromtimestamp(time.time() - self.start_time)
  88. simulated_time = self.base_timestamp + (elapsed_time - datetime(1970, 1, 1))
  89.  
  90. while self.chat_data and self.chat_data[0][0] <= simulated_time:
  91. self.current_messages.append(self.chat_data.pop(0))
  92.  
  93. if self.current_messages:
  94. interval = max(1, 1000 // len(self.current_messages)) # Spread messages within 1 second
  95. self.message_timer.start(interval)
  96.  
  97. self.update_time_label(simulated_time)
  98.  
  99. if not self.chat_data and not self.current_messages:
  100. self.timer.stop()
  101. self.message_timer.stop()
  102. self.is_playing = False
  103. self.play_button.setText("Play")
  104.  
  105. def display_next_message(self):
  106. if self.current_messages:
  107. timestamp, user, message = self.current_messages.pop(0)
  108. self.chat_display.append(f"<b>{user}:</b> {message}")
  109.  
  110. if not self.current_messages:
  111. self.message_timer.stop()
  112.  
  113. def seek_chat(self):
  114. if not self.chat_data:
  115. return
  116.  
  117. self.chat_display.clear()
  118. self.chat_data = self.load_chat(chat_file) # Reload and sort chat data
  119. self.start_time = time.time()
  120. self.base_timestamp = self.chat_data[self.slider.value()][0]
  121. self.update_chat()
  122.  
  123. def update_time_label(self, current_time):
  124. elapsed_seconds = (current_time - self.base_timestamp).total_seconds()
  125. elapsed_time_str = f"{int(elapsed_seconds // 60)}:{int(elapsed_seconds % 60):02}"
  126. total_time_str = f"{int(self.total_duration // 60)}:{int(self.total_duration % 60):02}"
  127. self.time_label.setText(f"{elapsed_time_str} / {total_time_str}")
  128.  
  129. if __name__ == "__main__":
  130. app = QApplication(sys.argv)
  131. chat_file = "chat_log.txt" # Replace with actual chat file path
  132. window = ChatSimulator(chat_file)
  133. window.show()
  134. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment