Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eco_project/backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YOUTUBE_API_KEY=
32 changes: 32 additions & 0 deletions eco_project/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,38 @@ def uploaded_file(filename):
def handle_chat_message(message):
emit('chat_message', message, broadcast=True)


from googleapiclient.discovery import build

@app.route('/api/youtube_videos')
def youtube_videos():
youtube_api_key = os.environ.get('YOUTUBE_API_KEY')
if not youtube_api_key:
return jsonify({"error": "YouTube API key is not configured."}), 500

try:
youtube = build('youtube', 'v3', developerKey=youtube_api_key)

search_response = youtube.search().list(
q="environmental protection",
part="snippet",
maxResults=10,
type="video"
).execute()

videos = []
for search_result in search_response.get("items", []):
videos.append({
"title": search_result["snippet"]["title"],
"video_id": search_result["id"]["videoId"]
})

return jsonify(videos)

except Exception as e:
return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
port = int(os.environ.get("PORT", 8080))
socketio.run(app, host='0.0.0.0', port=port, debug=False, allow_unsafe_werkzeug=True)
1 change: 1 addition & 0 deletions eco_project/backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ requests
gunicorn
google-cloud-logging
Flask-SocketIO
google-api-python-client
1 change: 1 addition & 0 deletions eco_project/backend/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ <h2>Community</h2>
<p>Share your ideas and connect with others.</p>
<ul>
<li><a href="videos.html">Watch Videos</a></li>
<li><a href="youtube.html">Watch YouTube Videos</a></li>
<li><a href="camera.html">Publish Videos/Photos</a></li>
<li><a href="forest_seeds.html">Forest Seeds Promotion</a></li>
<li><a href="chat.html">Join the Chat</a></li>
Expand Down
23 changes: 23 additions & 0 deletions eco_project/backend/static/youtube.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.video-container {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
padding: 20px;
}

.video-item {
width: 300px;
margin: 15px;
border: 1px solid #ccc;
box-shadow: 0 0 5px rgba(0,0,0,0.1);
}

.video-item iframe {
width: 100%;
height: 170px;
}

.video-item-title {
padding: 10px;
font-weight: bold;
}
30 changes: 30 additions & 0 deletions eco_project/backend/static/youtube.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YouTube Videos - Environment Protection</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="youtube.css">
</head>
<body>
<header>
<h1>YouTube Video Gallery</h1>
<nav>
<a href="index.html">Home</a>
</nav>
</header>
<main>
<section id="videos">
<h2>Featured Videos</h2>
<div class="video-container">
<!-- Video embeds will go here -->
</div>
</section>
</main>
<footer>
<p>&copy; 2025 Environment Protection Initiative</p>
</footer>
<script src="youtube.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions eco_project/backend/static/youtube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
document.addEventListener('DOMContentLoaded', () => {
const videoContainer = document.querySelector('.video-container');

async function fetchVideos() {
try {
const response = await fetch('/api/youtube_videos');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const videos = await response.json();
displayVideos(videos);
} catch (error) {
videoContainer.innerHTML = `<p>Error fetching videos: ${error.message}</p>`;
}
}

function displayVideos(videos) {
if (videos.length === 0) {
videoContainer.innerHTML = '<p>No videos to display.</p>';
return;
}
let html = '';
videos.forEach(video => {
const embedUrl = `https://www.youtube.com/embed/${video.video_id}`;
html += `
<div class="video-item">
<iframe src="${embedUrl}" frameborder="0" allowfullscreen></iframe>
<div class="video-item-title">${video.title}</div>
</div>
`;
});
videoContainer.innerHTML = html;
}

fetchVideos();
});
Loading