-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathsnov.js
More file actions
executable file
·237 lines (220 loc) · 7.62 KB
/
Copy pathsnov.js
File metadata and controls
executable file
·237 lines (220 loc) · 7.62 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
#!/usr/bin/env node
const CLIENT_ID = process.env.SNOV_CLIENT_ID
const CLIENT_SECRET = process.env.SNOV_CLIENT_SECRET
const BASE_URL = 'https://api.snov.io/v1'
if (!CLIENT_ID || !CLIENT_SECRET) {
console.error(JSON.stringify({ error: 'SNOV_CLIENT_ID and SNOV_CLIENT_SECRET environment variables required' }))
process.exit(1)
}
let cachedToken = null
async function getToken() {
if (cachedToken) return cachedToken
const res = await fetch('https://api.snov.io/v1/oauth/access_token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ grant_type: 'client_credentials', client_id: CLIENT_ID, client_secret: CLIENT_SECRET }),
})
const data = await res.json()
if (!data.access_token) {
throw new Error(data.error_description || data.error || 'Failed to obtain access token')
}
cachedToken = data.access_token
return cachedToken
}
async function api(method, path, body) {
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/json', Accept: 'application/json' }, body: body || undefined }
}
const token = await getToken()
const opts = {
method,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
}
if (body) opts.body = JSON.stringify(body)
const res = await fetch(`${BASE_URL}${path}`, opts)
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
const args = parseArgs(process.argv.slice(2))
const [cmd, sub, ...rest] = args._
async function main() {
let result
switch (cmd) {
case 'domain':
switch (sub) {
case 'search': {
const domain = args.domain
if (!domain) { result = { error: '--domain required' }; break }
const body = { domain, type: args.type || 'all', limit: Number(args.limit || 100), lastId: 0 }
result = await api('POST', '/get-domain-emails-with-info', body)
break
}
case 'count': {
const domain = args.domain
if (!domain) { result = { error: '--domain required' }; break }
result = await api('POST', '/get-domain-emails-count', { domain })
break
}
default:
result = { error: 'Unknown domain subcommand. Use: search, count' }
}
break
case 'email':
switch (sub) {
case 'find': {
const domain = args.domain
const firstName = args['first-name']
const lastName = args['last-name']
if (!domain || !firstName || !lastName) { result = { error: '--domain, --first-name, and --last-name required' }; break }
result = await api('POST', '/get-emails-from-names', { firstName, lastName, domain })
break
}
case 'verify': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
result = await api('POST', '/get-emails-verification-status', { emails: [email] })
break
}
default:
result = { error: 'Unknown email subcommand. Use: find, verify' }
}
break
case 'prospect':
switch (sub) {
case 'find': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
result = await api('POST', '/get-prospect-by-email', { email })
break
}
case 'add': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
const body = { email }
if (args['first-name']) body.firstName = args['first-name']
if (args['last-name']) body.lastName = args['last-name']
if (args['list-id']) body.listId = args['list-id']
result = await api('POST', '/add-prospect-to-list', body)
break
}
default:
result = { error: 'Unknown prospect subcommand. Use: find, add' }
}
break
case 'lists':
switch (sub) {
case 'list':
result = await api('GET', '/get-user-lists')
break
case 'prospects': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = { listId: id }
if (args.page) body.page = Number(args.page)
if (args['per-page']) body.perPage = Number(args['per-page'])
result = await api('POST', '/prospect-list', body)
break
}
default:
result = { error: 'Unknown lists subcommand. Use: list, prospects' }
}
break
case 'technology':
switch (sub) {
case 'check': {
const domain = args.domain
if (!domain) { result = { error: '--domain required' }; break }
result = await api('POST', '/get-technology-checker', { domain })
break
}
default:
result = { error: 'Unknown technology subcommand. Use: check' }
}
break
case 'drips':
switch (sub) {
case 'list':
result = await api('GET', '/get-user-campaigns')
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/get-emails-from-campaign?id=${encodeURIComponent(id)}`)
break
}
case 'add-prospect': {
const campaignId = args['campaign-id']
const email = args.email
if (!campaignId || !email) { result = { error: '--campaign-id and --email required' }; break }
const body = { campaignId, email }
if (args['first-name']) body.firstName = args['first-name']
if (args['last-name']) body.lastName = args['last-name']
result = await api('POST', '/add-prospect-to-email-campaign', body)
break
}
default:
result = { error: 'Unknown drips subcommand. Use: list, get, add-prospect' }
}
break
default:
result = {
error: 'Unknown command',
usage: {
domain: {
search: 'domain search --domain <domain> [--type all|personal|generic] [--limit <n>]',
count: 'domain count --domain <domain>',
},
email: {
find: 'email find --domain <domain> --first-name <name> --last-name <name>',
verify: 'email verify --email <email>',
},
prospect: {
find: 'prospect find --email <email>',
add: 'prospect add --email <email> [--first-name <name>] [--last-name <name>] [--list-id <id>]',
},
lists: {
list: 'lists list',
prospects: 'lists prospects --id <id> [--page <n>] [--per-page <n>]',
},
technology: 'technology check --domain <domain>',
drips: {
list: 'drips list',
get: 'drips get --id <id>',
'add-prospect': 'drips add-prospect --campaign-id <id> --email <email> [--first-name <name>] [--last-name <name>]',
},
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})