Jump to content


Photo

Chapter 3: Using Twython To Tweet Prices


  • Please log in to reply
5 replies to this topic

#1 python_holgi

python_holgi

    New Member

  • Members
  • Pip
  • 1 posts

Posted 21 October 2013 - 12:13 PM

I've been using Twython instead of Tweepy to make tweeting your bean prices as easy as possible. The working code is as follows (just replace the dots in '...' with your Twitter app data):


# Send_to_twitter_status

import urllib.request
import time
import sys
import twython

def send_to_twitter(msg):

    CONSUMER_KEY = '...'
    CONSUMER_SECRET = '...'
    ACCESS_KEY = '...'
    ACCESS_SECRET = '...'
    
    twitter = twython.Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)
    twitter.update_status(status=msg)    

def get_price():

    page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.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(1)
        price = get_price()
    send_to_twitter("Buy!")

Testing the N answer several times needs you to vary the massage in send_to_twitter("Buy!") as Twitter does not allow duplicate tweets. So e.g. try send_to_twitter("Buy beans!") instead to run another N answer test.

Hope it helps some fellow learners to keep learning :-)

Edited by python_holgi, 21 October 2013 - 12:52 PM.


#2 muzima

muzima

    New Member

  • Members
  • Pip
  • 8 posts
  • Gender:Male
  • Location:Moscow,Russia
  • Interests:Snowboarding, classical music, Greek language. I'd like to learn how to create apps for my Android. Aim - creating app for my Greek dictionary by my own.

Posted 28 December 2013 - 02:21 AM

I have the following traceback

Traceback (most recent call last):
File "E:\PT\mine\3\twyton.py", line 4, in <module>
import twython
File "E:\PT\lib\site-packages\twython-3.1.2-py3.1.egg\twython\__init__.py", line 23, in <module>
from .api import Twython
File "E:\PT\lib\site-packages\twython-3.1.2-py3.1.egg\twython\api.py", line 12, in <module>
import requests
ImportError: No module named requests

#3 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 04 January 2014 - 11:57 PM

You need to install the requests module from PyPI. See: https://pypi.python..../requests/2.1.0

--Paul.

#4 cambre

cambre

    New Member

  • Members
  • Pip
  • 2 posts

Posted 08 January 2014 - 11:46 AM

I've been using Twython instead of Tweepy to make tweeting your bean prices as easy as possible. The working code is as follows (just replace the dots in '...' with your Twitter app data):


# Send_to_twitter_status

import urllib.request
import time
import sys
import twython

def send_to_twitter(msg):

    CONSUMER_KEY = '...'
    CONSUMER_SECRET = '...'
    ACCESS_KEY = '...'
    ACCESS_SECRET = '...'
    
    twitter = twython.Twython(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET)
    twitter.update_status(status=msg)    

def get_price():

    page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.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(1)
        price = get_price()
    send_to_twitter("Buy!")

Testing the N answer several times needs you to vary the massage in send_to_twitter("Buy!") as Twitter does not allow duplicate tweets. So e.g. try send_to_twitter("Buy beans!") instead to run another N answer test.

Hope it helps some fellow learners to keep learning :-)

You have absolutely made my day. I just created an account to thank you. I see you have made one post and it is the most important one I have come across so far. Thank you!

I did this on CentOS 6.5 using Python 3.3.3.

I have spent the past 3 days going through suggestions from a different post. I used the instructions provided from here in regards to setting up the Twitter Application, which is needed in order to implement the solution you provided.

Before I got to Chapter 3, I was using Python on Windows 7. I decided to use my CentOS machine as I've been wanting to put that to good use and overall, it has been a good experience. It's been fun learning about all the different packages that are out there that you can use with Python. I'm going to continue to use my CentOS machine as I try to learn how to program. I really didn't want to have to skip the rest of Chapter 3 and thanks to you, I won't have to. :D

#5 Olearner

Olearner

    New Member

  • Members
  • Pip
  • 1 posts

Posted 21 April 2014 - 11:26 PM

It worked really fine for me, i just had to install the library of twython and then register a new app on twitter dev.

I`m using windows 8.1 and python 3.4, to install twython on windows y used comand prompt.

copy twython to folder

C:\Python34\Lib

windows+r

cmd

then

cd \Python34\Scripts

easy_install-3.4.exe

and start twython installment

thats about it, thanks cambre and python_holgi

#6 João Alexandre

João Alexandre

    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 May 2015 - 03:00 PM

python_holgi your code was the only one that worked for me. Thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users