3

I notice that if I Tweet normally (from the browser) with a message followed by a YouTube video link, Twitter displays the video's thumbnail, as follows:

However, if I use the following code to send the Tweet instead:

import tweepy
import json
youtube_url = r'https://www.youtube.com/watch?v=tj-fmOnbBpU&t=0s'
# account tokens
twitter_keys = json.load(open('twitter_keys.json'))
auth = tweepy.OAuthHandler(twitter_keys["consumer_key"], twitter_keys["consumer_secret"]) # authentication of consumer key and secret
auth.set_access_token(twitter_keys["access_token"], twitter_keys["access_token_secret"]) # authentication of access token and secret
api = tweepy.API(auth)
twitter_text = "My message " + youtube_url 
api.update_status(status ="{}".format(twitter_text)) # send a tweet

I get something like this:

As you can see, Twitter doesn't show the preview for the URL, even after a few days. I don't understand why this is. How can I fix my code so that the Tweet sent through the API shows the preview of the YouTube video link?

joejoejoejoe4
  • 1,206
  • 1
  • 18
  • 38
  • 2
    I can't reproduce this. Even with your example code, Twitter properly turns the YouTube link into a card. Your screenshot shows a different message than your example code. Is this all of your code? – Harmon758 Feb 18 '20 at 08:49
  • What you said right there helped me realize the issue. The key difference was the way the URL was structured. It worked in my example code because there was only one forward slash in the youtube.com/watch part of the URL. In my other tweets where it didn't work, I had two slashes (you can see this in the picture). – joejoejoejoe4 Feb 18 '20 at 11:54

3 Answers3

1

As pointed out, the issue was that the URL was using two forward slashes for the path, as can be seen in the screenshot.

Harmon758
  • 5,084
  • 3
  • 22
  • 39
0
  1. See this Stack Overflow post on how the youtube thumbnail URL is structured.

  2. Use this post to see how you can programmatically download the thumbnail locally with the requests library.

  3. Then you can use Tweepy's update_with_media() to upload it as part of the tweet.

The end result will look something like this:

import tweepy
import json
import requests

# video info
youtube_id = "tj-fmOnbBpU"
youtube_url = f"https://www.youtube.com/watch?v={youtube_id}&t=0s"

# account tokens
twitter_keys = json.load(open('twitter_keys.json'))
auth = tweepy.OAuthHandler(twitter_keys["consumer_key"], twitter_keys["consumer_secret"]) # authentication of consumer key and secret
auth.set_access_token(twitter_keys["access_token"], twitter_keys["access_token_secret"]) # authentication of access token and secret
api = tweepy.API(auth)
twitter_text = f"My message: {youtube_url}"


# downloading thumbnail
url = f"https://img.youtube.com/vi/{youtube_id}/1.jpg"

filename = url.split("/")[-1]
r = requests.get(url, timeout=0.5)

if r.status_code == 200:
    with open(filename, 'wb') as f:
        f.write(r.content)

# Send tweet
api.update_with_media(filename, status=twitter_text)
Gleland
  • 270
  • 6
  • 14
  • Unfortunately, this doesn't quite achieve the desired result. Instead I get a tweet looking like this: https://i.imgur.com/Jx3kmm1.png. This simply embeds a (low resolution) picture of the thumbnail directly into the tweet, which is not the goal. I'm wanting to have the YouTube video player itself embedded in the tweet. This happens automatically when I post a tweet normally, just not when using the API. – joejoejoejoe4 Feb 17 '20 at 06:51
  • Please never use update_with_media, that was deprecated years ago. – Andy Piper Feb 18 '20 at 13:09
0

I know this was asked almost a year ago but I recently was having trouble with this and found a solution which worked for me.

http://youtube.com/watch?v=[YOUR VIDEO ID]&feature=emb_title

For example, this YouTube video:

https://www.youtube.com/watch?v=KUh2O8HylUM

Would need to be formatted as:

http://youtube.com/watch?v=KUh2O8HylUM&feature=emb_title

Luke Halley
  • 305
  • 1
  • 2
  • 8