-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpypi.go
76 lines (65 loc) · 1.94 KB
/
pypi.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
package main
import (
"encoding/json"
"fmt"
"net/mail"
"regexp"
"strings"
"golang.org/x/exp/maps"
)
func (a *app) pypiGetAllPackageNames() ([]string, error) {
html, err := a.httpRequest("https://pypi.org/simple/")
if err != nil {
return nil, err
}
re := regexp.MustCompile(`<a href="/simple/(.+?)/"`)
matches := re.FindAllSubmatch(html, -1)
if matches == nil {
return nil, fmt.Errorf("could not get package names")
}
packages := make([]string, len(matches))
for i := range matches {
packages[i] = string(matches[i][1])
}
return packages, nil
}
type pypiPackageJSON struct {
Info struct {
AuthorEmail string `json:"author_email"`
MaintainerEmail string `json:"maintainer_email"`
} `json:"info"`
}
func (a *app) pypiGetPackageMaintainer(name string) ([]string, error) {
url := fmt.Sprintf("https://pypi.org/pypi/%s/json", name)
resp, err := a.httpRequest(url)
if err != nil {
return nil, err
}
var data pypiPackageJSON
if err := json.Unmarshal(resp, &data); err != nil {
return nil, fmt.Errorf("error on json unmarshal for %s: %w", url, err)
}
maintainers := make(map[string]struct{})
data.Info.AuthorEmail = strings.TrimSpace(data.Info.AuthorEmail)
data.Info.MaintainerEmail = strings.TrimSpace(data.Info.MaintainerEmail)
if data.Info.AuthorEmail != "" && data.Info.AuthorEmail != "UNKNOWN" {
email, err := mail.ParseAddress(data.Info.AuthorEmail)
if err != nil {
a.log.Debugf("invalid author %s: %v", data.Info.AuthorEmail, err)
} else {
maintainers[email.Address] = struct{}{}
}
}
if data.Info.MaintainerEmail != "" && data.Info.MaintainerEmail != "UNKNOWN" {
email, err := mail.ParseAddress(data.Info.MaintainerEmail)
if err != nil {
a.log.Debugf("invalid maintainer %s: %v", data.Info.MaintainerEmail, err)
} else {
maintainers[email.Address] = struct{}{}
}
}
return maps.Keys(maintainers), nil
}
func getPypiLink(name string) string {
return fmt.Sprintf("https://pypi.org/project/%s/", name)
}