Skip to content

Commit

Permalink
Recurring to R2 (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
RossLitzenberger authored Aug 21, 2024
1 parent 78ae800 commit 46486ac
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
3 changes: 1 addition & 2 deletions scripts/upload-to-r2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import "dotenv/config";
import { getAllPost, filterPosts } from "../src/app/lib/data.js";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";

const response = await getAllPost();
const posts = filterPosts(response.data.posts.nodes);
const posts = await getAllPost();

if ("errors" in posts) {
console.error(posts.errors);
Expand Down
95 changes: 50 additions & 45 deletions src/app/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface Node {
}

export interface PageInfo {
hasNextPage: boolean | undefined;
hasNextPage: boolean;
endCursor: string | undefined;
}

Expand Down Expand Up @@ -99,62 +99,67 @@ function getStartAndEndOfDayInUTC() {
};
}

export async function getAllPost(
endCursor?: string | null,
): Promise<PostResponse> {
export async function getAllPost(): Promise<Post[]> {
// Get the current UTC date and time based on PST day
const [postedAfter, postedBefore] = Object.values(getStartAndEndOfDayInUTC());

return await fetch("https://api.producthunt.com/v2/api/graphql", {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.PH_API_KEY}`,
},
method: "POST",
body: JSON.stringify({
query: GET_ALL_POSTS,
variables: {
first: 100,
after: endCursor,
postedAfter: postedAfter,
postedBefore: postedBefore,
let hasNextPage = true;
let after = null;
const allPosts: Post[] = [];

while (hasNextPage) {
const response = await fetch("https://api.producthunt.com/v2/api/graphql", {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.PH_API_KEY}`,
},
}),
}).then((res) => res.json());
method: "POST",
body: JSON.stringify({
query: GET_ALL_POSTS,
variables: {
first: 100,
after: after,
postedAfter: postedAfter,
postedBefore: postedBefore,
},
}),
})
const result: PostResponse = await response.json();

const data = result.data?.posts;
allPosts.push(...data?.nodes);
after = data.pageInfo.endCursor;
hasNextPage = data.pageInfo.hasNextPage;
}

return allPosts;
}

export const filterPosts = (posts: Post[]) => {
return posts.filter((post) => {
// if the name, description or tagline contains "AI", "GPT", "artificial intelligence" or "machine learning" skip it
export const filterPosts = (posts: Post[]): Post[] => {
const excludedTerms = [
"ai",
"gpt",
"artificial intelligence",
"machine learning",
];

const containsExcludedTerm = (text: string): boolean =>
excludedTerms.some(term => text.toLowerCase().includes(term));

return posts.filter(post => {
if (
post.name.toLowerCase().includes("ai") ||
post.tagline.toLowerCase().includes("ai") ||
post.description.toLowerCase().includes("ai") ||
post.name.toLowerCase().includes("gpt") ||
post.tagline.toLowerCase().includes("gpt") ||
post.description.toLowerCase().includes("gpt") ||
post.name.toLowerCase().includes("artificial intelligence") ||
post.tagline.toLowerCase().includes("artificial intelligence") ||
post.description.toLowerCase().includes("artificial intelligence") ||
post.name.toLowerCase().includes("machine learning") ||
post.tagline.toLowerCase().includes("machine learning") ||
post.description.toLowerCase().includes("machine learning")
containsExcludedTerm(post.name) ||
containsExcludedTerm(post.tagline) ||
containsExcludedTerm(post.description)
) {
return false;
}

if (
post.topics.nodes.some(
(node) =>
node.name.toLowerCase().includes("ai") ||
node.description.toLowerCase().includes("ai") ||
node.name.toLowerCase().includes("gpt") ||
node.description.toLowerCase().includes("gpt") ||
node.name.toLowerCase().includes("artificial intelligence") ||
node.description.toLowerCase().includes("artificial intelligence") ||
node.name.toLowerCase().includes("machine learning") ||
node.description.toLowerCase().includes("machine learning"),
node =>
containsExcludedTerm(node.name) ||
containsExcludedTerm(node.description)
)
) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/app/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Loading() {
return (
<div>
<h1>Loading</h1>
</div>
)
}
6 changes: 4 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Metadata } from "next";
import { Analytics } from "@vercel/analytics/react";
import { Post } from "./lib/data";
import { filterPosts, Post } from "./lib/data";

const META_INFO = {
title: "OGHUNT - ZERO AI Slop™",
Expand Down Expand Up @@ -28,10 +28,12 @@ export const metadata: Metadata = {
};

export default async function Page() {
const posts: Post[] = await fetch(
const results: Post[] = await fetch(
"https://pub-3db3ed9313c4427fadfa81f0323b18f8.r2.dev/latest.json",
).then((res) => res.json());

const posts = filterPosts(results)

return (
<main className="flex min-h-screen flex-col p-4 md:p-24">
<h1 className="text-4xl md:text-5xl font-bold pb-6 md:pb-20 px-8 bg-gradient-to-r from-pink-300 to-orange-300 bg-clip-text text-transparent">
Expand Down

0 comments on commit 46486ac

Please sign in to comment.