recent data
This commit is contained in:
@@ -3,12 +3,16 @@ from langchain_core.prompts.prompt import PromptTemplate
|
||||
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
|
||||
import os
|
||||
import requests
|
||||
from googleapiclient.discovery import build
|
||||
from googleapiclient.errors import HttpError
|
||||
import requests
|
||||
from loguru import logger
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
|
||||
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
||||
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
||||
PERPLEXITYAI_API_KEY = os.getenv('PERPLEXITY_AI_API')
|
||||
llm = ChatOpenAI(model="gpt-4o")
|
||||
|
||||
@@ -158,3 +162,108 @@ def product_categorizer(product_lists: list, product_categories=product_categori
|
||||
output = initiator_router.invoke({"product_lists":product_lists, "product_categories":product_categories})
|
||||
return output
|
||||
|
||||
def get_youtube_channel_info(channel_name: str):
|
||||
API_KEY = 'YOUTUBE_API_KEY' # Replace with your API key
|
||||
|
||||
url = 'https://www.googleapis.com/youtube/v3/search'
|
||||
params = {
|
||||
'part': 'id',
|
||||
'q': channel_name,
|
||||
'type': 'channel',
|
||||
'key': API_KEY
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
if 'items' in data and len(data['items']) > 0:
|
||||
channel_id = data['items'][0]['id']['channelId']
|
||||
channel_info_url = 'https://www.googleapis.com/youtube/v3/channels'
|
||||
channel_info_params = {
|
||||
'part': 'snippet,statistics',
|
||||
'id': channel_id,
|
||||
'key': API_KEY
|
||||
}
|
||||
channel_info_response = requests.get(channel_info_url, params=channel_info_params)
|
||||
|
||||
if channel_info_response.status_code == 200:
|
||||
channel_info_data = channel_info_response.json()
|
||||
if 'items' in channel_info_data and len(channel_info_data['items']) > 0:
|
||||
channel_info = channel_info_data['items'][0]
|
||||
title = channel_info['snippet']['title']
|
||||
description = channel_info['snippet']['description']
|
||||
subscriber_count = channel_info['statistics']['subscriberCount']
|
||||
view_count = channel_info['statistics']['viewCount']
|
||||
video_count = channel_info['statistics']['videoCount']
|
||||
return {
|
||||
"Title": title,
|
||||
"Description": description,
|
||||
"Subscribers": subscriber_count,
|
||||
"Total Views": view_count,
|
||||
"Total Videos": video_count
|
||||
}
|
||||
else:
|
||||
return "Channel not found."
|
||||
else:
|
||||
return f"Error: {channel_info_response.status_code}"
|
||||
else:
|
||||
return "Channel not found."
|
||||
else:
|
||||
return f"Error: {response.status_code}"
|
||||
|
||||
def get_channel_videos(channel_name, max_results=5):
|
||||
try:
|
||||
API_KEY = 'YOUTUBE_API_KEY'
|
||||
# Build the YouTube service object
|
||||
youtube = build('youtube', 'v3', developerKey=API_KEY)
|
||||
|
||||
# Search for the channel by name
|
||||
search_response = youtube.search().list(
|
||||
q=channel_name,
|
||||
type='channel',
|
||||
part='id',
|
||||
maxResults=1
|
||||
).execute()
|
||||
|
||||
# Extract the channel ID
|
||||
items = search_response.get('items')
|
||||
if not items:
|
||||
print("No channel found with that name.")
|
||||
return []
|
||||
|
||||
channel_id = items[0]['id']['channelId']
|
||||
|
||||
# Retrieve the uploads playlist ID
|
||||
channels_response = youtube.channels().list(
|
||||
part='contentDetails',
|
||||
id=channel_id
|
||||
).execute()
|
||||
|
||||
uploads_playlist_id = channels_response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
|
||||
|
||||
# Retrieve the videos from the uploads playlist
|
||||
playlist_items_response = youtube.playlistItems().list(
|
||||
part='snippet',
|
||||
playlistId=uploads_playlist_id,
|
||||
maxResults=max_results
|
||||
).execute()
|
||||
|
||||
# Extract video URLs
|
||||
video_urls = []
|
||||
for item in playlist_items_response['items']:
|
||||
video_id = item['snippet']['resourceId']['videoId']
|
||||
video_url = f"https://www.youtube.com/watch?v={video_id}"
|
||||
video_urls.append(video_url)
|
||||
|
||||
return video_urls
|
||||
|
||||
except HttpError as e:
|
||||
print(f"An HTTP error occurred: {e}")
|
||||
return []
|
||||
|
||||
def get_channel_details(channel_name):
|
||||
channel_info = get_youtube_channel_info(channel_name)
|
||||
recent_videos = get_channel_videos(channel_name)
|
||||
channel_info['recent_videos'] = recent_videos
|
||||
return channel_info
|
||||
Reference in New Issue
Block a user