mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
* Autogenerate services.m3o.com * Openapi for all * Gen * Fix * Whaat * Fix dep * Fix * Hmm * Install make * Debug * Debug 1 * Location -> locations * Fix * Intall protoc gen micro * F * F * F * Push * Rename secret * Fix npm install * Fix script * Fix v2 * Ignore errors * Ignore v2 * F * F * F * Docs index * Add hugo theme * Hugo tania fixes * Change gen * Change gen 2 * Install hugo * Change gen * Gen fix * Change hugo install * Change hugo install * CNAME * Change articles wording * Tiny fix * Fix gen * Redoc it all * Fix gen * Fixing up protos * Fix proto * Fix gen * Fix * Trigger build * Fix copy * Openapi docs * Flatten * Changes * No date vol2 * Changes * Add make to chat * Fixes * Change * api spec * replace RSS * fix link * Dont continue on error * increase the width * use micro at master * change box colours * move some things * Pushing new readmes to see how they look like * Add skip file * Readmes * Nicer api link * Remove stutter * FIx mistake * set service font weight * Messages readme fix * add other font bold * Notes * Remove post from url * Revert "Remove post from url" This reverts commit 5fea2c23d0bafa910f5dc4d4cc63f71f578530e3. * move exampleSite to site * replace exampleSite with site * update readme * use filename for post * update index * Add source urls * set source as params * set source as params * Fix entries * Generator in go * Fixes to generator * F * Change doc gen * FIx cname * Fixing protos * Change to makefiles * Fix gen script Co-authored-by: Asim Aslam <asim@aslam.me>
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"github.com/SlyMarbo/rss"
|
|
log "github.com/micro/micro/v3/service/logger"
|
|
feeds "github.com/micro/services/feeds/proto"
|
|
posts "github.com/micro/services/posts/proto"
|
|
)
|
|
|
|
func (e *Feeds) fetchAll() {
|
|
fs := []*feeds.Feed{}
|
|
err := e.feeds.List(e.feedsNameIndex.ToQuery(nil), &fs)
|
|
if err != nil {
|
|
log.Errorf("Error listing feeds: %v", err)
|
|
return
|
|
}
|
|
if len(fs) == 0 {
|
|
log.Infof("No feeds to fetch")
|
|
return
|
|
}
|
|
for _, feed := range fs {
|
|
err = e.fetch(feed.Url)
|
|
if err != nil {
|
|
log.Errorf("Error saving post: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (e *Feeds) fetch(url string) error {
|
|
log.Infof("Fetching address %v", url)
|
|
fd, err := rss.Fetch(url)
|
|
if err != nil {
|
|
return fmt.Errorf("Error fetching address %v: %v", url, err)
|
|
}
|
|
domain := getDomain(url)
|
|
|
|
for _, item := range fd.Items {
|
|
id := fmt.Sprintf("%x", md5.Sum([]byte(item.ID)))
|
|
err = e.entries.Save(feeds.Entry{
|
|
Id: id,
|
|
Url: item.Link,
|
|
Title: item.Title,
|
|
Domain: domain,
|
|
Content: item.Summary,
|
|
Date: item.Date.Unix(),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("Error saving item: %v", err)
|
|
}
|
|
// @todo make this optional
|
|
_, err := e.postsService.Save(context.TODO(), &posts.SaveRequest{
|
|
Id: id,
|
|
Title: item.Title,
|
|
Content: item.Content,
|
|
Timestamp: item.Date.Unix(),
|
|
Metadata: map[string]string{
|
|
"domain": domain,
|
|
"link": item.Link,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getDomain(address string) string {
|
|
uri, _ := url.Parse(address)
|
|
return uri.Host
|
|
}
|