-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_data.py
59 lines (49 loc) · 1.88 KB
/
fetch_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from googleapiclient.discovery import build
import requests
import os
# Your YouTube Data API Key
API_KEY = "YOUR_API_KEY"
# Initialize YouTube API client
youtube = build('youtube', 'v3', developerKey=API_KEY)
def fetch_videos(channel_id, max_results=10):
# Fetch videos from a specific channel
request = youtube.search().list(
part="snippet",
channelId=channel_id,
maxResults=max_results,
type="video",
order="date" # Fetch the latest videos
)
response = request.execute()
video_data = []
for item in response['items']:
video_id = item['id']['videoId']
title = item['snippet']['title']
thumbnail_url = item['snippet']['thumbnails']['high']['url']
# Fetch video statistics
stats = youtube.videos().list(part="statistics", id=video_id).execute()
view_count = stats['items'][0]['statistics'].get('viewCount', 0)
like_count = stats['items'][0]['statistics'].get('likeCount', 0)
video_data.append({
"title": title,
"video_id": video_id,
"thumbnail_url": thumbnail_url,
"views": int(view_count),
"likes": int(like_count)
})
return video_data
def save_thumbnails(video_data, output_folder="thumbnails"):
# Save thumbnails to a local folder
os.makedirs(output_folder, exist_ok=True)
for video in video_data:
thumbnail_url = video['thumbnail_url']
thumbnail_path = os.path.join(output_folder, f"{video['video_id']}.jpg")
with open(thumbnail_path, "wb") as f:
f.write(requests.get(thumbnail_url).content)
print(f"Saved thumbnail: {thumbnail_path}")
# Example usage
channel_id = "UCXXhZ89ffJvNg7XuDmS_Zlw" # Replace with a real channel ID
video_data = fetch_videos(channel_id, max_results=5)
print(video_data)
# Save thumbnails locally
save_thumbnails(video_data)