Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Author: Douglass Clem, crashsystems.net
- Title: PyTwitPic
- Description: As the name implies, PyTwitPic is a simple Python implementation
- of the Twitpic.com API.
- License: GPL v3
- """
- import pycurl
- from BeautifulSoup import BeautifulStoneSoup #for xml parsing
- # I'm setting up some variables for the API URLs, so that there will be one
- # easy place to change them in the future.
- upload_url = "http://twitpic.com/api/upload"
- upload_post_url = "http://twitpic.com/api/uploadAndPost"
- def upload(username, password, file_path):
- """Uploads the specified picture file. Returns URL of picture when upload
- succeeds, and an error message when it fails."""
- #Post the pic
- server = pycurl.Curl()
- server.setopt(server.POST, 1)
- server.setopt(server.URL, upload_url)
- server.setopt(server.HTTPPOST, [("media", (server.FORM_FILE, file_path)),
- ("username", username), ("password", password)])
- xml = server.perform()
- #Now we have the raw XML response, so let's get the useful bits out of it.
- response = __parse_xml(xml)
- return response
- def upload_post(username, password, file_path, message):
- """Uploads the specified picture and posts the status message. Returns URL
- when it succeeds, and an error message when it fails."""
- #Post the pic
- server = pycurl.Curl()
- server.setopt(server.POST, 1)
- server.setopt(server.URL, upload_url)
- server.setopt(server.HTTPPOST, [("media", (server.FORM_FILE, file_path)),
- ("username", username), ("password", password), ("message", message)])
- xml = server.perform()
- #Now we have the raw XML response, so let's get the useful bits out of it.
- status = __parse_xml(xml)
- return status
- def __parse_xml(raw_message):
- """This function is not part of the API, but is used within the module to
- process the xml response sent from twitpic.com."""
- soup = BeautifulStoneSoup(raw_message)
- if (len(soup.findAll(stat="ok")) == 1):
- return str(soup.rsp.mediaurl.contents)
- elif (len(soup.findAll(code="1001")) == 1):
- return "Invalid twitter username or password"
- elif (len(soup.findAll(code="1002")) == 1):
- return "Image not found"
- elif (len(soup.findAll(code="1003")) == 1):
- return "Invalid image type"
- else:
- return "Image larger than 4MB"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement