Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // import chromium from '@sparticuz/chromium-min';
- // import puppeteer from 'puppeteer-core';
- import * as cheerio from 'cheerio';
- import { ScrapperServiceResponse } from './scrapper.schema';
- import { Address, privateKeyToAccount } from 'viem/accounts';
- import { env } from '@/app/lib/env';
- import { createWalletClient, http, parseEther, WalletClient } from 'viem';
- import { degen } from 'viem/chains';
- import { actionsService } from '@/app/modules/actions/actions.service';
- import { ETH_NATIVE_ADDRESS, EXT_REVIEW_ACTION_ID } from '@/app/lib/constants';
- import { UserType } from '@/app/lib/types';
- class ChromeStoreScraperService {
- async scrapeReviews(user: UserType): Promise<ScrapperServiceResponse> {
- const isCompleted = await actionsService.checkIfActionIsCompleted(
- user,
- EXT_REVIEW_ACTION_ID
- );
- if (isCompleted) {
- return {
- type: 'error',
- error: 'User has already completed the action',
- status: 400
- };
- }
- try {
- const response = await fetch(
- 'https://chromewebstore.google.com/detail/pjolohbafcgpmdingblobfffkmobopec/reviews'
- );
- const html = await response.text();
- const $ = cheerio.load(html);
- // Create today's date in PST
- const today = new Date();
- // Convert to PST (UTC-8)
- const pstOffset = -8;
- const userOffset = today.getTimezoneOffset() / 60;
- const offsetDiff = pstOffset - userOffset;
- const pstDate = new Date(today.getTime() + offsetDiff * 60 * 60 * 1000);
- const todayString = pstDate.toLocaleDateString('en-US', {
- month: 'short',
- day: 'numeric',
- year: 'numeric'
- });
- const reviews = $('section.T7rvce')
- .map((_, element) => {
- const author = $(element).find('.LfYwpe').text().trim();
- const ratingText = $(element)
- .find('[aria-label*="stars"]')
- .attr('aria-label');
- const rating = ratingText
- ? parseInt(ratingText.match(/(\d+)/)?.[1] || '0')
- : 0;
- const date = $(element).find('.ydlbEf').text().trim();
- const content = $(element).find('.f2bEpf').text().trim();
- return {
- author,
- rating,
- date,
- content
- };
- })
- .get();
- console.log('Scraped reviews:', reviews);
- console.log('Today (UTC):', todayString);
- console.log('First review date:', reviews[0]?.date);
- const hasReviewToday = reviews.some(
- (review) => review.date === todayString
- );
- if (!hasReviewToday) {
- return {
- type: 'error',
- error: 'No review found today',
- status: 400
- };
- }
- await this.tipUser(user);
- return {
- type: 'success',
- data: {
- reviews,
- total_reviews: reviews.length,
- has_review_today: hasReviewToday
- },
- status: 200
- };
- } catch (error) {
- return {
- type: 'error',
- error:
- error instanceof Error
- ? error.message
- : 'An unexpected error occurred',
- status: 500
- };
- }
- }
- // Rest of the class remains the same
- async tipUser(user: UserType) {
- const amount = 5;
- const walletClient = this.generateWallet();
- const hash = await this.sendTransaction(
- walletClient,
- user.address as Address,
- amount
- );
- await actionsService.completeAction(user, {
- action_id: EXT_REVIEW_ACTION_ID,
- value: {
- hash,
- chain_id: degen.id,
- amount,
- token_address: ETH_NATIVE_ADDRESS,
- symbol: 'degen'
- },
- activity_text: `Left a review for the extension and earned ${amount} $DEGEN! 🎉`
- });
- }
- private generateWallet() {
- const account = privateKeyToAccount(env.TIP_SHOTS_PRIVATE_KEY as Address);
- return createWalletClient({
- account,
- chain: degen,
- transport: http()
- });
- }
- private async sendTransaction(
- walletClient: WalletClient,
- userAddress: Address,
- amount: number
- ) {
- const hash = await walletClient.sendTransaction({
- account: walletClient.account!,
- to: userAddress,
- value: parseEther(amount.toString()),
- chain: degen
- });
- return hash;
- }
- }
- export const chromeStoreScraperService = new ChromeStoreScraperService();
Advertisement
Add Comment
Please, Sign In to add comment