cpod

Yet another cron-friendly podcatcher

git clone https://git.8pit.net/cpod.git

 1// Copyright (C) 2013-2015 Sören Tempel
 2//
 3// This program is free software: you can redistribute it and/or modify
 4// it under the terms of the GNU General Public License as published by
 5// the Free Software Foundation, either version 3 of the License, or
 6// (at your option) any later version.
 7//
 8// This program is distributed in the hope that it will be useful,
 9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16package main
17
18import (
19	"fmt"
20	"github.com/nmeum/cpod/opml"
21	"github.com/nmeum/cpod/store"
22	"github.com/nmeum/cpod/util"
23	"os"
24	"path/filepath"
25)
26
27func usage() {
28	fmt.Fprintf(os.Stderr, "USAGE: cpod-import FILE...\n")
29	os.Exit(1)
30}
31
32func load(files []string) (out []opml.Outline, err error) {
33	for _, file := range files {
34		var op *opml.OPML
35
36		op, err = opml.Load(file)
37		if err != nil {
38			return
39		}
40
41		for _, o := range op.Outlines {
42			out = append(out, o)
43		}
44	}
45
46	return
47}
48
49func main() {
50	if len(os.Args) <= 1 {
51		usage()
52	}
53
54	storeDir := filepath.Join(util.EnvDefault("XDG_CONFIG_HOME", ".config"), "cpod")
55	if err := os.MkdirAll(storeDir, 0755); err != nil {
56		panic(err)
57	}
58
59	store, err := store.Load(filepath.Join(storeDir, "urls"))
60	if err != nil && !os.IsNotExist(err) {
61		panic(err)
62	}
63
64	outlines, err := load(os.Args)
65	if err != nil {
66		panic(err)
67	}
68
69	for _, outline := range outlines {
70		url := outline.URL
71		if !store.Contains(url) {
72			store.Add(url)
73		}
74	}
75
76	if err = store.Save(); err != nil {
77		panic(err)
78	}
79}