Formidable Fox
Posts: 14
Joined: Wed Apr 01, 2015 12:17 am
Location: California

Using Twython to upload video to Twitter

Sun Feb 21, 2016 6:29 am

Hello, I'm new to coding and slowly learning Python. Currently I'm working on a project that incorporates uploading a video at a certain time once a day to Twitter. So far this is what I have:

Code: Select all

from twython import Twython
from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

def main():
    video = open('/home/pi/Downloads/sample_mpeg4.mp4', 'rb')
    response = twitter.upload_video(media=video, media_type='video/mp4')
    twitter.update_status(status='Testing upload', media_ids=[response['media_id']])

if __name__ == '__main__':
    main()
When I run the program though I run into this problem:
Traceback (most recent call last):
File "/home/pi/tweeting-timelapse/tweet.py", line 22, in <module>
main()
File "/home/pi/tweeting-timelapse/tweet.py", line 18, in main
response = twitter.upload_video(media=video, media_type='video/mp4')
AttributeError: 'Twython' object has no attribute 'upload_video'
Can anyone help me with this? I'm using documentation from Twython at https://github.com/ryanmcgrath/twython/tree/master/docs and using aspects of another project at https://www.raspberrypi.org/learning/tweeting-babbage/

DirkS
Posts: 9057
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Using Twython to upload video to Twitter

Sun Feb 21, 2016 9:08 am

Which version of twython do you use?
I think you can print the version in your script with

Code: Select all

print(twitter.__version__)

Formidable Fox
Posts: 14
Joined: Wed Apr 01, 2015 12:17 am
Location: California

Re: Using Twython to upload video to Twitter

Sun Feb 21, 2016 3:44 pm

I just tried that and I got this response:
Traceback (most recent call last):
File "/home/pi/tweeting-timelapse/print.py", line 20, in <module>
main()
File "/home/pi/tweeting-timelapse/print.py", line 17, in main
print(twitter.__version__)
AttributeError: 'Twython' object has no attribute '__version__'

smithy1155
Posts: 9
Joined: Wed Mar 23, 2016 12:42 pm

Re: Using Twython to upload video to Twitter

Thu Nov 17, 2016 1:04 pm

I've just came across the same problem! Did anyone have a solution for this?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Using Twython to upload video to Twitter

Thu Nov 17, 2016 6:53 pm

Code: Select all

response = twitter.upload_media(media=video, media_type='video/mp4')
Try that.

Dave.
Apple say... Monkey do !!

smithy1155
Posts: 9
Joined: Wed Mar 23, 2016 12:42 pm

Re: Using Twython to upload video to Twitter

Sat Nov 19, 2016 2:33 pm

Thank you for your reply I just keep getting error messages on using both .upload_media & .upload_video

Says on each one, object has no attribute, but thank you.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Using Twython to upload video to Twitter

Sat Nov 19, 2016 4:56 pm

This has just worked for me with a photo so the upload_media is okay, not had any luck with video yet but working on it...

Code: Select all

photo = open("C:\Users\davef_000\Desktop\DSC_0028.JPG", 'rb')
print "Opened file..."
response = myTwit.upload_media(media=photo)
print "Uploaded photo..."
myTwit.update_status(status='Ignore this, testing auto upload...', media_ids=[response['media_id']])
print "All done..."
Apple say... Monkey do !!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Using Twython to upload video to Twitter

Sat Nov 19, 2016 5:03 pm

When I try this...

Code: Select all

video = open("C:\Users\davef_000\Desktop\SampleVideo_720x480_1mb.mp4", 'rb')
print "Opened file..."
response = myTwit.upload_media(media=video, media_type='video/mp4')
print "Uploaded video..."
myTwit.update_status(status='Ignore this, testing auto upload...', media_ids=[response['media_id']])
print "All done..."
I get this...
Opened file...
Traceback (most recent call last):
File "C:/Users/davef_000/PycharmProjects/Twitter/Twitter.py", line 27, in <module>
response = myTwit.upload_media(media=video, media_type='video/mp4')
File "build\bdist.win-amd64\egg\twython\endpoints.py", line 145, in upload_media
File "build\bdist.win-amd64\egg\twython\api.py", line 268, in post
File "build\bdist.win-amd64\egg\twython\api.py", line 258, in request
File "build\bdist.win-amd64\egg\twython\api.py", line 194, in _request
twython.exceptions.TwythonError: Twitter API returned a 400 (Bad Request), An error occurred processing your request.
Now I'm thinking Twython's just a bit rubbish !!

Dave.
Apple say... Monkey do !!

smithy1155
Posts: 9
Joined: Wed Mar 23, 2016 12:42 pm

Re: Using Twython to upload video to Twitter

Mon Nov 21, 2016 2:46 pm

your not the only one! i've got the photo working without a problem but no further with the video! might just give it a miss!

thank you for your help!

zcapozzi
Posts: 24
Joined: Fri Jul 04, 2014 5:42 pm

Re: Using Twython to upload video to Twitter

Tue Jul 11, 2017 1:13 am

Just in case this was the problem, Twython video uploads must be less than 30 seconds. Again, that may not be the issue here, but it would cause the error message you received.

Girtch27
Posts: 5
Joined: Sat Dec 16, 2017 4:33 pm

Re: Using Twython to upload video to Twitter

Mon Sep 24, 2018 8:04 pm

I know this is old but did anyone work this out recently?
I want to capture a video, which is h264, convert it to mp4 then tweet the video using python. I can tweet pictures but not videos so i'm close. See below portion of python3.5.3 and the error.

I can see the video was converted to mp4 and I can manually upload the mp4 video file when I tweet it from a webpage. So the video file is accepted by twiter, I can also play it so the converted video file seems good to me.

Portion of my python program for the video:

camera.start_recording('/home/pi/Desktop/Birdhouse/video.h264')
sleep(2)
camera.stop_recording()
sleep(0.5)
#add code here to convert h264 format to mp4 as twitter only accepts mp4???
subprocess.call(["/usr/bin/avconv","-i","/home/pi/Desktop/Birdhouse/video.h264","-c:v","libx264","/home/pi/Desktop/Birdhouse/video.mp4"])

video = open('/home/pi/Desktop/Birdhouse/video.mp4', 'rb')

#load mp4 video to twitter and get the media ID
response = myTweet.upload_video(media=video, media_type='video/mp4') #******<------- this line not working********

#tweet random message to trick twitter API
r = randint(0, len(messages)-1)
message = messages[r]
myTweet.update_status(status=message, media_ids=[response['media_id']])

-------------------------
The upload doesn't seem to work, it doesn't get a valid media_id, i get the following:

Traceback (most recent call last):
File "/home/pi/birdhouse camera.py", line 88, in <module>
response = myTweet.upload_video(media=video, media_type='video/mp4')
File "/usr/lib/python3/dist-packages/twython/endpoints.py", line 184, in upload_video
media_chunk.write(data)
TypeError: string argument expected, got 'bytes'

I've tried following the twython documents but just doesn't seem to work, wondering if the version i'm using is wrong or old. Or did someone ever get this to work?

Girtch27
Posts: 5
Joined: Sat Dec 16, 2017 4:33 pm

Re: Using Twython to upload video to Twitter

Tue Sep 25, 2018 4:09 am

According to GitHub its a know issue and seems to be fixed awhile ago. I used nano and see my endpoints.py is not updated to BytesIO(), its still String command, so I used nano and updated it. That part now works but now stops for another error in Twython. So i'd like to figure out how to update all Twython files to the latest to capture all updates\fixes and be able to follow Twython's documents, but not sure how.
Will "sudo apt-get update" then "sudo apt-get dist-upgrade" update it?

Here is the issue:
https://github.com/ryanmcgrath/twython/ ... esIO%28%29

Quick Update:
I tried removing Twython "sudo apt-get remove python-twython" then re-install it in hopes to get the latest .py files but it didn't as it detected twython was still there. So I tried using a purge command... didn't help either. I ended up manually deleting some twython dirs "rd -r twython" then re-installed worked and copied the files but it put it twython in a python 3.5 dir, it was python 3 and 2.7.

Return to “Python”

Who is online

Users browsing this forum: Idahowalker and 13 guests