Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "os"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- )
- const (
- contentType = "Content-Type"
- mediaTypeFormType = "application/x-www-form-urlencoded"
- apiSignHeader = "apisign"
- accountKeyHeader = "accountkey"
- login = "login"
- )
- type WykopHandler struct {
- appKey string
- authResponse AuthenticationResponse
- connectionKey string
- secret string
- login string
- }
- type AuthenticationResponse struct {
- Login string
- Email string
- ViolationUrl string `json:"violation_url"`
- Userkey string
- }
- var wh WykopHandler
- func main() {
- wh := WykopHandler{
- appKey: "APPKEY",
- authResponse: AuthenticationResponse{},
- connectionKey: "CONNECTIONKEY",
- secret: "SECRET",
- login: "asdasdce2w",
- }
- wypok := wh.LoginToWypok()
- fmt.Print(wypok)
- }
- type WykopError struct {
- ErrorObject WykopErrorMessage `json:"error"`
- }
- type WykopErrorMessage struct {
- Code int
- Message string
- }
- func (wh *WykopHandler) LoginToWypok() *WykopError {
- responseBody := wh.sendPostRequestForBody(getLoginUrl(wh))
- wh.authResponse = AuthenticationResponse{}
- return wh.getObjectFromJson(responseBody, &wh.authResponse)
- }
- func (wh *WykopHandler) sendPostRequestForBody(address string) string {
- apiHashedSignHeader := wh.hashRequest(address)
- data := url.Values{}
- data.Set(login, wh.login)
- data.Set(accountKeyHeader, wh.connectionKey)
- client := &http.Client{}
- r, _ := http.NewRequest("POST", address, strings.NewReader(data.Encode())) // URL-encoded payload
- r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
- r.Header.Add("Host", "a2.wykop.pl")
- r.Header.Add("User-Agent", "PostmanRuntime/7.24.1")
- r.Header.Add("Accept-Encoding", "gzip, deflate, br")
- r.Header.Add("Accept", "*/*")
- r.Header.Add("Accept-Encoding", "gzip, deflate, br")
- r.Header.Add("Connection", "keep-alive")
- r.Header.Add(apiSignHeader, apiHashedSignHeader)
- fmt.Println("response:")
- fmt.Println(r)
- resp, _ := client.Do(r)
- fmt.Println(resp.Status)
- io.Copy(os.Stdout, resp.Body)
- if resp.StatusCode == http.StatusOK {
- fmt.Print("Status ok!")
- bodyBytes, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Fatal(err)
- }
- return string(bodyBytes)
- }
- return "???"
- }
- func (wh *WykopHandler) sendRequestAndReturnStruct(urlAddress string, target interface{}) (wypokError *WykopError) {
- responseBody := wh.sendPostRequestForBody(urlAddress)
- wypokError = wh.getObjectFromJson(responseBody, &target)
- return
- }
- func (wh *WykopHandler) getObjectFromJson(bodyResponse string, target interface{}) (wypokError *WykopError) {
- b := []byte(bodyResponse)
- if err := json.Unmarshal(b, &wypokError); err != nil {
- // failed to unmarshall error, this is kinda ok, means that API worked
- }
- if wypokError != nil && wypokError.ErrorObject.Message != "" {
- return wypokError
- }
- // if wypokError.ErrorObject.Message != "" {
- // wypokError = new(WykopError)
- // wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
- // wypokError.ErrorObject.Code = -1
- // return wypokError
- // }
- if targetError := json.Unmarshal(b, target); targetError != nil {
- // this might happen when wypok is being ddosed/updated or Kiner is parting hard in the server room
- // this might happen when a.wykop.pl returned html, or empty response, shit happens.
- wypokError = new(WykopError)
- wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
- wypokError.ErrorObject.Code = -1
- return wypokError
- }
- return nil
- }
- func (wh *WykopHandler) hashRequest(address string) string {
- toHash := fmt.Sprintf("%s%s%s,%s", wh.secret, address, wh.login, wh.connectionKey)
- fmt.Println("apisign:\n" + toHash)
- mString := []byte(toHash)
- hash := md5.Sum([]byte(mString))
- fmt.Println("hashed apisign:")
- fmt.Println(hex.EncodeToString(hash[:]))
- return hex.EncodeToString(hash[:])
- }
- func (wh *WykopHandler) SetAppKey(appKey string) {
- wh.appKey = appKey
- }
- func (wh *WykopHandler) SetSecret(secret string) {
- wh.secret = secret
- }
- func (wh *WykopHandler) SetConnectionKey(connectionKey string) {
- wh.connectionKey = connectionKey
- }
- func (wh *WykopHandler) SetLogin(login string) {
- wh.login = login
- }
- func getLoginUrl(wh *WykopHandler) string {
- return fmt.Sprintf("https://a2.wykop.pl/Login/Index/appkey/%s/", wh.appKey)
- }
Advertisement
Add Comment
Please, Sign In to add comment