teempe

Go wypokie api not working v2

Apr 20th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.60 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "crypto/md5"
  5.     "encoding/hex"
  6.     "encoding/json"
  7.     "fmt"
  8.     "io"
  9.     "io/ioutil"
  10.     "log"
  11.     "os"
  12.  
  13.     "net/http"
  14.     "net/url"
  15.     "strconv"
  16.     "strings"
  17. )
  18.  
  19. const (
  20.     contentType       = "Content-Type"
  21.     mediaTypeFormType = "application/x-www-form-urlencoded"
  22.     apiSignHeader     = "apisign"
  23.     accountKeyHeader  = "accountkey"
  24.     login             = "login"
  25. )
  26.  
  27. type WykopHandler struct {
  28.     appKey        string
  29.     authResponse  AuthenticationResponse
  30.     connectionKey string
  31.     secret        string
  32.     login         string
  33. }
  34.  
  35. type AuthenticationResponse struct {
  36.     Login        string
  37.     Email        string
  38.     ViolationUrl string `json:"violation_url"`
  39.     Userkey      string
  40. }
  41.  
  42. var wh WykopHandler
  43.  
  44. func main() {
  45.     wh := WykopHandler{
  46.         appKey:        "APPKEY",
  47.         authResponse:  AuthenticationResponse{},
  48.         connectionKey: "CONNECTIONKEY",
  49.         secret:        "SECRET",
  50.         login:         "asdasdce2w",
  51.     }
  52.  
  53.     wypok := wh.LoginToWypok()
  54.     fmt.Print(wypok)
  55. }
  56.  
  57. type WykopError struct {
  58.     ErrorObject WykopErrorMessage `json:"error"`
  59. }
  60.  
  61. type WykopErrorMessage struct {
  62.     Code    int
  63.     Message string
  64. }
  65.  
  66. func (wh *WykopHandler) LoginToWypok() *WykopError {
  67.  
  68.     responseBody := wh.sendPostRequestForBody(getLoginUrl(wh))
  69.     wh.authResponse = AuthenticationResponse{}
  70.  
  71.     return wh.getObjectFromJson(responseBody, &wh.authResponse)
  72. }
  73.  
  74. func (wh *WykopHandler) sendPostRequestForBody(address string) string {
  75.     apiHashedSignHeader := wh.hashRequest(address)
  76.  
  77.     data := url.Values{}
  78.     data.Set(login, wh.login)
  79.     data.Set(accountKeyHeader, wh.connectionKey)
  80.  
  81.     client := &http.Client{}
  82.     r, _ := http.NewRequest("POST", address, strings.NewReader(data.Encode())) // URL-encoded payload
  83.     r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  84.     r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
  85.     r.Header.Add("Host", "a2.wykop.pl")
  86.     r.Header.Add("User-Agent", "PostmanRuntime/7.24.1")
  87.     r.Header.Add("Accept-Encoding", "gzip, deflate, br")
  88.     r.Header.Add("Accept", "*/*")
  89.     r.Header.Add("Accept-Encoding", "gzip, deflate, br")
  90.     r.Header.Add("Connection", "keep-alive")
  91.     r.Header.Add(apiSignHeader, apiHashedSignHeader)
  92.  
  93.     fmt.Println("response:")
  94.     fmt.Println(r)
  95.     resp, _ := client.Do(r)
  96.  
  97.     fmt.Println(resp.Status)
  98.     io.Copy(os.Stdout, resp.Body)
  99.  
  100.     if resp.StatusCode == http.StatusOK {
  101.         fmt.Print("Status ok!")
  102.         bodyBytes, err := ioutil.ReadAll(resp.Body)
  103.         if err != nil {
  104.             log.Fatal(err)
  105.         }
  106.         return string(bodyBytes)
  107.     }
  108.  
  109.     return "???"
  110. }
  111.  
  112. func (wh *WykopHandler) sendRequestAndReturnStruct(urlAddress string, target interface{}) (wypokError *WykopError) {
  113.     responseBody := wh.sendPostRequestForBody(urlAddress)
  114.     wypokError = wh.getObjectFromJson(responseBody, &target)
  115.     return
  116. }
  117.  
  118. func (wh *WykopHandler) getObjectFromJson(bodyResponse string, target interface{}) (wypokError *WykopError) {
  119.     b := []byte(bodyResponse)
  120.     if err := json.Unmarshal(b, &wypokError); err != nil {
  121.         // failed to unmarshall error, this is kinda ok, means that API worked
  122.     }
  123.  
  124.     if wypokError != nil && wypokError.ErrorObject.Message != "" {
  125.         return wypokError
  126.     }
  127.  
  128.     // if wypokError.ErrorObject.Message != "" {
  129.     //  wypokError = new(WykopError)
  130.     //  wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
  131.     //  wypokError.ErrorObject.Code = -1
  132.     //  return wypokError
  133.     // }
  134.  
  135.     if targetError := json.Unmarshal(b, target); targetError != nil {
  136.         // this might happen when wypok is being ddosed/updated or Kiner is parting hard in the server room
  137.         // this might happen when a.wykop.pl returned html, or empty response, shit happens.
  138.         wypokError = new(WykopError)
  139.         wypokError.ErrorObject.Message = "Coś poszło nie tak, wykop api nie zwróciło ani błędu ani obiektu"
  140.         wypokError.ErrorObject.Code = -1
  141.         return wypokError
  142.     }
  143.     return nil
  144. }
  145.  
  146. func (wh *WykopHandler) hashRequest(address string) string {
  147.     toHash := fmt.Sprintf("%s%s%s,%s", wh.secret, address, wh.login, wh.connectionKey)
  148.     fmt.Println("apisign:\n" + toHash)
  149.     mString := []byte(toHash)
  150.     hash := md5.Sum([]byte(mString))
  151.     fmt.Println("hashed apisign:")
  152.     fmt.Println(hex.EncodeToString(hash[:]))
  153.     return hex.EncodeToString(hash[:])
  154. }
  155.  
  156. func (wh *WykopHandler) SetAppKey(appKey string) {
  157.     wh.appKey = appKey
  158. }
  159.  
  160. func (wh *WykopHandler) SetSecret(secret string) {
  161.     wh.secret = secret
  162. }
  163.  
  164. func (wh *WykopHandler) SetConnectionKey(connectionKey string) {
  165.     wh.connectionKey = connectionKey
  166. }
  167.  
  168. func (wh *WykopHandler) SetLogin(login string) {
  169.     wh.login = login
  170. }
  171.  
  172. func getLoginUrl(wh *WykopHandler) string {
  173.     return fmt.Sprintf("https://a2.wykop.pl/Login/Index/appkey/%s/", wh.appKey)
  174. }
Advertisement
Add Comment
Please, Sign In to add comment