-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
312 lines (262 loc) · 7.33 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"time"
"github.com/fatih/color"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
companyID string
companyName string
companyIDs []string
companyNames []string
companyFile string
username string
password string
inputFileName string
outputFileName string
format string
delimeter string
delay int
userAgent string
start int
count int
emptyThreshold int
configFileName string
cookieURL *url.URL
totalResults int = 0
)
const (
csvHeader = "firstname,lastname,occupation,link,companyID"
)
func init() {
flag.StringSliceVar(&companyIDs, "id", []string{}, "Company IDs (required)")
flag.StringSliceVar(&companyNames, "name", []string{}, "Company names to lookup")
flag.StringVarP(&configFileName, "config", "c", "", "Configuration File")
flag.StringVarP(&username, "username", "u", "", "LinkedIn Username (required)")
flag.StringVarP(&password, "password", "p", "", "LinkedIn Password (required)")
flag.StringVarP(&inputFileName, "input-file", "i", "", "Input File")
flag.StringVarP(&outputFileName, "output-file", "o", "", "Output File")
flag.IntVarP(&delay, "delay", "d", 1, "Number of seconds to delay between requests")
flag.StringVarP(&format, "format", "f", "", "Format of output (csv, {first}.{last})")
flag.IntVarP(&start, "start", "s", 0, "What part of list to start at")
flag.IntVarP(&count, "count", "n", 20, "Results per request")
flag.StringVarP(&userAgent, "user-agent", "a", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0", "User agent")
flag.IntVarP(&emptyThreshold, "empty-threshold", "e", 3, "Number of empty responses before quitting worker")
flag.StringVarP(&delimeter, "delimeter", "m", ",", "Delimeter")
flag.Parse()
fmt.Println(configFileName)
viper.BindPFlags(flag.CommandLine)
viper.SetConfigType("yaml")
// If no config provided, attempt to load from ./config.yml
if configFileName == "" {
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.ReadInConfig()
} else {
log.Printf("Using config file: %s\n", configFileName)
f, err := os.Open(configFileName)
if err != nil {
panic(err)
}
defer f.Close()
viper.ReadConfig(f)
}
// Use config variables
companyIDs = viper.GetStringSlice("id")
companyNames = viper.GetStringSlice("name")
username = viper.GetString("username")
password = viper.GetString("password")
inputFileName = viper.GetString("input-file")
outputFileName = viper.GetString("output-file")
format = viper.GetString("format")
delimeter = viper.GetString("delimeter")
delay = viper.GetInt("delay")
userAgent = viper.GetString("user-agent")
start = viper.GetInt("start")
count = viper.GetInt("count")
emptyThreshold = viper.GetInt("empty-threshold")
if inputFileName == "" {
if len(companyIDs) == 0 && len(companyNames) == 0 && companyFile == "" {
fmt.Println("No input option specified. Supports:\n")
fmt.Println("\t--id Company id")
fmt.Println("\t--name Company name")
fmt.Println("\t--id-file File of company ids or names\n")
flag.Usage()
os.Exit(1)
}
if username == "" {
fmt.Println("--username or -u required")
flag.Usage()
os.Exit(1)
}
if password == "" {
fmt.Println("--password or -p required")
flag.Usage()
os.Exit(1)
}
}
cookieURL, _ = url.Parse("https://www.linkedin.com/uas/login")
}
func main() {
personList := []Person{}
// Open output file
var outFile *os.File
var err error
if outputFileName != "" {
outFile, err = os.OpenFile(outputFileName,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
printError(err.Error())
}
defer outFile.Close()
}
// If formatting input file
if inputFileName != "" {
fileBytes, err := ioutil.ReadFile(inputFileName)
if err != nil {
printError(err.Error())
}
fileLoaded := false
// Check if file is json
err = json.Unmarshal(fileBytes, &personList)
if err == nil {
fileLoaded = true
}
// Check if file is csv
if !fileLoaded {
fileString := string(fileBytes)
lines := strings.Split(fileString, "\n")
lineFields := strings.Split(lines[0], delimeter)
if len(lineFields) >= 2 {
for _, line := range lines {
if line == csvHeader {
continue
}
fields := strings.Split(line, delimeter)
if len(fields) < 2 {
continue
}
p := Person{
FirstName: fields[0],
LastName: fields[1],
}
if len(fields) >= 3 {
p.Occupation = fields[2]
}
if len(fields) >= 4 {
p.PublicIdentifier = fields[3]
}
if len(fields) >= 5 {
p.CompanyID = fields[4]
}
// If formatting name
if !strings.Contains(format, "raw") {
p, err = FormatName(p)
if err != nil {
printError(fmt.Sprintf("Unable to format %s %s. %s\n", p.FirstName, p.LastName, err.Error()))
continue
}
}
personList = appendIfMissing(personList, p)
}
fileLoaded = true
}
}
if !fileLoaded {
printError(fmt.Sprintf("%s is in an unknown format, quitting...\n", inputFileName))
return
}
output, err := FormatOutput(personList)
if err != nil {
printError(fmt.Sprintf("%s - %s", "Unable to format", err.Error()))
}
if outputFileName != "" {
outFile.Write(output)
} else {
fmt.Println(string(output))
}
return
}
// Login to LinkedIn
jar, _ := cookiejar.New(nil)
c := http.Client{Jar: jar}
err = login(&c, username, password)
if err != nil {
printError(fmt.Sprintf("%s Error logging in. - %s\n", username, err.Error()))
os.Exit(1)
}
printInfo(fmt.Sprintf("%s Logged in successfully!\n", username))
// Get company IDs from names
for _, name := range companyNames {
id, err := getCompanyID(&c, name)
if err != nil {
printError(err.Error())
}
companyIDs = append(companyIDs, id)
}
// Loop through each company
for _, id := range companyIDs {
startIndex := 0
emptyResponses := 0
// Until no more results
for emptyResponses < emptyThreshold {
// Make API call
people, err := getPeople(&c, id, startIndex)
if err != nil {
printError(fmt.Sprintf("Error getting people, %s", err.Error()))
}
// Loop over results
for _, p := range people {
printSuccess(fmt.Sprintf("Discovered %s %s - %s", p.FirstName, p.LastName, p.Occupation))
// Don't format name if 'raw' format
if !strings.Contains(format, "raw") {
p, err = FormatName(p)
if err != nil {
printError(fmt.Sprintf("Unable to format %s %s. %s", p.FirstName, p.LastName, err.Error()))
continue
}
}
// Add new people to list
personList = appendIfMissing(personList, p)
}
startIndex += count
if len(people) == 0 {
emptyResponses++
}
time.Sleep(time.Duration(delay) * time.Second)
}
}
// Format scraped person list
output, err := FormatOutput(personList)
if err != nil {
log.Fatal(err)
}
// Write output
if outputFileName != "" {
_, err := outFile.Write(output)
if err != nil {
printError(fmt.Sprintf("Unable to write to output file %s. %s\n", outputFileName, err.Error()))
}
} else {
fmt.Println(string(output))
}
}
func printError(s string) {
fmt.Println(color.RedString("[!]"), s)
}
func printSuccess(s string) {
fmt.Println(color.GreenString("[+]"), s)
}
func printInfo(s string) {
fmt.Println(color.BlueString("[*]"), s)
}