]> git.lizzy.rs Git - go-anidb.git/blob - examples/mylistadd/main.go
458200f0e376ccfa9c0128db4f5a17a265ea71c5
[go-anidb.git] / examples / mylistadd / main.go
1 package main
2
3 import (
4         "encoding/hex"
5         "flag"
6         "fmt"
7         "github.com/Kovensky/go-anidb"
8         ed2khash "github.com/Kovensky/go-ed2k"
9         "io"
10         "os"
11 )
12
13 var (
14         username = flag.String("username", "", "AniDB Username")
15         password = flag.String("password", "", "AniDB Password")
16         apikey   = flag.String("apikey", "", "UDP API key (optional)")
17 )
18
19 type ProgressReader struct {
20         io.Reader
21
22         Prefix  string
23         Size    int64
24         pos     int64
25         prevpos int64
26 }
27
28 func (r *ProgressReader) Read(p []byte) (n int, err error) {
29         n, err = r.Reader.Read(p)
30
31         if r.pos-512*1024 > r.prevpos || r.prevpos == 0 {
32                 // only every 512KB
33                 fmt.Printf("%s%.2f%%\r", r.Prefix, float64(r.pos)*100/float64(r.Size))
34                 r.prevpos = r.pos
35         }
36         r.pos += int64(n)
37         return
38 }
39
40 func (r *ProgressReader) Close() (err error) {
41         fmt.Printf("%s%.2f%%\n", r.Prefix, float64(r.pos)*100/float64(r.Size))
42         return nil
43 }
44
45 func hashFile(path string) (ed2k string, size int64) {
46         fh, err := os.Open(path)
47         if err != nil {
48                 return
49         }
50         defer fh.Close()
51
52         stat, err := fh.Stat()
53         if err != nil {
54                 return
55         }
56         size = stat.Size()
57
58         rd := ProgressReader{
59                 Reader: fh,
60                 Prefix: fmt.Sprintf("Hashing %s: ", path),
61                 Size:   size,
62         }
63         defer rd.Close()
64
65         hash := ed2khash.New(true)
66         _, err = io.Copy(hash, &rd)
67         if err != nil {
68                 return
69         }
70
71         ed2k = hex.EncodeToString(hash.Sum(nil))
72         return
73 }
74
75 func main() {
76         flag.Parse()
77
78         if *username == "" || *password == "" {
79                 fmt.Println("Username and password must be supplied")
80                 os.Exit(1)
81         }
82
83         adb := anidb.NewAniDB()
84         adb.SetCredentials(*username, *password, *apikey)
85         defer adb.Logout()
86
87         max := len(flag.Args())
88         done := make(chan bool, max)
89
90         for _, path := range flag.Args() {
91                 ed2k, size := hashFile(path)
92                 if ed2k != "" {
93                         go func() {
94                                 f := <-adb.FileByEd2kSize(ed2k, size)
95                                 state := anidb.MyListStateHDD
96                                 done <- <-adb.MyListAdd(f, &anidb.MyListSet{State: &state}) != 0
97                         }()
98                 } else {
99                         go func() { done <- false }()
100                 }
101         }
102
103         count := 0
104         for ok := range done {
105                 if ok {
106                         count++
107                 }
108                 max--
109                 if max == 0 {
110                         break
111                 }
112         }
113
114         fmt.Println("Added", count, "files to mylist")
115 }