-
Notifications
You must be signed in to change notification settings - Fork 18
/
bsa.go
85 lines (75 loc) · 2.12 KB
/
bsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
)
type BsaAd struct {
Ad
Pixel []string
ReferralLink string
TagLine string
BackgroundColor string
}
type BsaResponse struct {
Ads []map[string]interface{}
}
var hystrixBsa = "BSA"
func sendBsaRequest(r *http.Request, propertyId string) (BsaResponse, error) {
var res BsaResponse
ua := r.UserAgent()
ip := getIpAddress(r)
//ip = "208.98.185.89"
req, _ := http.NewRequest("GET", "https://srv.buysellads.com/ads/"+propertyId+".json?segment=placement:dailynowco&forwardedip="+ip+"&useragent="+url.QueryEscape(ua), nil)
req = req.WithContext(r.Context())
err := getJsonHystrix(hystrixBsa, req, &res, false)
if err != nil {
return BsaResponse{}, err
}
return res, nil
}
var fetchBsa = func(r *http.Request, propertyId string) (*BsaAd, error) {
res, err := sendBsaRequest(r, propertyId)
if err != nil {
return nil, err
}
ads := res.Ads
for _, ad := range ads {
if _, ok := ad["statlink"]; ok {
retAd := BsaAd{}
retAd.Description, _ = ad["description"].(string)
if len(retAd.Description) == 0 {
retAd.Description, _ = ad["title"].(string)
}
retAd.Image, _ = ad["smallImage"].(string)
if len(retAd.Image) == 0 {
retAd.Image, _ = ad["image"].(string)
}
retAd.Link, _ = ad["statlink"].(string)
// Prepend https: to the link if it's missing
if !strings.HasPrefix(retAd.Link, "https:") {
retAd.Link = fmt.Sprintf("https:%s", retAd.Link)
}
retAd.ReferralLink, _ = ad["ad_via_link"].(string)
retAd.Source = "Carbon"
retAd.Company, _ = ad["company"].(string)
if len(retAd.Company) == 0 {
retAd.Company = retAd.Source
}
retAd.TagLine, _ = ad["companyTagline"].(string)
retAd.BackgroundColor, _ = ad["backgroundColor"].(string)
retAd.ProviderId = "carbon"
if pixel, ok := ad["pixel"].(string); ok {
retAd.Pixel = strings.Split(pixel, "||")
for index := range retAd.Pixel {
retAd.Pixel[index] = strings.Replace(retAd.Pixel[index], "[timestamp]", ad["timestamp"].(string), -1)
}
} else {
retAd.Pixel = []string{}
}
return &retAd, nil
}
}
return nil, nil
}