fmab777 commited on
Commit
7e4a1c6
·
verified ·
1 Parent(s): 768889e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -13
main.py CHANGED
@@ -1,4 +1,4 @@
1
- # main.py (Revised: Starlette Lifespan + ptb_app._running fix + HTTPX Config + Updated Prompts)
2
  import os
3
  import re
4
  import logging
@@ -28,7 +28,7 @@ from telegram.error import NetworkError, RetryAfter # Import RetryAfter
28
  from telegram.request import HTTPXRequest # Import the request class
29
 
30
  # --- Other Libraries ---
31
- import httpx # Import httpx directly
32
  from youtube_transcript_api import YouTubeTranscriptApi
33
  import requests
34
  from bs4 import BeautifulSoup
@@ -43,14 +43,14 @@ logging.basicConfig(
43
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
44
  level=logging.DEBUG
45
  )
46
- logging.getLogger("httpx").setLevel(logging.WARNING) # Keep httpx logs quieter unless debugging it
47
  if ApifyClient: logging.getLogger("apify_client").setLevel(logging.WARNING)
48
  logging.getLogger("telegram.ext").setLevel(logging.INFO)
49
  logging.getLogger('telegram.bot').setLevel(logging.INFO)
50
  logging.getLogger("urllib3").setLevel(logging.INFO)
51
  logging.getLogger('gunicorn.error').setLevel(logging.INFO)
52
- logging.getLogger('uvicorn').setLevel(logging.INFO) # Uvicorn logs (incl. access)
53
- logging.getLogger('starlette').setLevel(logging.INFO) # Starlette logs
54
  logger = logging.getLogger(__name__)
55
  logger.info("Logging configured.")
56
 
@@ -75,6 +75,11 @@ logger.info("Secret loading attempt finished.")
75
 
76
 
77
  # --- Bot Logic Functions ---
 
 
 
 
 
78
 
79
  # Helper Functions
80
  def is_youtube_url(url):
@@ -517,18 +522,37 @@ async def setup_bot_config() -> Application:
517
  logger.critical("CRITICAL: TELEGRAM_TOKEN environment variable not found.")
518
  raise ValueError("TELEGRAM_TOKEN environment variable not set.")
519
 
520
- httpx_settings = {
521
- "timeout": httpx.Timeout(timeout=10.0, read=10.0, connect=5.0, pool=10.0),
522
- "limits": httpx.Limits(max_keepalive_connections=10, max_connections=100, keepalive_expiry=15.0) # Increased expiry
523
- }
524
- logger.info(f"Creating PTB HTTPXRequest with settings: {httpx_settings}")
525
- custom_request = HTTPXRequest(connection_pool_size=10, connect_timeout=5.0, read_timeout=10.0, http_version="1.1", httpx_settings=httpx_settings)
526
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  application_builder = Application.builder().token(TELEGRAM_TOKEN)
528
  application_builder.request(custom_request)
529
- application_builder.get_updates_request(custom_request)
 
 
530
  application = application_builder.build()
531
 
 
532
  application.add_handler(CommandHandler("start", start))
533
  application.add_handler(CommandHandler("help", help_command))
534
  application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_potential_url))
 
1
+ # main.py (Revised: Correct HTTPXRequest init + Starlette Lifespan + Updated Prompts)
2
  import os
3
  import re
4
  import logging
 
28
  from telegram.request import HTTPXRequest # Import the request class
29
 
30
  # --- Other Libraries ---
31
+ # import httpx # No longer needed for direct import
32
  from youtube_transcript_api import YouTubeTranscriptApi
33
  import requests
34
  from bs4 import BeautifulSoup
 
43
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
44
  level=logging.DEBUG
45
  )
46
+ logging.getLogger("httpx").setLevel(logging.WARNING)
47
  if ApifyClient: logging.getLogger("apify_client").setLevel(logging.WARNING)
48
  logging.getLogger("telegram.ext").setLevel(logging.INFO)
49
  logging.getLogger('telegram.bot').setLevel(logging.INFO)
50
  logging.getLogger("urllib3").setLevel(logging.INFO)
51
  logging.getLogger('gunicorn.error').setLevel(logging.INFO)
52
+ logging.getLogger('uvicorn').setLevel(logging.INFO)
53
+ logging.getLogger('starlette').setLevel(logging.INFO)
54
  logger = logging.getLogger(__name__)
55
  logger.info("Logging configured.")
56
 
 
75
 
76
 
77
  # --- Bot Logic Functions ---
78
+ # (Keep ALL your functions: is_youtube_url, extract_youtube_id,
79
+ # get_transcript_via_supadata, get_transcript_via_apify,
80
+ # get_youtube_transcript, get_website_content_via_requests,
81
+ # get_website_content_via_urltotext_api, generate_summary)
82
+ # Ensure the generate_summary has the updated prompts from previous response
83
 
84
  # Helper Functions
85
  def is_youtube_url(url):
 
522
  logger.critical("CRITICAL: TELEGRAM_TOKEN environment variable not found.")
523
  raise ValueError("TELEGRAM_TOKEN environment variable not set.")
524
 
525
+ # --- Configure HTTPX client settings via individual parameters ---
526
+ # Increased read/pool timeouts slightly, connect timeout standard.
527
+ # No direct way to set keepalive_expiry via HTTPXRequest in PTB v20.
528
+ connect_timeout = 5.0
529
+ read_timeout = 15.0 # Slightly increased
530
+ write_timeout = 15.0 # Matching read timeout
531
+ pool_timeout = 15.0 # Slightly increased pool timeout
532
+
533
+ logger.info(f"Creating PTB HTTPXRequest with timeouts: "
534
+ f"connect={connect_timeout}, read={read_timeout}, "
535
+ f"write={write_timeout}, pool={pool_timeout}")
536
+
537
+ # Create a custom request object with these settings
538
+ # connection_pool_size default is 10, which is usually fine.
539
+ custom_request = HTTPXRequest(
540
+ connect_timeout=connect_timeout,
541
+ read_timeout=read_timeout,
542
+ write_timeout=write_timeout, # Added write_timeout
543
+ pool_timeout=pool_timeout,
544
+ http_version="1.1"
545
+ )
546
+
547
+ # Use Application.builder() and pass the custom request object
548
  application_builder = Application.builder().token(TELEGRAM_TOKEN)
549
  application_builder.request(custom_request)
550
+ application_builder.get_updates_request(custom_request) # Apply same settings for getUpdates if used
551
+
552
+ # Build the application instance
553
  application = application_builder.build()
554
 
555
+ # --- Register Handlers (same as before) ---
556
  application.add_handler(CommandHandler("start", start))
557
  application.add_handler(CommandHandler("help", help_command))
558
  application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_potential_url))