Skip to content

Commit

Permalink
Use YtDlpApi for instagram flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ailinykh committed May 1, 2024
1 parent 5753864 commit 47a5331
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 171 deletions.
48 changes: 0 additions & 48 deletions api/instagram.go

This file was deleted.

79 changes: 0 additions & 79 deletions api/instagram_api.go

This file was deleted.

2 changes: 1 addition & 1 deletion api/tiktok_media_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TikTokMediaFactory struct {
}

func (factory *TikTokMediaFactory) CreateMedia(url string) ([]*core.Media, error) {
item, err := factory.api.get(url)
item, err := factory.api.Get(url)
if err != nil {
factory.l.Error(err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions api/youtube_media_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type YoutubeMediaFactory struct {

// CreateMedia is a core.IMediaFactory interface implementation
func (y *YoutubeMediaFactory) CreateMedia(url string) ([]*core.Media, error) {
resp, err := y.api.get(url)
resp, err := y.api.Get(url)
if err != nil {
y.l.Error(err)
return nil, err
Expand Down Expand Up @@ -73,7 +73,7 @@ func (y *YoutubeMediaFactory) getFormats(resp *YtDlpResponse) (*YtDlpFormat, *Yt

// CreateVideo is a core.IVideoFactory interface implementation
func (y *YoutubeMediaFactory) CreateVideo(id string) (*core.Video, error) {
resp, err := y.api.get(id)
resp, err := y.api.Get(id)
if err != nil {
y.l.Error(err)
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions api/ytdlp_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type YoutubeApi interface {
get(string) (*YtDlpResponse, error)
Get(string) (*YtDlpResponse, error)
}

func CreateYtDlpApi(cookie string, l core.ILogger) YoutubeApi {
Expand All @@ -22,7 +22,7 @@ type YtDlpApi struct {
l core.ILogger
}

func (api *YtDlpApi) get(url string) (*YtDlpResponse, error) {
func (api *YtDlpApi) Get(url string) (*YtDlpResponse, error) {
args := []string{
"--quiet",
"--no-warnings",
Expand Down
5 changes: 2 additions & 3 deletions pullanusbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ func main() {
iDoNotCare := usecases.CreateIDoNotCare()
telebot.AddHandler(iDoNotCare)

instaAPI := api.CreateInstagramAPI(logger, os.Getenv("INSTAGRAM_COOKIE"))
downloadVideoFactory := helpers.CreateDownloadVideoFactory(logger, fileDownloader, converter)
instaFlow := usecases.CreateInstagramFlow(logger, instaAPI, downloadVideoFactory, localMediaSender, sendVideoStrategySplitDecorator)
instaAPI := api.CreateYtDlpApi(path.Join(getWorkingDir(), "cookies.txt"), logger)
instaFlow := usecases.CreateInstagramFlow(logger, instaAPI, localMediaSender)
removeInstaSourceDecorator := usecases.CreateRemoveSourceDecorator(logger, instaFlow, core.SInstagramFlowRemoveSource, boolSettingProvider)
telebot.AddHandler(removeInstaSourceDecorator)

Expand Down
60 changes: 24 additions & 36 deletions usecases/instagram_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import (
"github.com/ailinykh/pullanusbot/v2/core"
)

func CreateInstagramFlow(l core.ILogger, api api.InstAPI, createVideo core.IVideoFactory, sendMedia core.ISendMediaStrategy, sendVideo core.ISendVideoStrategy) core.ITextHandler {
return &InstagramFlow{l, api, createVideo, sendMedia, sendVideo}
func CreateInstagramFlow(l core.ILogger, api api.YoutubeApi, sendMedia core.ISendMediaStrategy) core.ITextHandler {
return &InstagramFlow{l, api, sendMedia}
}

type InstagramFlow struct {
l core.ILogger
api api.InstAPI
createVideo core.IVideoFactory
sendMedia core.ISendMediaStrategy
sendVideo core.ISendVideoStrategy
l core.ILogger
api api.YoutubeApi
sendMedia core.ISendMediaStrategy
}

// HandleText is a core.ITextHandler protocol implementation
Expand Down Expand Up @@ -56,23 +54,13 @@ func (flow *InstagramFlow) HandleText(message *core.Message, bot core.IBot) erro

func (flow *InstagramFlow) handleReel(url string, message *core.Message, bot core.IBot) error {
flow.l.Infof("processing %s", url)
reel, err := flow.api.GetReel(url)
resp, err := flow.api.Get(url)
if err != nil {
flow.l.Error(err)
return err
}

if len(reel.Items) < 1 {
return fmt.Errorf("insufficient reel items")
}

item := reel.Items[0]

caption := item.Caption.Text
if info := item.ClipsMetadata.MusicInfo; info != nil {
caption = fmt.Sprintf("\n🎶 <a href='%s'>%s - %s</a>\n\n%s", info.MusicAssetInfo.ProgressiveDownloadURL, info.MusicAssetInfo.DisplayArtist, info.MusicAssetInfo.Title, caption)
}
caption = fmt.Sprintf("<a href='%s'>📷</a> <b>%s</b> <i>(by %s)</i>\n%s", url, item.User.FullName, message.Sender.DisplayName(), caption)
caption := fmt.Sprintf("<a href='%s'>📷</a> <b>%s</b> <i>(by %s)</i>\n%s", url, resp.Uploader, message.Sender.DisplayName(), resp.Description)
if len(caption) > 1024 {
// strip by last space or line break if caption size limit exceeded
index := strings.LastIndex(caption[:1024], " ")
Expand All @@ -83,31 +71,31 @@ func (flow *InstagramFlow) handleReel(url string, message *core.Message, bot cor
caption = caption[:index]
}

if item.VideoDuration < 360 { // apparently 6 min file takes less than 50 MB
return flow.sendAsMedia(item, caption, message, bot)
}

video, err := flow.createVideo.CreateVideo(item.VideoVersions[0].URL)
vf, err := flow.getPreferredVideoFormat(resp)
if err != nil {
flow.l.Error(err)
return err
}
defer video.Dispose()

return flow.sendVideo.SendVideo(video, caption, bot)
media := core.Media{
Caption: caption,
ResourceURL: vf.Url,
URL: url,
}
return flow.sendMedia.SendMedia([]*core.Media{&media}, bot)
}

func (flow *InstagramFlow) sendAsMedia(item api.IgReelItem, caption string, message *core.Message, bot core.IBot) error {
media := &core.Media{
ResourceURL: item.VideoVersions[0].URL,
URL: "https://www.instagram.com/reel/" + item.Code + "/",
Title: item.User.FullName,
Caption: caption,
func (flow *InstagramFlow) getPreferredVideoFormat(resp *api.YtDlpResponse) (*api.YtDlpFormat, error) {
idx := -1
for i, f := range resp.Formats {
if strings.HasPrefix(f.FormatId, "dash-") {
continue
}
idx = i
}

err := flow.sendMedia.SendMedia([]*core.Media{media}, bot)
if err != nil {
flow.l.Error(err)
if idx < 0 {
return nil, fmt.Errorf("no appropriate format found")
}
return err
return resp.Formats[idx], nil
}

0 comments on commit 47a5331

Please sign in to comment.