Получите медиа из твиттера прямых сообщений используя tweepy - PullRequest
0 голосов
/ 10 января 2020

Как получить медиа от DM в Твиттере, используя Tweepy?

import tweepy
import re, requests, os
import config

auth = tweepy.OAuthHandler(config.consumer_key, config.consumer_secret)
auth.set_access_token(config.access_token, config.access_token_secret)
api = tweepy.API(auth)
user = api.me()

def test_media():
    dm = api.list_direct_messages(5, )
    direct = dm[0]
    target = direct.message_create
    # print(target['message_data'])
    teks  = target['message_data']['text']
    gambar = target['message_data']['attachment']['media']['media_url']
    print(gambar)
    pattern = re.compile("[#UMSmf]")
    filename = 'temp.jpg'
    if pattern.search(teks):
        try :
            if gambar != '':
                response = requests.get(url='%s' %gambar,  headers={'Authorization':'{}'.format(auth.oauth)}, allow_redirects=False)
                print(response)
                if response.status_code == 200:
                    with open(filename, 'wb') as image :
                        for resp in response:
                            image.write(resp)
                            print('Gambar berhasil di download')
                    api.update_with_media(filename=filename, status=teks)
                    print('update dengan media berhasil di twit')
        except tweepy.TweepError as e:
            print(e.reason)

Я всегда получал ответ слишком много перенаправлений, я пытаюсь понять документацию, но не могу. Может кто-нибудь помочь мне решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...