github.com/diadata-org/diadata@v1.4.593/documentation/tutorials/exchangescrapers.md (about) 1 # Write your own exchange scraper 2 3 ## Add your own scraper 4 5 Before you begin writing a scraper, please check whether the exchange under consideration offers integration through websockets. In this case please implement your scraper using the websocket instead of a RESTful API. 6 7 Now, let's assume you want to scrape a data source that provides trade information. Create a new file in `exchange-scrapers/` and call it `MySourceScraper.go`. In order for us to be able to call your scraper from our system, you should introduce a `type MySourceScraper struct` that implements the `APIScraper` interface from the scrapers package: 8 9 ```go 10 type APIScraper interface { 11 io.Closer 12 // ScrapePair returns a PairScraper that continuously scrapes trades for a 13 // single pair from this APIScraper 14 ScrapePair(pair dia.Pair) (PairScraper, error) 15 // FetchAvailablePairs returns a list with all available trade pairs (usually 16 // fetched from an exchange's API) 17 FetchAvailablePairs() (pairs []dia.Pair, err error) 18 // Channel returns a channel that can be used to receive trades 19 Channel() chan *dia.Trade 20 } 21 ``` 22 23 From the `MySourceScraper` type you derive a `MySourcePairScraper` type which restricts the scraper to a specific pair. Next, you should write a function with signature `NewMySourceScraper(exchangeName string) *MySourceScraper` initializing a scraper. We suggest that this function calls a method `func (s *MySourceScraper) mainLoop()` in a go routine, constantly receiving trade information through the trade channel of `MySourceScraper` as long as the channel is open. The collection of new trading information inside the `mainLoop()` should be done by an update method with signature `func (s *MySourceScraper) Update()`. Finally, in order to implement the interface `APIScraper` you should include `ScrapePair` returning a `MySourcePairScraper` for a specific pair, so our main collection method can iterate over all possible trading pairs. 24 25 Also, please take care of proper error handling and cleanup. More precisely, you should include a method `Error()` which returns an error as soon as the scraper's channel closes, and methods `Close()` and `cleanup()` handling the closing/shutting down of channels. 26 27 Furthermore, in order for our system to see your scraper, add a reference to it in `Config.go` in the dia package, and to the switch statement in `APIScraper.go` in the scrapers package: 28 29 ```go 30 func NewAPIScraper(exchange string, key string, secret string) APIScraper { 31 switch exchange { 32 case dia.MySourceExchange: 33 return NewMySourceScraper(key, secret, dia.MySourceExchange) 34 } 35 } 36 ``` 37 38 ## Steps to run a scraper locally 39 40 1. Navigate to the `deployments/local/exchange-scraper` directory of the project. 41 2. Run the required services using `docker-compose up -d`, this will run and prepare Redis, PostgreSQL, and InfluxDB databases. 42 3. Set the required environment variables using the following commands: 43 44 ```sh 45 export USE_ENV=true 46 export INFLUXURL=http://localhost:8086 47 export INFLUXUSER=test 48 export INFLUXPASSWORD=test 49 export POSTGRES_USER=postgres 50 export POSTGRES_PASSWORD=password 51 export POSTGRES_HOST=localhost 52 export POSTGRES_DB=postgres 53 export REDISURL=localhost:6379 54 ``` 55 56 Or simple by sourcing the `local.env` inside the `deployments/local/exchange-scraper` directory. 57 58 4. Execute `main.go` from `cmd/services/pairDiscoveryService` for fetching the available pairs and setting them in the Redis database. 59 60 ```text 61 cd cmd/services/pairDiscoveryService 62 go run main.go -exchange MySource -mode remoteFetch 63 ``` 64 65 5. Finally, run the scraping executable flagged as follows: 66 67 ```text 68 cd cmd/exchange-scrapers/collector 69 go run collector.go -exchange MySource 70 ``` 71 72 For an illustration you can have a look at the `KrakenScraper.go`. 73 74 ### kafka tips 75 76 Sometimes kafka docker container cant create topics, required for writing 77 Kafka needs to write results of exchange scrapper from command 78 79 ``` 80 go run /cmd/exchange-scrapers/collector/collector.go -exchange StellarExchange 81 ``` 82 83 It can be resolved it by ssh login to kafka container and run kafka scripts 84 ``` 85 your-console> docker exec -it <kafka-docker-container-id> bash 86 ... 87 root@ad1f7deb4762:/# kafka-topics.sh --zookeeper zookeeper --topic trades --create --partitions 2 --replication-factor 1 88 Created topic trades. 89 root@ad1f7deb4762:/# kafka-topics.sh --zookeeper zookeeper --topic tradesReplicadia --create --partitions 2 --replication-factor 1 90 Created topic tradesReplicadia. 91 root@ad1f7deb4762:/# kafka-topics.sh --zookeeper zookeeper --topic tradestest --create --partitions 2 --replication-factor 1 92 Created topic tradestest. 93 ```