|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "fmt" |
6 | | - "github.com/chromedp/cdproto/browser" |
7 | | - "github.com/chromedp/chromedp" |
8 | | - "gopkg.in/yaml.v3" |
9 | 5 | "log" |
10 | 6 | "os" |
11 | | -) |
12 | 7 |
|
13 | | -const loginUrl string = "https://novec.smarthub.coop/Login.html" |
| 8 | + "gopkg.in/yaml.v3" |
| 9 | + |
| 10 | + "electric-usage-downloader/internal/app" |
| 11 | +) |
14 | 12 |
|
15 | 13 | var ( |
16 | 14 | startDate = "09/29/2022" |
17 | 15 | endDate = "09/30/2022" |
18 | 16 | ) |
19 | 17 |
|
20 | | -type Config struct { |
21 | | - Username string |
22 | | - Password string |
23 | | -} |
24 | | - |
25 | 18 | func main() { |
26 | | - |
27 | 19 | // read config |
28 | 20 | file, err := os.ReadFile("config.yaml") |
29 | 21 | if err != nil { |
30 | 22 | log.Fatal(err) |
31 | 23 | } |
32 | | - config := &Config{} |
| 24 | + config := &app.Config{} |
33 | 25 | err = yaml.Unmarshal(file, config) |
34 | 26 | if err != nil { |
35 | 27 | log.Fatal(err) |
36 | 28 | } |
37 | 29 |
|
38 | | - ctx, cancel := chromedp.NewExecAllocator(context.Background(), append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false))...) |
39 | | - defer cancel() |
40 | | - |
41 | | - ctx, cancel = chromedp.NewContext(ctx, chromedp.WithLogf(log.Printf)) |
42 | | - defer cancel() |
43 | | - |
44 | | - //ctx, cancel = context.WithTimeout(ctx, 15*time.Second) |
45 | | - //defer cancel() |
46 | | - |
47 | | - done := make(chan string, 1) |
48 | | - chromedp.ListenTarget(ctx, func(ev interface{}) { |
49 | | - if ev, ok := ev.(*browser.EventDownloadProgress); ok { |
50 | | - if ev.TotalBytes != 0 { |
51 | | - fmt.Printf("State: %s, completed: %.2f, total: %.2f\n", ev.State.String(), ev.ReceivedBytes, ev.TotalBytes) |
52 | | - if ev.State == browser.DownloadProgressStateCompleted { |
53 | | - done <- ev.GUID |
54 | | - close(done) |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - }) |
59 | | - |
60 | | - chromedp.Run(ctx, |
61 | | - chromedp.Navigate(loginUrl), |
62 | | - //chromedp.WaitVisible("#LoginUsernameTextBox"), |
63 | | - chromedp.SetValue("#LoginUsernameTextBox", config.Username, chromedp.NodeVisible), |
64 | | - chromedp.SetValue("#LoginPasswordTextBox", config.Password), |
65 | | - chromedp.Click("#LoginSubmitButton"), |
66 | | - //chromedp.WaitVisible(`//a[. = "Log Out"]`), |
67 | | - browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName). |
68 | | - WithDownloadPath("/tmp"). |
69 | | - WithEventsEnabled(true), |
70 | | - chromedp.Click("#MyUsageDropDown > a", chromedp.NodeVisible), |
71 | | - chromedp.Click(`//div[.="Usage Explorer"]`, chromedp.NodeVisible), |
72 | | - chromedp.Click(`//img[@alt='Usage Management']`, chromedp.NodeVisible), |
73 | | - chromedp.Click(`(//input[@name="timeFrameRadio"])[3]`, chromedp.NodeVisible), |
74 | | - chromedp.SetValue(`(//input[contains(@class, "form-control-readonly")])[1]`, startDate), |
75 | | - chromedp.SetValue(`(//input[contains(@class, "form-control-readonly")])[2]`, endDate), |
76 | | - chromedp.Click(`(//input[@name="fileFormatRadio"])[2]`), |
77 | | - chromedp.Click(`//button[.="Download Usage Data"]`), |
78 | | - ) |
79 | | - guid := <-done |
80 | | - fmt.Printf("hello %s", guid) |
| 30 | + path, err := app.DownloadCsv(config, startDate, endDate) |
| 31 | + if err != nil { |
| 32 | + log.Fatal(err) |
| 33 | + } |
| 34 | + fmt.Printf("file downloaded: %s", path) |
| 35 | + // parse csv! |
| 36 | + records, err := app.ParseCsv(path) |
| 37 | + if err != nil { |
| 38 | + log.Fatal(err) |
| 39 | + } |
| 40 | + fmt.Printf("%+v", records) |
81 | 41 | } |
0 commit comments