Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- import os
- from datetime import datetime, timedelta
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QTextBrowser, QSlider, QLabel, QLineEdit
- from PyQt5.QtCore import QTimer, Qt
- class ChatSimulator(QWidget):
- def __init__(self, chat_file):
- super().__init__()
- self.setWindowTitle("Livestream Chat Simulator")
- self.setGeometry(100, 100, 500, 450)
- self.chat_data = self.load_chat(chat_file)
- self.start_time = None
- self.is_playing = False
- self.base_timestamp = self.chat_data[0][0] if self.chat_data else None
- self.total_duration = (self.chat_data[-1][0] - self.base_timestamp).total_seconds() if self.chat_data else 0
- self.pending_messages = []
- self.message_timer = QTimer(self)
- self.message_timer.timeout.connect(self.display_next_message)
- self.current_messages = []
- self.init_ui()
- self.timer = QTimer(self)
- self.timer.timeout.connect(self.update_chat)
- def init_ui(self):
- layout = QVBoxLayout()
- self.chat_display = QTextBrowser(self)
- layout.addWidget(self.chat_display)
- self.play_button = QPushButton("Play", self)
- self.play_button.clicked.connect(self.toggle_playback)
- layout.addWidget(self.play_button)
- self.slider = QSlider(Qt.Horizontal, self)
- self.slider.setMinimum(0)
- self.slider.setMaximum(len(self.chat_data) - 1 if self.chat_data else 0)
- self.slider.sliderReleased.connect(self.seek_chat)
- layout.addWidget(self.slider)
- self.time_label = QLabel("0:00 / 0:00", self)
- layout.addWidget(self.time_label)
- self.setLayout(layout)
- def load_chat(self, chat_file):
- chat_entries = []
- with open(chat_file, "r", encoding="utf-8") as file:
- raw_data = file.read().strip().split("#####\n\n")
- for entry in raw_data:
- lines = [line for line in entry.strip().split("\n") if line.strip()] # Remove empty lines
- if len(lines) >= 3 and lines[2].startswith("date: "):
- user = lines[0]
- try:
- timestamp = datetime.strptime(lines[2].replace("date: ", ""), "%Y-%m-%dT%H:%M:%S")
- message = "\n".join(lines[3:]) # Start from index 3, avoiding any extra blank lines
- chat_entries.append((timestamp, user, message))
- except ValueError:
- print(f"Skipping malformed timestamp in entry: {entry}")
- continue
- return sorted(chat_entries, key=lambda x: x[0]) # Sort by timestamp
- def toggle_playback(self):
- if not self.chat_data:
- return
- if self.is_playing:
- self.timer.stop()
- self.message_timer.stop()
- self.play_button.setText("Play")
- else:
- self.start_time = time.time()
- self.timer.start(100) # Check every 100ms
- self.play_button.setText("Pause")
- self.is_playing = not self.is_playing
- def update_chat(self):
- if not self.is_playing or not self.base_timestamp:
- return
- elapsed_time = datetime.utcfromtimestamp(time.time() - self.start_time)
- simulated_time = self.base_timestamp + (elapsed_time - datetime(1970, 1, 1))
- while self.chat_data and self.chat_data[0][0] <= simulated_time:
- self.current_messages.append(self.chat_data.pop(0))
- if self.current_messages:
- interval = max(1, 1000 // len(self.current_messages)) # Spread messages within 1 second
- self.message_timer.start(interval)
- self.update_time_label(simulated_time)
- if not self.chat_data and not self.current_messages:
- self.timer.stop()
- self.message_timer.stop()
- self.is_playing = False
- self.play_button.setText("Play")
- def display_next_message(self):
- if self.current_messages:
- timestamp, user, message = self.current_messages.pop(0)
- self.chat_display.append(f"<b>{user}:</b> {message}")
- if not self.current_messages:
- self.message_timer.stop()
- def seek_chat(self):
- if not self.chat_data:
- return
- self.chat_display.clear()
- self.chat_data = self.load_chat(chat_file) # Reload and sort chat data
- self.start_time = time.time()
- self.base_timestamp = self.chat_data[self.slider.value()][0]
- self.update_chat()
- def update_time_label(self, current_time):
- elapsed_seconds = (current_time - self.base_timestamp).total_seconds()
- elapsed_time_str = f"{int(elapsed_seconds // 60)}:{int(elapsed_seconds % 60):02}"
- total_time_str = f"{int(self.total_duration // 60)}:{int(self.total_duration % 60):02}"
- self.time_label.setText(f"{elapsed_time_str} / {total_time_str}")
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- chat_file = "chat_log.txt" # Replace with actual chat file path
- window = ChatSimulator(chat_file)
- window.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment