-
Notifications
You must be signed in to change notification settings - Fork 8
/
faceapp.go
131 lines (121 loc) · 2.82 KB
/
faceapp.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
package faceapp
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"time"
)
const (
ENDPOINT = "https://api.faceapp.io/api/v2.6"
)
type Filter string
const (
FilterSmile Filter = "smile"
FilterSmile2 Filter = "smile_2"
FilterHot Filter = "hot"
FilterOld Filter = "old"
FilterYoung Filter = "young"
FilterFemale Filter = "female"
FilterMale Filter = "male"
FilterBlack Filter = "black"
FilterCaucasian Filter = "caucasian"
FilterAsian Filter = "asian"
FilterIndian Filter = "indian"
FilterFemale2 Filter = "female_2"
FilterPan Filter = "pan"
FilterHitman Filter = "hitman"
)
func (f Filter) NeedsCrop() bool {
return f == FilterMale || f == FilterFemale ||
f == FilterBlack || f == FilterCaucasian ||
f == FilterAsian || f == FilterIndian ||
f == FilterFemale2 || f == FilterPan ||
f == FilterHitman
}
type Session struct {
cli http.Client
appid string
}
func (s *Session) request(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "FaceApp/2.0.561 (Linux; Android 6.0)")
req.Header.Set("X-FaceApp-DeviceID", s.appid)
req.Header.Set("X-FaceApp-Priority", "1")
return req, nil
}
func (s *Session) Init() error {
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
s.cli.Jar = jar
s.cli.Timeout = time.Second * 15
var temp [8]byte
if _, err := rand.Read(temp[:]); err != nil {
return err
}
s.appid = hex.EncodeToString(temp[:])
return nil
}
func (s *Session) UploadImage(in io.Reader) (string, error) {
var body bytes.Buffer
mw := multipart.NewWriter(&body)
part, err := mw.CreateFormFile("file", "image.jpg")
if err != nil {
return "", err
}
_, err = io.Copy(part, in)
if err := mw.Close(); err != nil {
return "", err
}
req, err := s.request("POST", ENDPOINT+"/photos", &body)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", mw.FormDataContentType())
res, err := s.cli.Do(req)
if err != nil {
return "", err
}
var answer struct {
Code string `json:"code"`
}
if err := json.NewDecoder(res.Body).Decode(&answer); err != nil {
res.Body.Close()
return "", err
}
res.Body.Close()
if answer.Code == "" {
return "", errors.New("did not recognize image")
}
return answer.Code, nil
}
func (s *Session) GetImage(out io.Writer, code string, fil Filter, cropped bool) error {
url := fmt.Sprintf(ENDPOINT+"/photos/%s/filters/%s?cropped=", code, fil)
if cropped || fil.NeedsCrop() {
url += "1"
} else {
url += "0"
}
res, err := s.request("GET", url, nil)
if err != nil {
return err
}
req, err := s.cli.Do(res)
if err != nil {
return err
}
io.Copy(out, req.Body)
req.Body.Close()
return nil
}