Compare commits

...

5 Commits

2 changed files with 71 additions and 35 deletions

View File

@@ -1,5 +1,17 @@
# YouTube Live Chat in Terminal # YouTube Live Chat in Terminal
---
> **ARCHIVED PROJECT**
>
> This project is considered archived and is no longer in active development.
>
> **Reason:** It relies on the YouTube Data API v3, which has significant quota limitations (10,000 points/day). Continuous polling for live chat messages quickly exhausts this quota, making this approach unsustainable for long-term or frequent use.
>
> **Recommendation:** For a more sustainable and quota-friendly solution, please refer to the **`youtube-chat-webhook-v2`** project. That project explores alternative methods for fetching YouTube live chat data that do not rely on the official API and its restrictive quotas.
---
## Project Description ## Project Description
This project aims to provide a customizable way to view YouTube Live Chat directly in your terminal. It is implemented in `main.py`. This project aims to provide a customizable way to view YouTube Live Chat directly in your terminal. It is implemented in `main.py`.
@@ -16,15 +28,15 @@ This project aims to provide a customizable way to view YouTube Live Chat direct
## Setup Instructions ## Setup Instructions
### 1. Clone the Repository (Future Step) ### 1. Clone the Repository
Once this project is on GitHub, you would clone it using: Clone the project from your Gitea instance:
```bash ```bash
git clone [repository-url] git clone https://gitea.ramforth.net/RamTech/youtube-chat-terminal.git
cd youtube_chat_terminal cd youtube-chat-terminal
``` ```
### 2. Create and Activate a Python Virtual Environment ### 2. Gitea Repository
It's highly recommended to use a virtual environment to manage project dependencies. This project is hosted on your Gitea instance at: `https://gitea.ramforth.net/RamTech/youtube-chat-terminal`
```bash ```bash
cd /home/joe/Cloud9/Documents/Obisdian/youtube_chat_terminal cd /home/joe/Cloud9/Documents/Obisdian/youtube_chat_terminal

32
main.py
View File

@@ -45,6 +45,9 @@ def get_authenticated_service():
API_SERVICE_NAME, API_VERSION, credentials=credentials) API_SERVICE_NAME, API_VERSION, credentials=credentials)
def get_live_chat_id(youtube, video_id): def get_live_chat_id(youtube, video_id):
retries = 0
max_retries = 5
while retries < max_retries:
try: try:
response = youtube.videos().list( response = youtube.videos().list(
part='liveStreamingDetails', part='liveStreamingDetails',
@@ -57,10 +60,20 @@ def get_live_chat_id(youtube, video_id):
return video['liveStreamingDetails']['activeLiveChatId'] return video['liveStreamingDetails']['activeLiveChatId']
return None return None
except googleapiclient.errors.HttpError as e: except googleapiclient.errors.HttpError as e:
if e.resp.status == 403 and "quotaExceeded" in str(e.content):
console.print(f"[red]Quota exceeded. Retrying in {2**retries} seconds...[/red]")
time.sleep(2**retries)
retries += 1
else:
console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]")
return None return None
console.print(f"[red]Failed to get live chat ID after {max_retries} retries due to quota issues.[/red]")
return None
def fetch_live_chat_messages(youtube, live_chat_id, page_token=None): def fetch_live_chat_messages(youtube, live_chat_id, page_token=None):
retries = 0
max_retries = 5
while retries < max_retries:
try: try:
request = youtube.liveChatMessages().list( request = youtube.liveChatMessages().list(
liveChatId=live_chat_id, liveChatId=live_chat_id,
@@ -70,8 +83,15 @@ def fetch_live_chat_messages(youtube, live_chat_id, page_token=None):
response = request.execute() response = request.execute()
return response return response
except googleapiclient.errors.HttpError as e: except googleapiclient.errors.HttpError as e:
if e.resp.status == 403 and "quotaExceeded" in str(e.content):
console.print(f"[red]Quota exceeded. Retrying in {2**retries} seconds...[/red]")
time.sleep(2**retries)
retries += 1
else:
console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]") console.print(f"[red]An HTTP error {e.resp.status} occurred: {e.content}[/red]")
return None return None
console.print(f"[red]Failed to fetch live chat messages after {max_retries} retries due to quota issues.[/red]")
return None
def main(): def main():
youtube = get_authenticated_service() youtube = get_authenticated_service()
@@ -87,17 +107,19 @@ def main():
console.print("[red]Could not find an active live chat for the given video ID.[/red]") console.print("[red]Could not find an active live chat for the given video ID.[/red]")
return return
console.print(f"[green]Found live chat ID: {live_chat_id}[/green]")
# Setup chat logging # Setup chat logging
log_dir = "chat_logs" log_dir = "chat_logs"
os.makedirs(log_dir, exist_ok=True) os.makedirs(log_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = os.path.join(log_dir, f"chat_{video_id}_{timestamp}.log") log_filename = os.path.join(log_dir, f"chat_{video_id}_{timestamp}.log")
log_file = open(log_filename, "w", encoding="utf-8") log_file = open(log_filename, "w", encoding="utf-8")
console.print(f"[green]Chat will be logged to {log_filename}[/green]")
# Display initial messages using rich.Live for a few seconds # Wait for 5 seconds, then clear the screen again
with Live(console=console, screen=True, refresh_per_second=4) as live:
live.update(Text(f"[green]Found live chat ID: {live_chat_id}[/green]\n[green]Chat will be logged to {log_filename}[/green]", justify="center"))
time.sleep(5) time.sleep(5)
os.system('clear')
next_page_token = None next_page_token = None
message_count = 0 message_count = 0
@@ -160,7 +182,9 @@ def main():
next_page_token = response.get('nextPageToken') next_page_token = response.get('nextPageToken')
polling_interval_millis = response['pollingIntervalMillis'] polling_interval_millis = response['pollingIntervalMillis']
time.sleep(polling_interval_millis / 1000.0) # Ensure a minimum sleep duration to conserve API quota
sleep_time = max(5, polling_interval_millis / 1000.0)
time.sleep(sleep_time)
else: else:
console.print("[yellow]No new messages or an error occurred. Retrying...[/yellow]") console.print("[yellow]No new messages or an error occurred. Retrying...[/yellow]")
time.sleep(5) # Wait 5 seconds before retrying on error time.sleep(5) # Wait 5 seconds before retrying on error