Skip to content

Commit

Permalink
chore: remove unused fields of domain.Media.
Browse files Browse the repository at this point in the history
  • Loading branch information
omegaatt36 committed Aug 4, 2024
1 parent 353a0ca commit 502988b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 46 deletions.
4 changes: 2 additions & 2 deletions app/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Server) addFilm(w http.ResponseWriter, r *http.Request) {
medias := make([]media, 0)
// Check if media has no child item
if len(domainMedia.Items) == 0 {
m, err := encodeMediaToBase64(domainMedia.Url, domainMedia.IsVideo)
m, err := encodeMediaToBase64(domainMedia.URL, domainMedia.IsVideo)
if err != nil {
logging.ErrorCtx(r.Context(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -73,7 +73,7 @@ func (s *Server) addFilm(w http.ResponseWriter, r *http.Request) {
medias = append(medias, m)
} else {
for _, item := range domainMedia.Items {
m, err := encodeMediaToBase64(item.Url, item.IsVideo)
m, err := encodeMediaToBase64(item.URL, item.IsVideo)
if err != nil {
logging.ErrorCtx(r.Context(), err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
24 changes: 8 additions & 16 deletions appmodule/instagram/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
"github.com/omegaatt36/instagramrobot/domain"
)

// InstagramFetcherRepo is the repository for fetching Instagram media.
type InstagramFetcherRepo struct {
// Extractor is the implement for fetching Instagram media.
type Extractor struct {
client *http.Client
}

// NewInstagramFetcher will create a new instance of InstagramFetcherRepo.
func NewInstagramFetcher() domain.InstagramFetcher {
return &InstagramFetcherRepo{
return &Extractor{
client: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Expand All @@ -39,24 +39,16 @@ func NewInstagramFetcher() domain.InstagramFetcher {
// fromEmbedResponse will automatically transforms the EmbedResponse to the Media
func fromEmbedResponse(embed EmbedResponse) domain.Media {
media := domain.Media{
Id: embed.Media.Id,
ShortCode: embed.Media.ShortCode,
Type: embed.Media.Type,
Comments: embed.Media.Comments.Count,
Likes: embed.Media.Likes.Count,
Url: embed.ExtractMediaURL(),
TakenAt: embed.Media.TakenAt.Unix(),
URL: embed.ExtractMediaURL(),
IsVideo: embed.IsVideo(),
Caption: embed.GetCaption(),
}

for _, item := range embed.Media.SliderItems.Edges {
media.Items = append(media.Items, domain.MediaItem{
Id: item.Node.Id,
ShortCode: item.Node.ShortCode,
Type: item.Node.Type,
IsVideo: item.Node.IsVideo,
Url: item.Node.ExtractMediaURL(),
IsVideo: item.Node.IsVideo,
URL: item.Node.ExtractMediaURL(),
})
}

Expand All @@ -65,7 +57,7 @@ func fromEmbedResponse(embed EmbedResponse) domain.Media {

// GetPostWithCode lets you to get information about specific Instagram post
// by providing its unique short code
func (repo *InstagramFetcherRepo) GetPostWithCode(code string) (domain.Media, error) {
func (repo *Extractor) GetPostWithCode(code string) (domain.Media, error) {
URL := "https://www.threads.net/@beerich168/post/C-LIcYCytrH/embed"

var embeddedMediaImage string
Expand Down Expand Up @@ -111,7 +103,7 @@ func (repo *InstagramFetcherRepo) GetPostWithCode(code string) (domain.Media, er

if embeddedMediaImage != "" {
return domain.Media{
Url: embeddedMediaImage,
URL: embeddedMediaImage,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions appmodule/telegram/media-sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *MediaSender) Send(media *domain.Media) error {
func (m *MediaSender) sendSingleMedia(media *domain.Media) error {
if media.IsVideo {
if _, err := m.bot.Send(m.msg.Chat, &telebot.Video{
File: telebot.FromURL(media.Url),
File: telebot.FromURL(media.URL),
Caption: caption,
}); err != nil {
return fmt.Errorf("couldn't send the single video, %w", err)
Expand All @@ -49,7 +49,7 @@ func (m *MediaSender) sendSingleMedia(media *domain.Media) error {
logging.Debugf("Sent single video with short code [%v]", media.ShortCode)
} else {
if _, err := m.bot.Send(m.msg.Chat, &telebot.Photo{
File: telebot.FromURL(media.Url),
File: telebot.FromURL(media.URL),
Caption: caption,
}); err != nil {
return fmt.Errorf("couldn't send the single photo, %w", err)
Expand All @@ -75,11 +75,11 @@ func (m *MediaSender) generateAlbumFromMedia(media *domain.Media) telebot.Album
for _, media := range media.Items {
if media.IsVideo {
album = append(album, &telebot.Video{
File: telebot.FromURL(media.Url),
File: telebot.FromURL(media.URL),
})
} else {
album = append(album, &telebot.Photo{
File: telebot.FromURL(media.Url),
File: telebot.FromURL(media.URL),
})
}
}
Expand Down
9 changes: 4 additions & 5 deletions appmodule/threads/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewExtractor() domain.ThreadsFetcher {

func (repo *Extractor) GetPostWithURL(URL *url.URL) (media domain.Media, err error) {
URL.RawQuery = ""
fmt.Println(URL.String())

URL = URL.JoinPath("embed")

Expand All @@ -43,10 +42,10 @@ func (repo *Extractor) GetPostWithURL(URL *url.URL) (media domain.Media, err err
// case single image or video
collector.OnHTML("div.SingleInnerMediaContainer", func(e *colly.HTMLElement) {
if src := e.ChildAttr("img", "src"); src != "" {
media.Url = src
media.URL = src
}
if src := e.ChildAttr("video > source", "src"); src != "" {
media.Url = src
media.URL = src
media.IsVideo = true
}
})
Expand All @@ -55,12 +54,12 @@ func (repo *Extractor) GetPostWithURL(URL *url.URL) (media domain.Media, err err
collector.OnHTML("div.MediaScrollImageContainer", func(e *colly.HTMLElement) {
if src := e.ChildAttr("img", "src"); src != "" {
media.Items = append(media.Items, domain.MediaItem{
Url: src,
URL: src,
})
}
if src := e.ChildAttr("video > source", "src"); src != "" {
media.Items = append(media.Items, domain.MediaItem{
Url: src,
URL: src,
IsVideo: true,
})
}
Expand Down
29 changes: 10 additions & 19 deletions domain/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ package domain
// )
type Source string

// MediaItem contains information about the Instagram post
// which is similar to the Instagram Media struct
// MediaItem contains media information
type MediaItem struct {
Id string `json:"id"`
ShortCode string `json:"shortcode"`
Type string `json:"type"`
IsVideo bool `json:"is_video"`
Url string `json:"url"`
IsVideo bool `json:"is_video"`
URL string `json:"url"`
}

// Media which contains a single Instagram post
// Media which contains a single post
type Media struct {
Id string `json:"id"`
ShortCode string `json:"shortcode"`
Type string `json:"type"`
Comments uint64 `json:"comments_count"`
Likes uint64 `json:"likes_count"`
Caption string `json:"caption"`
IsVideo bool `json:"is_video"`
Url string `json:"url"`
Items []MediaItem `json:"items"`
TakenAt int64 `json:"taken_at"` // Timestamp
Source Source `json:"source"`
ShortCode string
Caption string
IsVideo bool
URL string
Items []MediaItem
Source Source
}

// MediaSender defines the contract for sending media to the Telegram chat.
Expand Down

0 comments on commit 502988b

Please sign in to comment.