2
# Permission is hereby granted, free of charge, to any person
3
# obtaining a copy of this software and associated documentation
4
# files (the "Software"), to deal in the Software without
5
# restriction, including without limitation the rights to use,
6
# copy, modify, merge, publish, distribute, sublicense, and/or sell
7
# copies of the Software, and to permit persons to whom the
8
# Software is furnished to do so, subject to the following
11
# The above copyright notice and this permission notice shall be
12
# included in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
# OTHER DEALINGS IN THE SOFTWARE.
23
# Copyright (c) 2009 Mario Carrion <http://www.mariocarrion.com>
26
# Mario Carrion <mario@carrion.ws>
29
from xml.dom.minidom import parseString
30
from os.path import exists, basename
32
from ..provider import Provider, ServiceType
33
from ..multiform import MultiPartForm
37
API_URL="http://twitpic.com/api/uploadAndPost"
38
PROVIDER_NAME="twitpic"
40
class Twitpic(Provider):
43
Provider.__init__(self, True, [ServiceType.Image],name=PROVIDER_NAME,requiresAuthentication=True)
45
def share(self, fileUrl, username, password):
46
# Create the form with simple fields
47
form = MultiPartForm()
48
form.add_field('public', 'yes')
49
form.add_field('username', username)
50
form.add_field('password', password)
51
# form.add_field('username', 'mchackweek')
52
# form.add_field('password', '700423a')
55
form.add_file('media', str(basename(fileUrl)), f)
59
request = urllib2.Request(API_URL)
60
request.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)')
62
request.add_header('Content-type', form.get_content_type())
63
request.add_header('Content-length', len(body))
64
request.add_data(body)
67
xml = urllib2.urlopen(request).read()
69
return self._parseResponse(xml)
71
def _parseOKResponse(self,d):
72
mu = d.getElementsByTagName('mediaurl')
73
if mu==None or len(mu)!=1:
74
return "Cound not decode server XML response (no mediaurl element)"
75
return self._getText(mu[0].childNodes)
77
def _parseResponse(self, xmlres):
78
d = parseString(xmlres)
80
rsp = d.getElementsByTagName('rsp')
81
if rsp==None or len(rsp)!=1:
82
return "Cound not decode server XML response (no rsp element)"
83
sa =rsp[0].attributes.get('status')
85
return "Cound not decode server XML response (no stat attriubute)"
87
return self._parseErrorResponse(d)
89
return self._parseOKResponse(d)
91
return "Cound not decode server XML response (unrecognized stat attriubute value)"
95
def _getText(self, nodelist):
98
if node.nodeType == node.TEXT_NODE: