Zachster1996
Posts: 58
Joined: Wed Aug 14, 2013 7:16 am

Tweeting Pictures

Tue Jan 28, 2014 12:55 am

I was wondering how you would tweet pictures on the Pi through Python. I can't use Twython for a reason that is too long to go into.

Thanks.

User avatar
DougieLawson
Posts: 34907
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tweeting Pictures

Wed Jan 29, 2014 1:17 am

You can do it with Tweepy http://github.com/tweepy

Code: Select all

#!/usr/bin/python
import os
import sys
import tweepy

Ckey = 'Consumer_Key_here'
Csec = 'Consumer_Secret_here'
Akey = 'Application_Key_here'
Asec = 'Application_Secret_here'

auth = tweepy.OAuthHandler(Ckey, Csec)
auth.set_access_token(Akey, Asec)
twapi = tweepy.API(auth)

fn = os.path.abspath(sys.argv[1])
status = sys.argv[2]

twapi.update_with_media(fn, status=status)
Parm#1 is the image filename
Parm#2 is the text for the status.

https://twitter.com/IMSlights/status/428334873859067905
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2841
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Tweeting Pictures

Thu Jan 30, 2014 11:08 pm

Just had a play with Dougie's code. Thanks for posting that :)

Two things to note.

1/ I had tweepy 2.1 installed and it doesn't support update_with_media()

Couldn't get the booger working until I updated tweepy (which I only installed in September 2013) to 2.2. Then it worked fine. :)

2/ Your text for your status update should be in quotes when you enter it on the command line. Otherwise it stops at the first space.

Other than that, excellent tip there Dougie. The documentation for tweepy is pretty out of date, which makes life difficult for beginners (and a lot of other people too).
Alex Eames RasPi.TV, RasP.iO

User avatar
DougieLawson
Posts: 34907
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Tweeting Pictures

Thu Jan 30, 2014 11:37 pm

alexeames wrote: Couldn't get the booger working until I updated tweepy (which I only installed in September 2013) to 2.2. Then it worked fine.
I took ages finding a working version. V2.2 was the third one I tried. There was a old fork of tweepy code that didn't work with https.

Most of my machine generated tweets come from a Perl program use Net::Twitter. I'm not a big fan of python. I prefer the write once, debug never feature of Perl to the semantic white space feature of python.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2841
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Tweeting Pictures

Fri Jan 31, 2014 8:15 am

DougieLawson wrote:Most of my machine generated tweets come from a Perl program use Net::Twitter. I'm not a big fan of python. I prefer the write once, debug never feature of Perl to the semantic white space feature of python.
I used to do back-end stuff for websites in PERL. I've got a shopping cart and banner ad server I wrote 10 years ago still working.

/*I learnt REXX during my pre-Uni year at IBM*/
/*The Systems Product Interpreter*/

I do like Python though - very much. Although I was happy with PERL's {}
Alex Eames RasPi.TV, RasP.iO

Return to “Python”