~chrisppy/beagles

ref: v0.2.0 beagles/util/util.go -rw-r--r-- 4.1 KiB
c14a6fceChris Palmer Updates for 0.2.0 release 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package util

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"

	"git.sr.ht/~chrisppy/beagles/config"
	"github.com/mmcdole/gofeed"
)

// ParseFeed will parse the url given.  It will recover from any panic present
// and return the proper error
func ParseFeed(path string) (f *gofeed.Feed, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("panic occurred in gofeed: %v", r)
		}
	}()

	var r io.Reader
	switch {
	case strings.HasPrefix(path, "http"):
		client := &http.Client{}

		var req *http.Request
		req, err = http.NewRequest(http.MethodGet, path, nil)
		if err != nil {
			return
		}

		var rsp *http.Response
		rsp, err = client.Do(req)
		if err != nil {
			return
		}

		if rsp == nil {
			err = fmt.Errorf("no response for %s", path)
			return
		}

		defer func() {
			if err = rsp.Body.Close(); err != nil {
				return
			}
		}()

		if rsp.StatusCode != http.StatusOK {
			err = fmt.Errorf("%d: %s for %s", rsp.StatusCode, rsp.Status, path)
			return
		}

		r = rsp.Body
	case strings.HasPrefix(path, "gemini"):
		err = fmt.Errorf("gemini is not yet supported")
		return
	default:
		var b []byte
		b, err = ioutil.ReadFile(filepath.Clean(path))
		if err != nil {
			return
		}

		r = bytes.NewReader(b)
	}

	fp := gofeed.NewParser()
	f, err = fp.Parse(r)

	return
}

// OpenBrowser will send the url to be opened by the browser
func OpenBrowser(url string, cfg *config.Browser) error {
	if url == "" {
		return fmt.Errorf("link is empty, cannot open")
	}

	if cfg != nil {
		if cfg.HTTP != nil {
			nargs := make([]string, len(cfg.HTTP.Args))
			for i, arg := range cfg.HTTP.Args {
				if arg == "[URL]" {
					nargs[i] = url
					continue
				}
				nargs[i] = arg
			}

			cmd := exec.Command(cfg.HTTP.Bin, nargs...)

			return cmd.Run()
		}
	}

	var err error
	switch runtime.GOOS {
	case "linux":
		err = exec.Command("xdg-open", url).Start()
	case "windows":
		err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
	case "darwin":
		err = exec.Command("open", url).Start()
	default:
		err = fmt.Errorf("unsupported platform")
	}

	return err
}

// DownloadFile will download to a local file.
func DownloadFile(path string, url string) error {
	resp, err := http.Get(url)
	if err != nil {
		return err
	}

	if _, err = os.Stat(filepath.Clean(path)); os.IsNotExist(err) {
		f, err := os.Create(filepath.Clean(path))
		if err != nil {
			if err := resp.Body.Close(); err != nil {
				return err
			}

			return err
		}

		if _, err = io.Copy(f, resp.Body); err != nil {
			if err := f.Close(); err != nil {
				if err := resp.Body.Close(); err != nil {
					return err
				}
				return err
			}

			if err := resp.Body.Close(); err != nil {
				return err
			}

			return err
		}

		if err := f.Close(); err != nil {
			if err := resp.Body.Close(); err != nil {
				return err
			}

			return err
		}

		if err := resp.Body.Close(); err != nil {
			return err
		}

		return nil
	}

	return fmt.Errorf("file has already been downloaded")
}

// ExtPlay will call out to an externale program to play the audio
func ExtPlay(path, bin string, args []string) {
	nargs := make([]string, len(args))
	for i, arg := range args {
		if arg == "[FILE]" {
			nargs[i] = path
			continue
		}
		nargs[i] = arg
	}

	cmd := exec.Command(bin, nargs...)

	if err := cmd.Run(); err != nil {
		return
	}
}