Last active
March 4, 2019 16:04
-
-
Save augier/d85a3a7d55392091c026302d280f4487 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "errors" | |
| "fmt" | |
| "io/ioutil" | |
| "os" | |
| "time" | |
| "github.com/gocarina/gocsv" | |
| ) | |
| func main() { | |
| bets, err := readBets() | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| var ads []*AdVuSchema | |
| for _, bet := range bets { | |
| ad, err := bet.ConvertToAdVu() | |
| if err == nil && ad != nil { | |
| ads = append(ads, ad) | |
| continue | |
| } | |
| if err != nil { | |
| fmt.Printf("error: %s, ad: %v\n", err, ad) | |
| } | |
| } | |
| err = writeCSV(ads) | |
| if err != nil { | |
| fmt.Println(err) | |
| os.Exit(1) | |
| } | |
| os.Exit(0) | |
| } | |
| func readBets() ([]*BetLite, error) { | |
| // Open our jsonFile | |
| jsonFile, err := os.Open("bets.json") | |
| // if we os.Open returns an error then handle it | |
| if err != nil { | |
| return nil, err | |
| } | |
| // defer the closing of our jsonFile so that we can parse it later on | |
| defer jsonFile.Close() | |
| // read our opened xmlFile as a byte array. | |
| byteValue, _ := ioutil.ReadAll(jsonFile) | |
| // we initialize our Users array | |
| var bets []*BetLite | |
| // we unmarshal our byteArray which contains our | |
| // jsonFile's content into 'users' which we defined above | |
| err = json.Unmarshal(byteValue, &bets) | |
| return bets, err | |
| } | |
| type BetLite struct { | |
| Type string `json:"type"` | |
| Promo string `json:"promo"` | |
| RawType string `json:"rawType"` | |
| Sport string `json:"sport"` | |
| EventName string `json:"eventName"` | |
| Competition string `json:"competition"` | |
| ExpiresAs time.Time `json:"expiresAt"` | |
| Options []struct { | |
| Name string `json:"name"` | |
| Num int `json:"oddsNum"` | |
| Den int `json:"oddsDen"` | |
| Meta map[string]string `json:"meta"` | |
| } `json:"options"` | |
| } | |
| // ConvertToAdVu converts from F8 to AdVu schema | |
| // Focus only on Full-Time Result and Promo (PriceBoost) markets for this | |
| // example | |
| func (b *BetLite) ConvertToAdVu() (*AdVuSchema, error) { | |
| if b.Type == "Full-Time Result" { | |
| if len(b.Options) != 3 { | |
| return nil, errors.New("Full-Time Result wrong option length") | |
| } | |
| adVu := b.ConvertCommon() | |
| adVu.OddsHome = formatOdds(b.Options[0].Num, b.Options[0].Den) | |
| adVu.OddsDraw = formatOdds(b.Options[1].Num, b.Options[1].Den) | |
| adVu.OddsAway = formatOdds(b.Options[2].Num, b.Options[2].Den) | |
| return adVu, nil | |
| } | |
| if b.Type == "Promo" && b.Promo == "Price Boost" { | |
| // Assuming that Price Boosts only have a single option for now | |
| if len(b.Options) != 1 { | |
| return nil, errors.New("Price Boost wrong option length") | |
| } | |
| adVu := b.ConvertCommon() | |
| adVu.PromoType = b.RawType | |
| adVu.OfferText = b.Options[0].Name | |
| adVu.OddsNow = formatOdds(b.Options[0].Num, b.Options[0].Den) | |
| adVu.OddsWas = fmt.Sprintf("%s/%s", b.Options[0].Meta["was_odds_num"], b.Options[0].Meta["was_odds_den"]) | |
| return adVu, nil | |
| } | |
| return nil, nil | |
| } | |
| func formatOdds(num, den int) string { | |
| return fmt.Sprintf("%d/%d", num, den) | |
| } | |
| func (b *BetLite) ConvertCommon() *AdVuSchema { | |
| return &AdVuSchema{ | |
| VideoName: "placeholder", | |
| Client: "william-hill", | |
| Country: "UK", | |
| Sport: b.Sport, | |
| Fixture: b.EventName, | |
| CompetitionName: b.Competition, | |
| Date: b.ExpiresAs.Format("Monday, 2th January"), | |
| } | |
| } | |
| type AdVuSchema struct { | |
| VideoName string `csv:"video_name"` | |
| Client string `csv:"client"` | |
| Country string `csv:"country"` | |
| Sport string `csv:"sport"` | |
| PromoType string `csv:"promo_type"` | |
| Fixture string `csv:"fixture"` | |
| CompetitionName string `csv:"competition_name"` | |
| Date string `csv:"date"` | |
| OfferText string `csv:"offer_text"` | |
| OddsWas string `csv:"odds_was"` | |
| OddsNot string `csv:"odds_not"` | |
| OddsNow string `csv:"odds_now"` | |
| OddsHome string `csv:"odds_home"` | |
| OddsDraw string `csv:"odds_draw"` | |
| OddsAway string `csv:"odds_away"` | |
| } | |
| func writeCSV(ads []*AdVuSchema) error { | |
| clientsFile, err := os.OpenFile("clients.csv", os.O_RDWR|os.O_CREATE, os.ModePerm) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer clientsFile.Close() | |
| return gocsv.MarshalFile(ads, clientsFile) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment