first commit
This commit is contained in:
66
urlexpander.go
Normal file
66
urlexpander.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExpandURL ExpandURL
|
||||
func ExpandURL(url string) (string, error) {
|
||||
expandedURL := url
|
||||
|
||||
client := &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
expandedURL = req.URL.String()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return expandedURL, nil
|
||||
}
|
||||
|
||||
func ExpandURL2(myURL, searchURL string) (string, error) {
|
||||
nextURL := myURL
|
||||
var i int
|
||||
for i < 100 {
|
||||
client := &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}}
|
||||
|
||||
resp, err := client.Get(nextURL)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//fmt.Println("StatusCode:", resp.StatusCode)
|
||||
//fmt.Println(resp.Request.URL)
|
||||
|
||||
if resp.StatusCode == 200 {
|
||||
//fmt.Println("Done!")
|
||||
break
|
||||
} else {
|
||||
nextURLTest := resp.Header.Get("Location")
|
||||
if strings.Contains(nextURL, searchURL) {
|
||||
break
|
||||
}
|
||||
nextURL = nextURLTest
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return nextURL, nil
|
||||
}
|
||||
Reference in New Issue
Block a user