-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsearch.py
More file actions
342 lines (305 loc) · 11.3 KB
/
search.py
File metadata and controls
342 lines (305 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
"""
CLI commands for search operations (parameter-based discovery).
"""
import click
from typing import Optional
from ..utils import create_client, output_result, handle_error
@click.group("search")
@click.option(
"--api-key",
envvar="BRIGHTDATA_API_TOKEN",
help="Bright Data API key (or set BRIGHTDATA_API_TOKEN env var)",
)
@click.option(
"--output-format",
type=click.Choice(["json", "pretty", "minimal", "markdown"], case_sensitive=False),
default="json",
help="Output format (json, pretty, minimal, markdown)",
)
@click.option("--output-file", type=click.Path(), help="Save output to file")
@click.pass_context
def search_group(
ctx: click.Context, api_key: Optional[str], output_format: str, output_file: Optional[str]
) -> None:
"""
Search operations - Parameter-based discovery.
Discover data using search parameters rather than specific URLs.
"""
ctx.ensure_object(dict)
ctx.obj["api_key"] = api_key
ctx.obj["output_format"] = output_format
ctx.obj["output_file"] = output_file
# ============================================================================
# SERP Services (Google, Bing, Yandex)
# ============================================================================
@search_group.command("google")
@click.argument("query", required=True)
@click.option("--location", help="Geographic location (e.g., 'United States', 'New York')")
@click.option("--language", default="en", help="Language code (e.g., 'en', 'es', 'fr')")
@click.option(
"--device",
default="desktop",
type=click.Choice(["desktop", "mobile", "tablet"]),
help="Device type",
)
@click.option("--num-results", type=int, default=10, help="Number of results to return")
@click.pass_context
def search_google(
ctx: click.Context,
query: str,
location: Optional[str],
language: str,
device: str,
num_results: int,
) -> None:
"""Search Google and get results."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.google(
query=query,
location=location,
language=language,
device=device,
num_results=num_results,
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
@search_group.command("bing")
@click.argument("query", required=True)
@click.option("--location", help="Geographic location")
@click.option("--language", default="en", help="Language code")
@click.option("--num-results", type=int, default=10, help="Number of results to return")
@click.pass_context
def search_bing(
ctx: click.Context, query: str, location: Optional[str], language: str, num_results: int
) -> None:
"""Search Bing and get results."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.bing(
query=query, location=location, language=language, num_results=num_results
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
@search_group.command("yandex")
@click.argument("query", required=True)
@click.option("--location", help="Geographic location")
@click.option("--language", default="ru", help="Language code")
@click.option("--num-results", type=int, default=10, help="Number of results to return")
@click.pass_context
def search_yandex(
ctx: click.Context, query: str, location: Optional[str], language: str, num_results: int
) -> None:
"""Search Yandex and get results."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.yandex(
query=query, location=location, language=language, num_results=num_results
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
# ============================================================================
# LinkedIn Search
# ============================================================================
@search_group.group("linkedin")
def linkedin_search_group() -> None:
"""LinkedIn search operations."""
pass
@linkedin_search_group.command("posts")
@click.argument("profile-url", required=True)
@click.option("--start-date", help="Start date (YYYY-MM-DD)")
@click.option("--end-date", help="End date (YYYY-MM-DD)")
@click.option("--timeout", type=int, default=180, help="Timeout in seconds")
@click.pass_context
def linkedin_search_posts(
ctx: click.Context,
profile_url: str,
start_date: Optional[str],
end_date: Optional[str],
timeout: int,
) -> None:
"""Discover LinkedIn posts from profile within date range."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.linkedin.posts(
profile_url=profile_url, start_date=start_date, end_date=end_date, timeout=timeout
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
@linkedin_search_group.command("profiles")
@click.argument("first-name", required=True)
@click.option("--last-name", help="Last name")
@click.option("--timeout", type=int, default=180, help="Timeout in seconds")
@click.pass_context
def linkedin_search_profiles(
ctx: click.Context, first_name: str, last_name: Optional[str], timeout: int
) -> None:
"""Find LinkedIn profiles by name."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.linkedin.profiles(
firstName=first_name, lastName=last_name, timeout=timeout
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
@linkedin_search_group.command("jobs")
@click.option("--url", help="Job URL (optional)")
@click.option("--keyword", help="Job keyword")
@click.option("--location", help="Job location")
@click.option("--country", help="Country code")
@click.option("--time-range", help="Time range filter")
@click.option("--job-type", help="Job type filter")
@click.option("--experience-level", help="Experience level filter")
@click.option("--remote", is_flag=True, help="Remote jobs only")
@click.option("--company", help="Company name filter")
@click.option("--location-radius", type=int, help="Location radius in miles")
@click.option("--timeout", type=int, default=180, help="Timeout in seconds")
@click.pass_context
def linkedin_search_jobs(
ctx: click.Context,
url: Optional[str],
keyword: Optional[str],
location: Optional[str],
country: Optional[str],
time_range: Optional[str],
job_type: Optional[str],
experience_level: Optional[str],
remote: bool,
company: Optional[str],
location_radius: Optional[int],
timeout: int,
) -> None:
"""Find LinkedIn jobs by criteria."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.linkedin.jobs(
url=url,
keyword=keyword,
location=location,
country=country,
timeRange=time_range,
jobType=job_type,
experienceLevel=experience_level,
remote=remote,
company=company,
locationRadius=location_radius,
timeout=timeout,
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
# ============================================================================
# ChatGPT Search
# ============================================================================
@search_group.group("chatgpt")
def chatgpt_search_group() -> None:
"""ChatGPT search operations."""
pass
@chatgpt_search_group.command("prompt")
@click.argument("prompt", required=True)
@click.option("--country", help="Country code (2-letter format)")
@click.option("--web-search", is_flag=True, help="Enable web search")
@click.option("--secondary-prompt", help="Secondary/follow-up prompt")
@click.option("--timeout", type=int, default=180, help="Timeout in seconds")
@click.pass_context
def chatgpt_search_prompt(
ctx: click.Context,
prompt: str,
country: Optional[str],
web_search: bool,
secondary_prompt: Optional[str],
timeout: int,
) -> None:
"""Send a prompt to ChatGPT via search service."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.chatGPT.chatGPT(
prompt=prompt,
country=country,
webSearch=web_search if web_search else None,
secondaryPrompt=secondary_prompt,
timeout=timeout,
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
# ============================================================================
# Instagram Search
# ============================================================================
@search_group.group("instagram")
def instagram_search_group() -> None:
"""Instagram search operations."""
pass
@instagram_search_group.command("posts")
@click.argument("url", required=True)
@click.option("--num-posts", type=int, help="Number of posts to discover")
@click.option("--start-date", help="Start date (MM-DD-YYYY)")
@click.option("--end-date", help="End date (MM-DD-YYYY)")
@click.option("--post-type", help="Post type filter")
@click.option("--timeout", type=int, default=240, help="Timeout in seconds")
@click.pass_context
def instagram_search_posts(
ctx: click.Context,
url: str,
num_posts: Optional[int],
start_date: Optional[str],
end_date: Optional[str],
post_type: Optional[str],
timeout: int,
) -> None:
"""Discover Instagram posts from profile."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.instagram.posts(
url=url,
num_of_posts=num_posts,
start_date=start_date,
end_date=end_date,
post_type=post_type,
timeout=timeout,
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()
@instagram_search_group.command("reels")
@click.argument("url", required=True)
@click.option("--num-posts", type=int, help="Number of reels to discover")
@click.option("--start-date", help="Start date (MM-DD-YYYY)")
@click.option("--end-date", help="End date (MM-DD-YYYY)")
@click.option("--timeout", type=int, default=240, help="Timeout in seconds")
@click.pass_context
def instagram_search_reels(
ctx: click.Context,
url: str,
num_posts: Optional[int],
start_date: Optional[str],
end_date: Optional[str],
timeout: int,
) -> None:
"""Discover Instagram reels from profile."""
try:
client = create_client(ctx.obj["api_key"])
result = client.search.instagram.reels(
url=url,
num_of_posts=num_posts,
start_date=start_date,
end_date=end_date,
timeout=timeout,
)
output_result(result, ctx.obj["output_format"], ctx.obj["output_file"])
except Exception as e:
handle_error(e)
raise click.Abort()