SHARE
TWEET

Untitled

a guest May 21st, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bytes"
  5.     "encoding/json"
  6.     "fmt"
  7.     "io/ioutil"
  8.     "net/http"
  9.     "time"
  10.  
  11.     "database/sql"
  12.     "strconv"
  13.  
  14.     _ "github.com/go-sql-driver/mysql"
  15. )
  16.  
  17. type SBAPIgetSalesPostBody struct {
  18.     Apikey         string `json:"apikey"`
  19.     Type           int    `json:"type"`
  20.     AppId          int    `json:"appid"`
  21.     After_saleid   string `json:"after_saleid"`
  22.     Items_per_page int    `json:"items_per_page"`
  23. }
  24. type Client struct {
  25.     ClientId      int64
  26.     Api_Key       string
  27.     IDtoStartFrom string
  28.     EndId         string
  29.     State         int
  30. }
  31. type ResponseItem struct {
  32.     Id           string  `json:"id"`
  33.     Price        float64 `json:"price"`
  34.     Commission   float64 `json:"commission"`
  35.     Classid      string  `json:"classid"`
  36.     Instanceid   string  `json:"instanceid"`
  37.     Appid        int     `json"appid"`
  38.     Assetid      string  `json:"assetid"`
  39.     Name         string  `json:"name"`
  40.     State        int     `json:"state"`
  41.     List_time    int64   `json:"list_time"`
  42.     Last_updated int64   `json:"last_updated"`
  43.     Wear         float64 `json:"wear"`
  44.     Txid         string  `json: "txid"`
  45. }
  46. type ServerResponse struct {
  47.     Response []ResponseItem
  48. }
  49.  
  50. func GetClients() []Client {
  51.     ALLClients := []Client{
  52.         Client{
  53.             ClientId:      76561198061661326,
  54.             Api_Key:       "58421-b9e3a61d-7155-407e-b0b8-b8b8beaf15f0",
  55.             IDtoStartFrom: "",
  56.             EndId:         "",
  57.             State:         4,
  58.         },
  59.     }
  60.     return ALLClients
  61. }
  62. func (c *Client) ClientRoutine(db *sql.DB) {
  63.     c.GetEndId(db)
  64.     idx := -1
  65.     for true {
  66.         ItemList := c.GetItems()
  67.         if ItemList == nil {
  68.             return
  69.         }
  70.         idx = c.ContainsEndId(ItemList)
  71.         if idx == -1 {
  72.             fmt.Println("EndId nicht vorhanden")
  73.             idx = len(ItemList)
  74.             c.SaveInDB(db, ItemList, idx)
  75.             c.SetStartId(ItemList)
  76.         } else {
  77.             if idx == 0 {
  78.                 return
  79.             }
  80.             c.SaveInDB(db, ItemList, idx)
  81.             return
  82.             fmt.Println("EndId vorhanden")
  83.         }
  84.         time.Sleep(5 * time.Second)
  85.     }
  86. }
  87.  
  88. //idxEnd muss standardmäsig auf len(Items)
  89. func (c *Client) SaveInDB(db *sql.DB, Items []ResponseItem, idxEnd int) {
  90.     str := strconv.FormatInt(c.ClientId, 10)
  91.     // var Price string
  92.     // var Commission string
  93.     // var Wear string
  94.     for i := 0; i < idxEnd; i++ {
  95.         Price := fmt.Sprintf("%f", Items[i].Price)
  96.         Commission := fmt.Sprintf("%f", Items[i].Commission)
  97.         Wear := fmt.Sprintf("%f", Items[i].Wear)
  98.  
  99.         Insert, err := db.Query("INSERT INTO SBSales (SBSales.userid, SBSales.market_hash_name, SBSales.id, SBSales.price, SBSales.state, SBSales.commission, SBSales.listed, SBSales.last_changed, SBSales.wear, SBSales.txid)VALUES(" + str + ",\"" + Items[i].Name + "\", \"" + Items[i].Id + "\", " + Price + ", " + strconv.Itoa(c.State) + ", " + Commission + ", \"" + time.Unix(Items[i].List_time, 0).Format("2006-01-02 15:04:05") + "\",now() ," + Wear + ", \"" + Items[i].Txid + "\")")
  100.         fmt.Println("Added Item", i)
  101.         if err != nil {
  102.             panic(err.Error())
  103.         }
  104.         Insert.Close()
  105.     }
  106. }
  107.  
  108. //Gets all items
  109. func (CurrentClient *Client) GetItems() []ResponseItem {
  110.  
  111.     getSalesPostBody := &SBAPIgetSalesPostBody{
  112.         Apikey:         CurrentClient.Api_Key,
  113.         Type:           CurrentClient.State,
  114.         AppId:          730,
  115.         After_saleid:   CurrentClient.IDtoStartFrom,
  116.         Items_per_page: PageSize,
  117.     }
  118.     var s ServerResponse
  119.     postBody, _ := json.Marshal(getSalesPostBody)
  120.     body := post(api_base+"/GetSales", string(postBody))
  121.     in := []byte(body)
  122.     err := json.Unmarshal(in, &s)
  123.     if err != nil {
  124.         panic(err)
  125.     }
  126.     return s.Response
  127. }
  128.  
  129. func post(url string, jsonData string) string {
  130.     var jsonStr = []byte(jsonData)
  131.  
  132.     req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  133.     req.Header.Set("Content-Type", "application/json")
  134.     req.Header.Set("x-requested-with", "XMLHttpRequest")
  135.  
  136.     client := &http.Client{}
  137.     resp, err := client.Do(req)
  138.     if err != nil {
  139.         panic(err)
  140.     }
  141.     defer resp.Body.Close()
  142.  
  143.     //fmt.Println("response Status:", resp.Status)
  144.     body, _ := ioutil.ReadAll(resp.Body)
  145.     return string(body)
  146. }
  147.  
  148. //Gives the Client the Endid
  149. func (c *Client) GetEndId(db *sql.DB) {
  150.     var id string
  151.     str := strconv.FormatInt(c.ClientId, 10)
  152.     fmt.Println(str)
  153.     rows, err := db.Query("SELECT id FROM SBSales WHERE state = " + strconv.Itoa(c.State) + " AND userid = " + str + "")
  154.     if err != nil {
  155.         panic(err.Error())
  156.     }
  157.     for rows.Next() {
  158.         rows.Scan(&id)
  159.         c.EndId = id
  160.     }
  161.     fmt.Println("EndId:")
  162.     fmt.Println(c.EndId)
  163. }
  164.  
  165. //Gives Index for Endid in array
  166. func (c *Client) ContainsEndId(Items []ResponseItem) int {
  167.     for i := 0; i < PageSize; i++ {
  168.         if Items[i].Id == c.EndId {
  169.             return i
  170.         }
  171.     }
  172.     return -1
  173. }
  174.  
  175. //Sets Client start Id to the next to start from
  176. func (c *Client) SetStartId(Items []ResponseItem) {
  177.     c.IDtoStartFrom = Items[PageSize-1].Id
  178. }
  179.  
  180. var api_base string = "https://api.skinbaron.de"
  181. var PageSize int = 1000
  182.  
  183. func main() {
  184.  
  185.     fmt.Println("Server started...")
  186.     //all Clients
  187.     Clients := GetClients()
  188.     fmt.Println("*Clients loaded*")
  189.     for true {
  190.         //loop through all Clients and open them in new threads
  191.         //Idtostartfrom und EndId auslesen aus database auslesen
  192.         db, err := sql.Open("mysql", "root:mCwfStG3xpqxqP4&5S9aj4ySjZpk7Sr9@tcp(rt-general-db.chy0hf5bkt4k.eu-central-1.rds.amazonaws.com:3306)/RamboTrades")
  193.         delete, err := db.Query("DELETE FROM SBSales")
  194.         defer delete.Close()
  195.         if err != nil {
  196.             panic(err.Error())
  197.         }
  198.         defer db.Close()
  199.  
  200.         for idx, _ := range Clients {
  201.             go Clients[idx].ClientRoutine(db)
  202.         }
  203.  
  204.         time.Sleep(time.Hour)
  205.     }
  206.  
  207. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top