Tweepy,来自芝加哥艺术学院的上传媒体错误 API = 无效参数

Tweepy, upload media error API from The art institute of chicago = Invalid argument

尝试使用 Twitter 机器人玩得开心。

想法是:根据芝加哥艺术学院API,发布一条包含信息(艺术家、日期、地点...)和媒体(图片)的推文。

我无法在此处上传媒体,您可以在下面看到我正在尝试修复的回溯。

我会很感激! B

import tweepy
import requests
import random
import time
import io
############################# My logs ######################################
def twitter_api():
    consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    return api

############################# My fonctions #################################

############################# The Loop ######################################

while True:

        get_number()
        r = requests.get(f"https://api.artic.edu/api/v1/artworks/{get_number()}")
        a = r.json()
        get_Titre(),get_Artist(),get_Date(),get_Place(),get_Im()

        requests2 = (f"https://www.artic.edu/iiif/2/{get_Im()}/full/843,/0/default.jpg")

        print("Imge ok:....................", requests2)
        print(type(requests2))
        message = (get_Titre()+ get_Artist()+str(get_Date())+get_Place())
        print("La tête du tweet sera:", message)
        twitter_api().update_status_with_media(message,requests2)
        time.sleep(14400)

这是回溯:

Traceback (most recent call last):
  File "C:\PycharmProjects\TwitterBot\main.py", line 76, in <module>
    twitter_api().update_status_with_media(message,requests2)
  File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 46, in wrapper
    return method(*args, **kwargs)
  File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 1181, in update_status_with_media
    files = {'media[]': stack.enter_context(open(filename, 'rb'))}
OSError: [Errno 22] Invalid argument: 'https://www.artic.edu/iiif/2/904ea189-c852-5f84-c614-a26a851f9b74/full/843,/0/default.jpg'

参见 update_status_with_media 的文档 - 第二个参数必须是文件名。

update_status_with_media(text, filename, file, ...)

但是第三个参数可以是 file-like object 这意味着对象具有函数 .read().

如果你会使用 urllib.request 那么它会给出具有 .read() 的对象并且它有效。

顺便说一句:您必须使用任何文本作为第二个参数 - 可以是假文件名但函数需要它。

import os
import urllib.request
import tweepy

url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"

# --- create file_like_object ---

file_like_object = urllib.request.urlopen(url)

# --- send tweet --- 

consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

twitter_api = tweepy.API(auth)

# use any filename as second argument, and file-like object as third argument
twitter_api.update_status_with_media(text, 'fake_name.jpg', file=file_like_object)

使用 requests 你必须使用 io.BytesIO 才能创建 file-like object

import os
import io
import requests
import tweepy

url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"

# --- create file_like_object ---

response = requests.get(url)
file_like_object = io.BytesIO(response.content)

# --- send tweet --- 

consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

twitter_api = tweepy.API(auth)

# use any filename as second argument, and file-like object as third argument
twitter_api.update_status_with_media(text, 'fake_name.jpg', file=file_like_object)

编辑:

最终你可以使用 stream=True 然后 response.raw 给出 file-like object 但这不是很流行。

# --- create file_like_object ---

response = requests.get(url, stream=True)
file_like_object = response.raw