-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplaylist_extraction.py
More file actions
315 lines (250 loc) · 11.3 KB
/
playlist_extraction.py
File metadata and controls
315 lines (250 loc) · 11.3 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
"""
Playlist Extraction Example - PyTubeSearch
This example demonstrates how to work with YouTube playlists:
- Extracting playlist contents
- Getting playlist metadata
- Analyzing playlist structure
- Handling different playlist types
Usage:
python playlist_extraction.py
python playlist_extraction.py "PLI523PxNjNxwzlFjRBgDdPjlyA0_ZNtwJ"
"""
import sys
from pytubesearch import PyTubeSearch
def extract_playlist_example(playlist_id: str):
"""Extract complete playlist information."""
print(f"📋 Extracting playlist: {playlist_id}")
print("-" * 50)
with PyTubeSearch() as client:
try:
playlist = client.get_playlist_data(playlist_id)
# Metadata
print("📊 PLAYLIST METADATA:")
if playlist.metadata:
print(f" Raw metadata available: YES")
# Try to extract common metadata fields
if isinstance(playlist.metadata, dict):
title = playlist.metadata.get("title", "Unknown")
print(f" Title: {title}")
else:
print(" Raw metadata available: NO")
print()
# Content analysis
print("📹 CONTENT ANALYSIS:")
print(f" Total videos: {len(playlist.items)}")
if playlist.items:
# Video statistics
videos_with_duration = [v for v in playlist.items if v.length]
live_videos = [v for v in playlist.items if v.is_live]
print(f" Videos with duration info: {len(videos_with_duration)}")
print(f" Live videos: {len(live_videos)}")
print()
# Show first few videos
print("🎥 FIRST 5 VIDEOS:")
for i, video in enumerate(playlist.items[:5], 1):
print(f" {i}. {video.title}")
print(f" Channel: {video.channel_title or 'Unknown'}")
print(f" Duration: {video.length or 'Unknown'}")
print(f" Video ID: {video.id}")
if video.is_live:
print(" 🔴 LIVE")
print()
if len(playlist.items) > 5:
print(f" ... and {len(playlist.items) - 5} more videos")
else:
print(" No videos found in playlist")
except Exception as e:
print(f"❌ Playlist extraction failed: {e}")
def search_and_extract_playlists_example(query: str):
"""Search for playlists and extract their contents."""
print(f"🔍 Searching for playlists: {query}")
print("-" * 50)
with PyTubeSearch() as client:
try:
# Search for playlists
from pytubesearch import SearchOptions
playlist_options = [SearchOptions(type="playlist")]
results = client.search(query, options=playlist_options, limit=3)
if not results.items:
print("No playlists found")
return
print(f"Found {len(results.items)} playlists:")
print()
# Extract each playlist
for i, playlist_item in enumerate(results.items, 1):
print(f"{i}. 📋 {playlist_item.title}")
print(f" ID: {playlist_item.id}")
if playlist_item.video_count:
print(f" Video Count: {playlist_item.video_count}")
try:
# Get detailed playlist data
playlist_data = client.get_playlist_data(playlist_item.id, limit=3)
print(f" Extracted Videos: {len(playlist_data.items)}")
for j, video in enumerate(playlist_data.items, 1):
print(f" {j}. {video.title[:50]}...")
print(f" Channel: {video.channel_title}")
except Exception as e:
print(f" ❌ Failed to extract: {e}")
print()
except Exception as e:
print(f"❌ Playlist search failed: {e}")
def playlist_analysis_example(playlist_id: str):
"""Analyze playlist content and structure."""
print(f"🔬 Analyzing playlist: {playlist_id}")
print("-" * 50)
with PyTubeSearch() as client:
try:
playlist = client.get_playlist_data(playlist_id)
if not playlist.items:
print("Playlist is empty or couldn't be accessed")
return
# Channel analysis
print("📺 CHANNEL ANALYSIS:")
channel_count = {}
for video in playlist.items:
if video.channel_title:
channel_count[video.channel_title] = (
channel_count.get(video.channel_title, 0) + 1
)
print(f" Unique channels: {len(channel_count)}")
# Top channels
top_channels = sorted(channel_count.items(), key=lambda x: x[1], reverse=True)[:5]
print(" Top channels:")
for channel, count in top_channels:
print(f" {channel}: {count} videos")
print()
# Title analysis
print("📝 TITLE ANALYSIS:")
all_words = []
for video in playlist.items:
all_words.extend(video.title.lower().split())
# Word frequency
word_count = {}
for word in all_words:
if len(word) > 3: # Only count meaningful words
word_count[word] = word_count.get(word, 0) + 1
top_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:10]
print(f" Most common words:")
for word, count in top_words:
print(f" {word}: {count} times")
print()
# Duration analysis (if available)
print("⏱️ DURATION ANALYSIS:")
videos_with_duration = [v for v in playlist.items if v.length]
if videos_with_duration:
print(
f" Videos with duration info: {len(videos_with_duration)}/{len(playlist.items)}"
)
# Try to parse some common duration formats
short_videos = []
medium_videos = []
long_videos = []
for video in videos_with_duration:
duration_str = str(video.length)
if any(
indicator in duration_str for indicator in ["0:", "1:", "2:", "3:", "4:"]
):
short_videos.append(video)
elif any(
indicator in duration_str
for indicator in ["5:", "6:", "7:", "8:", "9:", "10:"]
):
medium_videos.append(video)
else:
long_videos.append(video)
print(f" Short videos (0-4 min): {len(short_videos)}")
print(f" Medium videos (5-10 min): {len(medium_videos)}")
print(f" Long videos (10+ min): {len(long_videos)}")
else:
print(" No duration information available")
print()
# Live content analysis
live_videos = [v for v in playlist.items if v.is_live]
print("🔴 LIVE CONTENT:")
print(f" Live videos: {len(live_videos)}")
if live_videos:
print(" Live video titles:")
for live_video in live_videos[:3]:
print(f" • {live_video.title}")
except Exception as e:
print(f"❌ Playlist analysis failed: {e}")
def compare_playlists_example(playlist_ids: list):
"""Compare multiple playlists."""
print(f"⚖️ Comparing {len(playlist_ids)} playlists")
print("-" * 50)
with PyTubeSearch() as client:
playlist_data = []
# Extract all playlists
for i, playlist_id in enumerate(playlist_ids, 1):
try:
print(f"📋 Extracting playlist {i}: {playlist_id}")
data = client.get_playlist_data(playlist_id, limit=50) # Limit for comparison
playlist_data.append(
{"id": playlist_id, "data": data, "video_count": len(data.items)}
)
print(f" ✅ {len(data.items)} videos extracted")
except Exception as e:
print(f" ❌ Failed: {e}")
playlist_data.append({"id": playlist_id, "data": None, "video_count": 0})
print()
# Comparison
print("📊 COMPARISON RESULTS:")
for i, pdata in enumerate(playlist_data, 1):
print(f"Playlist {i} ({pdata['id']}):")
if pdata["data"]:
print(f" Videos: {pdata['video_count']}")
# Channel diversity
channels = set()
for video in pdata["data"].items:
if video.channel_title:
channels.add(video.channel_title)
print(f" Unique channels: {len(channels)}")
# Live content
live_count = sum(1 for v in pdata["data"].items if v.is_live)
print(f" Live videos: {live_count}")
else:
print(" Status: Failed to extract")
print()
# Find common videos
if len([p for p in playlist_data if p["data"]]) >= 2:
print("🔗 OVERLAP ANALYSIS:")
valid_playlists = [p for p in playlist_data if p["data"]]
for i, playlist1 in enumerate(valid_playlists):
for j, playlist2 in enumerate(valid_playlists[i + 1 :], i + 1):
videos1 = set(v.id for v in playlist1["data"].items)
videos2 = set(v.id for v in playlist2["data"].items)
overlap = videos1.intersection(videos2)
print(f" Playlist {i+1} ↔ Playlist {j+1}: {len(overlap)} common videos")
def main():
"""Main function to run playlist extraction examples."""
print("📋 PyTubeSearch - Playlist Extraction Examples")
print("=" * 60)
# Default playlist IDs for examples
default_playlists = [
"PLI523PxNjNxwzlFjRBgDdPjlyA0_ZNtwJ", # Example tech playlist
"PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi", # 3Blue1Brown Linear Algebra
]
# Get playlist ID from command line or use default
if len(sys.argv) > 1:
playlist_id = sys.argv[1]
playlist_ids = [playlist_id]
else:
playlist_id = default_playlists[0]
playlist_ids = default_playlists
# Run examples
extract_playlist_example(playlist_id)
print("\n" + "=" * 60 + "\n")
search_and_extract_playlists_example("python tutorial")
print("\n" + "=" * 60 + "\n")
playlist_analysis_example(playlist_id)
print("\n" + "=" * 60 + "\n")
if len(playlist_ids) > 1:
compare_playlists_example(playlist_ids)
print("\n✅ All playlist extraction examples completed!")
print("\n💡 Next steps:")
print(" - Try pagination_example.py for handling large result sets")
print(" - Try batch_processing.py for multiple queries")
print(" - Try error_handling.py for robust error handling")
if __name__ == "__main__":
main()