Repository navigation
Expand file tree
/
Copy pathmodrinth.js
More file actions
282 lines (236 loc) · 8.4 KB
/
Copy pathmodrinth.js
File metadata and controls
282 lines (236 loc) · 8.4 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// ============================================
// LOQOR.DEV — Modrinth API Integration
// ============================================
const MODRINTH_API = 'https://api.modrinth.com/v2';
const USER_ID = 'Loqor';
const CACHE_KEY = 'loqor_mods_cache_v1';
const CACHE_TTL = 1000 * 60 * 60 * 12; // 12 hours
// ============================================
// CACHING
// ============================================
function loadCache() {
try {
const cached = localStorage.getItem(CACHE_KEY);
if (!cached) return null;
const { data, timestamp } = JSON.parse(cached);
const isExpired = Date.now() - timestamp > CACHE_TTL;
if (isExpired) {
localStorage.removeItem(CACHE_KEY);
return null;
}
return data;
} catch (e) {
console.warn('Cache read error:', e);
return null;
}
}
function saveCache(data) {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify({
data,
timestamp: Date.now()
}));
} catch (e) {
console.warn('Cache write error:', e);
}
}
// ============================================
// API FUNCTIONS
// ============================================
async function fetchUserProjects() {
const response = await fetch(`${MODRINTH_API}/user/${USER_ID}/projects`, {
headers: {
'User-Agent': 'loqor.dev/1.0'
}
});
if (!response.ok) {
throw new Error(`Modrinth API error: ${response.status}`);
}
return response.json();
}
async function fetchProjectVersions(slug) {
const response = await fetch(`${MODRINTH_API}/project/${slug}/version`, {
headers: {
'User-Agent': 'loqor.dev/1.0'
}
});
if (!response.ok) {
throw new Error(`Modrinth API error: ${response.status}`);
}
return response.json();
}
// ============================================
// FORMATTING
// ============================================
function formatNumber(num) {
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
}
return num.toString();
}
function formatProjectType(type) {
const types = {
'mod': 'Minecraft Mod',
'modpack': 'Modpack',
'resourcepack': 'Resource Pack',
'shader': 'Shader',
'datapack': 'Data Pack',
'plugin': 'Plugin'
};
return types[type] || type;
}
function timeAgo(dateString) {
const date = new Date(dateString);
const now = new Date();
const seconds = Math.floor((now - date) / 1000);
const intervals = [
{ label: 'year', seconds: 31536000 },
{ label: 'month', seconds: 2592000 },
{ label: 'week', seconds: 604800 },
{ label: 'day', seconds: 86400 },
{ label: 'hour', seconds: 3600 },
{ label: 'minute', seconds: 60 }
];
for (const interval of intervals) {
const count = Math.floor(seconds / interval.seconds);
if (count >= 1) {
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ago`;
}
}
return 'just now';
}
// ============================================
// RENDERING
// ============================================
function renderModCard(project) {
const iconUrl = project.icon_url || './img/modrinth.png';
const modrinthUrl = `https://modrinth.com/${project.project_type}/${project.slug}`;
return `
<article class="mod-card">
<div class="mod-card-header">
<img src="${iconUrl}" alt="${project.title}" class="mod-icon" loading="lazy">
<div class="mod-info">
<span class="mod-type">${formatProjectType(project.project_type)}</span>
<h4 class="mod-title">${project.title}</h4>
</div>
</div>
<p class="mod-desc">${project.description}</p>
<div class="mod-stats">
<div class="mod-stat">
<span class="mod-stat-value">${formatNumber(project.downloads)}</span>
<span class="mod-stat-label">Downloads</span>
</div>
<a href="${modrinthUrl}" target="_blank" class="mod-link">View Project →</a>
</div>
</article>
`;
}
function renderTimelineItem(version, projectTitle) {
return `
<div class="timeline-item">
<span class="timeline-dot"></span>
<div class="timeline-content">
<span class="timeline-project">${projectTitle}</span>
<span class="timeline-version">${version.version_number}</span>
</div>
<span class="timeline-date">${timeAgo(version.date_published)}</span>
</div>
`;
}
function updateStats(projects) {
const totalDownloads = projects.reduce((sum, p) => sum + p.downloads, 0);
const projectCount = projects.length;
const downloadsEl = document.getElementById('total-downloads');
const countEl = document.getElementById('project-count');
if (downloadsEl) {
downloadsEl.textContent = formatNumber(totalDownloads);
}
if (countEl) {
countEl.textContent = projectCount;
}
}
// ============================================
// MAIN INITIALIZATION
// ============================================
async function initMods() {
const modsGrid = document.getElementById('mods-grid');
const timelineList = document.getElementById('timeline-list');
if (!modsGrid) {
console.warn('Mods grid element not found');
return;
}
// Try to load from cache first
const cached = loadCache();
if (cached) {
console.log('Loading from cache');
renderContent(cached.projects, cached.versions, modsGrid, timelineList);
return;
}
try {
// Fetch projects
const projects = await fetchUserProjects();
// Sort by downloads (most popular first)
projects.sort((a, b) => b.downloads - a.downloads);
// Fetch versions for timeline (get latest versions from each project)
const versionsPromises = projects.slice(0, 10).map(async (project) => {
try {
const versions = await fetchProjectVersions(project.slug);
return versions.slice(0, 3).map(v => ({
...v,
projectTitle: project.title
}));
} catch (e) {
console.warn(`Failed to fetch versions for ${project.slug}:`, e);
return [];
}
});
const allVersions = (await Promise.all(versionsPromises)).flat();
// Sort by date and take latest 8
allVersions.sort((a, b) => new Date(b.date_published) - new Date(a.date_published));
const recentVersions = allVersions.slice(0, 8);
// Cache the data
saveCache({ projects, versions: recentVersions });
// Render
renderContent(projects, recentVersions, modsGrid, timelineList);
} catch (error) {
console.error('Failed to load Modrinth data:', error);
// Show error state
modsGrid.innerHTML = `
<div class="mod-card" style="grid-column: 1 / -1; text-align: center; padding: 3rem;">
<p style="color: var(--text-muted);">Failed to load projects. Please try again later.</p>
</div>
`;
if (timelineList) {
timelineList.innerHTML = `
<div class="timeline-item" style="justify-content: center;">
<p style="color: var(--text-muted);">Failed to load updates.</p>
</div>
`;
}
}
}
function renderContent(projects, versions, modsGrid, timelineList) {
// Update stats
updateStats(projects);
// Render mods grid
modsGrid.innerHTML = projects.map(renderModCard).join('');
// Render timeline
if (timelineList && versions.length > 0) {
timelineList.innerHTML = versions.map(v => renderTimelineItem(v, v.projectTitle)).join('');
} else if (timelineList) {
timelineList.innerHTML = `
<div class="timeline-item" style="justify-content: center;">
<p style="color: var(--text-muted);">No recent updates.</p>
</div>
`;
}
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initMods);
} else {
initMods();
}