simple file download checker

This commit is contained in:
Alex Grintsvayg 2017-09-12 18:15:26 -04:00
parent 3b2d6f82bc
commit c24db1580a
3 changed files with 122 additions and 10 deletions

View file

@ -6,6 +6,7 @@ import (
"strconv"
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
"github.com/ybbus/jsonrpc"
)
@ -45,6 +46,7 @@ func decode(data interface{}, targetStruct interface{}) error {
}
func (d *Client) callNoDecode(command string, params map[string]interface{}) (interface{}, error) {
log.Debugln("Calling " + command)
r, err := d.conn.CallNamed(command, params)
if err != nil {
return nil, err
@ -192,9 +194,27 @@ func (d *Client) StreamCostEstimate(url string, size *uint64) (*StreamCostEstima
})
}
func (d *Client) FileList() (*FileListResponse, error) {
type FileListOptions struct {
SDHash *string
StreamHash *string
FileName *string
ClaimID *string
Outpoint *string
RowID *string
Name *string
}
func (d *Client) FileList(options FileListOptions) (*FileListResponse, error) {
response := new(FileListResponse)
return response, d.call(response, "file_list", map[string]interface{}{})
return response, d.call(response, "file_list", map[string]interface{}{
"sd_hash": options.SDHash,
"stream_hash": options.StreamHash,
"file_name": options.FileName,
"claim_id": options.ClaimID,
"outpoint": options.Outpoint,
"rowid": options.RowID,
"name": options.Name,
})
}
func (d *Client) Resolve(url string) (*ResolveResponse, error) {

View file

@ -2,9 +2,9 @@ package jsonrpc
import (
"encoding/json"
"github.com/go-errors/errors"
"reflect"
"github.com/go-errors/errors"
lbryschema "github.com/lbryio/lbryschema.go/pb"
)
@ -202,7 +202,7 @@ type BlobGetResponse struct {
SuggestedFileName string `json:"suggested_file_name"`
}
type StreamCostEstimateResponse *float64
type StreamCostEstimateResponse float64
type GetResponse File
type FileListResponse []File

104
main.go
View file

@ -1,24 +1,116 @@
package main
import (
"math/rand"
"flag"
"os"
"strconv"
"time"
"github.com/lbryio/lbry.go/jsonrpc"
"github.com/davecgh/go-spew/spew"
"github.com/go-errors/errors"
log "github.com/sirupsen/logrus"
)
const maxPrice = float64(10)
const waitForStart = 5 * time.Second
const waitForEnd = 3 * time.Minute
func main() {
rand.Seed(time.Now().UnixNano())
log.Println("Starting...")
log.SetLevel(log.DebugLevel)
flag.Parse()
name := flag.Arg(0)
if name == "" {
log.Errorln("Usage: " + os.Args[0] + " URL")
return
}
conn := jsonrpc.NewClient("")
log.Println("Starting...")
response, err := conn.Resolve("one")
err := testUri(conn, name)
if err != nil {
panic(err)
}
spew.Dump(response)
/*
todo:
- check multiple names in parallel
- limit how many parallel checks run
- within each name, check if its done every 20 seconds or so. no need to wait a fixed amount of time
- but set a max limit on how much to wait (based on filesize?)
- report aggregate stats to slack
*/
}
func testUri(conn *jsonrpc.Client, url string) error {
log.Infoln("Testing " + url)
price, err := conn.StreamCostEstimate(url, nil)
if err != nil {
return err
}
if price == nil {
return errors.New("could not get price of " + url)
}
if float64(*price) > maxPrice {
return errors.New("the price of " + url + " is too damn high")
}
startTime := time.Now()
get, err := conn.Get(url, nil, nil)
if err != nil {
return err
} else if get == nil {
return errors.New("received no response for 'get' of " + url)
}
if get.Completed {
log.Infoln("cannot test " + url + " because we already have it")
return nil
}
getDuration := time.Since(startTime)
log.Infoln("'get' for " + url + " took " + getDuration.String())
log.Infoln("waiting " + waitForStart.String() + " to see if " + url + " starts")
time.Sleep(waitForStart)
fileStartedResult, err := conn.FileList(jsonrpc.FileListOptions{Outpoint: &get.Outpoint})
if err != nil {
return err
}
if fileStartedResult == nil || len(*fileStartedResult) < 1 {
log.Errorln(url + " failed to start in " + waitForStart.String())
} else if (*fileStartedResult)[0].Completed {
log.Errorln(url + " already finished after " + waitForStart.String() + ". boom!")
} else if (*fileStartedResult)[0].WrittenBytes == 0 {
log.Errorln(url + " says it started, but has 0 bytes downloaded after " + waitForStart.String())
} else {
log.Infoln(url + " started, with " + strconv.FormatUint((*fileStartedResult)[0].WrittenBytes, 10) + " bytes downloaded")
}
log.Infoln("waiting " + waitForEnd.String() + " for file to finish")
time.Sleep(waitForEnd)
fileFinishedResult, err := conn.FileList(jsonrpc.FileListOptions{Outpoint: &get.Outpoint})
if err != nil {
return err
}
if fileFinishedResult == nil || len(*fileFinishedResult) < 1 {
log.Errorln(url + " failed to start at all")
} else if !(*fileFinishedResult)[0].Completed {
log.Errorln(url + " says it started, but has not finished after " + waitForEnd.String() + " (" + strconv.FormatUint((*fileFinishedResult)[0].WrittenBytes, 10) + " bytes written)")
} else {
log.Infoln(url + " finished, with " + strconv.FormatUint((*fileFinishedResult)[0].WrittenBytes, 10) + " bytes downloaded")
}
return nil
}