Skip to content

Replace magic values with named constants and unify yt-dlp H.264 format selection - #1

Draft
ShitijHalder with Copilot wants to merge 2 commits into
mainfrom
copilot/add-min-github-token-length-constant
Draft

Replace magic values with named constants and unify yt-dlp H.264 format selection#1
ShitijHalder with Copilot wants to merge 2 commits into
mainfrom
copilot/add-min-github-token-length-constant

Conversation

Copilot AI commented Aug 22, 2026

Copy link
Copy Markdown

This PR removes several unexplained literals in GitHub integration/retry paths and standardizes yt-dlp format selection so preview and main download flows use the same H.264-first strategy. It also aligns one type annotation with broader Python compatibility guidance.

  • gh.py: replace magic numbers with named constants

    • Introduces:
      • MIN_GITHUB_TOKEN_LENGTH = 30 (with rationale comment)
      • GITHUB_COMMIT_RETRY_DELAY_FACTOR = 0.6
      • VIDEOS_JSON_RETRY_DELAY_FACTOR = 0.4
      • ENTRY_VERIFICATION_DELAY_FACTOR = 2.0
    • Replaces inline usages in token validation and retry/backoff calculations.
  • gh.py: typing compatibility cleanup

    • Updates _load_videos_json_and_sha return annotation from tuple[...] to Tuple[...] using typing.Tuple.
  • tg.py: consolidate and standardize yt-dlp format selector

    • Adds module-level constant:
      • YT_DLP_H264_PREFERRED_FORMAT
    • Replaces duplicated inline selectors in download dry-run and actual download commands.
    • Uses documented codec negation syntax (vcodec!=av01) instead of vcodec!*=av01.
  • tg.py: align preview download behavior with main flow

    • Updates /get_video_preview download command to use the same shared H.264-preferring selector, preventing preview/main codec-selection drift.
# tg.py
YT_DLP_H264_PREFERRED_FORMAT = (
    "bestvideo[ext=mp4][vcodec^=avc]+bestaudio[ext=m4a]/"
    "bestvideo[ext=mp4][vcodec!=av01]+bestaudio[ext=m4a]/"
    "bestvideo[vcodec^=avc]+bestaudio/best"
)

# reused in all yt-dlp download paths
"-f", YT_DLP_H264_PREFERRED_FORMAT
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"The magic number 30 for token length validation is unexplained. Consider defining this as a named constant (e.g., `MIN_GITHUB_TOKEN_LENGTH = 30`) with a comment explaining the rationale, or remove this heuristic check if token format validation isn't critical.","fixFiles":[{"filePath":"gh.py","diff":"diff --git a/gh.py b/gh.py\n--- a/gh.py\n+++ b/gh.py\n@@ -18,6 +18,10 @@\n \n # GitHub API imports\n from github import Github, InputGitTreeElement\n+\n+# Heuristic minimum length check to catch obviously invalid/placeholder tokens.\n+# This is not a strict format validator for all possible GitHub token types.\n+MIN_GITHUB_TOKEN_LENGTH = 30\n from github.GithubException import GithubException\n \n # --- Logging Setup ---\n@@ -289,7 +293,7 @@\n \n def authenticate_github():\n     \"\"\"Authenticates with GitHub API using a Personal Access Token.\"\"\"\n-    if not GITHUB_ACCESS_TOKEN or len(GITHUB_ACCESS_TOKEN) < 30: # Basic check for placeholder/empty token\n+    if not GITHUB_ACCESS_TOKEN or len(GITHUB_ACCESS_TOKEN) < MIN_GITHUB_TOKEN_LENGTH: # Basic check for placeholder/empty token\n         logger.error(\"GITHUB_ACCESS_TOKEN is not set or is invalid. Cannot authenticate with GitHub.\")\n         return None\n     try:\n"}]},{"message":"The magic number 0.6 for delay calculation is unexplained. Consider defining this as a named constant (e.g., `GITHUB_COMMIT_RETRY_DELAY_FACTOR = 0.6`) to clarify the retry backoff strategy.","fixFiles":[{"filePath":"gh.py","diff":"diff --git a/gh.py b/gh.py\n--- a/gh.py\n+++ b/gh.py\n@@ -8,6 +8,8 @@\n from typing import Tuple, Optional, Callable\n from env_config import get_env\n \n+GITHUB_COMMIT_RETRY_DELAY_FACTOR = 0.6\n+\n # Google Drive API imports\n from google.oauth2.credentials import Credentials\n from google_auth_oauthlib.flow import InstalledAppFlow\n@@ -348,7 +350,7 @@\n             status = getattr(e, \"status\", None)\n             is_conflict = status == 409 or \"does not match\" in str(e).lower()\n             if is_conflict and attempt < max_retries:\n-                delay = 0.6 * attempt\n+                delay = GITHUB_COMMIT_RETRY_DELAY_FACTOR * attempt\n                 logger.warning(\n                     f\"GitHub conflict while committing '{file_path}' (attempt {attempt}/{max_retries}). \"\n                     f\"Retrying in {delay:.1f}s...\"\n"}]},{"message":"The return type annotation uses lowercase `tuple` which requires Python 3.9+. For broader compatibility, consider using `Tuple` from the typing module (already imported on line 8) or add a comment documenting the minimum Python version requirement.","fixFiles":[{"filePath":"gh.py","diff":"diff --git a/gh.py b/gh.py\n--- a/gh.py\n+++ b/gh.py\n@@ -365,7 +365,7 @@\n     return None\n \n \n-def _load_videos_json_and_sha(repo, branch: str) -> tuple[list, Optional[str]]:\n+def _load_videos_json_and_sha(repo, branch: str) -> Tuple[list, Optional[str]]:\n     \"\"\"Loads videos.json list and current blob SHA (if file exists).\"\"\"\n     try:\n         contents = repo.get_contents(GITHUB_VIDEOS_JSON_PATH, ref=branch)\n"}]},{"message":"The magic number 0.4 for delay calculation is unexplained. Consider defining this as a named constant (e.g., `VIDEOS_JSON_RETRY_DELAY_FACTOR = 0.4`) to clarify the retry backoff strategy and distinguish it from other retry delays in the codebase.","fixFiles":[{"filePath":"gh.py","diff":"diff --git a/gh.py b/gh.py\n--- a/gh.py\n+++ b/gh.py\n@@ -8,6 +8,8 @@\n from typing import Tuple, Optional, Callable\n from env_config import get_env\n \n+VIDEOS_JSON_RETRY_DELAY_FACTOR = 0.4\n+\n # Google Drive API imports\n from google.oauth2.credentials import Credentials\n from google_auth_oauthlib.flow import InstalledAppFlow\n@@ -467,7 +469,7 @@\n             status = getattr(e, \"status\", None)\n             is_conflict = status == 409 or \"does not match\" in str(e).lower()\n             if is_conflict and attempt < max_retries:\n-                delay = 0.4 * attempt\n+                delay = VIDEOS_JSON_RETRY_DELAY_FACTOR * attempt\n                 logger.warning(\n                     f\"Conflict while upserting videos.json (attempt {attempt}/{max_retries}). \"\n                     f\"Retrying in {delay:.1f}s...\"\n"}]},{"message":"The magic number 2.0 for delay calculation is unexplained. Consider defining this as a named constant (e.g., `ENTRY_VERIFICATION_DELAY_FACTOR = 2.0`) to clarify the retry backoff strategy.","fixFiles":[{"filePath":"gh.py","diff":"diff --git a/gh.py b/gh.py\n--- a/gh.py\n+++ b/gh.py\n@@ -8,6 +8,8 @@\n from typing import Tuple, Optional, Callable\n from env_config import get_env\n \n+ENTRY_VERIFICATION_DELAY_FACTOR = 2.0\n+\n # Google Drive API imports\n from google.oauth2.credentials import Credentials\n from google_auth_oauthlib.flow import InstalledAppFlow\n@@ -690,7 +692,...

Co-authored-by: ShitijHalder <89300443+ShitijHalder@users.noreply.github.com>
Copilot AI changed the title [WIP] Add named constant for minimum GitHub token length Replace magic values with named constants and unify yt-dlp H.264 format selection Aug 22, 2026
Copilot AI requested a review from ShitijHalder August 22, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants