Skip to content

Commit

Permalink
fix: separate individual feeders
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse0Michael committed Dec 14, 2022
1 parent cce8472 commit b2d6333
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 403 deletions.
2 changes: 1 addition & 1 deletion internal/server/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestServer_feed(t *testing.T) {
name: "successful feed retrieval",
req: httptest.NewRequest(http.MethodGet, "/feed?twitterID=60887026&instagramID=50957893&bloggerID=2628647666607369284&soundcloudID=20560365&swarmID=jesse&deviantartID=mini-michael/33242408", nil),
fetcher: &MockFetcher{
expected: service.FetcherRequest{TwitterID: "60887026", InstagramID: "50957893", BloggerID: "2628647666607369284", SoundcloudID: "20560365", SwarmID: "jesse", DeviantartID: "mini-michael/33242408"},
expected: service.FetcherRequest{TwitterID: "60887026", InstagramID: "50957893", BloggerID: "2628647666607369284", SoundCloudID: "20560365", SwarmID: "jesse", DeviantArtID: "mini-michael/33242408"},
items: []service.FeedItem{
{ID: "test", Source: "testing"},
},
Expand Down
59 changes: 59 additions & 0 deletions internal/service/blogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package service

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/tidwall/gjson"
)

type Blogger struct {
}

func NewBlogger() *Blogger {
return &Blogger{}
}

func (b *Blogger) Feed(ctx context.Context, id string) ([]FeedItem, error) {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet,
fmt.Sprintf("https://www.googleapis.com/blogger/v2/blogs/%s/posts", id), nil)
if err != nil {
return nil, err
}
q := url.Values{}
q.Add("key", "AIzaSyBU3_KGZO90Vu_s8Lhbl7lJAEsaIouAEaY")
q.Add("fetchBodies", "true")
q.Add("maxResults", "20")
req.URL.RawQuery = q.Encode()

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

items := []FeedItem{}
for _, blog := range gjson.GetBytes(body, "items").Array() {
time, err := time.Parse(time.RFC3339, blog.Get("published").String())
if err != nil {
return nil, err
}
item := FeedItem{
ID: blog.Get("id").String(),
TS: time.Unix(),
Source: "blogger",
URL: blog.Get("url").String(),
Content: blog.Get("content").String(),
}
items = append(items, item)
}
return items, nil
}
53 changes: 53 additions & 0 deletions internal/service/deviantart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package service

import (
"context"
"fmt"

"github.com/mmcdole/gofeed"
)

type DeviantArt struct {
}

func NewDeviantArt() *DeviantArt {
return &DeviantArt{}
}

func (d *DeviantArt) Feed(ctx context.Context, id string) ([]FeedItem, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(fmt.Sprintf("https://backend.deviantart.com/rss.xml?q=gallery:%s", id))
if err != nil {
return nil, err
}

items := []FeedItem{}
for _, art := range feed.Items {
var image string
if media, ok := art.Extensions["media"]; ok {
if content, ok := media["content"]; ok && len(content) > 0 {
if url, ok := content[0].Attrs["url"]; ok {
image = url
}
}
}

if image == "" {
continue
}

item := FeedItem{
ID: art.Title,
TS: art.PublishedParsed.Unix(),
Source: "deviantart",
Media: []FeedItemMedia{{
URL: image,
Kind: "image",
}},
URL: art.Link,
Content: art.Title,
}
items = append(items, item)
}
return items, nil
}
Loading

0 comments on commit b2d6333

Please sign in to comment.