bear / python-twitter

A Python wrapper around the Twitter API.
Apache License 2.0
3.41k stars 955 forks source link

Media Keys missing in some tweets #693

Closed Azagwen closed 3 years ago

Azagwen commented 3 years ago

I'm trying to access the media keys of some tweets, but I found out while testing that some of them don't include a "media" key in their Json, despite having images or videos posted inside the tweet.

I was unable to test if the tweets missing the key have common attributes, I suspect that quote tweets have a higher tendency to not contain it, but also noticed that this issue isn't exclusive to them, so it's a dead end for me.

I don't know either if it's an issue with the Twitter API or this Module, but it can't hurt to ask.

my code goes as follows :

import json
import twitter

def get_status_info_from_url(url: str):
    split_url = url.split("/")
    status_id = split_url.pop(len(split_url) - 1)
    user_id = split_url.pop(len(split_url) - 2)
    return f"@{user_id}", status_id

current_status = get_status_info_from_url(url)

try:
    tweet_json = json.loads(api.GetStatus(current_status[1]).AsJsonString())
except twitter.TwitterError:
    print(f"Error encountered with: {url}")
    return
if "media" not in tweet_json:
    print(f"No media found in: {url}")
    return
else:
    tweet_media = tweet_json["media"]
    print(tweet_media)
Azagwen commented 3 years ago

after a bit more failed Media pickups:

Azagwen commented 3 years ago

Ended up fixing it by adding

api.tweet_mode = "extended"

after my api variable declaration Hope this helps others, because that's a weird issue with an easy fix 👍