agenticworkflowsspace commited on
Commit
c4a46ce
·
verified ·
1 Parent(s): 9df2e18

Upload tools/get_youtube_viral.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tools/get_youtube_viral.py +86 -0
tools/get_youtube_viral.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ from googleapiclient.discovery import build
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+ def get_youtube_viral(query=None, max_results=6, mode="search"):
9
+ """
10
+ mode='search' : search by query (niche research)
11
+ mode='trending': fetch YouTube trending videos (no query, uses chart=mostPopular)
12
+ """
13
+ api_key = os.getenv('YOUTUBE_API_KEY')
14
+ if not api_key:
15
+ print("Error: YOUTUBE_API_KEY not found in .env")
16
+ return []
17
+
18
+ try:
19
+ youtube = build('youtube', 'v3', developerKey=api_key)
20
+
21
+ if mode == "trending":
22
+ # Fetch actual YouTube trending
23
+ request = youtube.videos().list(
24
+ part='snippet,statistics',
25
+ chart='mostPopular',
26
+ regionCode='US',
27
+ maxResults=max_results
28
+ )
29
+ response = request.execute()
30
+ results = []
31
+ for item in response.get('items', []):
32
+ vid_id = item['id']
33
+ stats = item.get('statistics', {})
34
+ results.append({
35
+ 'title': item['snippet']['title'],
36
+ 'channel': item['snippet']['channelTitle'],
37
+ 'category': item['snippet'].get('categoryId', ''),
38
+ 'url': f"https://www.youtube.com/watch?v={vid_id}",
39
+ 'published_at': item['snippet']['publishedAt'],
40
+ 'views': stats.get('viewCount', '0'),
41
+ 'likes': stats.get('likeCount', '0'),
42
+ 'video_id': vid_id,
43
+ })
44
+ return results
45
+
46
+ else:
47
+ # Niche search
48
+ request = youtube.search().list(
49
+ q=query,
50
+ part='snippet',
51
+ type='video',
52
+ order='relevance',
53
+ maxResults=max_results,
54
+ publishedAfter='2026-03-01T00:00:00Z'
55
+ )
56
+ response = request.execute()
57
+ results = []
58
+ for item in response.get('items', []):
59
+ video_id = item['id']['videoId']
60
+ results.append({
61
+ 'title': item['snippet']['title'],
62
+ 'channel': item['snippet']['channelTitle'],
63
+ 'url': f"https://www.youtube.com/watch?v={video_id}",
64
+ 'published_at': item['snippet']['publishedAt'],
65
+ 'video_id': video_id,
66
+ })
67
+ return results
68
+
69
+ except Exception as e:
70
+ print(f"Error: {e}")
71
+ return []
72
+
73
+
74
+ if __name__ == "__main__":
75
+ mode = sys.argv[1] if len(sys.argv) > 1 else "search"
76
+ query = sys.argv[2] if len(sys.argv) > 2 else "AI trends 2026"
77
+
78
+ videos = get_youtube_viral(query=query, mode=mode)
79
+ for v in videos:
80
+ print(f"TITLE: {v['title']}")
81
+ print(f"CHANNEL: {v['channel']}")
82
+ print(f"URL: {v['url']}")
83
+ print(f"PUBLISHED: {v['published_at']}")
84
+ if 'views' in v:
85
+ print(f"VIEWS: {v['views']}")
86
+ print("---")