Tuesday, December 11, 2012

chapter3 of head first programming (complete procedure and program to send message to twitter)

  • First of all go to TWITTER and log in to your account.
  • Follow the below pictures to create a new app. 
Note: Instructions are written inside the pictures.

















Now we are all done with Twitter application authentication process !!
So long. Take some tea or coffee break !!
sweat

################################################################################
INSTALLATION INSTRUCTIONS AND LINKS FOR pip AND twitter version 1.16.0 OR LOWER .
################################################################################
NOTE: YOU NEED TO USE COMMAND PROMPT (ON WINDOWS) AND TERMINAL (ON LINUX AND MAC) WITH ADMIN PRIVILEGE FOR INSTALLING PIP AND TWITTER API.

For Linux (yum and apt based):

1. yum install python-pip python3-pip

                  or 
    sudo apt-get install python-pip python3-pip

For Windows click me


For Mac OS click me


                                        OR

FOLLOW THIS PIP INSTALLATION GUIDE (CLICK ME)

2. pip install twitter
3. pip3 install twitter

################################################################################
Script for python-2.7.x (tested using python-2.7.8) :
################################################################################
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import twitter as t
import urllib2 as ur
import time

CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Same as API key
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Same as API secret
ACCESS_TOKEN_KEY= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

'''
This function updates the status of my twitter account
requires twitter python module
'''
def tweet(status_msg):
    if len(status_msg) > 140 :
        raise Exception ('Status message too long !!!')
    else:
        authkey = t.Twitter(auth=t.OAuth(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
        result = authkey.statuses.update(status=status_msg)
        return result

'''
This function get the coffee price (both below a desired price and in emergency; the current price)
'''
def get_price(page):
    text = page.read().decode("utf8")
    ss_pos = text.find('>$')
    soss = ss_pos + 2
    eoss = soss + 4
    return float(text[soss:eoss])

'''
main program
requires urllib.request
requires time library also.
'''
print ("Welcome to Starbuzz Coffee price checking program")
choice = raw_input ("Do You want to know the coffee price immediately ?? (y/n) : ")
if choice == "y":
    page = ur.urlopen("http://beans.itcarlow.ie/prices.html")
    price = get_price(page)
    status_msg = "@starbuzzceo\nCurrent price of coffee = $" + str(price)
    res = tweet(status_msg)
else :
    price =99.99
    while price > 4.74 :
        time.sleep(10)
        page = ur.urlopen("http://beans.itcarlow.ie/prices-loyalty.html")
        price = get_price(page)
    status_msg = "@starbuzzceo\nCurrent price of coffee is $" + str(price) + "; BUY !!!"
    res = tweet(status_msg)

if res != None:
    print ("tweeted coffee price successfully to Starbuzz CEO")
else:

    print ("problem !!")

################################################################################ 
Script for python-3.x (tested using python-3.4.1) :
################################################################################
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import twitter as t
import urllib.request as ur
import time

CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Same as API key
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Same as API secret
ACCESS_TOKEN_KEY= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

'''
This function updates the status of my twitter account
requires twitter python module
'''
def tweet(status_msg):
    if len(status_msg) > 140 :
        raise Exception ('Status message too long !!!')
    else:
        authkey = t.Twitter(auth=t.OAuth(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
        result = authkey.statuses.update(status=status_msg)
        return result

'''
This function get the coffee price (both below a desired price and in emergency; the current price)
'''
def get_price(page):
    text = page.read().decode("utf8")
    ss_pos = text.find('>$')
    soss = ss_pos + 2
    eoss = soss + 4
    return float(text[soss:eoss])

'''
main program
requires urllib.request
requires time library also.
'''
print ("Welcome to Starbuzz Coffee price checking program")
choice = input ("Do You want to know the coffee price immediately ?? (y/n) : ")
if choice == "y":
    page = ur.urlopen("http://beans.itcarlow.ie/prices.html")
    price = get_price(page)
    status_msg = "@starbuzzceo\nCurrent price of coffee = $" + str(price)
    res = tweet(status_msg)
else :
    price =99.99
    while price > 4.74 :
        time.sleep(10)
        page = ur.urlopen("http://beans.itcarlow.ie/prices-loyalty.html")
        price = get_price(page)
    status_msg = "@starbuzzceo\nCurrent price of coffee is $" + str(price) + "; BUY !!!"
    res = tweet(status_msg)

if res != None:
    print ("tweeted coffee price successfully to Starbuzz CEO")
else:
    print ("problem !!")

################################################################################
SCRIPTS FOR PYTHON 2.7.X USING  tweepy
################################################################################
Try it yourself for Python 3.X.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import tweepy
import urllib2 
import time

CONSUMER_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'# Same as API key
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'# Same as API secret
ACCESS_TOKEN_KEY= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
ACCESS_TOKEN_SECRET= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

 '''
This below function updates the status of my twitter account
requires tweepy
'''
def tweet(status):
    if len(status) > 140 :
        raise Exception ('Status message too long !!!')
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
    api = tweepy.API(auth)
    result = api.update_status(status)
    return result 
# I do not feel any need of this though.
#it is always good to make a function return something

'''
This below function get the coffee price (both below a desired price and in emergency; the current price)
'''
def get_price(page):
    text = page.read().decode("utf8")
    ss_pos = text.find('>$')
    soss = ss_pos + 2
    eoss = soss + 4
    return float(text[soss:eoss])

'''
main program
requires urllib2 (incase of python 2.7.x series)
requires time library also.
'''
print ("Welcome to Starbuzz Coffee price checking program")
choice = raw_input ("Do You want to know the coffee price immediately ?? (y/n) : ")
if choice == "y":
    page_emergency = urllib2.urlopen("http://beans.itcarlow.ie/prices.html") 
    price = get_price(page_emergency)
    res = tweet("@starbuzzceo\nCurrent price of coffee = $" + str(price))
else :
    price =99.99
    while price > 4.74 :
        time.sleep(10)
        page_optimal = urllib2.urlopen("http://beans.itcarlow.ie/prices-loyalty.html") 
        price = get_price(page_optimal)
    res = tweet("@starbuzzceo\nCurrent price of coffee is $" + str(price) + "; BUY !!!")
if res != None:
    print ("tweeted coffee price successfully to starbuzz ceo")
else:
    print ("problem !!")
suggestions and comments are invited.


ahaaah        cool