-
Notifications
You must be signed in to change notification settings - Fork 43
feat: add actors search command to search Apify Store
#1047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patrikbraborec
wants to merge
2
commits into
master
Choose a base branch
from
feat/actors-search-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,140 @@ | ||||||
| import type { ActorStoreList } from 'apify-client'; | ||||||
| import { ApifyClient } from 'apify-client'; | ||||||
| import chalk from 'chalk'; | ||||||
|
|
||||||
| import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; | ||||||
| import { Args } from '../../lib/command-framework/args.js'; | ||||||
| import { Flags } from '../../lib/command-framework/flags.js'; | ||||||
| import { CompactMode, ResponsiveTable } from '../../lib/commands/responsive-table.js'; | ||||||
| import { CommandExitCodes } from '../../lib/consts.js'; | ||||||
| import { error, info, simpleLog } from '../../lib/outputs.js'; | ||||||
| import { getApifyClientOptions, printJsonToStdout } from '../../lib/utils.js'; | ||||||
|
|
||||||
| const pricingModelLabels: Record<string, string> = { | ||||||
| FREE: 'Free', | ||||||
| FLAT_PRICE_PER_MONTH: 'Subscription', | ||||||
| PRICE_PER_DATASET_ITEM: 'Pay per result', | ||||||
| PAY_PER_EVENT: 'Pay per event', | ||||||
| }; | ||||||
|
|
||||||
| function formatPricingModel(model?: string): string { | ||||||
| if (!model) return chalk.gray('Unknown'); | ||||||
|
|
||||||
| return pricingModelLabels[model] ?? model; | ||||||
| } | ||||||
|
|
||||||
| function truncateDescription(description?: string, maxLength = 60): string { | ||||||
| if (!description) return ''; | ||||||
|
|
||||||
| if (description.length <= maxLength) return description; | ||||||
|
|
||||||
| return `${description.slice(0, maxLength - 1)}…`; | ||||||
| } | ||||||
|
|
||||||
| export class ActorsSearchCommand extends ApifyCommand<typeof ActorsSearchCommand> { | ||||||
| static override name = 'search' as const; | ||||||
|
|
||||||
| static override description = | ||||||
| 'Searches Actors in the Apify Store.\n\nSearches the Apify Store for Actors matching the given query. Results can be filtered by category, author, pricing model, and more. This command does not require authentication.'; | ||||||
|
|
||||||
| static override args = { | ||||||
| query: Args.string({ | ||||||
| description: 'Search query to find Actors by title, name, description, username, or readme.', | ||||||
| required: false, | ||||||
| }), | ||||||
| }; | ||||||
|
|
||||||
| static override flags = { | ||||||
| 'sort-by': Flags.string({ | ||||||
| description: 'Sort order for the results.', | ||||||
| options: ['relevance', 'popularity', 'newest', 'lastUpdate'], | ||||||
| default: 'relevance', | ||||||
| }), | ||||||
| category: Flags.string({ | ||||||
| description: 'Filter by category (e.g. AI).', | ||||||
| }), | ||||||
| username: Flags.string({ | ||||||
| description: 'Filter by Actor author username.', | ||||||
| }), | ||||||
| 'pricing-model': Flags.string({ | ||||||
| description: 'Filter by pricing model.', | ||||||
| options: ['FREE', 'FLAT_PRICE_PER_MONTH', 'PRICE_PER_DATASET_ITEM', 'PAY_PER_EVENT'], | ||||||
| }), | ||||||
| limit: Flags.integer({ | ||||||
| description: 'Maximum number of results to return.', | ||||||
| default: 20, | ||||||
| }), | ||||||
| offset: Flags.integer({ | ||||||
| description: 'Number of results to skip for pagination.', | ||||||
| default: 0, | ||||||
| }), | ||||||
| }; | ||||||
|
|
||||||
| static override enableJsonFlag = true; | ||||||
|
|
||||||
| async run() { | ||||||
| const { query } = this.args; | ||||||
| const { json, sortBy, category, username, pricingModel, limit, offset } = this.flags; | ||||||
|
|
||||||
| const client = new ApifyClient(getApifyClientOptions()); | ||||||
|
|
||||||
| let result; | ||||||
|
|
||||||
| try { | ||||||
| result = await client.store().list({ | ||||||
| search: query, | ||||||
| sortBy, | ||||||
| category, | ||||||
| username, | ||||||
| pricingModel, | ||||||
| limit, | ||||||
| offset, | ||||||
| }); | ||||||
| } catch (err) { | ||||||
| process.exitCode = CommandExitCodes.RunFailed; | ||||||
| error({ | ||||||
| message: `Failed to search Apify Store: ${err instanceof Error ? err.message : String(err)}`, | ||||||
| stdout: true, | ||||||
| }); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (result.count === 0) { | ||||||
| if (json) { | ||||||
| printJsonToStdout(result); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| info({ message: 'No Actors found matching your search.', stdout: true }); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| if (json) { | ||||||
| printJsonToStdout(result); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| const table = new ResponsiveTable({ | ||||||
| allColumns: ['Name', 'Description', 'Users (30d)', 'Pricing'], | ||||||
| mandatoryColumns: ['Name', 'Pricing'], | ||||||
| columnAlignments: { | ||||||
| 'Users (30d)': 'right', | ||||||
| Name: 'left', | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| for (const item of result.items as ActorStoreList[]) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| table.pushRow({ | ||||||
| Name: `${item.title}\n${chalk.gray(`${item.username}/${item.name}`)}`, | ||||||
| Description: truncateDescription(item.description), | ||||||
| 'Users (30d)': chalk.cyan(`${item.stats?.totalUsers30Days ?? 0}`), | ||||||
| Pricing: formatPricingModel(item.currentPricingInfo?.pricingModel), | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| simpleLog({ | ||||||
| message: table.render(CompactMode.WebLikeCompact), | ||||||
| stdout: true, | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vladfrangu stdout or stderr ? 🤔