11// eslint-disable-next-line import/extensions
2- import type { Tool } from '@mastra/core/tools' ;
3- // eslint-disable-next-line import/extensions
42import { createTool } from '@mastra/core/tools' ;
53import { ApifyClient , log } from 'apify' ;
64import { z } from 'zod' ;
@@ -17,46 +15,45 @@ const instagramScraperOutputSchema = z.object({
1715} ) ;
1816
1917// Define the Instagram Scraper Tool
20- export const instagramScraperTool : Tool < typeof instagramScraperInputSchema , typeof instagramScraperOutputSchema > =
21- createTool ( {
22- id : 'instagram-profile-posts-scraper' ,
23- description : "Tool to scrape Instagram profile posts using Apify's Instagram Scraper." ,
24- inputSchema : instagramScraperInputSchema ,
25- outputSchema : instagramScraperOutputSchema ,
26- execute : async ( { context } ) => {
27- const token = process . env . APIFY_TOKEN ;
28- if ( ! token ) {
29- throw new Error ( 'APIFY_TOKEN environment variable is missing!' ) ;
30- }
31-
32- const { handle, maxPosts } = context ;
33- const runInput = {
34- directUrls : [ `https://www.instagram.com/${ handle } /` ] ,
35- resultsLimit : maxPosts ,
36- resultsType : 'posts' ,
37- searchLimit : 1 ,
38- } ;
39-
40- const apifyClient = new ApifyClient ( { token } ) ;
41-
42- // Call the Apify Instagram Scraper Actor
43- const run = await apifyClient . actor ( 'apify/instagram-scraper' ) . call ( runInput ) ;
44- if ( ! run ) {
45- throw new Error ( 'Failed to start the Actor apify/instagram-scraper' ) ;
46- }
47-
48- // Fetch dataset items
49- const datasetId = run . defaultDatasetId ;
50- const dataset = await apifyClient . dataset ( datasetId ) . listItems ( ) ;
51- const datasetItems : unknown [ ] = dataset . items ;
52-
53- // Validate and convert dataset items to InstagramPosts
54- try {
55- const posts : InstagramPosts = InstagramPosts . fromRaw ( datasetItems ) ;
56- return { posts : posts . root } ;
57- } catch ( error ) {
58- log . warning ( `Received invalid dataset items: ${ JSON . stringify ( datasetItems ) } . Error: ${ error } ` ) ;
59- throw new Error ( 'Received invalid dataset items.' ) ;
60- }
61- } ,
62- } ) ;
18+ export const instagramScraperTool = createTool ( {
19+ id : 'instagram-profile-posts-scraper' ,
20+ description : "Tool to scrape Instagram profile posts using Apify's Instagram Scraper." ,
21+ inputSchema : instagramScraperInputSchema ,
22+ outputSchema : instagramScraperOutputSchema ,
23+ execute : async ( inputData ) => {
24+ const token = process . env . APIFY_TOKEN ;
25+ if ( ! token ) {
26+ throw new Error ( 'APIFY_TOKEN environment variable is missing!' ) ;
27+ }
28+
29+ const { handle, maxPosts } = inputData ;
30+ const runInput = {
31+ directUrls : [ `https://www.instagram.com/${ handle } /` ] ,
32+ resultsLimit : maxPosts ,
33+ resultsType : 'posts' ,
34+ searchLimit : 1 ,
35+ } ;
36+
37+ const apifyClient = new ApifyClient ( { token } ) ;
38+
39+ // Call the Apify Instagram Scraper Actor
40+ const run = await apifyClient . actor ( 'apify/instagram-scraper' ) . call ( runInput ) ;
41+ if ( ! run ) {
42+ throw new Error ( 'Failed to start the Actor apify/instagram-scraper' ) ;
43+ }
44+
45+ // Fetch dataset items
46+ const datasetId = run . defaultDatasetId ;
47+ const dataset = await apifyClient . dataset ( datasetId ) . listItems ( ) ;
48+ const datasetItems : unknown [ ] = dataset . items ;
49+
50+ // Validate and convert dataset items to InstagramPosts
51+ try {
52+ const posts : InstagramPosts = InstagramPosts . fromRaw ( datasetItems ) ;
53+ return { posts : posts . root } ;
54+ } catch ( error ) {
55+ log . warning ( `Received invalid dataset items: ${ JSON . stringify ( datasetItems ) } . Error: ${ error } ` ) ;
56+ throw new Error ( 'Received invalid dataset items.' ) ;
57+ }
58+ } ,
59+ } ) ;
0 commit comments