Spaces:
Running
Running
File size: 1,444 Bytes
c437756 |
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 |
import requests
import os
import json
from dotenv import load_dotenv
load_dotenv()
PRODUCT_HUNT_BASE_URL = "https://api.producthunt.com/v2/api/graphql"
def fetch_product_hunt_posts(industry, product_type):
"""Fetches the top 10 Product Hunt posts matching the given industry and product type."""
developer_token = os.getenv("PRODUCT_HUNT_DEVELOPER_TOKEN")
# Updated GraphQL Query
query = """
query {
posts(first: 10, order: VOTES) {
edges {
node {
id
name
tagline
votesCount
website
commentsCount
}
}
}
}
"""
# Send GraphQL Request (No filter variable)
headers = {"Authorization": f"Bearer {developer_token}"}
response = requests.post(
PRODUCT_HUNT_BASE_URL,
json={"query": query},
headers=headers
)
response.raise_for_status()
# Process Results
try:
data = response.json()
print(json.dumps(data, indent=2)) # Print raw data for debugging
posts = [edge["node"] for edge in data["data"]["posts"]["edges"]]
return posts
except KeyError:
print("Unexpected API response format.")
return []
# Example Usage:
if __name__ == "__main__":
industry = "tech"
product_type = "saas"
posts = fetch_product_hunt_posts(industry, product_type)
print(json.dumps(posts, indent=2))
|