O'Reilly Forums: Sending Messages To Twitter - O'Reilly Forums

Jump to content

  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Sending Messages To Twitter

#1 User is offline   David Griffiths_1283427796 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 02-September 10

Posted 02 September 2010 - 03:55 AM

Good morning learners! :-)

I noticed this morning that Twitter have finally disabled a feature called "Basic Authentication" which allows you to easily post messages to Twitter.

Sound familiar? It should. We use it for posting Twitter messages in the code on Chapter 3.

But in the same way that eight-track tapes got replaced with these spiffy new "audio cassettes", Basic Authentication is now a thing of the past.

Bummer.

However, don't worry. We *have* found a workaround. Twitter is keen to keep your Tweet account as secure as possible and instead of using Basic Authentication, they are now using a newer system called "OAuth". OAuth is very cool, and very secure unfortunately it's... kind of tricky.

In fact it's been described as about as straightforward as performing brain surgery on a roller coaster.

Fortunately, there's a really useful guide on

http://jmillerinc.com/2010/05/31/twitter-f...on-using-oauth/

that tells you how to enable OAuth for your application. There's quite a few steps and we will get a similar set of instructions out in the next few days, in a more Head First style, but it's a great place to start.

By the end you should be able to replace the code from the book with something like this:

def send_to_twitter(msg):
import sys
import tweepy
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
ACCESS_KEY = '...'
ACCESS_SECRET = '...'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(msg)



The CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY and ACCESS_SECRET values need to need to some magic mojo that the above will tell you how to create.

If you drop the above code into your program in place of the send_to_twitter(msg) function in the book, you should find yourself back and Tweeting in no time.

DG
0

#2 User is offline   freight 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 30-August 10

Posted 07 September 2010 - 12:26 PM

QUOTE (David Griffiths_1283427796 @ Sep 2 2010, 03:55 AM) <{POST_SNAPBACK}>
Good morning learners! :-)

I noticed this morning that Twitter have finally disabled a feature called "Basic Authentication" which allows you to easily post messages to Twitter.

Sound familiar? It should. We use it for posting Twitter messages in the code on Chapter 3.

But in the same way that eight-track tapes got replaced with these spiffy new "audio cassettes", Basic Authentication is now a thing of the past.

Bummer.

However, don't worry. We *have* found a workaround. Twitter is keen to keep your Tweet account as secure as possible and instead of using Basic Authentication, they are now using a newer system called "OAuth". OAuth is very cool, and very secure unfortunately it's... kind of tricky.

In fact it's been described as about as straightforward as performing brain surgery on a roller coaster.

Fortunately, there's a really useful guide on

http://jmillerinc.com/2010/05/31/twitter-f...on-using-oauth/

that tells you how to enable OAuth for your application. There's quite a few steps and we will get a similar set of instructions out in the next few days, in a more Head First style, but it's a great place to start.

By the end you should be able to replace the code from the book with something like this:

def send_to_twitter(msg):
import sys
import tweepy
CONSUMER_KEY = '...'
CONSUMER_SECRET = '...'
ACCESS_KEY = '...'
ACCESS_SECRET = '...'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(msg)



The CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY and ACCESS_SECRET values need to need to some magic mojo that the above will tell you how to create.

If you drop the above code into your program in place of the send_to_twitter(msg) function in the book, you should find yourself back and Tweeting in no time.

DG


Anyone gotten this script up and running?
The info on Tweepy leads me to believe that it only functions under Python 2 and not 3000.

I've been trying to install this under macports to no success.

Thanks for any follow-up.
0

#3 User is offline   freight 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 30-August 10

Posted 13 September 2010 - 02:56 PM

Has anyone gotten this new technique to work via the command line. Especially to Apple users like myself?

Thanks.
0

#4 User is offline   anxiousmodernman 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 11-September 10

Posted 14 September 2010 - 06:15 PM

QUOTE (freight @ Sep 13 2010, 02:56 PM) <{POST_SNAPBACK}>
Has anyone gotten this new technique to work via the command line. Especially to Apple users like myself?

Thanks.


I'm a new learner myself, but my basic understanding is that Python users will need to download and install a third party library of code to use the new OAuth system. It's called Tweepy. Tweepy may be only one of several third party libraries that can work with the new Twitter authentication system.

Python 3.x comes with lots of libraries pre-installed, like the urllib that we use in Ch.3

JMiller Inc Blog has a link to the code-sharing website Git with his third party library, Tweepy.

Unfortunately, I don't know how to use either Git or to install third party libraries.

Ideally, O'Reilly should post both the library (with attribution to some third party) and the explanation for it's installation and modification of the code on the front page of the Head First Programming site.

Digging through web forums may be an essential skill for programmers, though. smile.gif

For the record, I've really been enjoying Head First Programming, and I applaud the ambition of the authors for rolling the dice on having learners do something so cool so early on in the book. But we've got a major problem here.

Bottom line:
1) we need instructions on how to download a library that'll do what we need with OAuth
2) we need instructions on its installation on all platforms
3) we need these things to be flagged on the main page of Head First Programming

This post has been edited by anxiousmodernman: 14 September 2010 - 06:19 PM

0

#5 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 251
  • Joined: 20-August 09

Posted 16 September 2010 - 12:21 AM

QUOTE (anxiousmodernman @ Sep 14 2010, 06:15 PM) <{POST_SNAPBACK}>
Bottom line:
1) we need instructions on how to download a library that'll do what we need with OAuth
2) we need instructions on its installation on all platforms
3) we need these things to be flagged on the main page of Head First Programming


Thanks for these comments and suggestions.

We are aware of the issues here. It's just a fact of life that no author(s) can predict what a 3rd party will do with their system *after* a book publishes. Unfortunately, both David and I provide "support" to this forum on a voluntary basis... we are not contracted with O'Reilly to do this. As such, if we can fit it in, we can devote our spare time to solving issues brought up here by readers of Head First Programming. What's making things difficult for us at this time is that both David and I are neck-deep in other projects at the moment, which makes it hard to find the time to help out here. For instance, I'm so swamped that I haven't event tried out David's solution (above), even though I'd like to try it to see what the issues are... :-(

We are as upset as everyone at Twitter's decision to change what was a reasonably straightforward process into something that is a bit of a nightmare. Looking at this is on my list of things to do just as soon as my current project ends (toward the end of November).

Please bear with us until then.

Regards.

Paul.

0

#6 User is offline   colinsegovis 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 16-September 10

Posted 16 September 2010 - 08:20 PM

The JMillerInc Blog instructions are superb and if followed will allow you to access twitter with OAuth.

My problem is that I am using a Mac and have installed Python 3.1, but the default OSX install is Python 2.x. I can get the tweepy library running with Python 2.x but cannot get tweepy to install on my python 3.1 install. How do I point python 3.1 to the tweepy install that was installed for python 2.x.

I also tried to install tweepy on python 3.1 with setuptools but that library is also not installed on python 3.1.

Help!

C
0

#7 User is offline   dogriffiths 

  • Active Member
  • PipPip
  • Group: O'Reilly Author
  • Posts: 15
  • Joined: 01-July 07
  • Interests:Ruby, Rails, Ruby on Rails, Les Dawson

Posted 17 September 2010 - 05:56 AM

I'm away from a laptop right (I am doing this on a phone) but I will be able to check on my Mac to what the issues might be. The instructions are very well done on the site I linked to but I'll see what I can do on a clean environment and include all the stepsfor the Tweepy install.

We'll see what we can do to flatten out this particular bump in the road :-)
0

#8 User is offline   Dananjaya86 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 16-September 10
  • Gender:Male
  • Location:Sri Lanka

Posted 19 September 2010 - 05:31 AM

Use this program with Python 2.7. You can post Bean prices to Twitter from this. If i'm correct Tweepy does not support Python 3 yet.

CODE
#!/usr/bin/env python

import sys
import tweepy
import time
import urllib2

def send_to_twitter(msg):
    CONSUMER_KEY = '....'
    CONSUMER_SECRET = '...'
    ACCESS_KEY = '...'
    ACCESS_SECRET = '...'

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    api.update_status(msg)
    
def get_price():
    page = urllib2.urlopen("http://beans.itcarlow.ie/prices.html")
    text = page.read().decode("utf8")
    where = text.find('>$')
    start_of_price = where + 2
    end_of_price = start_of_price + 4
    return float(text[start_of_price:end_of_price])
    
price_now = raw_input("Do you want to see the price now (Y/N)? ")

if price_now == "Y":
    send_to_twitter(get_price())
else:
    price = 99.99
    while price > 4.74:
        time.sleep(900)
        price = get_price()
    send_to_twitter("Buy!")


This script works well for me. You'll have to install tweepy and register your app in Twitter. You can see instructions to do that here. You will receive CONSUMER_KEY, ACCESS_KEY, etc. from twitter when you register your app. replace the relevant fields in the script when you get those keys.

The URL for fetching prices of the beans can be the one specified in the program or in case it isn't available use this: http://beans-r-us.appspot.com/prices.html

Please feel free to correct errors or point out things to look out for in this program.

This post has been edited by Dananjaya86: 19 September 2010 - 05:42 AM

0

#9 User is offline   Dananjaya86 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 16-September 10
  • Gender:Male
  • Location:Sri Lanka

Posted 19 September 2010 - 08:39 AM

QUOTE (dogriffiths @ Sep 17 2010, 05:56 AM) <{POST_SNAPBACK}>
I'm away from a laptop right (I am doing this on a phone) but I will be able to check on my Mac to what the issues might be. The instructions are very well done on the site I linked to but I'll see what I can do on a clean environment and include all the stepsfor the Tweepy install.

We'll see what we can do to flatten out this particular bump in the road :-)



Steps to install Tweepy (Tested in Windows 7):

1. Go here and download the latest Tweepy source.

2. Then Extract archive
> cd tweepy-1.2
> python setup.py install (run as admin/root)

3. At this point, I recieved an error stating module called 'setuptools' are missing.

4. If you get that error go here and download the latest .exe file of setuptools. If you don't recieve such errors you are good to go using Tweepy.

5. Double-click the setuptools.exe file you downloaded and install it. (Keep in mind that setuptools currently supports Python 2.X branch only.)

6. At this point you should have installed required libraries.

7. Now refer to this post and create a Python twitter client that allows you to tweet from command line.

8. I've also submitted a script that gets the bean prices and post it in twitter as explained in the book. Please refer to that as well..
0

#10 User is offline   gregastarr 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 17-September 10

Posted 21 September 2010 - 06:08 AM

@Dananjaya86 - nice posts...quite helpful!
0

#11 User is offline   Raffomania 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 24-September 10

Posted 24 September 2010 - 11:30 AM

Well, this sucks. I followed the instructions, got everything, but now i have to find out that i have to do everything in python 2.xx -.-
I think I am going to skip that chapter. at least i got until the point where i had to use twitter. luckily there's not much left to learn.
0

#12 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 24 September 2010 - 12:11 PM

i'm glad i checked the forums, i just got to this chapter and could not get it to work. now i'm going to skip this part and keep reading. thanks!
0

#13 User is offline   arcticrobot 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 24-September 10
  • Location:Detroit, MI

Posted 24 September 2010 - 12:50 PM

Fast and easy way to install Tweepy for python3 in Ubuntu 10.04:
1. Download tweepy 1.3 for python3
2. Create folder tweepy somewhere in your home directory and unzip tweepy there with
tar xzvf tweepy-1.4-py3.tar.gz command.
2. Download distribute_setup.py Distribute is the setuptools fork for python3.
3. Run distribute installation with sudo python3 distribute_setup.py command.
4. Go to folder, where you extracted tweepy archive and run sudo python3 setup.py install - this will install tweepy library for your computer.
5. Use instructions for authenticating your programm with twitter as explained here
6. Use code provided by Dave Griffith in the first comment to define function.
7. Understand, that learning process sometimes throws interesting challenges at you and don't give up
8. You can find me on twitter by the same name as here and I will be glad to answer your questions. Note, however, that I am as big newbie at python as you are.

This post has been edited by arcticrobot: 24 September 2010 - 12:52 PM

0

#14 User is offline   fmpc1878 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 25-September 10

Posted 25 September 2010 - 01:56 PM

Thanks arcticrobot, I was having a bit of trouble with it but I've got it installed now!
0

#15 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 27 September 2010 - 06:42 AM

is this available for linux only? nothing for windows?
0

#16 User is offline   Raffomania 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 24-September 10

Posted 28 September 2010 - 08:46 AM

thanks so much arcticrobot, works like a charm.
0

#17 User is offline   arcticrobot 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 24-September 10
  • Location:Detroit, MI

Posted 28 September 2010 - 12:15 PM

QUOTE (drewdin @ Sep 27 2010, 06:42 AM) <{POST_SNAPBACK}>
is this available for linux only? nothing for windows?

I will check what is available for windows at the end of this/beginning of the next week. Stay in touch.
0

#18 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 28 September 2010 - 02:40 PM

thanks!
0

#19 User is offline   Dananjaya86 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 16-September 10
  • Gender:Male
  • Location:Sri Lanka

Posted 29 September 2010 - 06:50 AM

QUOTE (drewdin @ Sep 27 2010, 06:42 AM) <{POST_SNAPBACK}>
is this available for linux only? nothing for windows?


Well I successfully posted bean prices to Twitter in Windows...If you're looking for setuptools for Windows...you can fetch an .exe from here...although it works with Python 2.X only...

This post has been edited by Dananjaya86: 29 September 2010 - 06:51 AM

0

#20 User is offline   arcticrobot 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 24-September 10
  • Location:Detroit, MI

Posted 07 October 2010 - 08:06 AM

I just compiled instruction on using tweepy with python 3 on windows machine:

1. I assume you have already installed python 3.1 on your windows machine and its located in C:\Python31. If not: please download and install it.

2. As with Linux example above download Tweepy for Python 3

3. To extract tar.gz file on windows machine download 7zip from 7-zip.org and install it.

4. Download distribute_setup.py and double click on it to install.

5. Create C:\tweepy directory

6. Open 7-zip GUI and navigate to the location of downloaded tweepy-1.4-py3.tar.gz . Click on it to open, then click on the .tar file inside it to get access to content. Extract contents of the .tar file to C:\tweepy

7. To properly set python variable on windows machine go to: My Computer(right click) -> Properties -> Advanced -> Environment Variables -> System Variables.
If you have PATH variable there, click Edit and add following to the line: ;C:\Python31 (note the semicolon in front of C)
If you don't have PATH variable there, click New, Variable Name: PATH, Variable Value: C:\Python31(no semicolon this time) OK your way out of variables.

8. Open your command prompt: Start -> Run... -> type "cmd" (do not include parentheses, they are only to refer to command"

9. Navigate to C:\tweepy where we previously extracted tweepy content by typing "cd C:\tweepy"

10. Install tweepy for Python 3 on windows by issuing "python setup.py install"

11. Now you can use tweepy with Python 3 on your Windows machine. I just tested it and it works.


0

Share this topic:


  • 3 Pages +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users