-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.go
48 lines (39 loc) · 1.16 KB
/
instagram.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
package slinky
import (
"fmt"
"net/url"
"strings"
)
// Instagram Profile/Page: ^https://www\.instagram.com/[A-Za-z0-9._]{1,30}/?$
func decodeInstagramURL(url *url.URL) (*URL, error) {
if url.Scheme == "http" {
url.Scheme = "https"
}
if url.Scheme != "https" {
return nil, fmt.Errorf("%w: invalid Instagram scheme", ErrInvalidURL)
}
if url.Host != "instagram.com" && url.Host != "www.instagram.com" {
return nil, fmt.Errorf("%w: invalid Instagram host", ErrInvalidURL)
}
path := strings.TrimSuffix(url.Path, "/")
if len(path) < 1 || path[0] != '/' {
return nil, fmt.Errorf("%w: invalid Instagram path", ErrInvalidURL)
}
username := strings.TrimPrefix(path, "/")
if strings.ContainsFunc(username, isNotInstagramHandleRune) {
return nil, fmt.Errorf("%w: invalid Instagram username", ErrInvalidURL)
}
return &URL{
Service: Instagram,
Type: "Profile",
ID: username,
Data: map[string]string{
"username": username,
},
URL: url,
}, nil
}
const instagramHandleAlpha = "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmonpqrstuvwxyz0123456789._"
func isNotInstagramHandleRune(r rune) bool {
return !strings.ContainsRune(instagramHandleAlpha, r)
}