|
| 1 | +''' |
| 2 | + Python program that uses the YouTube Data API to fetch the top 10 trending YouTube videos. |
| 3 | +You’ll need to have an API key from Google Cloud Platform to use the YouTube Data API. |
| 4 | + |
| 5 | +First, install the google-api-python-client library if you haven’t already: |
| 6 | +pip install google-api-python-client |
| 7 | + |
| 8 | +Replace 'YOUR_API_KEY' with your actual API key. This script will fetch and print the titles, |
| 9 | +channels, and view counts of the top 10 trending YouTube videos in India. |
| 10 | +You can change the regionCode to any other country code if needed. |
| 11 | + |
| 12 | +Then, you can use the following code: |
| 13 | + |
| 14 | +''' |
| 15 | + |
| 16 | +from googleapiclient.discovery import build |
| 17 | + |
| 18 | +# Replace with your own API key |
| 19 | +API_KEY = 'YOUR_API_KEY' |
| 20 | +YOUTUBE_API_SERVICE_NAME = 'youtube' |
| 21 | +YOUTUBE_API_VERSION = 'v3' |
| 22 | + |
| 23 | +def get_trending_videos(): |
| 24 | + youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY) |
| 25 | + |
| 26 | + # Call the API to get the top 10 trending videos |
| 27 | + request = youtube.videos().list( |
| 28 | + part='snippet,statistics', |
| 29 | + chart='mostPopular', |
| 30 | + regionCode='IN', # Change this to your region code |
| 31 | + maxResults=10 |
| 32 | + ) |
| 33 | + response = request.execute() |
| 34 | + |
| 35 | + # Print the video details |
| 36 | + for item in response['items']: |
| 37 | + title = item['snippet']['title'] |
| 38 | + channel = item['snippet']['channelTitle'] |
| 39 | + views = item['statistics']['viewCount'] |
| 40 | + print(f'Title: {title}\nChannel: {channel}\nViews: {views}\n') |
| 41 | + |
| 42 | +if __name__ == '__main__': |
| 43 | + get_trending_videos() |
0 commit comments