Skip to content

Commit

Permalink
Replaced broken pytube to get title and channel name.
Browse files Browse the repository at this point in the history
  • Loading branch information
talosross committed Nov 17, 2024
1 parent b4aa0cb commit 70b0ec7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ chaquopy {
// A requirement specifier, with or without a version number:
install("youtube-transcript-api")
install("openai==1.39.0")
install("pytube")
install("newspaper4k")
install("pydantic<2")
//install("google-generativeai==0.1.0rc1")
Expand Down
42 changes: 36 additions & 6 deletions app/src/main/python/youtube.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled
import re
from openai import OpenAI
from pytube import YouTube
from newspaper import Article
import socket
#import google.generativeai as genai
Expand Down Expand Up @@ -30,6 +29,40 @@ def extract_youtube_video_id(url: str) -> str:
return found.group(1)
return None

def get_video_title(url):
try:
# Get the webpage content
response = requests.get(url)
if response.status_code != 200:
return "Error: Could not fetch video page"

# Find title using regex
title_match = re.search(r'<title>(.+?)</title>', response.text)
if title_match:
# Clean up the title (remove " - YouTube" suffix)
title = title_match.group(1)
title = title.replace(' - YouTube', '')
return title
return "Error: Title not found"

except Exception as e:
return f"An error occurred: {e}"

def get_channel_name(url):
try:
response = requests.get(url)
if response.status_code != 200:
return "Error: Could not fetch video page"

# Look for channel name in meta tags
channel_match = re.search(r'<link itemprop="name" content="(.+?)">', response.text)
if channel_match:
return channel_match.group(1)
return "Error: Channel name not found"

except Exception as e:
return f"An error occurred: {e}"

def generate_summary(text: str, length: int, type: str, language: str, title: str, key: str, model: str) -> str:
"""
Generate a summary of the provided text
Expand Down Expand Up @@ -248,15 +281,12 @@ def summarize(url: str, length: int, language: str, key: str, model: str) -> dic
text = YouTubeTranscriptApi.get_transcript(video_id, languages=[language[0]])
transcript = " ".join([line["text"] for line in text])

# Create YouTube video object
yt_video = YouTube(url)

# Get the title of the video
title = yt_video.title
title = get_video_title(url)
result['title'] = title

# Get the author of the video
author = yt_video.author
author = get_channel_name(url)
if author == "unknown":
author = None
result['author'] = author
Expand Down

0 comments on commit 70b0ec7

Please sign in to comment.