Send Twitter update using Python

Discussion in 'Web Development' started by pein87, Jan 17, 2011.

  1. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    Well I've been reading this book called "head first beginning programming" so I can learn Python without all the bloat of normal Python books. I modified a function that they used to accept your username and password as parameters. I might add string length checking to so that you dont get errors for the message being to long.

    Code:
    # includes start
    
    import urllib.request
    
    #includes end
    
    def sendTwitterUpdate(msg,twitUserName,twitPassword):
        passManager = urllib.request.HTTPPasswordMgr()
        passManager.add_password("Twitter API","http://twitter.com/statuses",twitUserName,twitPassword)
        httpHandle = urllib.request.HTTPBasicAuthHandler(passManager)
        pageOpener = urllib.request.build_opener(httpHandle)
        urllib.request.install_opener(pageOpener)
        params = urllib.parse.urlencode({'status': msg})
        response = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
        response.read()
    
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice