Skip to content

Commit 139d436

Browse files
committed
Added a method to manually set IDM path
1 parent a0b8b8a commit 139d436

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,109 @@
11
# idm
2+
Golang wrapper for Internet Download Manager (IDM) CLI.
3+
4+
### Install
5+
6+
To use in a go project:
7+
```
8+
go get github.com/Navid2zp/idm
9+
```
10+
11+
### Usage
12+
13+
```go
14+
package main
15+
16+
import (
17+
"fmt"
18+
"github.com/Navid2zp/idm"
19+
"time"
20+
)
21+
22+
func main() {
23+
download, _ := idm.NewDownload("https://codeload.github.com/Navid2zp/idm/zip/master")
24+
// Silent mode and quit after finish
25+
download.Silent().QuitAfterFinish()
26+
// set download path
27+
download.SetFilePath("C:")
28+
// Start the download
29+
_ = download.Start()
30+
31+
// Wait till file is appeared in the given path
32+
err := download.VerifyDownload(time.Second * 10)
33+
if err != nil {
34+
fmt.Println("couldn't verify file download:", err.Error())
35+
}
36+
}
37+
```
38+
39+
#### Parameters
40+
Check out the IDM methods here: https://www.internetdownloadmanager.com/support/command_line.html
41+
42+
```go
43+
// Turns on the silent mode when IDM doesn't ask any questions
44+
download.Silent()
45+
46+
// IDM will hang up your connection after the successful downloading
47+
download.HangUpAfterFinish()
48+
49+
// IDM will exit after the successful downloading.
50+
// This parameter works only for the first copy
51+
download.QuitAfterFinish()
52+
53+
// Defines the local path where to save the file
54+
download.SetFilePath("C:/Users/MyPC/Downloads")
55+
56+
// Defines local file name to save the file
57+
download.SetFileName("myFile.zip")
58+
59+
// adds specified file to download queue, but don't start downloading
60+
download.AddToQueue()
61+
```
62+
63+
**Starting the main IDM queue:**
64+
65+
```go
66+
err := idm.StartMainQueue()
67+
```
68+
69+
##### Verify IDM installation
70+
71+
```go
72+
installed, _ := idm.VerifyIDM()
73+
74+
if !installed {
75+
fmt.Println("Couldn't find IDM")
76+
} else {
77+
fmt.Println("IDM found")
78+
}
79+
```
80+
81+
This package uses [go-win64api][1]'s `InstalledSoftwareList` method to list all the installed applications and checks if IDM is present. Then it will try to find it's path. It fails to verify IDM if `IDMan.exe` won't be present in the same directory as `Uninstall.exe`
82+
83+
You can set the IDM path manually:
84+
85+
```go
86+
idm.SetIDMPath("path/to/idm.exe")
87+
```
88+
89+
90+
##### Verify download
91+
92+
There is no way to be sure if a download is completed or not since we can't actually control the IDM. The closest way to check if a download is finished is by checking the download path and look for the file.
93+
94+
```go
95+
err := download.VerifyDownload(time.second * 30)
96+
```
97+
98+
**NOTE:** You have to specify download path using `SetFilePath` method to use this option. Also I recommend setting the file name using `SetFileName` method to make sure that we know where the file is to verify it.
99+
100+
If filename isn't specified, program will try to find it from URL header which might not be available or be different than what IDM chooses causing this method to fail the verification.
101+
102+
103+
License
104+
----
105+
106+
[MIT][2]
107+
108+
[1]: https://github.com/iamacarpet/go-win64api
109+
[2]: https://github.com/Navid2zp/idm/blob/master/LICENSE

errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ var DownloadFilePathNotProvidedError = errors.New("no file path is provided for
2626
// WaitForDownloadTimeOutError represents the error when time out duration is reached while waiting for a file download to be complete
2727
var WaitForDownloadTimeOutError = errors.New("file didn't appear in specified duration")
2828

29+
// IDMPathError represents the error when the given IDM path doesn't exist
30+
var IDMPathError = errors.New("failed to verify the given idm path")
31+
2932
// ApplicationsListError represents the error when program can't list the installed programs
3033
// NOTE: only windows
3134
type ApplicationsListError struct {

examples/download.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"fmt"
5-
"idm"
5+
"github.com/Navid2zp/idm"
66
"time"
77
)
88

@@ -19,14 +19,14 @@ func main() {
1919
}
2020

2121
download, _ := idm.NewDownload("https://codeload.github.com/Navid2zp/idm/zip/master")
22-
download.Silent()
22+
download.Silent().QuitAfterFinish()
2323
// set download path
2424
download.SetFilePath("C:")
2525
// Start the download
2626
_ = download.Start()
2727

2828
// Wait till file is appeared in the given path
29-
err = download.WaitForFinish(time.Second * 10)
29+
err = download.VerifyDownload(time.Second * 10)
3030
if err != nil {
3131
fmt.Println("couldn't verify file download:", err.Error())
3232
}

idm.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package idm
22

33
import (
44
wapi "github.com/iamacarpet/go-win64api"
5+
"os"
56
"path/filepath"
67
"strings"
78
"time"
@@ -17,6 +18,16 @@ func GetIDMPath() string {
1718
return idmPath
1819
}
1920

21+
func SetIDMPath(path string) error {
22+
if _, err := os.Stat(path); err != nil {
23+
return IDMPathError
24+
}
25+
idmPath = path
26+
idmInstalled = true
27+
idmInstallationChecked = true
28+
return nil
29+
}
30+
2031
// VerifyIDM verifies the IDM installation on the machine
2132
func VerifyIDM() (bool, error) {
2233
if idmInstallationChecked {
@@ -83,6 +94,8 @@ func NewDownload(url string) (*Download, error) {
8394
return &download, nil
8495
}
8596

97+
// AddToQueue adds specified file to download queue, but don't start downloading
98+
// Calling Start() after calling this method won't download the queued download (starts a new download in IDM).
8699
func (d *Download) AddToQueue() error {
87100
return AddToQueue(d.URL)
88101
}
@@ -130,10 +143,10 @@ func (d *Download) Start() error {
130143
return startDownload(d.buildArgs())
131144
}
132145

133-
// WaitForFinish waits for download to be completed
146+
// VerifyDownload waits for download to be completed
134147
// Only works if you specified the download path
135148
// NOTE: File path must've been provided and file name must either be provided or be present in url header
136-
func (d *Download) WaitForFinish(timeout time.Duration) error {
149+
func (d *Download) VerifyDownload(timeout time.Duration) error {
137150
if d.Path == "" {
138151
return DownloadFilePathNotProvidedError
139152
}

0 commit comments

Comments
 (0)