-
Notifications
You must be signed in to change notification settings - Fork 0
/
goGEM-WikiAPI.go
339 lines (282 loc) · 10 KB
/
goGEM-WikiAPI.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package gogemwikiapi
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
// "net/http/httputil" // DEBUG
"net/url"
"os"
"path/filepath"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/publicsuffix"
)
// Login requests the loginURL, which should be a defined constant that leads to the first link in the iGEM login chain.
// Input : username, password, loginURL.
// Output: Pointer to Client with active session, if login was successful, otherwise an error will be returned.
func Login(username, password, loginUrl string) (*http.Client, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
client := &http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error { // Redirects are not allowed, because this tends to mess with the cookie jar
return http.ErrUseLastResponse
}}
req, err := http.NewRequest("POST", loginUrl, strings.NewReader(url.Values{"return_to": {""}, "username": {username}, "password": {password}, "Login": {"Login"}}.Encode())) // "Login" is the name of the submit button in the login form
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
final_url := ""
for resp.StatusCode == 302 { // If we are redirected, we have to follow the redirect manually so we can gather all the cookies
loc_url, err := resp.Location()
if err != nil {
return nil, err
}
resp, err = client.Get(strings.ReplaceAll(loc_url.String(), " ", "")) // Sometimes there are spaces in the URL, which causes problems, so we remove them
if err != nil {
return nil, err
}
final_url = loc_url.String()
}
if resp.StatusCode == 200 && strings.Contains(final_url, "Login_Confirmed") { // If we are not redirected further, and the last page url contains "Login_Confirmed" we are logged in
return client, nil
}
return nil, errors.New("loginFailed") // Ooops, something went wrong!
}
// Logout logs the user out of the iGEM website. logoutURL is the URL to the first page in the logout chain.
// Input: Pointer to Client, logoutURL.
// Output: No return value, but an error will be returned if something went wrong.
func Logout(client *http.Client, logoutUrl string) error {
req, err := http.NewRequest("GET", logoutUrl, nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
final_url := ""
for resp.StatusCode == 302 { // If we are redirected, we have to follow the redirect manually so we can gather all the cookies
loc_url, err := resp.Location()
if err != nil {
log.Fatal(err)
}
resp, err = client.Get(strings.ReplaceAll(loc_url.String(), " ", "")) // Sometimes there are spaces in the URL, which causes problems, so we remove them
if err != nil {
log.Fatal(err)
}
final_url = loc_url.String()
}
if resp.StatusCode == 200 && strings.Contains(final_url, "Logout_Confirmed") { // If we are not redirected further, and the last page url contains "Logout_Confirmed" we are logged out
return nil
}
return errors.New("logoutFailed") // Ooops, something went wrong!
}
// Upload is the wrapper for the whole upload process.
// Input: Session, Wiki year, Teamname with spaces as underscore, filepath to the file to upload, offset, if it is a file(media files etc.), and if already uploaded files should be overwritten (already uploaded is determined by a hash compare).
// Output: String containing the URL to the uploaded page / the file overview page, or an error if something went wrong.
func Upload(client *http.Client, year int, teamname, pathToFile, offset string, file, force bool) (string,error) {
var err error
location := ""
filename := ""
history_url := ""
edit_url := ""
upload_url := ""
if offset != "" {
offset = offset + "/"
}
//Construct Filelocation relative to the Teamroot or iGEM "File:" Page, for pages and files respectively
if file {
filename = filepath.Base(pathToFile)
location = "T--" + teamname + "--" + filename
history_url, err = constructURL(year, teamname, location, file, false, false, false)
if err != nil {
return "", err
}
edit_url, err = constructURL(year, teamname, location, file, true, false, false)
if err != nil {
return "", err
}
upload_url, err = constructURL(year, teamname, location, file, true, false, false)
if err != nil {
return "", err
}
} else {
filename := strings.Split(filepath.Base(pathToFile), ".")[0]
if strings.Contains(strings.Split(filepath.Base(pathToFile), ".")[1], "min") {
filename = filename + "-min"
}
if !strings.Contains(filename, "index") {
location = offset + filename
} else {
location = offset
}
history_url, err = constructURL(year, teamname, location, file, false, true, false)
if err != nil {
return "", err
}
edit_url, err = constructURL(year, teamname, location, file, false, false, false)
if err != nil {
return "", err
}
upload_url, err = constructURL(year, teamname, location, file, true, false, false)
if err != nil {
return "", err
}
//DEBUG:
// println(filename)
// println(location)
}
// if !file {
// println("Location: " +location)
// println("History URL: " +history_url)
// println("-----------------------------------------------------------------")
// }
// return "", nil
//Generate Hash for the Object that will be uploaded
fhash := gen_hash(pathToFile)
//Check if the file already exists)
already_uploaded, err := alreadyUploaded(client, history_url, fhash, file)
if err != nil {
return "", err
}
if already_uploaded && !force { // If the file is already uploaded and force is not set, we do not upload the file
return history_url, errors.New("fileAlreadyUploaded")
}
//Get the edit tokens
payload := getTokens(client, edit_url)
// DEBUG:
// for key, value := range payload {
// println(key, value)
// }
// panic(err)
//Add the type specific data to the payload
if file{
fh, err := os.Open(pathToFile)
if err != nil {
return "", err
}
payload["wpUploadFile"] = fh
payload["wpDestFile"] = strings.NewReader(location)
payload["wpUploadDescription"] = strings.NewReader("Hash:" + fhash)
payload["wpIgnoreWarning"] = strings.NewReader("1")
} else {
fh , err := ioutil.ReadFile(pathToFile)
if err != nil {
return "", err
}
payload["wpTextbox1"] = strings.NewReader(string(fh))
payload["wpSummary"] = strings.NewReader("Hash:" + fhash)
}
form, data_type := createMIMEMultipart(payload) // Create the multipart form for the upload
req, err := http.NewRequest("POST", upload_url, form)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", data_type)
// DEBUG:
// reqDump, err := httputil.DumpRequest(req, true)
// println(string(reqDump))
// panic(err)
full_url, err := iGEMRequest(client, req)
if err != nil {
return "", err
}
return full_url, nil
}
func Redirect(client *http.Client, year int, teamname, source, target string) (string,error) {
edit_url, err := constructURL(year, teamname, source, false, false, false, true)
if err != nil {
return "",err
}
upload_url, err := constructURL(year, teamname, source, false, true, false, true)
if err != nil {
return "",err
}
payload := getTokens(client, edit_url)
if target != "/"{
payload["wpTextbox1"] = strings.NewReader("#REDIRECT[[Team:" + teamname + "/" + target + "]]")
} else {
payload["wpTextbox1"] = strings.NewReader("#REDIRECT[[Team:" + teamname + target + "]]")
}
form, data_type := createMIMEMultipart(payload) // Create the multipart form for the upload
req, err := http.NewRequest("POST", upload_url, form)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", data_type)
return iGEMRequest(client, req)
}
func GetFileUrl(file_overview_url string, client *http.Client) (string, error){
ret_url := ""
resp, err := client.Get(file_overview_url)
if err != nil || resp.StatusCode != 200 {
return "", err
}
defer resp.Body.Close()
// DEBUG:
// dumpBody,_ := httputil.DumpResponse(resp, true)
// println(string(dumpBody))
body, err := goquery.NewDocumentFromReader(resp.Body) // Parse the HTML document which shows a image preview and the edit history
if err != nil {
return "", err
}
body.Find("div[class=fullMedia]").Each(func(i int, s *goquery.Selection) { // In the div with class fullMedia, find the image link
ret_url = s.Find("a").AttrOr("href", "")
})
return ret_url, nil
}
//TODO: remove hardcoded stuff
func QueryPages(prefixURL, teamname, offset string, client *http.Client) ([]string, error) {
url := ""
if offset == "" {
url = fmt.Sprintf("%s&prefix=Team:%s&namespace=0&hideredirects=1", prefixURL, teamname)
} else {
url = fmt.Sprintf("%s&prefix=Team:%s/%s&namespace=0&hideredirects=1", prefixURL, teamname, offset)
}
return getAllPages(url, client)
}
func DeletePage(pageurl string, year int, client *http.Client) error {
url := fmt.Sprintf("https://%d.igem.org%s", year, pageurl)
payload := getTokens(client, url + "?action=edit")
payload["wpTextbox1"] = strings.NewReader(`<div class="purged-page-empty"></div>`)
form, data_type := createMIMEMultipart(payload) // Create the multipart form for the upload
req, err := http.NewRequest("POST", url + "?action=submit", form)
if err != nil {
return err
}
req.Header.Add("Content-Type", data_type)
// DEBUG:
// reqDump, err := httputil.DumpRequest(req, true)
// println(string(reqDump))
// panic(err)
resp, err := client.Do(req) // Send the request
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 302 { // If we are not redirected further, we have an error
// println("Upload did probably fail. Status: " + fmt.Sprint(resp.StatusCode) +"... Continuing")
return errors.New("uploadDidFail")
}
for resp.StatusCode == 302 { // If we are redirected, we have to follow the redirect manually so we can gather all the cookies
loc_url, err := resp.Location()
if err != nil {
return err
}
resp, err = client.Get(strings.ReplaceAll(loc_url.String(), " ", "")) // Sometimes there are spaces in the URL, which causes problems, so we remove them
if err != nil {
return err
}
}
return nil
}