1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Send Twitter update using Python

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

  1. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    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