Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Add in functionality to upload media to twitter - #148

Closed
drswork wants to merge 6 commits into
dghubble:mainfrom
drswork:media
Closed

Add in functionality to upload media to twitter#148
drswork wants to merge 6 commits into
dghubble:mainfrom
drswork:media

Conversation

@drswork

@drswork drswork commented Jul 21, 2019

Copy link
Copy Markdown

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

Dan and others added 6 commits July 15, 2019 17:23
Adds in code to fetch twitter's config information.
This change lets you upload media to twitter and check on the status of uploads that twitter is processing.

Note that unlike most of the other twitter functions, the upload function wraps three different API calls, the INIT, APPEND, and FINALIZE calls to media/upload.json. I did this instead of exposing them for clients to manage mostly because I couldn't see any case where a client would actually want to manage the individual phases of an upload.

Uploads use the base64 encoded content variant, rather than the raw file content upload variant, mostly because I couldn't get the raw file upload version to work and there was a limit to how much I was willing to fight the code to get things to work. It seems unlikely that the extra bandwidth required for the larger encoding will make any difference in any place this code is used.
@drswork

drswork commented Jul 21, 2019

Copy link
Copy Markdown
Author

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.

@shreve

shreve commented Sep 30, 2019

Copy link
Copy Markdown

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.

@drswork

drswork commented Sep 30, 2019

Copy link
Copy Markdown
Author

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)

@shreve

shreve commented Sep 30, 2019

Copy link
Copy Markdown

@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.

@drswork drswork mentioned this pull request Oct 25, 2019
@0x46616c6b

Copy link
Copy Markdown

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.

@drswork

drswork commented Dec 27, 2019

Copy link
Copy Markdown
Author

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.

@0x46616c6b

0x46616c6b commented Dec 27, 2019

Copy link
Copy Markdown

But what if it merged and we improve this afterwards?

EDIT: Need the approval from @dghubble

@drswork

drswork commented Dec 27, 2019

Copy link
Copy Markdown
Author

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

@Vizualni

Copy link
Copy Markdown

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

@ghost

ghost commented Feb 7, 2020

Copy link
Copy Markdown

Hi,

Hope you are all well !

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

Cheers,
X

@drswork

drswork commented Feb 7, 2020

Copy link
Copy Markdown
Author

@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.

@ghost

ghost commented Feb 7, 2020

Copy link
Copy Markdown

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

@ghost

ghost commented Feb 8, 2020

Copy link
Copy Markdown

@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.

@drswork

drswork commented Feb 9, 2020

Copy link
Copy Markdown
Author

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.

@ghost

ghost commented Feb 10, 2020

Copy link
Copy Markdown

@drswork thanks, it works like a charm

@sivy

sivy commented Feb 24, 2020

Copy link
Copy Markdown

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

sivy added a commit to sivy/goldfrog that referenced this pull request Feb 28, 2020
This required switching my personal fork of
the go-lang twitter client. If this PR gets
merged then I can switch back to the original.

dghubble/go-twitter#148
sivy added a commit to sivy/goldfrog that referenced this pull request Feb 28, 2020
* Implement Twitter media uploading

This required switching my personal fork of
the go-lang twitter client. If this PR gets
merged then I can switch back to the original.

dghubble/go-twitter#148

* Implement mastodon uploads

holy carp bathman it worked

* try and fix up go.mod
@DanB91

DanB91 commented Mar 4, 2020

Copy link
Copy Markdown

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.

@BennyThink

BennyThink commented Oct 22, 2020

Copy link
Copy Markdown

Waiting to see this being merged!

@cmuench

cmuench commented Nov 28, 2020

Copy link
Copy Markdown

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

@janisz

janisz commented Dec 6, 2020

Copy link
Copy Markdown

I tested it and it's working

@janisz janisz mentioned this pull request Dec 6, 2020
@rodrigo-brito

rodrigo-brito commented Feb 14, 2021

Copy link
Copy Markdown

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)
	}

@gregorytucker

Copy link
Copy Markdown

Any status on this getting merged?

@awfulwoman

Copy link
Copy Markdown

@dghubble what do you think about getting this merged?

@dfelici87

Copy link
Copy Markdown

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

@zeing

zeing commented Mar 16, 2022

Copy link
Copy Markdown

I am waiting for

1 similar comment
@zeing

zeing commented Mar 16, 2022

Copy link
Copy Markdown

I am waiting for

@drewmccal

Copy link
Copy Markdown

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

@ghazimuharam

Copy link
Copy Markdown

will be good if it's merged

@NachoNievaG

Copy link
Copy Markdown

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

@drswork

drswork commented Jul 10, 2022

Copy link
Copy Markdown
Author

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.

@zufardhiyaulhaq

Copy link
Copy Markdown

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

@dghubble

dghubble commented Nov 4, 2022

Copy link
Copy Markdown
Owner

Thanks, but the project is being deprecated #207

@dghubble dghubble closed this Nov 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.