-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
252 lines (215 loc) · 6.61 KB
/
parser.go
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/akamensky/argparse"
_ "github.com/mattn/go-sqlite3"
"github.com/nandosousafr/podfeed"
"github.com/tcolgate/mp3"
)
func main() {
parser := argparse.NewParser("print", "Fetch Podcast url and cut mp3s")
url := parser.String("u", "url", &argparse.Options{Required: true, Help: "URL for podcast rss"})
start := parser.Int("s", "start", &argparse.Options{Help: "Time to cut at the start", Default: 0})
end := parser.Int("e", "end", &argparse.Options{Help: "Time to cut at the end", Default: 0})
numThreads := parser.Int("n", "threads", &argparse.Options{Help: "Number of threads to use", Default: 10})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
return
}
database, _ := sql.Open("sqlite3", "./podcasts.db")
fmt.Println("Fetching Podcast")
podcast, err := podfeed.Fetch(*url)
if err != nil {
log.Fatal(err)
}
fmt.Println(podcast.Title + " Fetched")
initializeDB(podcast, database)
newItems := insertToPodcast(podcast, database)
sem := make(chan int, *numThreads)
for _, item := range newItems {
sem <- 1
go func(item *podfeed.Item, dname string, start, end int, database *sql.DB) {
processItem(item, podcast.Title, start, end, database)
<-sem
}(item, podcast.Title, *start, *end, database)
}
}
func insertToPodcast(podcast podfeed.Podcast, database *sql.DB) []*podfeed.Item {
statement, _ := database.Prepare(`INSERT INTO podcast(Title, Subtitle, Description, url, Language, Author)
SELECT ?, ?, ?, ?, ?, ?
WHERE NOT EXISTS(SELECT 1 FROM podcast WHERE Title = ?)`)
statement.Exec(podcast.Title, podcast.Subtitle, podcast.Description, podcast.Link, podcast.Language, podcast.Author,
podcast.Title)
statement, _ = database.Prepare(`INSERT INTO owner (podcastid, Name, Email)
SELECT podcast.id, ?, ?
FROM podcast
WHERE podcast.Title = ?`)
statement.Exec(podcast.Owner.Name, podcast.Owner.Email, podcast.Title)
statement, _ = database.Prepare(`INSERT INTO category (podcastid, category)
SELECT podcast.id, ?
FROM podcast
WHERE podcast.Title = ?`)
statement.Exec(podcast.Category.Text, podcast.Title)
statement, _ = database.Prepare(`INSERT INTO item (podcastid, Title, Link, Duration,
Author, Summary, Subtitle, Description, Image, EnclosureUrl)
SELECT podcast.id, ?, ?, ?, ?, ?, ?, ?, ?, ?
FROM podcast
WHERE podcast.Title = ?`)
rows, _ := database.Query(`SELECT item.Title, item.Processed
FROM item
INNER JOIN podcast ON item.podcastid = podcast.id
AND podcast.Title = ?`, podcast.Title)
if isExist, _ := exists(podcast.Title); !isExist {
os.Mkdir(podcast.Title, 0700)
}
var isProcessed bool
var title string
var result int
var item *podfeed.Item
var newItems []*podfeed.Item
rows.Next()
rows.Scan(&title, &isProcessed)
for i := len(podcast.Items) - 1; i >= 0; i-- {
item = &podcast.Items[i]
fmt.Println("Processing " + item.Title)
result = strings.Compare(title, item.Title)
fmt.Println("Compared " + strconv.Itoa(result))
if result != 0 {
fmt.Println("Inserting " + item.Title)
statement.Exec(item.Title, item.Link, item.Duration, item.Author, item.Summary,
item.Subtitle, item.Description, item.Image.Href, item.Enclosure.Url, podcast.Title)
newItems = append(newItems, item)
} else if !isProcessed {
newItems = append(newItems, item)
rows.Next()
rows.Scan(&title, &isProcessed)
} else {
rows.Next()
rows.Scan(&title, &isProcessed)
}
}
return newItems
}
func processItem(item *podfeed.Item, dname string, start, end int, database *sql.DB) string {
fmt.Println("Processing " + item.Title)
url := item.Enclosure.Url
filename := filepath.Join(dname + "/" + item.Title + ".mp3")
processMP3(url, start, end, filename)
statement, _ := database.Prepare(`UPDATE item
SET Processed = 1
WHERE Title = ?`)
result, err := statement.Exec(item.Title)
if err != nil {
log.Fatal(err)
}
rows, err := result.RowsAffected()
if err != nil {
log.Fatal(err)
}
if rows != 1 {
log.Fatalf("expected to affect 1 row, affected %d", rows)
}
return filename
}
func initializeDB(podcast podfeed.Podcast, database *sql.DB) {
statement, _ := database.Prepare(`CREATE TABLE IF NOT EXISTS podcast (
id INTEGER PRIMARY KEY, Title TEXT, Subtitle TEXT, Description TEXT,
url TEXT, Language TEXT, Author TEXT, UNIQUE(Title))`)
statement.Exec()
statement, _ = database.Prepare(`CREATE TABLE IF NOT EXISTS owner (
id INTEGER PRIMARY KEY, podcastid INTEGER, Name TEXT, Email TEXT,
FOREIGN KEY(podcastid) REFERENCES podcast(id), UNIQUE(podcastid))`)
statement.Exec()
statement, _ = database.Prepare(`CREATE TABLE IF NOT EXISTS category (
id INTEGER PRIMARY KEY, podcastid INTEGER, category TEXT,
FOREIGN KEY(podcastid) REFERENCES podcast(id), UNIQUE(podcastid))`)
statement.Exec()
statement, _ = database.Prepare(`CREATE TABLE IF NOT EXISTS item (
id INTEGER PRIMARY KEY, podcastid INTEGER, Title TEXT, Link TEXT,
Duration TEXT, Author TEXT, Summary TEXT, Subtitle TEXT,
Description TEXT, Image TEXT, EnclosureUrl TEXT,
Processed boolean DEFAULT false,
FOREIGN KEY(podcastid) REFERENCES podcast(id), UNIQUE(Title))`)
statement.Exec()
}
func processMP3(url string, headSkip, tailSkip int, fn string) {
// nanosec to millsec
headSkip *= 1000 * 1000
tailSkip *= 1000 * 1000
resp, err := http.Get(url)
if err != nil {
fmt.Println("Cannot find file from url " + url)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
outFile, err := os.Create(fn)
if err != nil {
fmt.Println(err)
return
}
defer outFile.Close()
if headSkip == 0 && tailSkip == 0 {
outFile.Write(buf)
} else {
skipped := 0
d := mp3.NewDecoder(bytes.NewReader(buf))
var f mp3.Frame
var duration int
origDuration := getDuration(d)
tailSkip = origDuration - tailSkip
d = mp3.NewDecoder(bytes.NewReader(buf))
duration = 0
for {
if err := d.Decode(&f, &skipped); err != nil {
fmt.Println(err)
return
}
duration = duration + int(f.Duration())
buf, err := ioutil.ReadAll(f.Reader())
if err != nil {
fmt.Println(err)
return
}
if duration > headSkip && duration < tailSkip {
outFile.Write(buf)
}
}
}
}
func getDuration(d *mp3.Decoder) int {
var f mp3.Frame
var duration int
skipped := 0
duration = 0
for {
if err := d.Decode(&f, &skipped); err != nil {
fmt.Println(err)
return duration
}
duration = duration + int(f.Duration())
}
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}