O'Reilly Forums: Had Problems With Send_to_twitter - O'Reilly Forums

Jump to content

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

Had Problems With Send_to_twitter would not send to twitter

#1 User is offline   classicgamer88 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 13-February 10

Posted 13 February 2010 - 06:53 PM

well i am new to programming and just bought the head first programming book, struggled for almost a week with the send_to_twitter program and do not know why it won't send to twitter, curious to know if anyone else had the same problem. and if you did what did you do
thanks
0

#2 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 14 February 2010 - 03:29 AM

QUOTE (classicgamer88 @ Feb 13 2010, 06:53 PM) <{POST_SNAPBACK}>
well i am new to programming and just bought the head first programming book, struggled for almost a week with the send_to_twitter program and do not know why it won't send to twitter, curious to know if anyone else had the same problem. and if you did what did you do
thanks


Sorry to hear you are having problems with this. Can you be a little more detailed about what the problem is (are you seeing any error messages)? In the meantime, a few things to check would be:

1. Are you trying to send more than 140 characters to Twitter - if you are, Twitter will silently reject the message (i.e., without telling you).

2. Are you using a valid id and password? If you can login to Twitter via their web interface, then you are - be sure to check that your code uses the same values.

3. Are trying to connect to Twitter via a proxy server? If you are, then you may be outta luck, as the code does not take this type of set-up into consideration. Check the "network settings" in your browser or operating system to see if a proxy is being used. If it is, then that's the problem. :-(

4. Be sure to use the code from the website - it works and this has been verified by a bunch of other readers (as well as the book's technical authors):

http://www.headfirstlabs.com/books/hfprog/

5. If you are new to programming, you might find the "Julie Learns Python" blog of interest:

http://julielearnspython.wordpress.com/

6. The code in the book waits for 15 minutes between checks to the server. This can mean that you wait a long time for the price of coffee beans to drop to an acceptable level. To speed things up a little, change the code to not wait so long. (I think the value in the code is 900 seconds ... change that to 60 to check the server every minute - don't set it to anything lower as you might overload the server and the last thing you want is the nice folk at Webland Security [see pg 66] paying a visit!).

I hope these suggestions help.

Paul.
0

#3 User is offline   classicgamer88 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 13-February 10

Posted 14 February 2010 - 08:11 AM

Thanks Paul I appreciate the help

here is the error that comes up when i put in my username

Do you want to see the price now (Y/N) ? y
Enter username for Twitter API at twitter.com:
classicgamer88

Warning (from warnings module):
File "C:\Python31\lib\getpass.py", line 88
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter password for
classicgamer88 in Twitter API at twitter.com:
0

#4 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 14 February 2010 - 08:25 AM

QUOTE (classicgamer88 @ Feb 14 2010, 08:11 AM) <{POST_SNAPBACK}>
Do you want to see the price now (Y/N) ? y
Enter username for Twitter API at twitter.com:
classicgamer88

Warning (from warnings module):
File "C:\Python31\lib\getpass.py", line 88
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter password for
classicgamer88 in Twitter API at twitter.com:


It looks like you are using a custom version of the code from the book. If you post the code for your program, it can then be looked at by me (and others) to see if we can work out what the problem is. Note, too, that you are getting a warning here, not an error.

Paul.
0

#5 User is offline   classicgamer88 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 13-February 10

Posted 17 February 2010 - 06:10 PM

thanks paul well here is the code


import urllib.request
import time

def send_to_twitter(msg):
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http: //twitter.com/statuses" , "classicgamer88 " , "python ")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request. build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( { 'status' : msg} )
resp = urllib.request.urlretrieve("http://twitter.com/statuses/update.json", params)
resp.read()

def get_price():
page = urllib.request.urlopen("http://www.beans-r-us.biz/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 = 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(60)
price = get_price()
send_to_twitter("buy")


0

#6 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 18 February 2010 - 12:08 PM

QUOTE (classicgamer88 @ Feb 17 2010, 06:10 PM) <{POST_SNAPBACK}>
CODE

password_manager.add_password("Twitter API",
"http: //twitter.com/statuses" , "classicgamer88 " , "python ")


(Without actually running this) I think the problem may be with that SPACE character after the "http:" bit on the second line of code above. With the space there, the URL is invalid and will cause a problem. Try running this without the space or download a working version of the function from the book's official website:

http://headfirstlabs.com/books/hfprog/chap...d_to_twitter.py

Hope this helps! BTW: I hope that's not your real password...

Paul.
0

#7 User is offline   classicgamer88 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 13-February 10

Posted 20 February 2010 - 07:15 AM

okay well here is the code with the space between the http gone, exactly like in the example

import urllib.request
import time

def send_to_twitter(msg):
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses" , "classicgamer88 " , "python ")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request. build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( { 'status' : msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

def get_price():
page = urllib.request.urlopen("http://www.beans-r-us.biz/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 = 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(60)
price = get_price()
send_to_twitter("buy")


but now it says it has a problem with the get price function


Do you want to see the price now (Y/N) ? Y
Traceback (most recent call last):
File "C:\Python31\gg.py", line 26, in <module>
send_to_twitter(get_price())
File "C:\Python31\gg.py", line 12, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
File "C:\Python31\lib\urllib\request.py", line 119, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python31\lib\urllib\request.py", line 353, in open
response = meth(req, response)
File "C:\Python31\lib\urllib\request.py", line 465, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python31\lib\urllib\request.py", line 385, in error
result = self._call_chain(*args)
File "C:\Python31\lib\urllib\request.py", line 325, in _call_chain
result = func(*args)
File "C:\Python31\lib\urllib\request.py", line 805, in http_error_401
url, req, headers)
File "C:\Python31\lib\urllib\request.py", line 783, in http_error_auth_reqed
return self.retry_http_basic_auth(host, req, realm)
File "C:\Python31\lib\urllib\request.py", line 793, in retry_http_basic_auth
return self.parent.open(req, timeout=req.timeout)
File "C:\Python31\lib\urllib\request.py", line 353, in open
response = meth(req, response)
File "C:\Python31\lib\urllib\request.py", line 465, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python31\lib\urllib\request.py", line 391, in error
return self._call_chain(*args)
File "C:\Python31\lib\urllib\request.py", line 325, in _call_chain
result = func(*args)
File "C:\Python31\lib\urllib\request.py", line 473, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
>>>


thanks again Paul, for taking some time to help me
0

#8 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 20 February 2010 - 08:01 AM

QUOTE (classicgamer88 @ Feb 20 2010, 07:15 AM) <{POST_SNAPBACK}>
...
password_manager.add_password("Twitter API",
"http://twitter.com/statuses" , "classicgamer88 " , "python ")

Do you want to see the price now (Y/N) ? Y
Traceback (most recent call last):
File "C:\Python31\gg.py", line 26, in <module>
send_to_twitter(get_price())
....
urllib.error.HTTPError: HTTP Error 401: Unauthorized


But, you are making progress, eh? rolleyes.gif

Although it looks like Python is complaining about get_price(), it's actually the last erorr message that you need to concentrate on... and it's telling you it is "Unauthorized" when it tries to connect to Twitter. The problem may be the extra SPACE in your password (at the end).

Paul.
0

#9 User is offline   jkwash 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 20-February 10

Posted 21 February 2010 - 08:57 PM

QUOTE (classicgamer88 @ Feb 20 2010, 07:15 AM) <{POST_SNAPBACK}>
okay well here is the code with the space between the http gone, exactly like in the example

import urllib.request
import time

def send_to_twitter(msg):
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses" , "classicgamer88 " , "python ")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request. build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( { 'status' : msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

def get_price():
page = urllib.request.urlopen("http://www.beans-r-us.biz/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 = 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(60)
price = get_price()
send_to_twitter("buy")


but now it says it has a problem with the get price function


Do you want to see the price now (Y/N) ? Y
Traceback (most recent call last):
File "C:\Python31\gg.py", line 26, in <module>
send_to_twitter(get_price())
File "C:\Python31\gg.py", line 12, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
File "C:\Python31\lib\urllib\request.py", line 119, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python31\lib\urllib\request.py", line 353, in open
response = meth(req, response)
File "C:\Python31\lib\urllib\request.py", line 465, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python31\lib\urllib\request.py", line 385, in error
result = self._call_chain(*args)
File "C:\Python31\lib\urllib\request.py", line 325, in _call_chain
result = func(*args)
File "C:\Python31\lib\urllib\request.py", line 805, in http_error_401
url, req, headers)
File "C:\Python31\lib\urllib\request.py", line 783, in http_error_auth_reqed
return self.retry_http_basic_auth(host, req, realm)
File "C:\Python31\lib\urllib\request.py", line 793, in retry_http_basic_auth
return self.parent.open(req, timeout=req.timeout)
File "C:\Python31\lib\urllib\request.py", line 353, in open
response = meth(req, response)
File "C:\Python31\lib\urllib\request.py", line 465, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python31\lib\urllib\request.py", line 391, in error
return self._call_chain(*args)
File "C:\Python31\lib\urllib\request.py", line 325, in _call_chain
result = func(*args)
File "C:\Python31\lib\urllib\request.py", line 473, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
>>>


thanks again Paul, for taking some time to help me


else:
price =99.99
while price > 4.74:
time. sleep(60)
price = get_price()

I noticed that the following line was missing in the above else statement: get_price().
The else portion of the statement should read:

else:
price = 99.99
while price > 4.74
time.sleep(60)
get_price()
price = get_price()

Try this to see if the code works. Be patient! You can work through this and come to see your handiwork work.


0

#10 User is offline   classicgamer88 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 10
  • Joined: 13-February 10

  Posted 09 March 2010 - 06:49 PM

Finally I got it too work thanks too both paul and Jkwash


Inbox Sent
Retweets by others Retweets by you Your tweets, retweeted


classicgamer88 buy
half a minute ago via API
Delete
classicgamer88 4.39

thanks again guys
0

#11 User is offline   RyanB22 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 21-March 10

Posted 05 April 2010 - 05:22 AM

EDIT: to save you the time of reading this post: problem solved. It appears I wrote an extra "a" into "password_manager" and that was the problem. I think I need to learn how to type precisely before I will ever be able to code.



I also had a lot of trouble running the twitter code. It worked when I just copy and pasted from the book's code download, but I can't tell the difference between my code and the code you provided for the book:

Here are the 2 relevant selections, with yours first, and mine second:

password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses", "myusername", "mypasswrd")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {'status': msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

password_manager = urllib.request.HTTPPasswordMgr()
password_manaager.add_password("Twitter API",
"http://twitter.com/statuses", "myusername", "mypasswrd")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {'status': msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

It works with yours, but not with mine. Here is the error code I get with mine:

Do you want to see the price now (Y/N)? Y
Traceback (most recent call last):
File "/Users/ryanbarnes/Documents/Ch3getprice.py", line 25, in <module>
send_to_twitter(get_price())
File "/Users/ryanbarnes/Documents/Ch3getprice.py", line 6, in send_to_twitter
password_manaager.add_password("Twitter API",
NameError: global name 'password_manaager' is not defined

So confused as to why my typing won't work.

This post has been edited by RyanB22: 05 April 2010 - 05:30 AM

0

#12 User is offline   RyanB22 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 21-March 10

Posted 05 April 2010 - 05:44 AM

However, I am still confused as to why "The msg variable in the code needs to become a parameter of the defined send_to_twitter function." :

def send_to_twitter(msg):

Can anyone explain?

The best I can think of is that "msg" just acts as a holding place for "get_price" and "Buy!" and that it is not actually used just used for myself for easy reference so I know where to put the message I want to send...and that I could have used anything as a parameter when I defined the code, such as "pricemessage" to indicate to myself that this is where I insert what message I send to twitter.

Am I close?
0

#13 User is offline   Ilpo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 10-August 11

Posted 10 August 2011 - 11:42 AM

Hi,

I'm also having a problem in sending to twitter. There wasn't errors in my code so I tried get only the send_to_twitter function working. Error message I get is this:

Traceback (most recent call last):
  File "XXX.py", line 24, in <module>
    send_to_twitter()
  File "XXX.py", line 20, in send_to_twitter
    resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 138, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 367, in open
    req = meth(req)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 1066, in do_request_
    raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.


I think the problem comes with python 3.2. There has been samekind of topics elsewhere:
http://stackoverflow...t-cannot-be-str

There is something with the encoding, but I'm not yet sure how to solve this.

Here's the same problem:
http://letshare.it/b...tter-error.html

Thanks for the great book anyway.
0

#14 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 11 August 2011 - 06:53 AM

Hi Ilpo.

The problem is not with Python 3.2, it's with Twitter. All the gory details are here: http://forums.oreill...ges-to-twitter/ - Twitter changed how programmers access their site after we went to press with Head First Programming.

--Paul.
0

#15 User is offline   Ilpo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 10-August 11

Posted 17 August 2011 - 11:31 PM

Thanks,

Actually this post of yours got me through.

Quote

So instead of these two lines of code at the end of the send_to_twitter() function:

resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

use this single line of code instead:

print(params)

It's not as much fun, but it will have to do.


Why do it the hard way...
0

#16 User is offline   progreso 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 07-March 12

Posted 07 March 2012 - 12:30 PM

hi there, i'm new in this , and i have this problem with the program of chapter 3
this is the code


import urllib.request
import time

def get_price():
page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")#get price from website
text = page.read().decode("utf8")
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
price=float(text[start_of_price:end_of_price])
return (price)


def send_to_twitter(msg):
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password('Twitter API','http://twitter.com/statuses','mikelsfo','maitane00')
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode({'status':msg})
resp = urllib.request.urlopen('http://twitter.com/statuses/update.json', params)
resp.read()

price_now = input('Would you like to check the price? Y/N ')
if price_now == 'y':
send_to_twitter(get_price())
else:
price = 99.99
while price > 4.74:
time.sleep(900)
get_price
price=get_price()
send_to_twitter()
and i got this problem report
Traceback (most recent call last):
File "/home/loco/Escritorio/ejercicios firts head/ejercicio3-1.py", line 26, in <module>
send_to_twitter(get_price())
File "/home/loco/Escritorio/ejercicios firts head/ejercicio3-1.py", line 21, in send_to_twitter
resp = urllib.request.urlopen('http://twitter.com/statuses/update.json', params)
File "/usr/lib/python3.2/urllib/request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.2/urllib/request.py", line 367, in open
req = meth(req)
File "/usr/lib/python3.2/urllib/request.py", line 1066, in do_request_
raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.
can you help me?
0

#17 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 306
  • Joined: 20-August 09

Posted 11 March 2012 - 06:00 AM

This is an ongoing issue with the Twitter code in the book. Please read the relevant entries in this forum to learn more. As an aside, did you mean to post your code with your Twitter user-id and password visible? Perhaps edit your post to remove or change you Twitter password ASAP.

Regards.

--Paul.
0

#18 User is offline   ricky71321 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 15-November 13

Posted 15 November 2013 - 02:19 PM

I just bought this book last week and I am also having issues with the code:
here is the error:

Traceback (most recent call last):
File "C:\Users\sysadmin\Desktop\pyhf\websitePriceGrab.py", line 34, in <module>
send_to_twitter()
File "C:\Users\sysadmin\Desktop\pyhf\websitePriceGrab.py", line 28, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "C:\Python33\lib\urllib\request.py", line 467, in open
req = meth(req)
File "C:\Python33\lib\urllib\request.py", line 1179, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

Any ideas if Head First willl come up with a working function def send_to_twitter() ?????

This post has been edited by ricky71321: 15 November 2013 - 02:23 PM

0

#19 User is offline   #TM# 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 63
  • Joined: 10-December 12
  • Gender:Not Telling

Posted 17 November 2013 - 03:15 AM



Hi ricky71321,

The same problem had been discussed in this forum already.

Twitter has changed the method of authentication and sending message and all other stuff.

You can follow the below link.

My Blog Link

I have clearly described the method of authentication (with pictures ) and complete code solution for the problem.

This post has been edited by #TM#: 17 November 2013 - 03:16 AM

0

#20 User is offline   ricky71321 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 15-November 13

Posted 18 November 2013 - 11:50 AM

Thanks for your post TM.

But I would like Oreilly or Head First to come up with a solution to this problem without the need to use the twitter library.
0

Share this topic:


  • 2 Pages +
  • 1
  • 2
  • 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