aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-01-28 07:36:37 +0300
committerEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-01-28 07:36:37 +0300
commit08dfbead8dd214217a5c4f850e1b418fbc666f3d (patch)
tree468d54cd4cf9521cfcb64fbf001bb60105174e6b /main.go
parent85ffd20221fcf978f55b7bd4e75f22923340f87d (diff)
downloadradiorus-rss-08dfbead8dd214217a5c4f850e1b418fbc666f3d.tar.gz
radiorus-rss-08dfbead8dd214217a5c4f850e1b418fbc666f3d.zip
add concurrency
Diffstat (limited to 'main.go')
-rw-r--r--main.go26
1 files changed, 19 insertions, 7 deletions
diff --git a/main.go b/main.go
index 599a02d..ced732b 100644
--- a/main.go
+++ b/main.go
@@ -23,6 +23,7 @@ import (
"regexp"
"strconv"
"strings"
+ "sync"
"time"
"github.com/gorilla/feeds"
@@ -65,10 +66,15 @@ func main() {
}
populateFeed(feed, page)
- describeFeed(feed)
- describeEpisodes(feed)
+
+ var wg sync.WaitGroup
+ wg.Add(1)
+ go describeFeed(feed, &wg)
+ wg.Add(len(feed.Items))
+ go describeEpisodes(feed, &wg)
feed.Created = time.Now()
+ wg.Wait()
output := createFeed(feed)
@@ -146,7 +152,8 @@ func populateFeed(feed *feeds.Feed, page []byte) {
}
}
-func describeFeed(feed *feeds.Feed) {
+func describeFeed(feed *feeds.Feed, wg *sync.WaitGroup) {
+ defer wg.Done()
programAboutUrl := strings.TrimSuffix(feed.Link.Href, "episodes") + "about"
programAboutPage := getPage(programAboutUrl)
programAbout := programAboutRe.FindSubmatch(programAboutPage)[1]
@@ -154,14 +161,19 @@ func describeFeed(feed *feeds.Feed) {
feed.Description = string(re.ReplaceAll(programAbout, []byte(``)))
}
-func describeEpisodes(feed *feeds.Feed) {
+func describeEpisodes(feed *feeds.Feed, wg *sync.WaitGroup) {
for _, item := range feed.Items {
- page := getPage(item.Link.Href)
- item.Description = describeEpisode(page)
+ describeEpisode(item, wg)
}
}
-func describeEpisode(page []byte) string {
+func describeEpisode(item *feeds.Item, wg *sync.WaitGroup) {
+ defer wg.Done()
+ page := getPage(item.Link.Href)
+ item.Description = processEpisodeDesc(page)
+}
+
+func processEpisodeDesc(page []byte) string {
return string(episodeDescRe.FindSubmatch(page)[1])
}