fix(source): producthunt by offical api
This commit is contained in:
+2
-1
@@ -2,4 +2,5 @@ G_CLIENT_ID=
|
||||
G_CLIENT_SECRET=
|
||||
JWT_SECRET=
|
||||
INIT_TABLE=true
|
||||
ENABLE_CACHE=true
|
||||
ENABLE_CACHE=true
|
||||
PRODUCTHUNT_API_TOKEN=
|
||||
@@ -35,7 +35,6 @@ export default defineSource(async () => {
|
||||
Accept: "application/json, text/plain, */*",
|
||||
},
|
||||
})
|
||||
console.log(res)
|
||||
return res.items.map(movie => ({
|
||||
id: movie.id,
|
||||
title: movie.title,
|
||||
|
||||
@@ -1,28 +1,56 @@
|
||||
import * as cheerio from "cheerio"
|
||||
import process from "node:process"
|
||||
import type { NewsItem } from "@shared/types"
|
||||
|
||||
const apiKey = process.env.PRODUCTHUNT_API_TOKEN
|
||||
const token = `Bearer ${apiKey}`
|
||||
export default defineSource(async () => {
|
||||
const baseURL = "https://www.producthunt.com"
|
||||
const html: any = await myFetch(baseURL)
|
||||
const $ = cheerio.load(html)
|
||||
const $main = $("[data-test=homepage-section-0] [data-test^=post-item]")
|
||||
if (!apiKey) {
|
||||
throw new Error("PRODUCTHUNT_API_TOKEN is not set")
|
||||
}
|
||||
const query = `
|
||||
query {
|
||||
posts(first: 30, order: VOTES) {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
name
|
||||
tagline
|
||||
votesCount
|
||||
url
|
||||
slug
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const response: any = await myFetch("https://api.producthunt.com/v2/api/graphql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": token,
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ query }),
|
||||
})
|
||||
|
||||
const news: NewsItem[] = []
|
||||
$main.each((_, el) => {
|
||||
const a = $(el).find("a").first()
|
||||
const url = a.attr("href")
|
||||
const title = $(el).find("a[data-test^=post-name]").text().replace(/^\d+\.\s*/, "")
|
||||
const id = $(el).attr("data-test")?.replace("post-item-", "")
|
||||
const vote = $(el).find("[data-test=vote-button]").text()
|
||||
if (url && id && title) {
|
||||
const posts = response?.data?.posts?.edges || []
|
||||
|
||||
for (const edge of posts) {
|
||||
const post = edge.node
|
||||
if (post.id && post.name) {
|
||||
news.push({
|
||||
url: `${baseURL}${url}`,
|
||||
title,
|
||||
id,
|
||||
id: post.id,
|
||||
title: post.name,
|
||||
url: post.url || `https://www.producthunt.com/posts/${post.slug}`,
|
||||
extra: {
|
||||
info: `△︎ ${vote}`,
|
||||
info: ` △︎ ${post.votesCount || 0}`,
|
||||
hover: post.tagline,
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return news
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user