~mario-carrion/gwibber/gwibber-services

« back to all changes in this revision

Viewing changes to gwibber/sharing/twitpic/twitpic.py

  • Committer: Mario Carrion
  • Date: 2009-07-27 00:39:14 UTC
  • Revision ID: mario@carrion.ws-20090727003914-84373nqngcph5pmy
Services/Provider implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
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
 
9
# conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be
 
12
# included in all copies or substantial portions of the Software.
 
13
#
 
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.
 
22
#
 
23
# Copyright (c) 2009 Mario Carrion <http://www.mariocarrion.com>
 
24
#
 
25
# Authors:
 
26
#       Mario Carrion <mario@carrion.ws>
 
27
#
 
28
 
 
29
from xml.dom.minidom import parseString
 
30
from os.path import exists, basename
 
31
 
 
32
from ..provider import Provider, ServiceType
 
33
from ..multiform import MultiPartForm
 
34
 
 
35
import urllib2
 
36
 
 
37
API_URL="http://twitpic.com/api/uploadAndPost"
 
38
PROVIDER_NAME="twitpic"
 
39
 
 
40
class Twitpic(Provider):
 
41
 
 
42
  def __init__(self):
 
43
    Provider.__init__(self, True, [ServiceType.Image],name=PROVIDER_NAME,requiresAuthentication=True)
 
44
 
 
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')
 
53
 
 
54
    f = open(fileUrl)
 
55
    form.add_file('media', str(basename(fileUrl)), f)
 
56
    f.close()
 
57
    
 
58
    # Build the request
 
59
    request = urllib2.Request(API_URL)
 
60
    request.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)')
 
61
    body = str(form)
 
62
    request.add_header('Content-type', form.get_content_type())
 
63
    request.add_header('Content-length', len(body))
 
64
    request.add_data(body)
 
65
 
 
66
    request.get_data()
 
67
    xml = urllib2.urlopen(request).read()
 
68
 
 
69
    return self._parseResponse(xml)
 
70
 
 
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)
 
76
 
 
77
  def _parseResponse(self, xmlres):
 
78
      d = parseString(xmlres)
 
79
      try:
 
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')
 
84
          if sa==None:
 
85
              return "Cound not decode server XML response (no stat attriubute)"
 
86
          if sa.value=='fail':
 
87
              return self._parseErrorResponse(d)
 
88
          elif sa.value=='ok':
 
89
              return self._parseOKResponse(d)
 
90
          else:
 
91
              return "Cound not decode server XML response (unrecognized stat attriubute value)"
 
92
      finally:
 
93
          d.unlink()
 
94
 
 
95
  def _getText(self, nodelist):
 
96
      rc = ""
 
97
      for node in nodelist:
 
98
          if node.nodeType == node.TEXT_NODE:
 
99
              rc = rc + node.data
 
100
      return rc
 
101
    
 
102