Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from Regner/feature/save-orders-locally
Browse files Browse the repository at this point in the history
Initial work on setting the client up to save market orders locally on disk.
  • Loading branch information
regner authored Jul 27, 2017
2 parents 0c2bfa9 + 1c9d9bb commit 09ff5d6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
10 changes: 9 additions & 1 deletion assemblers/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ func (ma *MarketAssembler) ProcessPacket(packet gopacket.Packet) {
ma.itemsBuffer = append(ma.itemsBuffer, udp.Payload[44:]...)

results := extractStrings(ma.itemsBuffer)
utils.SendMarketItems(results, ma.config.IngestUrl, ma.locationId)

if !ma.config.DisableUpload {
utils.SendMarketItems(results, ma.config.IngestUrl, ma.locationId)
}

if ma.config.SaveLocally {
utils.SaveMarketItems(results)
}


ma.processing = false
} else {
Expand Down
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ func main() {
config := utils.ClientConfig{}

flag.StringVar(&config.IngestUrl, "i", "https://albion-market.com/api/v1/ingest/", "URL to send market data to.")
flag.BoolVar(&config.DisableUpload, "d", false, "If specified no attempts will be made to upload data to remote server.")
flag.BoolVar(&config.SaveLocally, "s", false, "If specified all market orders will be saved locally.")
flag.Parse()

log.Printf("Using the following ingest: %v", config.IngestUrl)
if config.DisableUpload {
log.Print("Remote upload of market orders is disabled!")
} else {
log.Printf("Using the following ingest: %v", config.IngestUrl)
}

if config.SaveLocally {
log.Print("Saving market orders locally.")
}

devices, err := pcap.FindAllDevs()

Expand Down
31 changes: 31 additions & 0 deletions utils/file_save.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"os"
"time"
"fmt"
"log"
)

func SaveMarketItems(marketItems []string) {
date := time.Now().Local().Format("2006-01-02")
filename := fmt.Sprintf("marketorder-%v.txt", date)

f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil{
log.Printf("Error opeing file for saving: %v", err)
return
}

defer f.Close()

for _, order := range marketItems {
_, err := f.WriteString(fmt.Sprintf("%v\n", order))
if err != nil {
log.Printf("Error appending to file: %v", err)
return
}
}

log.Printf("Saved %v market orders to disk.", len(marketItems))
}
3 changes: 2 additions & 1 deletion utils/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

type ClientConfig struct {
DeviceName string
IngestUrl string
DisableUpload bool
SaveLocally bool
}

0 comments on commit 09ff5d6

Please sign in to comment.