-
-
Save marr/c574e7a854223e828e652753d08dd117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient, Prisma } from "@prisma/client"; | |
import { getActivity, TweetProps } from "~/api/twitter"; | |
// import { getActivity } from "~/api/twitter.mock"; | |
import { reduceByField } from "~/utils/general"; | |
const prisma = new PrismaClient(); | |
const byId = reduceByField("id"); | |
async function main() { | |
const response = await getActivity(); | |
// const allTweets = response.includes.tweets.reduce(byId, {}); | |
const allUsers = response.includes.users.reduce(byId, {}); | |
const mediaItems = response.includes.media.reduce(byId, {}); | |
const likes: any[] = response.data.map(like => { | |
const { referencedTweets, inReplyToUserId, attachments, ...tweet } = like; | |
return { | |
...tweet, | |
mediaKeys: attachments?.mediaKeys, | |
author: { | |
connectOrCreate: { | |
where: { id: tweet.authorId }, | |
create: { | |
...allUsers[tweet.authorId], | |
url: `https://twitter.com/${allUsers[tweet.authorId].username}` | |
} as Prisma.TwitterUserCreateInput | |
} | |
}, | |
media: { | |
connectOrCreate: tweet.attachments?.mediaKeys.map((mediaKey:string) => ({ | |
where: { mediaKey }, | |
create: mediaItems[mediaKey] as Prisma.MediaCreateInput | |
})) | |
} | |
} | |
}); | |
try { | |
const xxx = await Promise.all(likes.map(({ id, ...like }) => { | |
return prisma.tweet.upsert({ | |
where: { | |
id | |
}, | |
update: like as Prisma.TweetUpdateInput, | |
create: { | |
id, | |
...like | |
} as Prisma.TweetCreateInput | |
}); | |
})); | |
console.log(xxx); | |
} catch (error) { | |
if (error instanceof Prisma.PrismaClientKnownRequestError) { | |
if (error.code === 'P2002') { | |
console.log('Tweet already exists, skipping'); | |
} | |
} else{ | |
console.error(error); | |
} | |
} | |
} | |
export default function crawl() { | |
return main() | |
.catch((e) => { | |
throw e; | |
}) | |
.finally(async () => { | |
await prisma.$disconnect(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment