Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.96 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "strconv"
  6.  
  7.     "github.com/PuerkitoBio/goquery"
  8.     "golang.org/x/net/context"
  9. )
  10.  
  11. // AuctionSearchParam 구조체는 경매장 검색에 사용될 인자들의 집합입니다.
  12. type AuctionSearchParam struct {
  13.     ItemName    string
  14.     ServerGroup string
  15.     ItemGrade   string
  16. }
  17.  
  18. // AuctionSearchResult 구조체는 경매장 검색 결과를 표현합니다.
  19. type AuctionSearchResult struct {
  20.     Quantity    int
  21.     Image       string
  22.     TotalPrice  Price
  23.     SinglePrice Price
  24. }
  25.  
  26. // Price 구조체는 금, 은, 동으로 이루어진 가격 정보를 표현합니다.
  27. type Price struct {
  28.     Gold   int
  29.     Silver int
  30.     Bronze int
  31. }
  32.  
  33. // Int 메소드는 Price 구조체를 정수로 변환합니다.
  34. func (p Price) Int() int {
  35.     return p.Bronze + p.Silver*100 + p.Gold*10000
  36. }
  37.  
  38. // IntPrice 타입은 정수에서 Price 타입으로 변환하기 위한 메소드를 붙이기 위한 타입입니다.
  39. type IntPrice int
  40.  
  41. // Price 메소드는 정수를 Price 타입으로 변환합니다.
  42. func (i IntPrice) Price() Price {
  43.     return Price{
  44.         Gold:   int(i) / 10000,
  45.         Silver: (int(i) % 10000) / 100,
  46.         Bronze: (int(i) % 10000) % 100,
  47.     }
  48. }
  49.  
  50. // func (p Price) Add(p2 Price) (ret Price) {}
  51. // func (p Price) Sub(p2 Price) (ret Price) {}
  52. // func (p Price) Mul(n int) (ret Price) {}
  53.  
  54. // Div 메소드는 Price의 값을 주어진 정수로 나눕니다.
  55. func (p Price) Div(n int) (ret Price) {
  56.     return IntPrice(p.Int() / n).Price()
  57. }
  58.  
  59. // url
  60. const (
  61.     auctionURL = "https://archeage.xlgames.com/auctions/list/ajax"
  62. )
  63.  
  64. // query
  65. const (
  66.     rowQuery      = `.tlist`
  67.     priceQuery    = `.auction-bidmoney > .buybid em.gol_num`
  68.     quantityQuery = `.item-num`
  69.     imageQuery    = `.eq_img img`
  70. )
  71.  
  72. func auctionSearch(ctx context.Context, ap AuctionSearchParam) (searchResult string) {
  73.     searchForm := form(map[string]string{
  74.         "sortType":     "BUYOUT_PRICE_ASC",
  75.         "searchType":   "NAME",
  76.         "serverCode":   ap.ServerGroup,
  77.         "gradeId":      ap.ItemGrade,
  78.         "keyword":      ap.ItemName,
  79.         "equalKeyword": "true",
  80.     })
  81.  
  82.     doc, err := post(ctx, auctionURL, searchForm)
  83.     if err != nil {
  84.         return
  85.     }
  86.  
  87.     var searchResults []AuctionSearchResult
  88.  
  89.     doc.Find(rowQuery).Each(func(i int, row *goquery.Selection) {
  90.         var searchResult AuctionSearchResult
  91.  
  92.         // get price
  93.         sumIntPrice := 0
  94.         row.Find(priceQuery).Each(func(i int, moneyCell *goquery.Selection) {
  95.             n, _ := strconv.Atoi(moneyCell.Text())
  96.             sumIntPrice = (sumIntPrice * 100) + n
  97.         })
  98.         searchResult.TotalPrice = IntPrice(sumIntPrice).Price()
  99.         if searchResult.Quantity, err = strconv.Atoi(row.Find(quantityQuery).Text()); err != nil {
  100.             searchResult.Quantity = 1
  101.         }
  102.         searchResult.Image, _ = row.Find(imageQuery).Attr("src")
  103.         searchResult.SinglePrice = searchResult.TotalPrice.Div(searchResult.Quantity)
  104.  
  105.         searchResults = append(searchResults, searchResult)
  106.     })
  107.  
  108.     marshaled, err := json.MarshalIndent(searchResults, "", "\t")
  109.     if err != nil {
  110.         return
  111.     }
  112.  
  113.     return string(marshaled)
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement