Spaces:
Runtime error
Runtime error
File size: 3,065 Bytes
def63ae a57c7fe def63ae c3d2e83 def63ae a57c7fe def63ae |
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 |
from flask import Flask, jsonify
from flask_cors import CORS
from googleapiclient.discovery import build
import random
app = Flask(__name__)
CORS(app)
API_KEY = 'AIzaSyDO_RMlMevaTD5KHAeY415sKJLqWqrf0mQ'
youtube = build('youtube', 'v3', developerKey=API_KEY)
def get_video_info(video_id):
request = youtube.videos().list(
part='snippet,statistics',
id=video_id
)
response = request.execute()
if not response['items']:
return None
item = response['items'][0]
return {
'title': item['snippet']['title'],
'views': item['statistics'].get('viewCount', 0),
'likes': item['statistics'].get('likeCount', 0),
'channel': item['snippet']['channelTitle'],
'video_id': item['id'],
'thumbnail': item['snippet']['thumbnails']['high']['url'] # Add this line
}
def get_trending_videos():
request = youtube.videos().list(
part='snippet,statistics',
chart='mostPopular',
regionCode='US',
maxResults=50
)
response = request.execute()
videos = []
for item in response['items']:
video_data = {
'title': item['snippet']['title'],
'video_id': item['id'],
'views': item['statistics'].get('viewCount', 0),
'likes': item['statistics'].get('likeCount', 0),
'channel': item['snippet']['channelTitle'],
'thumbnail': item['snippet']['thumbnails']['high']['url'] # Add this line
}
videos.append(video_data)
return videos
@app.route('/random')
def random_videos():
try:
videos = get_trending_videos()
random_selection = random.sample(videos, min(5, len(videos)))
return jsonify(random_selection)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/trending')
def trending_videos():
try:
videos = get_trending_videos()
return jsonify(videos)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/info/<video_id>')
def video_info(video_id):
try:
info = get_video_info(video_id)
if info is None:
return jsonify({'error': 'Video not found'}), 404
return jsonify(info)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/search/<query>')
def search_videos(query):
try:
request = youtube.search().list(
part='snippet',
q=query,
type='video',
maxResults=10
)
response = request.execute()
videos = []
for item in response['items']:
video_id = item['id']['videoId']
video_info = get_video_info(video_id)
if video_info:
videos.append(video_info)
return jsonify(videos)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True) |