-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
822 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func (c *Client) CopyBackup() error { | ||
c.logf("Copying files from everquest_rof2...") | ||
// copy all files in everquest_rof2 to current path | ||
err := filepath.Walk("everquest_rof2", func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
dst := strings.TrimPrefix(path, "everquest_rof2/") | ||
|
||
fi, err := os.Stat(dst) | ||
if err == nil { | ||
// check if file mod date is newer and file size is around same | ||
if fi.ModTime().After(info.ModTime()) && fi.Size() > info.Size()-100 && fi.Size() < info.Size()+100 { | ||
return nil | ||
} | ||
|
||
} | ||
|
||
r, err := os.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("open %s: %w", path, err) | ||
} | ||
defer r.Close() | ||
|
||
err = os.MkdirAll(filepath.Dir(dst), os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("mkdir %s: %w", filepath.Dir(path), err) | ||
} | ||
|
||
w, err := os.Create(dst) | ||
if err != nil { | ||
return fmt.Errorf("create %s: %w", dst, err) | ||
} | ||
defer w.Close() | ||
|
||
_, err = io.Copy(w, r) | ||
if err != nil { | ||
return fmt.Errorf("copy %s: %w", dst, err) | ||
} | ||
|
||
err = w.Sync() | ||
if err != nil { | ||
return fmt.Errorf("sync %s: %w", dst, err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("walk: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/anacrolix/torrent" | ||
"github.com/anacrolix/torrent/metainfo" | ||
|
||
"github.com/c2h5oh/datasize" | ||
) | ||
|
||
// Torrent downloads the torrent | ||
func (c *Client) Torrent() error { | ||
cfg := torrent.NewDefaultClientConfig() | ||
cfg.DataDir = "." | ||
cfg.Debug = false | ||
cfg.Seed = false | ||
torrentClient, err := torrent.NewClient(cfg) | ||
if err != nil { | ||
return fmt.Errorf("newClient: %w", err) | ||
} | ||
|
||
data, err := torrentContent.ReadFile("rof2.torrent") | ||
if err != nil { | ||
return fmt.Errorf("readFile %s: %w", "rof2.torrent", err) | ||
} | ||
|
||
mi, err := metainfo.Load(bytes.NewReader(data)) | ||
if err != nil { | ||
return fmt.Errorf("metainfo load: %w", err) | ||
} | ||
tr, err := torrentClient.AddTorrent(mi) | ||
if err != nil { | ||
return fmt.Errorf("addTorrent: %w", err) | ||
} | ||
|
||
start := time.Now() | ||
|
||
<-tr.GotInfo() | ||
|
||
defer tr.Drop() | ||
go func() { | ||
tick := time.NewTicker(6 * time.Second) | ||
|
||
for { | ||
select { | ||
case <-tick.C: | ||
st := tr.Stats() | ||
|
||
dataRate := (datasize.ByteSize(float64(st.BytesRead.Int64())/time.Since(start).Seconds()) * datasize.B) | ||
remainingTime := float64(tr.Info().TotalLength()) / float64(dataRate) | ||
|
||
totalPercent := float64(tr.BytesCompleted()) / float64(tr.Info().TotalLength()) * float64(100) | ||
|
||
fmt.Printf("peers: %d, seeders: %d, %s/s %0.2f%% of %s, ETA %0.1f minutes\n", | ||
st.ActivePeers, | ||
st.ConnectedSeeders, | ||
dataRate.HR(), | ||
totalPercent, | ||
(datasize.ByteSize(tr.Info().TotalLength()) * datasize.B).HR(), | ||
remainingTime/60) | ||
case <-tr.Closed(): | ||
return | ||
} | ||
} | ||
}() | ||
c.logf("Downloading %s via Torrent", tr.Name()) | ||
tr.DownloadAll() | ||
torrentClient.WaitAll() | ||
|
||
err = c.CopyBackup() | ||
if err != nil { | ||
return fmt.Errorf("copyBackup: %w", err) | ||
} | ||
|
||
fmt.Printf("Finished in %0.2f seconds\n", time.Since(start).Seconds()) | ||
|
||
return nil | ||
} |
This file contains 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
Oops, something went wrong.