Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "crypto/md5"
- "encoding/hex"
- "encoding/json"
- "fmt"
- "github.com/parnurzeal/gorequest"
- "net/url"
- )
- const (
- 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: "Y",
- authResponse: AuthenticationResponse{},
- connectionKey: "Z",
- secret: "X",
- 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) sendPostRequestForBody(address string) string {
- body := url.Values{}
- body.Add(login, wh.login)
- body.Add(accountKeyHeader, wh.connectionKey)
- apiHashedSignHeader := wh.hashRequest(address)
- apisign := fmt.Sprintf("{'%s': '%s'}", apiSignHeader, apiHashedSignHeader)
- // DEBUG - TODO REMOVE
- fmt.Print(fmt.Sprintf("url: >%s<\n", address))
- fmt.Print(fmt.Sprintf("data: >%s<\n", body))
- fmt.Print(fmt.Sprintf("hashed sign_data: >%s<\n", apisign))
- // DEBUG - TODO REMOVE
- resp, bodyResp, errs := gorequest.New().Post(address).
- Set("Content-Type", "application/x-www-form-urlencoded").
- Set(apiSignHeader, apiHashedSignHeader).
- Set("host", "a2.wykop.pl").
- Send(body).
- //{'login': 'asdasdce2w', 'accountkey': 'Z'}
- End()
- //// DEBUG - TODO REMOVE
- fmt.Println("json returned: ")
- fmt.Println(bodyResp)
- fmt.Println(errs)
- fmt.Println(resp)
- //// DEBUG - TODO REMOVE
- return bodyResp
- }
- func (wh *WykopHandler) hashRequest(address string) string {
- toHash := fmt.Sprintf("%s%s%s,%s", wh.secret, address, wh.login, wh.connectionKey)
- mString := []byte(toHash)
- hash := md5.Sum([]byte(mString))
- return hex.EncodeToString(hash[:])
- }
- func getLoginUrl(wh *WykopHandler) string {
- return fmt.Sprintf("https://a2.wykop.pl/Login/Index/appkey/%s/", wh.appKey)
- }
- func (wh *WykopHandler) LoginToWypok() *WykopError {
- responseBody := wh.sendPostRequestForBody(getLoginUrl(wh))
- wh.authResponse = AuthenticationResponse{}
- return wh.getObjectFromJson(responseBody, &wh.authResponse)
- }
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement