Advertisement
teempe

Go wypokie api not working

Apr 17th, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.56 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "crypto/md5"
  5.     "encoding/hex"
  6.     "encoding/json"
  7.     "fmt"
  8.     "github.com/parnurzeal/gorequest"
  9.     "net/url"
  10. )
  11.  
  12. const (
  13.     apiSignHeader     = "apisign"
  14.     accountKeyHeader  = "accountkey"
  15.     login             = "login"
  16. )
  17.  
  18. type WykopHandler struct {
  19.     appKey        string
  20.     authResponse  AuthenticationResponse
  21.     connectionKey string
  22.     secret        string
  23.     login         string
  24. }
  25.  
  26. type AuthenticationResponse struct {
  27.     Login        string
  28.     Email        string
  29.     ViolationUrl string `json:"violation_url"`
  30.     Userkey      string
  31. }
  32.  
  33. var wh WykopHandler
  34.  
  35. func main() {
  36.     wh := WykopHandler{
  37.         appKey:        "Y",
  38.         authResponse:  AuthenticationResponse{},
  39.         connectionKey: "Z",
  40.         secret:        "X",
  41.         login:         "asdasdce2w",
  42.     }
  43.  
  44.     wypok := wh.LoginToWypok()
  45.     fmt.Print(wypok)
  46. }
  47.  
  48. type WykopError struct {
  49.     ErrorObject WykopErrorMessage `json:"error"`
  50. }
  51.  
  52. type WykopErrorMessage struct {
  53.     Code    int
  54.     Message string
  55. }
  56.  
  57. func (wh *WykopHandler) sendPostRequestForBody(address string) string {
  58.     body := url.Values{}
  59.     body.Add(login, wh.login)
  60.     body.Add(accountKeyHeader, wh.connectionKey)
  61.  
  62.     apiHashedSignHeader := wh.hashRequest(address)
  63.  
  64.     apisign := fmt.Sprintf("{'%s': '%s'}", apiSignHeader, apiHashedSignHeader)
  65.     // DEBUG - TODO REMOVE
  66.     fmt.Print(fmt.Sprintf("url: >%s<\n", address))
  67.     fmt.Print(fmt.Sprintf("data: >%s<\n", body))
  68.     fmt.Print(fmt.Sprintf("hashed sign_data: >%s<\n", apisign))
  69.     // DEBUG - TODO REMOVE
  70.  
  71.     resp, bodyResp, errs := gorequest.New().Post(address).
  72.         Set("Content-Type", "application/x-www-form-urlencoded").
  73.         Set(apiSignHeader, apiHashedSignHeader).
  74.         Set("host", "a2.wykop.pl").
  75.         Send(body).
  76. //{'login': 'asdasdce2w', 'accountkey': 'Z'}
  77.         End()
  78.  
  79.     //// DEBUG - TODO REMOVE
  80.     fmt.Println("json returned: ")
  81.     fmt.Println(bodyResp)
  82.     fmt.Println(errs)
  83.     fmt.Println(resp)
  84.     //// DEBUG - TODO REMOVE
  85.    
  86.     return bodyResp
  87. }
  88.  
  89. func (wh *WykopHandler) hashRequest(address string) string {
  90.     toHash := fmt.Sprintf("%s%s%s,%s", wh.secret, address, wh.login, wh.connectionKey)
  91.     mString := []byte(toHash)
  92.     hash := md5.Sum([]byte(mString))
  93.     return hex.EncodeToString(hash[:])
  94. }
  95.  
  96. func getLoginUrl(wh *WykopHandler) string {
  97.     return fmt.Sprintf("https://a2.wykop.pl/Login/Index/appkey/%s/", wh.appKey)
  98. }
  99.  
  100. func (wh *WykopHandler) LoginToWypok() *WykopError {
  101.  
  102.     responseBody := wh.sendPostRequestForBody(getLoginUrl(wh))
  103.     wh.authResponse = AuthenticationResponse{}
  104.  
  105.     return wh.getObjectFromJson(responseBody, &wh.authResponse)
  106. }
  107.  
  108. func (wh *WykopHandler) getObjectFromJson(bodyResponse string, target interface{}) (wypokError *WykopError) {
  109.     b := []byte(bodyResponse)
  110.     if err := json.Unmarshal(b, &wypokError); err != nil {
  111.         // failed to unmarshall error, this is kinda ok, means that API worked
  112.     }
  113.  
  114.     if wypokError != nil && wypokError.ErrorObject.Message != "" {
  115.         return wypokError
  116.     }
  117.  
  118.     // if wypokError.ErrorObject.Message != "" {
  119.     //  wypokError = new(WykopError)
  120.     //  wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
  121.     //  wypokError.ErrorObject.Code = -1
  122.     //  return wypokError
  123.     // }
  124.  
  125.     if targetError := json.Unmarshal(b, target); targetError != nil {
  126.         // this might happen when wypok is being ddosed/updated or Kiner is parting hard in the server room
  127.         // this might happen when a.wykop.pl returned html, or empty response, shit happens.
  128.         wypokError = new(WykopError)
  129.         wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
  130.         wypokError.ErrorObject.Code = -1
  131.         return wypokError
  132.     }
  133.     return nil
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement