Features/subscription/features for portal (#34)

* added a route to delete subscriptions based on the ID given

* added a new route to find a record based on the name and source

* added a route to query Discord Web Hooks by Server and Channel names

* tested the endpoints and they seem good to test more

* updated some routes for subscriptions and formatted files

* removed debug file

* fixing some panic calls

* swag
This commit is contained in:
James Tombleson 2022-12-04 08:49:17 -08:00 committed by GitHub
parent c161658487
commit ff4075383a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 478 additions and 291 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
.env
dev.session.sql
__debug_bin
.vscode

View File

@ -70,7 +70,7 @@ const docTemplate = `{
"responses": {}
}
},
"/articles/{id}": {
"/articles/{ID}": {
"get": {
"produces": [
"application/json"
@ -569,7 +569,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -582,7 +581,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -604,7 +602,6 @@ const docTemplate = `{
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -644,9 +641,6 @@ const docTemplate = `{
"/subscriptions/new/discordwebhook": {
"post": {
"tags": [
"Config",
"Source",
"Discord",
"Subscription"
],
"summary": "Creates a new subscription to link a post from a Source to a DiscordWebHook.",

View File

@ -61,7 +61,7 @@
"responses": {}
}
},
"/articles/{id}": {
"/articles/{ID}": {
"get": {
"produces": [
"application/json"
@ -560,7 +560,6 @@
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -573,7 +572,6 @@
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -595,7 +593,6 @@
"application/json"
],
"tags": [
"Config",
"Subscription"
],
"summary": "Returns the top 100 entries from the queue to be processed.",
@ -635,9 +632,6 @@
"/subscriptions/new/discordwebhook": {
"post": {
"tags": [
"Config",
"Source",
"Discord",
"Subscription"
],
"summary": "Creates a new subscription to link a post from a Source to a DiscordWebHook.",

View File

@ -12,7 +12,7 @@ paths:
summary: Lists the top 50 records
tags:
- Articles
/articles/{id}:
/articles/{ID}:
get:
parameters:
- description: uuid
@ -391,7 +391,6 @@ paths:
responses: {}
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Config
- Subscription
/subscriptions/byDiscordId:
get:
@ -406,7 +405,6 @@ paths:
responses: {}
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Config
- Subscription
/subscriptions/bySourceId:
get:
@ -421,7 +419,6 @@ paths:
responses: {}
summary: Returns the top 100 entries from the queue to be processed.
tags:
- Config
- Subscription
/subscriptions/discord/webhook/delete:
delete:
@ -454,8 +451,5 @@ paths:
responses: {}
summary: Creates a new subscription to link a post from a Source to a DiscordWebHook.
tags:
- Config
- Source
- Discord
- Subscription
swagger: "2.0"

View File

@ -20,4 +20,3 @@ type Sources interface {
ExtractTags(page *rod.Page) (string, error)
ExtractTitle(page *rod.Page) (string, error)
}

View File

@ -35,7 +35,7 @@ func (s *Server) listArticles(w http.ResponseWriter, r *http.Request) {
// @Param id path string true "uuid"
// @Produce application/json
// @Tags Articles
// @Router /articles/{id} [get]
// @Router /articles/{ID} [get]
func (s *Server) getArticleById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

View File

@ -44,8 +44,7 @@ func (s *Server) GetDiscordWebHooks(w http.ResponseWriter, r *http.Request) {
func (s *Server) GetDiscordWebHooksById(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
query := r.URL.Query()
_id := query["id"][0]
_id := chi.URLParam(r, "ID")
if _id == "" {
http.Error(w, "id is missing", http.StatusBadRequest)
return

View File

@ -1,2 +1 @@
package routes_test

View File

@ -12,7 +12,7 @@ import (
// GetSubscriptions
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Tags Config, Subscription
// @Tags Subscription
// @Router /subscriptions [get]
func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@ -36,7 +36,7 @@ func (s *Server) ListSubscriptions(w http.ResponseWriter, r *http.Request) {
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id query string true "id"
// @Tags Config, Subscription
// @Tags Subscription
// @Router /subscriptions/byDiscordId [get]
func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@ -73,7 +73,7 @@ func (s *Server) GetSubscriptionsByDiscordId(w http.ResponseWriter, r *http.Requ
// @Summary Returns the top 100 entries from the queue to be processed.
// @Produce application/json
// @Param id query string true "id"
// @Tags Config, Subscription
// @Tags Subscription
// @Router /subscriptions/bySourceId [get]
func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
@ -110,7 +110,7 @@ func (s *Server) GetSubscriptionsBySourceId(w http.ResponseWriter, r *http.Reque
// @Summary Creates a new subscription to link a post from a Source to a DiscordWebHook.
// @Param discordWebHookId query string true "discordWebHookId"
// @Param sourceId query string true "sourceId"
// @Tags Config, Source, Discord, Subscription
// @Tags Subscription
// @Router /subscriptions/new/discordwebhook [post]
func (s *Server) newDiscordWebHookSubscription(w http.ResponseWriter, r *http.Request) {
// Extract the values given

View File

@ -31,8 +31,12 @@ func (cc *CacheClient) Insert(key string, value string) {
func (cc *CacheClient) FindByKey(key string) (*model.CacheItem, error) {
for _, item := range cacheStorage {
if item.Group != cc.group { continue }
if item.Key != key { continue }
if item.Group != cc.group {
continue
}
if item.Key != key {
continue
}
// if it was tainted, renew the timer and remove the taint as this record was still needed
if item.IsTainted {
@ -47,8 +51,12 @@ func (cc *CacheClient) FindByKey(key string) (*model.CacheItem, error) {
func (cc *CacheClient) FindByValue(value string) (*model.CacheItem, error) {
for _, item := range cacheStorage {
if item.Group != cc.group { continue }
if item.Value != value { continue }
if item.Group != cc.group {
continue
}
if item.Value != value {
continue
}
// if it was tainted, renew the timer and remove the taint as this record was still needed
if item.IsTainted {
@ -59,4 +67,3 @@ func (cc *CacheClient) FindByValue(value string) (*model.CacheItem, error) {
}
return &model.CacheItem{}, ErrCacheRecordMissing
}

View File

@ -18,14 +18,18 @@ func TestInsert(t *testing.T) {
func TestFindGroupMissing(t *testing.T) {
cache := cache.NewCacheClient("faker")
_, err := cache.FindByKey("UnitTesting")
if err == nil { panic("Nothing was appended with the requested group.") }
if err == nil {
panic("Nothing was appended with the requested group.")
}
}
func TestFindGroupExists(t *testing.T) {
cache := cache.NewCacheClient("Testing")
cache.Insert("UnitTesting", "Something")
_, err := cache.FindByKey("UnitTesting")
if err != nil { panic("") }
if err != nil {
panic("")
}
}
func TestCacheStorage(t *testing.T) {
@ -35,6 +39,7 @@ func TestCacheStorage(t *testing.T) {
cache := cache.NewCacheClient("Testing")
_, err := cache.FindByKey("UnitTesting02")
if err != nil { panic("expected to find the value")}
if err != nil {
panic("expected to find the value")
}
}

View File

@ -39,7 +39,9 @@ func (cam CacheAgeMonitor) CheckExpiredEntries() {
func (cam CacheAgeMonitor) removeEntry(index int) []*model.CacheItem {
var temp []*model.CacheItem
for i, item := range cacheStorage {
if i != index { temp = append(temp, item )}
if i != index {
temp = append(temp, item)
}
}
return temp
}

View File

@ -49,6 +49,17 @@ func (cc *ConfigClient) GetConfig(key string) string {
return res
}
// Looks for a value in the env and will panic if it does not exist.
func (c ConfigClient) MustGetString(key string) string {
res, filled := os.LookupEnv(key)
if !filled {
msg := fmt.Sprintf("No value was found for '%v'", key)
panic(msg)
}
return res
}
func (cc *ConfigClient) GetFeature(flag string) (bool, error) {
cc.RefreshEnv()

View File

@ -1,8 +1,8 @@
package config_test
import (
"testing"
"os"
"testing"
"github.com/jtom38/newsbot/collector/services/config"
)
@ -15,6 +15,8 @@ func TestGetConfigExpectNull(t *testing.T) {
cc := config.New()
os.Setenv(config.REDDIT_PULL_HOT, "")
res := cc.GetConfig(config.REDDIT_PULL_HOT)
if res != "" { panic("expected blank")}
if res != "" {
panic("expected blank")
}
}

View File

@ -20,7 +20,9 @@ var FFXIVRecord database.Source = database.Source{
func TestFfxivGetParser(t *testing.T) {
fc := ffxiv.NewFFXIVClient(FFXIVRecord)
_, err := fc.GetParser()
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestFfxivPullFeed(t *testing.T) {
@ -30,8 +32,12 @@ func TestFfxivPullFeed(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if len(links) == 0 { panic("expected links to come back but got 0") }
if err != nil {
t.Error(err)
}
if len(links) == 0 {
t.Error("expected links to come back but got 0")
}
}
@ -42,14 +48,20 @@ func TestFfxivExtractThumbnail(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
thumb, err := fc.ExtractThumbnail(page)
if err != nil { panic(err) }
if thumb == "" { panic("expected a link but got nothing.")}
if err != nil {
t.Error(err)
}
if thumb == "" {
t.Error("expected a link but got nothing.")
}
}
func TestFfxivExtractPubDate(t *testing.T) {
@ -59,13 +71,17 @@ func TestFfxivExtractPubDate(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
_, err = fc.ExtractPubDate(page)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestFfxivExtractDescription(t *testing.T) {
@ -75,13 +91,17 @@ func TestFfxivExtractDescription(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
_, err = fc.ExtractDescription(page)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestFfxivExtractAuthor(t *testing.T) {
@ -91,14 +111,20 @@ func TestFfxivExtractAuthor(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
author, err := fc.ExtractAuthor(page)
if err != nil { panic(err) }
if author == "" { panic("failed to locate the author name") }
if err != nil {
t.Error(err)
}
if author == "" {
t.Error("failed to locate the author name")
}
}
func TestFfxivExtractTags(t *testing.T) {
@ -108,14 +134,20 @@ func TestFfxivExtractTags(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
res, err := fc.ExtractTags(page)
if err != nil { panic(err) }
if res == "" {panic("failed to locate the tags")}
if err != nil {
t.Error(err)
}
if res == "" {
t.Error("failed to locate the tags")
}
}
func TestFfxivExtractTitle(t *testing.T) {
@ -125,14 +157,20 @@ func TestFfxivExtractTitle(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
res, err := fc.ExtractTitle(page)
if err != nil { panic(err) }
if res == "" { panic("failed to locate the tags") }
if err != nil {
t.Error(err)
}
if res == "" {
t.Error("failed to locate the tags")
}
}
func TestFFxivExtractAuthorIamge(t *testing.T) {
@ -142,14 +180,20 @@ func TestFFxivExtractAuthorIamge(t *testing.T) {
defer parser.Close()
links, err := fc.PullFeed(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
page := fc.GetPage(parser, links[0])
defer page.Close()
res, err := fc.ExtractAuthorImage(page)
if err != nil { panic(err) }
if res == "" { panic("failed to locate the tags") }
if err != nil {
t.Error(err)
}
if res == "" {
t.Error("failed to locate the tags")
}
}
func TestFfxivCheckSource(t *testing.T) {

View File

@ -21,18 +21,24 @@ func getHttpContent(uri string) ([]byte, error) {
}
req, err := http.NewRequest("GET", uri, nil)
if err != nil { return nil, err }
if err != nil {
return nil, err
}
// set the user agent header to avoid kick backs.. as much
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.10; rv:75.0) Gecko/20100101 Firefox/75.0")
log.Printf("Requesting content from %v\n", uri)
resp, err := client.Do(req)
if err != nil { log.Fatalln(err) }
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return body, nil
}

View File

@ -47,7 +47,6 @@ func NewRedditClient(Record database.Source) *RedditClient {
// os.Setenv("GODEBUG", "http2client=0")
//}
func (rc *RedditClient) GetBrowser() *rod.Browser {
var browser *rod.Browser
if path, exists := launcher.LookPath(); exists {
@ -74,8 +73,6 @@ func (rc *RedditClient) GetContent() (model.RedditJsonContent, error) {
log.Printf("[Reddit] Collecting results on '%v'", rc.record.Name)
content, err := getHttpContent(Url)
if err != nil {
return items, err

View File

@ -31,7 +31,9 @@ func (rc rssClient) getCacheGroup() string {
func (rc rssClient) GetContent() error {
feed, err := rc.PullFeed()
if err != nil { return err }
if err != nil {
return err
}
cacheClient := cache.NewCacheClient(rc.getCacheGroup())
@ -49,7 +51,9 @@ func (rc rssClient) PullFeed() (*gofeed.Feed, error) {
feedUri := fmt.Sprintf("%v", rc.SourceRecord.Url)
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedUri)
if err != nil { return nil, err }
if err != nil {
return nil, err
}
return feed, nil
}

View File

@ -20,7 +20,11 @@ func TestRssClientConstructor(t *testing.T) {
func TestRssGetFeed(t *testing.T) {
client := input.NewRssClient(rssRecord)
feed, err := client.PullFeed()
if err != nil { t.Error(err) }
if len(feed.Items) >= 0 { t.Error("failed to collect items from the fees")}
if err != nil {
t.Error(err)
}
if len(feed.Items) >= 0 {
t.Error("failed to collect items from the fees")
}
}

View File

@ -104,31 +104,47 @@ func (tc *TwitchClient) GetContent() ([]database.Article, error) {
var article database.Article
AuthorName, err := tc.ExtractAuthor(video)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Authorname = sql.NullString{String: AuthorName}
Authorimage, err := tc.ExtractAuthorImage(user)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Authorimage = sql.NullString{String: Authorimage}
article.Description, err = tc.ExtractDescription(video)
if err != nil {return items, err }
if err != nil {
return items, err
}
article.Pubdate, err = tc.ExtractPubDate(video)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Sourceid = tc.SourceRecord.ID
article.Tags, err = tc.ExtractTags(video, user)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Thumbnail, err = tc.ExtractThumbnail(video)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Title, err = tc.ExtractTitle(video)
if err != nil { return items, err }
if err != nil {
return items, err
}
article.Url, err = tc.ExtractUrl(video)
if err != nil { return items, err }
if err != nil {
return items, err
}
items = append(items, article)
}
@ -210,8 +226,12 @@ func (tc *TwitchClient) ExtractDescription(post helix.Video) (string, error) {
// Extracts the avatar of the author with some validation.
func (tc *TwitchClient) ExtractAuthorImage(user helix.User) (string, error) {
if user.ProfileImageURL == "" { return "", ErrMissingAuthorImage }
if !strings.Contains(user.ProfileImageURL, "-profile_image-") { return "", ErrInvalidAuthorImage }
if user.ProfileImageURL == "" {
return "", ErrMissingAuthorImage
}
if !strings.Contains(user.ProfileImageURL, "-profile_image-") {
return "", ErrInvalidAuthorImage
}
return user.ProfileImageURL, nil
}
@ -231,6 +251,8 @@ func (tc *TwitchClient) ExtractTitle(post helix.Video) (string, error) {
}
func (tc *TwitchClient) ExtractUrl(post helix.Video) (string, error) {
if post.URL == "" { return "", ErrMissingUrl }
if post.URL == "" {
return "", ErrMissingUrl
}
return post.URL, nil
}

View File

@ -115,40 +115,62 @@ func TestTwitchReturnsVideoAuthor(t *testing.T) {
func TestTwitchReturnsThumbnail(t *testing.T) {
tc, err := input.NewTwitchClient()
if err != nil {t.Error(err) }
if err != nil {
t.Error(err)
}
tc.ReplaceSourceRecord(TwitchSourceRecord)
err = tc.Login()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
value, err := tc.ExtractThumbnail(posts[0])
if err != nil { t.Error(err) }
if value == "" { t.Error("uable to parse username") }
if err != nil {
t.Error(err)
}
if value == "" {
t.Error("uable to parse username")
}
}
func TestTwitchReturnsPubDate(t *testing.T) {
tc, err := input.NewTwitchClient()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
tc.ReplaceSourceRecord(TwitchSourceRecord)
err = tc.Login()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
date, err := tc.ExtractPubDate(posts[0])
log.Println(date)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
}
func TestTwitchReturnsDescription(t *testing.T) {
@ -181,17 +203,25 @@ func TestTwitchReturnsDescription(t *testing.T) {
func TestTwitchReturnsAuthorImage(t *testing.T) {
tc, err := input.NewTwitchClient()
if err != nil {t.Error(err) }
if err != nil {
t.Error(err)
}
tc.ReplaceSourceRecord(TwitchSourceRecord)
err = tc.Login()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil {t.Error(err) }
if err != nil {
t.Error(err)
}
_, err = tc.ExtractAuthorImage(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
}
func TestTwitchReturnsTags(t *testing.T) {
@ -212,10 +242,14 @@ func TestTwitchReturnsTags(t *testing.T) {
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
_, err = tc.ExtractTags(posts[0], user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
}
func TestTwitchReturnsTitle(t *testing.T) {
@ -236,42 +270,70 @@ func TestTwitchReturnsTitle(t *testing.T) {
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
res, err := tc.ExtractTitle(posts[0])
if err != nil { t.Error(err) }
if res == "" { t.Error("expected a filled string but got nil")}
if err != nil {
t.Error(err)
}
if res == "" {
t.Error("expected a filled string but got nil")
}
}
func TestTwitchReturnsUrl(t *testing.T) {
tc, err := input.NewTwitchClient()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
tc.ReplaceSourceRecord(TwitchSourceRecord)
err = tc.Login()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
user, err := tc.GetUserDetails()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
posts, err := tc.GetPosts(user)
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
res, err := tc.ExtractUrl(posts[0])
if err != nil { t.Error(err) }
if res == "" { t.Error("expected a filled string but got nil")}
if err != nil {
t.Error(err)
}
if res == "" {
t.Error("expected a filled string but got nil")
}
}
func TestTwitchGetContent(t *testing.T) {
tc, err := input.NewTwitchClient()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
tc.ReplaceSourceRecord(TwitchSourceRecord)
err = tc.Login()
if err != nil { t.Error(err) }
if err != nil {
t.Error(err)
}
posts, err := tc.GetContent()
if err != nil {t.Error(err) }
if len(posts) == 0 { t.Error("posts came back with 0 posts") }
if len(posts) != 20 { t.Error("expected 20 posts") }
if err != nil {
t.Error(err)
}
if len(posts) == 0 {
t.Error("posts came back with 0 posts")
}
if len(posts) != 20 {
t.Error("expected 20 posts")
}
}

View File

@ -44,13 +44,6 @@ func NewYoutubeClient(Record database.Source) YoutubeClient {
record: Record,
cacheGroup: "youtube",
}
/*
cc := NewConfigClient()
debug, err := strconv.ParseBool(cc.GetConfig(YOUTUBE_DEBUG))
if err != nil { panic("'YOUTUBE_DEBUG' was not a bool value")}
yc.Config.Debug = debug
*/
return yc
}
@ -109,14 +102,18 @@ func (yc *YoutubeClient) GetContent() ([]database.Article, error) {
return items, nil
}
func (yc *YoutubeClient) GetBrowser() *rod.Browser {
func (yc *YoutubeClient) GetBrowser() (*rod.Browser, error) {
//browser := rod.New().MustConnect()
var browser *rod.Browser
if path, exists := launcher.LookPath(); exists {
u := launcher.New().Bin(path).MustLaunch()
u, err := launcher.New().Bin(path).Launch()
if err != nil {
return browser, err
}
browser = rod.New().ControlURL(u).MustConnect()
}
return browser
return browser, nil
}
func (yc *YoutubeClient) GetPage(parser *rod.Browser, url string) *rod.Page {
@ -163,7 +160,11 @@ func (yc *YoutubeClient) GetAvatarUri() (string, error) {
var AvatarUri string
//browser := rod.New().MustConnect()
browser := yc.GetBrowser()
browser, err := yc.GetBrowser()
if err != nil {
return "", err
}
page := browser.MustPage(yc.record.Url)
res := page.MustElement("#channel-header-container > yt-img-shadow:nth-child(1) > img:nth-child(1)").MustAttribute("src")

View File

@ -19,35 +19,51 @@ var YouTubeRecord database.Source = database.Source{
func TestGetPageParser(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
_, err := yc.GetParser(YouTubeRecord.Url)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestGetChannelId(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
parser, err := yc.GetParser(YouTubeRecord.Url)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
_, err = yc.GetChannelId(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestPullFeed(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
parser, err := yc.GetParser(YouTubeRecord.Url)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
_, err = yc.GetChannelId(parser)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
_, err = yc.PullFeed()
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestGetAvatarUri(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
res, err := yc.GetAvatarUri()
if err != nil { panic(err) }
if res == "" { panic(input.ErrMissingAuthorImage)}
if err != nil {
t.Error(err)
}
if res == "" {
t.Error(input.ErrMissingAuthorImage)
}
}
func TestGetVideoTags(t *testing.T) {
@ -56,39 +72,59 @@ func TestGetVideoTags(t *testing.T) {
var videoUri = "https://www.youtube.com/watch?v=k_sQEXOBe68"
parser, err := yc.GetParser(videoUri)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
tags, err := yc.GetTags(parser)
if err == nil && tags == "" { panic("err was empty but value was missing.")}
if err != nil { panic(err) }
if err == nil && tags == "" {
t.Error("err was empty but value was missing.")
}
if err != nil {
t.Error(err)
}
}
func TestGetChannelTags(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
parser, err := yc.GetParser(YouTubeRecord.Url)
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
tags, err := yc.GetTags(parser)
if err == nil && tags == "" { panic("no err but expected value was missing.")}
if err != nil { panic(err) }
if err == nil && tags == "" {
t.Error("no err but expected value was missing.")
}
if err != nil {
t.Error(err)
}
}
func TestGetVideoThumbnail(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
parser, err := yc.GetParser("https://www.youtube.com/watch?v=k_sQEXOBe68")
if err != nil {panic(err) }
if err != nil {
t.Error(err)
}
thumb, err := yc.GetVideoThumbnail(parser)
if err == nil && thumb == "" { panic("no err but expected result was missing")}
if err != nil { panic(err) }
if err == nil && thumb == "" {
t.Error("no err but expected result was missing")
}
if err != nil {
t.Error(err)
}
}
func TestCheckSource(t *testing.T) {
yc := input.NewYoutubeClient(YouTubeRecord)
_, err := yc.GetContent()
if err != nil { panic(err) }
if err != nil {
t.Error(err)
}
}
func TestCheckUriCache(t *testing.T) {
@ -97,7 +133,9 @@ func TestCheckUriCache(t *testing.T) {
input.YoutubeUriCache = append(input.YoutubeUriCache, &item)
res := yc.CheckUriCache(&item)
if res == false { panic("expected a value to come back")}
if res == false {
t.Error("expected a value to come back")
}
}
func TestCheckUriCacheFails(t *testing.T) {
@ -105,6 +143,8 @@ func TestCheckUriCacheFails(t *testing.T) {
item := "demo1"
res := yc.CheckUriCache(&item)
if res == true { panic("expected no value to come back")}
if res == true {
t.Error("expected no value to come back")
}
}