-
Notifications
You must be signed in to change notification settings - Fork 10
/
swot.go
132 lines (109 loc) · 2.9 KB
/
swot.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
package swot
import (
"errors"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
valid "github.com/asaskevich/govalidator"
)
//IsAcademic returns true if the email address/URL belongs to an academic institution.
func IsAcademic(emailOrURL string) bool {
domainName, err := getDomainName(emailOrURL)
if err != nil {
return false
}
if isBlacklisted(domainName) {
return false
} else if isAcademicTLD(domainName) {
return true
}
_, err = getInstitutionName(domainName)
return err == nil
}
func getDomainName(emailOrURL string) (string, error) {
var domainName string
emailOrURL = strings.ToLower(strings.TrimSpace(emailOrURL))
if valid.IsEmail(emailOrURL) {
return strings.Split(emailOrURL, "@")[1], nil
} else if valid.IsURL(emailOrURL) {
if valid.IsRequestURL(emailOrURL) {
url, err := url.Parse(emailOrURL)
if err != nil {
return "", err
}
domainName = url.Host
domainName = strings.Split(domainName, ":")[0]
} else {
domainName = emailOrURL
}
domainName = strings.TrimPrefix(domainName, "www")
return domainName, nil
}
return "", errors.New("Domain name not found.")
}
func isBlacklisted(domainName string) bool {
for _, dn := range blacklist {
if strings.HasSuffix(domainName, dn) {
return true
}
}
return false
}
//isAcademicTLD returns true if the domainName is a top level academic domain
//or false otherwise.
func isAcademicTLD(domainName string) bool {
for _, tld := range academicTLDs {
if strings.HasSuffix(domainName, tld) {
return true
}
}
return false
}
//fileExits returns true if the file exists or false otherwise.
func fileExits(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
//GetSchoolName returns the name of the academic institution
//or an empty string if the name of the institution is not found.
func GetSchoolName(emailOrURL string) string {
domainName, err := getDomainName(emailOrURL)
if err != nil {
return ""
}
s, err := getInstitutionName(domainName)
if err != nil {
return ""
}
return strings.TrimSpace(s)
}
func getInstitutionName(domainName string) (string, error) {
domainParts := splitDomainName(domainName)
_, filename, _, ok := runtime.Caller(0)
if !ok {
return "", errors.New("Path to 'domains' directory not found")
}
path := filepath.Join(filepath.Dir(filename), "domains", domainParts[len(domainParts)-1])
for i := len(domainParts) - 2; i >= 0; i-- {
path = filepath.Join(path, domainParts[i])
if fileExits(path + ".txt") {
b, err := ioutil.ReadFile(path + ".txt")
if err != nil {
return "", err
}
return string(b), nil
}
}
return "", errors.New("Name of school not found")
}
//splitDomainName splits the domain name at the dots and returns a string array
//of the split parts.
//For example: uonbi.ac.ke ==> [uonbi ac ke]
func splitDomainName(domainName string) []string {
return strings.Split(domainName, ".")
}