7 "github.com/Kovensky/go-anidb"
8 ed2khash "github.com/Kovensky/go-ed2k"
14 username = flag.String("username", "", "AniDB Username")
15 password = flag.String("password", "", "AniDB Password")
16 apikey = flag.String("apikey", "", "UDP API key (optional)")
19 type ProgressReader struct {
28 func (r *ProgressReader) Read(p []byte) (n int, err error) {
29 n, err = r.Reader.Read(p)
31 if r.pos-512*1024 > r.prevpos || r.prevpos == 0 {
33 fmt.Printf("%s%.2f%%\r", r.Prefix, float64(r.pos)*100/float64(r.Size))
40 func (r *ProgressReader) Close() (err error) {
41 fmt.Printf("%s%.2f%%\n", r.Prefix, float64(r.pos)*100/float64(r.Size))
45 func hashFile(path string) (ed2k string, size int64) {
46 fh, err := os.Open(path)
52 stat, err := fh.Stat()
60 Prefix: fmt.Sprintf("Hashing %s: ", path),
65 hash := ed2khash.New(true)
66 _, err = io.Copy(hash, &rd)
71 ed2k = hex.EncodeToString(hash.Sum(nil))
78 if *username == "" || *password == "" {
79 fmt.Println("Username and password must be supplied")
83 adb := anidb.NewAniDB()
84 adb.SetCredentials(*username, *password, *apikey)
87 max := len(flag.Args())
88 done := make(chan bool, max)
90 for _, path := range flag.Args() {
91 ed2k, size := hashFile(path)
94 f := <-adb.FileByEd2kSize(ed2k, size)
95 state := anidb.MyListStateHDD
96 done <- <-adb.MyListAdd(f, &anidb.MyListSet{State: &state}) != 0
99 go func() { done <- false }()
104 for ok := range done {
114 fmt.Println("Added", count, "files to mylist")