-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
84 lines (73 loc) · 3.18 KB
/
Copy pathseed.js
File metadata and controls
84 lines (73 loc) · 3.18 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
const mongoose = require('mongoose');
require('dotenv').config();
const Product = require('./models/product.model.js');
const User = require('./models/user.model.js');
const bcrypt = require('bcryptjs');
const axios = require('axios');
const YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY;
const SEARCH_QUERIES = [
{ q: "Full HTML Course", cat: "Programming", price: 0 },
{ q: "CSS Tailwind Course", cat: "Design", price: 19.99 },
{ q: "Python Programming Full Course", cat: "Programming", price: 29.99 },
{ q: "Machine Learning Course", cat: "Science", price: 49.99 },
{ q: "React JS Course", cat: "Programming", price: 0 }
];
mongoose.connect(process.env.MONGO_URI)
.then(async () => {
console.log("Connected to MongoDB");
// 1. Setup Admin User
await User.deleteMany({});
const hashedPassword = await bcrypt.hash("shofi1234", 10);
await User.create({
email: "sayakumar9@gmail.com",
password: hashedPassword
});
console.log("Admin user created: sayakumar9@gmail.com");
// 2. Clear Products
await Product.deleteMany({});
console.log("Cleared existing products");
// 3. Fetch from YouTube
let allCourses = [];
for (const query of SEARCH_QUERIES) {
try {
console.log(`Fetching videos for: ${query.q}...`);
const response = await axios.get(`https://www.googleapis.com/youtube/v3/search`, {
params: {
part: 'snippet',
q: query.q,
type: 'video',
maxResults: 3, // Get top 3 for each topic
key: YOUTUBE_API_KEY
}
});
const videos = response.data.items.map(item => ({
name: item.snippet.title,
description: item.snippet.description,
price: query.price,
quantity: Math.floor(Math.random() * 100) + 10,
image: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
category: query.cat,
videoUrl: `https://www.youtube.com/watch?v=${item.id.videoId}`
}));
allCourses = [...allCourses, ...videos];
} catch (error) {
console.error(`Failed to fetch for ${query.q}:`, error.message);
// Fallback if quota exceeded or error
allCourses.push({
name: `${query.q} (Manual Fallback)`,
description: `A great course about ${query.q}.`,
price: query.price,
quantity: 50,
image: "https://via.placeholder.com/320x180?text=Course",
category: query.cat,
videoUrl: "https://www.youtube.com"
});
}
}
await Product.insertMany(allCourses);
console.log(`Database seeded with ${allCourses.length} courses from YouTube!`);
process.exit(0);
}).catch((err) => {
console.log("Connection failed", err);
process.exit(1);
});