go-twitter icon indicating copy to clipboard operation
go-twitter copied to clipboard

Add in functionality to upload media to twitter

Open drswork opened this issue 4 years ago • 31 comments

This wraps the media upload and upload status checking API calls so clients can upload media to twitter.

drswork avatar Jul 21 '19 14:07 drswork

And apparently this contains the Config fetch patch too, since branching is hard. (D'oh!) I can sort them both out into properly separate patches if you're not sure you want the config patch.

drswork avatar Jul 21 '19 14:07 drswork

Great work!

It would be nice if the init, append, and finalize calls were separated. This current implementation is a good MVP, but I'd also like to see concurrency and block retrying. If that's too much to add to the library, splitting those calls up would allow users to implement those things themselves.

shreve avatar Sep 30 '19 02:09 shreve

The first pass of the API I trued while I was working this out did have the calls surfaced individually, but when I was using it in my app I realized it didn't bring me any advantages and was just annoying to use. (Surfacing the calls while still providing a bundled API was both messy and required a lot of boilerplate testing that didn't seem to bring any value) Bundling everything together was just a lot easier to use, and I figured anyone who really wanted more low-level control was probably just calling the twitter API themselves.

The code also doesn't retry blocks and dies on first failure intentionally -- my assumption there was that this was server-side (or at least running on a permanently connected machine) code, so the vast majority of failure cases would involve just redoing everything and there wasn't much value in retries. I'm not averse to adding some retry logic, though.

As far as concurrency goes, are you talking about sending different blocks of the same file in parallel? Or some other form of concurrency? (The twitter API doesn't mention that parallel block uploads is allowable, and since the API is rate-limited it seems a good way to run into quota issues)

drswork avatar Sep 30 '19 16:09 drswork

@drswork Ok, thanks, I see your reasoning here. Most of the statements in the docs about retrying refer to flaky cell networks. I am in an OS class right now so I've got parallelization on the brain, but I didn't think about rate limiting.

Above all else I'd love to see this PR merged so it can start being used and iterated on if need be.

shreve avatar Sep 30 '19 21:09 shreve

Is there any progress here? I would love to have this feature in my project using this library. Do you need help to get this ready to merge? Would like to help to finish this feature.

0x46616c6b avatar Dec 27 '19 14:12 0x46616c6b

I'm not aware of any outstanding issues. (I've had a twitter bot running since the summer using this so at least the code works OK in the basic, or at least my, case) I think it's mostly that I sent it in when dghubble turned his attention to other things so it's kinda sitting.

drswork avatar Dec 27 '19 14:12 drswork

But what if it merged and we improve this afterwards?

EDIT: Need the approval from @dghubble

0x46616c6b avatar Dec 27 '19 14:12 0x46616c6b

Yep, @dghubble needs to give his OK. I'm good reworking the code if necessary, that's probably not a big deal.

drswork avatar Dec 27 '19 16:12 drswork

Hey @dghubble could you please check in this feature please :). It would be really nice to have it :)

Vizualni avatar Jan 18 '20 21:01 Vizualni

Hi,

Hope you are all well !

Is it possible to add media to a tweet with your pull request ?

Cheers, X

ghost avatar Feb 07 '20 06:02 ghost

@x0rzkov are you asking if with this patch can you send tweets with images attached? Yep, you can. You have to upload the image first and get its ID. Then you can send a tweet with that media ID attached. Folks will see a tweet with an image in it.

drswork avatar Feb 07 '20 14:02 drswork

@dghubble can you merge this pull request please ? or maybe you have a specific point of view on it ?!

ghost avatar Feb 07 '20 14:02 ghost

@drswork can you provide a quick code example of how it works ? ndlr. to attach a media to a tweet, I forked your code, and want to give a try.

ghost avatar Feb 08 '20 09:02 ghost

Attaching an image to a tweet is pretty simple. Code looks like:

  tweetParams := &twitter.StatusUpdateParams{}
  if msg.OutboundImage != nil {
    res, resp, err := t.Client.Media.Upload(msg.OutboundImage.Contents, msg.OutboundImage.Type)
    if err != nil {
      return err
    }
    if res.MediaID > 0 {
      tweetParams.MediaIds = []int64{res.MediaID}
    }
  }
  tweet, resp, err := t.Client.Statuses.Update(msg.OutboundStatus, tweetParams)

In this case msg is a struct internal to the server that has the status text and image contents and type in it for the tweet that needs to be sent out.

drswork avatar Feb 09 '20 16:02 drswork

@drswork thanks, it works like a charm

ghost avatar Feb 10 '20 04:02 ghost

@dghubble - one more vote to see this merged and released soon! 👍

sivy avatar Feb 24 '20 12:02 sivy

One thing I would add to this is support for media_category. One thing this allows is for users to upload GIFs >= 5mb. I had to modify this library to support that.

DanB91 avatar Mar 04 '20 21:03 DanB91

Waiting to see this being merged!

BennyThink avatar Oct 22 '20 08:10 BennyThink

@dghubble Thanks for your amazing library. Can I help to test this feature?

cmuench avatar Nov 28 '20 11:11 cmuench

I tested it and it's working

janisz avatar Dec 06 '20 09:12 janisz

I'm using this feature in production and works well. Simple example of usage:

        media, resp, err := client.Media.Upload(content, "tweet_image")
	if err != nil {
		log.Fatal(err)
	}

	tweet, resp, err := client.Statuses.Update("your tweet comment here...", &twitter.StatusUpdateParams{
		MediaIds: []int64{media.MediaID},
	})
	if err != nil {
		log.Fatal(err)
	}

rodrigo-brito avatar Feb 14 '21 20:02 rodrigo-brito

Any status on this getting merged?

gregorytucker avatar May 17 '21 17:05 gregorytucker

@dghubble what do you think about getting this merged?

whalecoiner avatar Sep 15 '21 08:09 whalecoiner

@dghubble great library.... can you merge uploadMedia functionality please?

dfelici87 avatar Sep 22 '21 08:09 dfelici87

I am waiting for

zeing avatar Mar 16 '22 11:03 zeing

I am waiting for

zeing avatar Mar 16 '22 11:03 zeing

Any updates on this? Not having the ability to upload photos is a deal breaker for me

drewmccal avatar Mar 21 '22 02:03 drewmccal

will be good if it's merged

ghazimuharam avatar May 19 '22 18:05 ghazimuharam

Any news on merging this? It's actually a winning feature

NachoNievaG avatar Jul 09 '22 21:07 NachoNievaG

FWIW I’m maintaining my branch (and I use the media functionality so I make sure to keep it working), so you can always use that if you need this.

drswork avatar Jul 10 '22 18:07 drswork

Thanks @drswork, I am switching to your repo instead.

zufardhiyaulhaq avatar Jul 11 '22 16:07 zufardhiyaulhaq