]> jfr.im git - z_archive/twitter.git/commitdiff
Support for raw xml
authormverdone <redacted>
Tue, 8 Apr 2008 20:17:16 +0000 (20:17 +0000)
committermverdone <redacted>
Tue, 8 Apr 2008 20:17:16 +0000 (20:17 +0000)
git-svn-id: http://svn.mike.verdone.ca/pyprojects/twitter/trunk@158 d723f978-dc38-0410-87ed-da353333cdcc

twitter.egg-info/PKG-INFO [deleted file]
twitter.egg-info/SOURCES.txt [deleted file]
twitter.egg-info/dependency_links.txt [deleted file]
twitter.egg-info/entry_points.txt [deleted file]
twitter.egg-info/paster_plugins.txt [deleted file]
twitter.egg-info/top_level.txt [deleted file]
twitter.egg-info/zip-safe [deleted file]
twitter/api.py

diff --git a/twitter.egg-info/PKG-INFO b/twitter.egg-info/PKG-INFO
deleted file mode 100644 (file)
index 815dd24..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-Metadata-Version: 1.0
-Name: twitter
-Version: 0.1dev-r156
-Summary: An API and command-line toolset for Twitter (twitter.com)
-Home-page: http://mike.verdone.ca/twitter/
-Author: Mike Verdone
-Author-email: mike.verdone@gmail.com
-License: UNKNOWN
-Description: UNKNOWN
-Keywords: twitter
-Platform: UNKNOWN
diff --git a/twitter.egg-info/SOURCES.txt b/twitter.egg-info/SOURCES.txt
deleted file mode 100644 (file)
index d1643e8..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-setup.cfg
-setup.py
-twitter/__init__.py
-twitter/api.py
-twitter/cmdline.py
-twitter.egg-info/PKG-INFO
-twitter.egg-info/SOURCES.txt
-twitter.egg-info/dependency_links.txt
-twitter.egg-info/entry_points.txt
-twitter.egg-info/paster_plugins.txt
-twitter.egg-info/top_level.txt
-twitter.egg-info/zip-safe
\ No newline at end of file
diff --git a/twitter.egg-info/dependency_links.txt b/twitter.egg-info/dependency_links.txt
deleted file mode 100644 (file)
index 8b13789..0000000
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/twitter.egg-info/entry_points.txt b/twitter.egg-info/entry_points.txt
deleted file mode 100644 (file)
index 958d403..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-
-      # -*- Entry points: -*-
-      [console_scripts]
-      twitter=twitter.cmdline:main
-      
\ No newline at end of file
diff --git a/twitter.egg-info/paster_plugins.txt b/twitter.egg-info/paster_plugins.txt
deleted file mode 100644 (file)
index 8e50835..0000000
+++ /dev/null
@@ -1 +0,0 @@
-PasteScript
diff --git a/twitter.egg-info/top_level.txt b/twitter.egg-info/top_level.txt
deleted file mode 100644 (file)
index 37d1823..0000000
+++ /dev/null
@@ -1 +0,0 @@
-twitter
diff --git a/twitter.egg-info/zip-safe b/twitter.egg-info/zip-safe
deleted file mode 100644 (file)
index 8b13789..0000000
+++ /dev/null
@@ -1 +0,0 @@
-
index 2556bf69e89e26acd5671efc5ebd36cbd99bad2d..aa0660fc18de9c7c1c44d2586d2c1c6be24b9a16 100644 (file)
@@ -3,7 +3,6 @@ from base64 import b64encode
 from urllib import urlencode
 
 import httplib
-import simplejson
 
 from exceptions import Exception
 
@@ -11,16 +10,18 @@ class TwitterError(Exception):
     pass
 
 class TwitterCall(object):
-    def __init__(self, username=None, password=None, uri=""):
+    def __init__(self, username, password, format, uri=""):
         self.username = username
         self.password = password
+        self.format = format
         self.uri = uri
     def __getattr__(self, k):
         try:
             return object.__getattr__(self, k)
         except AttributeError:
             return TwitterCall(
-                self.username, self.password, self.uri + "/" + k)
+                self.username, self.password, self.format, 
+                self.uri + "/" + k)
     def __call__(self, **kwargs):
         method = "GET"
         if self.uri.endswith('new') or self.uri.endswith('update'):
@@ -30,11 +31,15 @@ class TwitterCall(object):
             argStr = "?" + urlencode(kwargs.items())
         c = httplib.HTTPConnection("twitter.com")
         try:
-            c.putrequest(method, "/%s.json%s" %(self.uri, argStr))
+            c.putrequest(method, "/%s.%s%s" %(
+                self.uri, self.format, argStr))
             if (self.username):
-                c.putheader("Authorization", "Basic " 
-                            + b64encode("%s:%s" %(
-                                self.username, self.password)))
+                c.putheader(
+                    "Authorization", "Basic " + b64encode("%s:%s" %(
+                        self.username, self.password)))
+            if (method == "POST"):
+                # TODO specify charset
+                pass
             c.endheaders()
             r = c.getresponse()
             if (r.status == 304):
@@ -42,7 +47,11 @@ class TwitterCall(object):
             elif (r.status != 200):
                 raise TwitterError("Twitter sent status %i: %s" %(
                     r.status, r.read()))
-            return simplejson.loads(r.read())
+            if ("json" == self.format):
+                import simplejson
+                return simplejson.loads(r.read())
+            else:
+                return r.read()
         finally:
             c.close()
 
@@ -86,13 +95,31 @@ class Twitter(TwitterCall):
 
       # The screen name of the user who wrote the first 'tweet'
       x[0]['user']['screen_name']
+    
+    Getting raw XML data::
+    
+      If you prefer to get your Twitter data in XML format, pass
+      format="xml" to the Twitter object when you instantiate it:
+      
+      twitter = Twitter(format="xml")
       
+      The output will not be parsed in any way. It will be a raw string
+      of XML.
     """
-    def __init__(self, email=None, password=None):
+    def __init__(self, email=None, password=None, format="json"):
         """
         Create a new twitter API connector using the specified
-        credentials (email and password).
+        credentials (email and password). Format specifies the output
+        format ("json" (default) or "xml").
         """
-        TwitterCall.__init__(self, email, password)
+        if (format not in ("json", "xml")):
+            raise TwitterError("Unknown data format '%s'" %(format))
+        if (format == "json"):
+            try:
+                import simplejson
+            except ImportError:
+                raise TwitterError(
+                    "format not available: simplejson is not installed")
+        TwitterCall.__init__(self, email, password, format)
 
 __all__ = ["Twitter"]