Commit 04709fb6 authored by Josh Roesslein's avatar Josh Roesslein
Browse files

Initial commit! Added api interface. Added license and readme. Added models.

parents
*.pyc
*.swp
Copyright (c) 2009 Joshua Roesslein
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
==========================
Tweepy Twitter API Library
==========================
A Twitter API library for Python!
Author: Joshua Roesslein <jroesslein at gmail.com>
License: MIT
Dependencies:
Simplejson <http://undefined.org/python/#simplejson>
**Included in python 2.6+
"""
Tweepy Twitter API library
"""
__version__ = '1.0'
from models import *
from api import API
import urllib
import urllib2
try:
import json
except ImportError:
import simplejson as json
from misc import *
from models import *
"""
Twitter API Interface
"""
class API(object):
def __init__(self, username=None, password=None, host='twitter.com',
user_agent='tweepy', secure=False,
user_class=User, status_class=Status):
self._Status = status_class
self._User = user_class
self.username = username
self.host = host
if secure:
self._schema = 'https'
else:
self._schema = 'http'
# Setup headers
self._headers = {
'User-Agent': user_agent
}
self._opener = self._build_opener(username, password)
def public_timeline(self):
return self._fetch('statuses/public_timelinee.json')
@require_auth
def friends_timeline(self, since_id=None, max_id=None, count=None, page=None):
raise NotImplementedError
def _build_opener(self, username, password):
if username and password:
bauth = urllib2.HTTPBasicAuthHandler()
bauth.add_password(None, self.host, username, password)
return urllib2.build_opener(bauth)
else:
return urllib2.build_opener()
def _fetch(self, url, parameters=None, post_data=None):
# Build the url
if parameters:
_url = '%s://%s/%s?%s' % (self._schema, self.host, urllib.urlencode(parameters))
else:
_url = '%s://%s/%s' % (self._schema, self.host, url)
# Encode post data
post = None
if post_data:
post = urllib.encode(post_data)
# Build the request
req = urllib2.Request(_url, post, self._headers)
# Send request
try:
return self._opener.open(req)
except urllib2.HTTPError, e:
raise TweepError(json.loads(e.read())['error'])
"""
Only allow method to be called with authentication.
"""
def require_auth(func):
def wrapper(instance, *args, **kargs):
if instance.username and instance.password:
func(instance, *args, **kargs)
else:
print 'require auth'
return wrapper
"""
Tweepy exception
"""
class TweepError(Exception):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return self.reason
class Status(object):
pass
class User(object):
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment