-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
111 lines (85 loc) · 2.68 KB
/
session.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
package scratchgonnect
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
)
// Structs
type Session struct {
Username string
Token string
Cookie http.Cookie
HttpHeader http.Header
SessionId string
}
type UserActivity struct {
Id int `json:"id"`
CreatedDate string `json:"datetime_created"`
Username string `json:"actor_username"`
UserId int `json:"actor_id"`
ProjectId int `json:"project_id"`
ProjectName string `json:"project_title"`
Type string `json:"type"`
}
var csrfCookieDefault = http.Cookie{
Name: "scratchcsrftoken",
Value: "a",
}
// Struct functions
func (s Session) GetWhatsHappening() []UserActivity {
req, err := http.NewRequest("GET", "https://api.scratch.mit.edu/users/"+s.Username+"/following/users/activity", *new(io.Reader))
if err != nil {
panic(err)
}
req.Header = s.HttpHeader
resp, _ := http.DefaultClient.Do(req)
decoded := []UserActivity{}
json.NewDecoder(resp.Body).Decode(&decoded)
return decoded
}
func NewSession(username string, password string) *Session {
post := jSONPostLogin{
Username: username,
Password: password,
}
b, err := json.Marshal(post)
if err != nil {
panic(err)
}
req, _ := http.NewRequest("POST", "https://scratch.mit.edu/login/", bytes.NewReader(b))
// Set header and cookie for login request
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
req.Header.Add("user-agent", "(KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
req.Header.Add("x-csrftoken", "a")
req.Header.Add("x-requested-with", "XMLHttpRequest")
req.Header.Add("referer", "https://scratch.mit.edu/")
req.Header.Add("Cookie", "scratchcsrftoken=a;scratchlanguage=en;")
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 200 {
panic("Session creation failed! http response:" + to_string(resp.StatusCode))
}
newToken := new(TokenDelivery)
json.NewDecoder(resp.Body).Decode(newToken)
// Create header and cookie for session requests
defHeader := http.Header{}
defHeader.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36")
defHeader.Add("x-requested-with", "XMLHttpRequest")
defHeader.Add("x-csrftoken", "a")
defHeader.Add("referer", "https://scratch.mit.edu/")
defHeader.Add("X-Token", newToken[0].Token)
ses_id := strings.Split(resp.Header["Set-Cookie"][0], `"`)[1]
// Generate cookie form response
newSession := Session{
Token: newToken[0].Token,
SessionId: ses_id,
Username: username,
Cookie: http.Cookie{
Name: "scratchsessionsid",
Value: ses_id,
},
HttpHeader: defHeader,
}
return &newSession
}