Skip to content

Commit

Permalink
Strip domain from url
Browse files Browse the repository at this point in the history
  • Loading branch information
rupinr committed Oct 27, 2024
1 parent 95e4b2e commit e347b1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"fmt"
"net/url"
"news-master/app"
"news-master/datamodels/dto"
"news-master/datamodels/entity"
Expand Down Expand Up @@ -82,11 +83,19 @@ func CreateResult(result dto.Article) {
Language: result.Language,
Country: pq.StringArray(result.Country),
Category: pq.StringArray(result.Category),
Site: result.SourceUrl,
Site: getDomain(result.SourceUrl),
}
db().Create(&article)
}

func getDomain(rawURL string) string {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return fmt.Sprintf("INVALID_RAW_URL_%v", rawURL)
}
return parsedURL.Host
}

func GetActiveSites() []entity.Site {
var sites []entity.Site
db().Where(entity.Site{Active: true}).Find(&sites)
Expand Down
13 changes: 13 additions & 0 deletions tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"news-master/app"
"news-master/auth"
notification "news-master/cmd/process"
Expand Down Expand Up @@ -40,6 +41,10 @@ func FetchNewsTask() {
return
}

for i := range response.Results {
response.Results[i].SourceUrl = getDomain(response.Results[i].SourceUrl)
}

unmarshalErr := json.Unmarshal(body, &response)

if unmarshalErr != nil {
Expand All @@ -56,6 +61,14 @@ func FetchNewsTask() {
}
}

func getDomain(rawURL string) string {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return fmt.Sprintf("INVALID_RAW_URL_%v", rawURL)
}
return parsedURL.Host
}

func SendNewsletter() {
subscriptions := repository.GetSubscriptionsToProcess()

Expand Down

0 comments on commit e347b1b

Please sign in to comment.