|
|
import { NextResponse } from 'next/server'; |
|
|
import axios from 'axios'; |
|
|
|
|
|
export async function POST(request) { |
|
|
try { |
|
|
const { username, resourceType, resourceName } = await request.json(); |
|
|
|
|
|
if (!username) { |
|
|
return NextResponse.json( |
|
|
{ error: 'Username is required' }, |
|
|
{ status: 400 } |
|
|
); |
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await axios.get( |
|
|
`https://huggingface.co/${username}`, |
|
|
{ |
|
|
headers: { |
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' |
|
|
} |
|
|
} |
|
|
); |
|
|
|
|
|
const html = response.data; |
|
|
|
|
|
|
|
|
|
|
|
const avatarMatch = html.match(/https:\/\/cdn-avatars\.huggingface\.co\/[^"'\s]+\.(png|jpg|jpeg|webp)/); |
|
|
|
|
|
let avatarUrl = null; |
|
|
if (avatarMatch && avatarMatch[0]) { |
|
|
avatarUrl = avatarMatch[0]; |
|
|
} |
|
|
|
|
|
|
|
|
if (!avatarUrl) { |
|
|
|
|
|
const metaAvatarMatch = html.match(/<meta[^>]+property="og:image"[^>]+content="([^"]+)"/); |
|
|
if (metaAvatarMatch && metaAvatarMatch[1]) { |
|
|
avatarUrl = metaAvatarMatch[1]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!avatarUrl) { |
|
|
|
|
|
avatarUrl = `https://huggingface.co/front/assets/huggingface_logo-noborder.svg`; |
|
|
} |
|
|
|
|
|
|
|
|
let fullName = username; |
|
|
const nameMatch = html.match(/<h1[^>]*>([^<]+)<\/h1>/); |
|
|
if (nameMatch && nameMatch[1]) { |
|
|
fullName = nameMatch[1].trim(); |
|
|
} |
|
|
|
|
|
return NextResponse.json({ |
|
|
username: username, |
|
|
fullName: fullName, |
|
|
avatarUrl: avatarUrl, |
|
|
profileUrl: `https://huggingface.co/${username}`, |
|
|
type: 'user', |
|
|
resourceType: resourceType || null, |
|
|
resourceName: resourceName || null |
|
|
}); |
|
|
|
|
|
} catch (fetchError) { |
|
|
console.log('Error fetching profile page:', fetchError.message); |
|
|
|
|
|
|
|
|
return NextResponse.json({ |
|
|
username: username, |
|
|
fullName: username, |
|
|
avatarUrl: `https://huggingface.co/front/assets/huggingface_logo-noborder.svg`, |
|
|
profileUrl: `https://huggingface.co/${username}`, |
|
|
type: 'user', |
|
|
resourceType: resourceType || null, |
|
|
resourceName: resourceName || null |
|
|
}); |
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
console.error('Error in HuggingFace API route:', error); |
|
|
return NextResponse.json( |
|
|
{ error: 'Failed to fetch profile data' }, |
|
|
{ status: 500 } |
|
|
); |
|
|
} |
|
|
} |