Commits

a2c committed 3c54e4a

remove some file

Comments (0)

Files changed (13)

not-use/twit_util.py

-#!/usr/bin/env python
-# encoding: utf-8
-"""
-twit_util.py
-
-Created by atusi on 2010-10-02.
-Copyright (c) 2010 __MyCompanyName__. All rights reserved.
-"""
-import os
-import cgi
-import logging
- 
-from google.appengine.ext.webapp import template
-from google.appengine.ext import webapp
-from google.appengine.ext.webapp.util import run_wsgi_app
-from google.appengine.ext import db
-import wsgiref.handlers
- 
-from google.appengine.api import memcache 
-import uuid
-import tweepy
-#from simple_cookie2 import Cookies
- 
-CONSUMER_KEY    = 'qRuOwv9rUcdRTLYrwI7xVA'
-CONSUMER_SECRET = 'GTZJsU8XsZoz7vCKh4NOcpGBLhBwDSRPOX0RQhrGTH0'
-SESSION_EXPIRE  = 300
-
-# utility ======================================
-def is_dev():
-    return os.environ.get('SERVER_SOFTWARE', '').startswith('Development')
-
-if is_dev():
-  CALLBACK_URL    = 'http://127.0.0.1:8080/twit/oauth_callback'
-else:
-  CALLBACK_URL    = 'http://a2c-fts.appspot.com/twit/oauth_callback'
-
-#================================================
-
-
-# ユーザがtwitter認証を行う際のリクエストトークンを保持するモデル
-class RequestToken(db.Model):
-  token_key    = db.StringProperty(required=True)
-  token_secret = db.StringProperty(required=True)
- 
-# ログインしている場合はタイムライン表示
-class Home(webapp.RequestHandler):
-  def get(self):
-    #cookie = Cookies(self, max_age=SESSION_EXPIRE)
-    #if not cookie.has_key('sid'):
-    #  cookie['sid'] = str(uuid.uuid4())   # セッション開始
- 
-    (timeline, username) = (None, None)
-    #access_token = memcache.get(cookie['sid'])
-    access_token = memcache.get('sid')
-    if access_token:
-      auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
-      auth.set_access_token(access_token.key, access_token.secret)
-      username = auth.get_username()
-      api = tweepy.API(auth_handler=auth)
-      timeline = api.home_timeline(count=10)
-      #self.response.out.write(template.render('timeline.html', {'username':username, 'timeline': timeline}))
-    self.response.out.write({'username':username, 'timeline': timeline})
- 
-# tweet
-'''
-class Update(webapp.RequestHandler):
-  def post(self):
-    cookie = Cookies(self)
-    access_token = memcache.get(cookie['sid'])
-    if access_token:
-      auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
-      auth.set_access_token(access_token.key, access_token.secret)
-      api = tweepy.API(auth_handler=auth)
-      api.update_status(status=self.request.get('status'))
-    self.redirect('/twit')
-'''
-
-# twitterのOAuth認証
-class OAuth(webapp.RequestHandler):
-  def get(self):
-    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL)
-    auth_url = auth.get_authorization_url()
-    request_token = RequestToken(token_key=auth.request_token.key, token_secret=auth.request_token.secret)
-    request_token.put()
-    self.redirect(auth_url)   # twitterの認証画面へリダイレクト
- 
-# twitterのOAuth認証後のコールバック
-class OAuthCB(webapp.RequestHandler):
-  def get(self):
-    request_token_key = self.request.get("oauth_token")
-    request_verifier  = self.request.get('oauth_verifier')
-    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
-    request_token = RequestToken.gql("WHERE token_key=:1", request_token_key).get()
-    auth.set_request_token(request_token.token_key, request_token.token_secret)
-    access_token = auth.get_access_token(request_verifier)
-    cookie = Cookies(self)
-    memcache.set(cookie['sid'], access_token, SESSION_EXPIRE)
-    self.redirect('/twit')
- 
-# ログアウト
-# twitter認証側のCookieはいじれないのでセッションを削除する
-'''
-class Logout(webapp.RequestHandler):
-  def get(self):
-    cookie = Cookies(self)
-    if cookie.has_key('sid'):
-      memcache.delete(cookie['sid'])
-      del cookie['sid']
-    self.redirect('/twit')
-'''
-
-application = webapp.WSGIApplication(
-  [
-    ('/twit', Home),
-    #('/twit/update', Update),
-    #('/twit/logout',   Logout),
-    ('/twit/oauth',    OAuth),
-    ('/twit/oauth_callback', OAuthCB),
-  ],
-  debug=True)
- 
-if __name__ == "__main__":
-  run_wsgi_app(application)

tweepy/__init__.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-"""
-Tweepy Twitter API library
-"""
-__version__ = '1.7.1'
-__author__ = 'Joshua Roesslein'
-__license__ = 'MIT'
-
-from tweepy.models import Status, User, DirectMessage, Friendship, SavedSearch, SearchResult, ModelFactory
-from tweepy.error import TweepError
-from tweepy.api import API
-from tweepy.cache import Cache, MemoryCache, FileCache
-from tweepy.auth import BasicAuthHandler, OAuthHandler
-from tweepy.streaming import Stream, StreamListener
-from tweepy.cursor import Cursor
-
-# Global, unauthenticated instance of API
-api = API()
-
-def debug(enable=True, level=1):
-
-    import httplib
-    httplib.HTTPConnection.debuglevel = level
-

tweepy/api.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-import os
-import mimetypes
-
-from tweepy.binder import bind_api
-from tweepy.error import TweepError
-from tweepy.parsers import ModelParser
-from tweepy.utils import list_to_csv
-
-
-class API(object):
-    """Twitter API"""
-
-    def __init__(self, auth_handler=None,
-            host='api.twitter.com', search_host='search.twitter.com',
-             cache=None, secure=False, api_root='/1', search_root='',
-            retry_count=0, retry_delay=0, retry_errors=None,
-            parser=None):
-        self.auth = auth_handler
-        self.host = host
-        self.search_host = search_host
-        self.api_root = api_root
-        self.search_root = search_root
-        self.cache = cache
-        self.secure = secure
-        self.retry_count = retry_count
-        self.retry_delay = retry_delay
-        self.retry_errors = retry_errors
-        self.parser = parser or ModelParser()
-
-    """ statuses/public_timeline """
-    public_timeline = bind_api(
-        path = '/statuses/public_timeline.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = []
-    )
-
-    """ statuses/home_timeline """
-    home_timeline = bind_api(
-        path = '/statuses/home_timeline.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/friends_timeline """
-    friends_timeline = bind_api(
-        path = '/statuses/friends_timeline.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/user_timeline """
-    user_timeline = bind_api(
-        path = '/statuses/user_timeline.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['id', 'user_id', 'screen_name', 'since_id',
-                          'max_id', 'count', 'page']
-    )
-
-    """ statuses/mentions """
-    mentions = bind_api(
-        path = '/statuses/mentions.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """/statuses/:id/retweeted_by.format"""
-    retweeted_by = bind_api(
-        path = '/statuses/{id}/retweeted_by.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """/statuses/:id/retweeted_by/ids.format"""
-    retweeted_by_ids = bind_api(
-        path = '/statuses/{id}/retweeted_by/ids.json',
-        payload_type = 'ids',
-        allowed_param = ['id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/retweeted_by_me """
-    retweeted_by_me = bind_api(
-        path = '/statuses/retweeted_by_me.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/retweeted_to_me """
-    retweeted_to_me = bind_api(
-        path = '/statuses/retweeted_to_me.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/retweets_of_me """
-    retweets_of_me = bind_api(
-        path = '/statuses/retweets_of_me.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ statuses/show """
-    get_status = bind_api(
-        path = '/statuses/show.json',
-        payload_type = 'status',
-        allowed_param = ['id']
-    )
-
-    """ statuses/update """
-    update_status = bind_api(
-        path = '/statuses/update.json',
-        method = 'POST',
-        payload_type = 'status',
-        allowed_param = ['status', 'in_reply_to_status_id', 'lat', 'long', 'source', 'place_id'],
-        require_auth = True
-    )
-
-    """ statuses/destroy """
-    destroy_status = bind_api(
-        path = '/statuses/destroy.json',
-        method = 'DELETE',
-        payload_type = 'status',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ statuses/retweet """
-    retweet = bind_api(
-        path = '/statuses/retweet/{id}.json',
-        method = 'POST',
-        payload_type = 'status',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ statuses/retweets """
-    retweets = bind_api(
-        path = '/statuses/retweets/{id}.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['id', 'count'],
-        require_auth = True
-    )
-
-    """ users/show """
-    get_user = bind_api(
-        path = '/users/show.json',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name']
-    )
-
-    """ Perform bulk look up of users from user ID or screenname """
-    def lookup_users(self, user_ids=None, screen_names=None):
-        return self._lookup_users(list_to_csv(user_ids), list_to_csv(screen_names))
-
-    _lookup_users = bind_api(
-        path = '/users/lookup.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ Get the authenticated user """
-    def me(self):
-        return self.get_user(screen_name=self.auth.get_username())
-
-    """ users/search """
-    search_users = bind_api(
-        path = '/users/search.json',
-        payload_type = 'user', payload_list = True,
-        require_auth = True,
-        allowed_param = ['q', 'per_page', 'page']
-    )
-
-    """ statuses/friends """
-    friends = bind_api(
-        path = '/statuses/friends.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
-    )
-
-    """ statuses/followers """
-    followers = bind_api(
-        path = '/statuses/followers.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
-    )
-
-    """ direct_messages """
-    direct_messages = bind_api(
-        path = '/direct_messages.json',
-        payload_type = 'direct_message', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ direct_messages/sent """
-    sent_direct_messages = bind_api(
-        path = '/direct_messages/sent.json',
-        payload_type = 'direct_message', payload_list = True,
-        allowed_param = ['since_id', 'max_id', 'count', 'page'],
-        require_auth = True
-    )
-
-    """ direct_messages/new """
-    send_direct_message = bind_api(
-        path = '/direct_messages/new.json',
-        method = 'POST',
-        payload_type = 'direct_message',
-        allowed_param = ['user', 'screen_name', 'user_id', 'text'],
-        require_auth = True
-    )
-
-    """ direct_messages/destroy """
-    destroy_direct_message = bind_api(
-        path = '/direct_messages/destroy.json',
-        method = 'DELETE',
-        payload_type = 'direct_message',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ friendships/create """
-    create_friendship = bind_api(
-        path = '/friendships/create.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
-        require_auth = True
-    )
-
-    """ friendships/destroy """
-    destroy_friendship = bind_api(
-        path = '/friendships/destroy.json',
-        method = 'DELETE',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ friendships/exists """
-    exists_friendship = bind_api(
-        path = '/friendships/exists.json',
-        payload_type = 'json',
-        allowed_param = ['user_a', 'user_b']
-    )
-
-    """ friendships/show """
-    show_friendship = bind_api(
-        path = '/friendships/show.json',
-        payload_type = 'friendship',
-        allowed_param = ['source_id', 'source_screen_name',
-                          'target_id', 'target_screen_name']
-    )
-
-    """ friends/ids """
-    friends_ids = bind_api(
-        path = '/friends/ids.json',
-        payload_type = 'ids',
-        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
-    )
-
-    """ friendships/incoming """
-    friendships_incoming = bind_api(
-        path = '/friendships/incoming.json',
-        payload_type = 'ids',
-        allowed_param = ['cursor']
-    )
-
-    """ friendships/outgoing"""
-    friendships_outgoing = bind_api(
-        path = '/friendships/outgoing.json',
-        payload_type = 'ids',
-        allowed_param = ['cursor']
-    )
-
-    """ followers/ids """
-    followers_ids = bind_api(
-        path = '/followers/ids.json',
-        payload_type = 'ids',
-        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
-    )
-
-    """ account/verify_credentials """
-    def verify_credentials(self):
-        try:
-            return bind_api(
-                path = '/account/verify_credentials.json',
-                payload_type = 'user',
-                require_auth = True
-            )(self)
-        except TweepError:
-            return False
-
-    """ account/rate_limit_status """
-    rate_limit_status = bind_api(
-        path = '/account/rate_limit_status.json',
-        payload_type = 'json'
-    )
-
-    """ account/update_delivery_device """
-    set_delivery_device = bind_api(
-        path = '/account/update_delivery_device.json',
-        method = 'POST',
-        allowed_param = ['device'],
-        payload_type = 'user',
-        require_auth = True
-    )
-
-    """ account/update_profile_colors """
-    update_profile_colors = bind_api(
-        path = '/account/update_profile_colors.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['profile_background_color', 'profile_text_color',
-                          'profile_link_color', 'profile_sidebar_fill_color',
-                          'profile_sidebar_border_color'],
-        require_auth = True
-    )
-
-    """ account/update_profile_image """
-    def update_profile_image(self, filename):
-        headers, post_data = API._pack_image(filename, 700)
-        return bind_api(
-            path = '/account/update_profile_image.json',
-            method = 'POST',
-            payload_type = 'user',
-            require_auth = True
-        )(self, post_data=post_data, headers=headers)
-
-    """ account/update_profile_background_image """
-    def update_profile_background_image(self, filename, *args, **kargs):
-        headers, post_data = API._pack_image(filename, 800)
-        bind_api(
-            path = '/account/update_profile_background_image.json',
-            method = 'POST',
-            payload_type = 'user',
-            allowed_param = ['tile'],
-            require_auth = True
-        )(self, post_data=post_data, headers=headers)
-
-    """ account/update_profile """
-    update_profile = bind_api(
-        path = '/account/update_profile.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['name', 'url', 'location', 'description'],
-        require_auth = True
-    )
-
-    """ favorites """
-    favorites = bind_api(
-        path = '/favorites.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['id', 'page']
-    )
-
-    """ favorites/create """
-    create_favorite = bind_api(
-        path = '/favorites/create/{id}.json',
-        method = 'POST',
-        payload_type = 'status',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ favorites/destroy """
-    destroy_favorite = bind_api(
-        path = '/favorites/destroy/{id}.json',
-        method = 'DELETE',
-        payload_type = 'status',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ notifications/follow """
-    enable_notifications = bind_api(
-        path = '/notifications/follow.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ notifications/leave """
-    disable_notifications = bind_api(
-        path = '/notifications/leave.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ blocks/create """
-    create_block = bind_api(
-        path = '/blocks/create.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ blocks/destroy """
-    destroy_block = bind_api(
-        path = '/blocks/destroy.json',
-        method = 'DELETE',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ blocks/exists """
-    def exists_block(self, *args, **kargs):
-        try:
-            bind_api(
-                path = '/blocks/exists.json',
-                allowed_param = ['id', 'user_id', 'screen_name'],
-                require_auth = True
-            )(self, *args, **kargs)
-        except TweepError:
-            return False
-        return True
-
-    """ blocks/blocking """
-    blocks = bind_api(
-        path = '/blocks/blocking.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['page'],
-        require_auth = True
-    )
-
-    """ blocks/blocking/ids """
-    blocks_ids = bind_api(
-        path = '/blocks/blocking/ids.json',
-        payload_type = 'json',
-        require_auth = True
-    )
-
-    """ report_spam """
-    report_spam = bind_api(
-        path = '/report_spam.json',
-        method = 'POST',
-        payload_type = 'user',
-        allowed_param = ['id', 'user_id', 'screen_name'],
-        require_auth = True
-    )
-
-    """ saved_searches """
-    saved_searches = bind_api(
-        path = '/saved_searches.json',
-        payload_type = 'saved_search', payload_list = True,
-        require_auth = True
-    )
-
-    """ saved_searches/show """
-    get_saved_search = bind_api(
-        path = '/saved_searches/show/{id}.json',
-        payload_type = 'saved_search',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ saved_searches/create """
-    create_saved_search = bind_api(
-        path = '/saved_searches/create.json',
-        method = 'POST',
-        payload_type = 'saved_search',
-        allowed_param = ['query'],
-        require_auth = True
-    )
-
-    """ saved_searches/destroy """
-    destroy_saved_search = bind_api(
-        path = '/saved_searches/destroy/{id}.json',
-        method = 'DELETE',
-        payload_type = 'saved_search',
-        allowed_param = ['id'],
-        require_auth = True
-    )
-
-    """ help/test """
-    def test(self):
-        try:
-            bind_api(
-                path = '/help/test.json',
-            )(self)
-        except TweepError:
-            return False
-        return True
-
-    def create_list(self, *args, **kargs):
-        return bind_api(
-            path = '/%s/lists.json' % self.auth.get_username(),
-            method = 'POST',
-            payload_type = 'list',
-            allowed_param = ['name', 'mode', 'description'],
-            require_auth = True
-        )(self, *args, **kargs)
-
-    def destroy_list(self, slug):
-        return bind_api(
-            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
-            method = 'DELETE',
-            payload_type = 'list',
-            require_auth = True
-        )(self)
-
-    def update_list(self, slug, *args, **kargs):
-        return bind_api(
-            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
-            method = 'POST',
-            payload_type = 'list',
-            allowed_param = ['name', 'mode', 'description'],
-            require_auth = True
-        )(self, *args, **kargs)
-
-    lists = bind_api(
-        path = '/{user}/lists.json',
-        payload_type = 'list', payload_list = True,
-        allowed_param = ['user', 'cursor'],
-        require_auth = True
-    )
-
-    lists_memberships = bind_api(
-        path = '/{user}/lists/memberships.json',
-        payload_type = 'list', payload_list = True,
-        allowed_param = ['user', 'cursor'],
-        require_auth = True
-    )
-
-    lists_subscriptions = bind_api(
-        path = '/{user}/lists/subscriptions.json',
-        payload_type = 'list', payload_list = True,
-        allowed_param = ['user', 'cursor'],
-        require_auth = True
-    )
-
-    list_timeline = bind_api(
-        path = '/{owner}/lists/{slug}/statuses.json',
-        payload_type = 'status', payload_list = True,
-        allowed_param = ['owner', 'slug', 'since_id', 'max_id', 'per_page', 'page']
-    )
-
-    get_list = bind_api(
-        path = '/{owner}/lists/{slug}.json',
-        payload_type = 'list',
-        allowed_param = ['owner', 'slug']
-    )
-
-    def add_list_member(self, slug, *args, **kargs):
-        return bind_api(
-            path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
-            method = 'POST',
-            payload_type = 'list',
-            allowed_param = ['id'],
-            require_auth = True
-        )(self, *args, **kargs)
-
-    def remove_list_member(self, slug, *args, **kargs):
-        return bind_api(
-            path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
-            method = 'DELETE',
-            payload_type = 'list',
-            allowed_param = ['id'],
-            require_auth = True
-        )(self, *args, **kargs)
-
-    list_members = bind_api(
-        path = '/{owner}/{slug}/members.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['owner', 'slug', 'cursor']
-    )
-
-    def is_list_member(self, owner, slug, user_id):
-        try:
-            return bind_api(
-                path = '/%s/%s/members/%s.json' % (owner, slug, user_id),
-                payload_type = 'user'
-            )(self)
-        except TweepError:
-            return False
-
-    subscribe_list = bind_api(
-        path = '/{owner}/{slug}/subscribers.json',
-        method = 'POST',
-        payload_type = 'list',
-        allowed_param = ['owner', 'slug'],
-        require_auth = True
-    )
-
-    unsubscribe_list = bind_api(
-        path = '/{owner}/{slug}/subscribers.json',
-        method = 'DELETE',
-        payload_type = 'list',
-        allowed_param = ['owner', 'slug'],
-        require_auth = True
-    )
-
-    list_subscribers = bind_api(
-        path = '/{owner}/{slug}/subscribers.json',
-        payload_type = 'user', payload_list = True,
-        allowed_param = ['owner', 'slug', 'cursor']
-    )
-
-    def is_subscribed_list(self, owner, slug, user_id):
-        try:
-            return bind_api(
-                path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id),
-                payload_type = 'user'
-            )(self)
-        except TweepError:
-            return False
-
-    """ trends/available """
-    trends_available = bind_api(
-        path = '/trends/available.json',
-        payload_type = 'json',
-        allowed_param = ['lat', 'long']
-    )
-
-    """ trends/location """
-    trends_location = bind_api(
-        path = '/trends/{woeid}.json',
-        payload_type = 'json',
-        allowed_param = ['woeid']
-    )
-
-    """ search """
-    search = bind_api(
-        search_api = True,
-        path = '/search.json',
-        payload_type = 'search_result', payload_list = True,
-        allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user', 'max_id', 'since', 'until', 'result_type']
-    )
-    search.pagination_mode = 'page'
-
-    """ trends """
-    trends = bind_api(
-        path = '/trends.json',
-        payload_type = 'json'
-    )
-
-    """ trends/current """
-    trends_current = bind_api(
-        path = '/trends/current.json',
-        payload_type = 'json',
-        allowed_param = ['exclude']
-    )
-
-    """ trends/daily """
-    trends_daily = bind_api(
-        path = '/trends/daily.json',
-        payload_type = 'json',
-        allowed_param = ['date', 'exclude']
-    )
-
-    """ trends/weekly """
-    trends_weekly = bind_api(
-        path = '/trends/weekly.json',
-        payload_type = 'json',
-        allowed_param = ['date', 'exclude']
-    )
-
-    """ geo/reverse_geocode """
-    reverse_geocode = bind_api(
-        path = '/geo/reverse_geocode.json',
-        payload_type = 'json',
-        allowed_param = ['lat', 'long', 'accuracy', 'granularity', 'max_results']
-    )
-
-    """ geo/nearby_places """
-    nearby_places = bind_api(
-        path = '/geo/nearby_places.json',
-        payload_type = 'json',
-        allowed_param = ['lat', 'long', 'ip', 'accuracy', 'granularity', 'max_results']
-    )
-
-    """ geo/id """
-    geo_id = bind_api(
-        path = '/geo/id/{id}.json',
-        payload_type = 'json',
-        allowed_param = ['id']
-    )
-
-    """ Internal use only """
-    @staticmethod
-    def _pack_image(filename, max_size):
-        """Pack image from file into multipart-formdata post body"""
-        # image must be less than 700kb in size
-        try:
-            if os.path.getsize(filename) > (max_size * 1024):
-                raise TweepError('File is too big, must be less than 700kb.')
-        except os.error, e:
-            raise TweepError('Unable to access file')
-
-        # image must be gif, jpeg, or png
-        file_type = mimetypes.guess_type(filename)
-        if file_type is None:
-            raise TweepError('Could not determine file type')
-        file_type = file_type[0]
-        if file_type not in ['image/gif', 'image/jpeg', 'image/png']:
-            raise TweepError('Invalid file type for image: %s' % file_type)
-
-        # build the mulitpart-formdata body
-        fp = open(filename, 'rb')
-        BOUNDARY = 'Tw3ePy'
-        body = []
-        body.append('--' + BOUNDARY)
-        body.append('Content-Disposition: form-data; name="image"; filename="%s"' % filename)
-        body.append('Content-Type: %s' % file_type)
-        body.append('')
-        body.append(fp.read())
-        body.append('--' + BOUNDARY + '--')
-        body.append('')
-        fp.close()
-        body = '\r\n'.join(body)
-
-        # build headers
-        headers = {
-            'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
-            'Content-Length': len(body)
-        }
-
-        return headers, body
-

tweepy/auth.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-from urllib2 import Request, urlopen
-import base64
-
-from tweepy import oauth
-from tweepy.error import TweepError
-from tweepy.api import API
-
-
-class AuthHandler(object):
-
-    def apply_auth(self, url, method, headers, parameters):
-        """Apply authentication headers to request"""
-        raise NotImplementedError
-
-    def get_username(self):
-        """Return the username of the authenticated user"""
-        raise NotImplementedError
-
-
-class BasicAuthHandler(AuthHandler):
-
-    def __init__(self, username, password):
-        self.username = username
-        self._b64up = base64.b64encode('%s:%s' % (username, password))
-
-    def apply_auth(self, url, method, headers, parameters):
-        headers['Authorization'] = 'Basic %s' % self._b64up
-
-    def get_username(self):
-        return self.username
-
-
-class OAuthHandler(AuthHandler):
-    """OAuth authentication handler"""
-
-    OAUTH_HOST = 'api.twitter.com'
-    OAUTH_ROOT = '/oauth/'
-
-    def __init__(self, consumer_key, consumer_secret, callback=None, secure=False):
-        self._consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
-        self._sigmethod = oauth.OAuthSignatureMethod_HMAC_SHA1()
-        self.request_token = None
-        self.access_token = None
-        self.callback = callback
-        self.username = None
-        self.secure = secure
-
-    def _get_oauth_url(self, endpoint, secure=False):
-        if self.secure or secure:
-            prefix = 'https://'
-        else:
-            prefix = 'http://'
-
-        return prefix + self.OAUTH_HOST + self.OAUTH_ROOT + endpoint
-
-    def apply_auth(self, url, method, headers, parameters):
-        request = oauth.OAuthRequest.from_consumer_and_token(
-            self._consumer, http_url=url, http_method=method,
-            token=self.access_token, parameters=parameters
-        )
-        request.sign_request(self._sigmethod, self._consumer, self.access_token)
-        headers.update(request.to_header())
-
-    def _get_request_token(self):
-        try:
-            url = self._get_oauth_url('request_token')
-            request = oauth.OAuthRequest.from_consumer_and_token(
-                self._consumer, http_url=url, callback=self.callback
-            )
-            request.sign_request(self._sigmethod, self._consumer, None)
-            resp = urlopen(Request(url, headers=request.to_header()))
-            return oauth.OAuthToken.from_string(resp.read())
-        except Exception, e:
-            raise TweepError(e)
-
-    def set_request_token(self, key, secret):
-        self.request_token = oauth.OAuthToken(key, secret)
-
-    def set_access_token(self, key, secret):
-        self.access_token = oauth.OAuthToken(key, secret)
-
-    def get_authorization_url(self, signin_with_twitter=False):
-        """Get the authorization URL to redirect the user"""
-        try:
-            # get the request token
-            self.request_token = self._get_request_token()
-
-            # build auth request and return as url
-            if signin_with_twitter:
-                url = self._get_oauth_url('authenticate')
-            else:
-                url = self._get_oauth_url('authorize')
-            request = oauth.OAuthRequest.from_token_and_callback(
-                token=self.request_token, http_url=url
-            )
-
-            return request.to_url()
-        except Exception, e:
-            raise TweepError(e)
-
-    def get_access_token(self, verifier=None):
-        """
-        After user has authorized the request token, get access token
-        with user supplied verifier.
-        """
-        try:
-            url = self._get_oauth_url('access_token')
-
-            # build request
-            request = oauth.OAuthRequest.from_consumer_and_token(
-                self._consumer,
-                token=self.request_token, http_url=url,
-                verifier=str(verifier)
-            )
-            request.sign_request(self._sigmethod, self._consumer, self.request_token)
-
-            # send request
-            resp = urlopen(Request(url, headers=request.to_header()))
-            self.access_token = oauth.OAuthToken.from_string(resp.read())
-            return self.access_token
-        except Exception, e:
-            raise TweepError(e)
-
-    def get_xauth_access_token(self, username, password):
-        """
-        Get an access token from an username and password combination.
-        In order to get this working you need to create an app at
-        http://twitter.com/apps, after that send a mail to api@twitter.com
-        and request activation of xAuth for it.
-        """
-        try:
-            url = self._get_oauth_url('access_token', secure=True) # must use HTTPS
-            request = oauth.OAuthRequest.from_consumer_and_token(
-                oauth_consumer=self._consumer,
-                http_method='POST', http_url=url,
-                parameters = {
-		            'x_auth_mode': 'client_auth',
-		            'x_auth_username': username,
-		            'x_auth_password': password
-                }
-            )
-            request.sign_request(self._sigmethod, self._consumer, None)
-
-            resp = urlopen(Request(url, data=request.to_postdata()))
-            self.access_token = oauth.OAuthToken.from_string(resp.read())
-            return self.access_token
-        except Exception, e:
-            raise TweepError(e)
-
-    def get_username(self):
-        if self.username is None:
-            api = API(self)
-            user = api.verify_credentials()
-            if user:
-                self.username = user.screen_name
-            else:
-                raise TweepError("Unable to get username, invalid oauth token!")
-        return self.username
-

tweepy/binder.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-import httplib
-import urllib
-import time
-import re
-
-from tweepy.error import TweepError
-from tweepy.utils import convert_to_utf8_str
-
-re_path_template = re.compile('{\w+}')
-
-
-def bind_api(**config):
-
-    class APIMethod(object):
-
-        path = config['path']
-        payload_type = config.get('payload_type', None)
-        payload_list = config.get('payload_list', False)
-        allowed_param = config.get('allowed_param', [])
-        method = config.get('method', 'GET')
-        require_auth = config.get('require_auth', False)
-        search_api = config.get('search_api', False)
-
-        def __init__(self, api, args, kargs):
-            # If authentication is required and no credentials
-            # are provided, throw an error.
-            if self.require_auth and not api.auth:
-                raise TweepError('Authentication required!')
-
-            self.api = api
-            self.post_data = kargs.pop('post_data', None)
-            self.retry_count = kargs.pop('retry_count', api.retry_count)
-            self.retry_delay = kargs.pop('retry_delay', api.retry_delay)
-            self.retry_errors = kargs.pop('retry_errors', api.retry_errors)
-            self.headers = kargs.pop('headers', {})
-            self.build_parameters(args, kargs)
-
-            # Pick correct URL root to use
-            if self.search_api:
-                self.api_root = api.search_root
-            else:
-                self.api_root = api.api_root
-
-            # Perform any path variable substitution
-            self.build_path()
-
-            if api.secure:
-                self.scheme = 'https://'
-            else:
-                self.scheme = 'http://'
-
-            if self.search_api:
-                self.host = api.search_host
-            else:
-                self.host = api.host
-
-            # Manually set Host header to fix an issue in python 2.5
-            # or older where Host is set including the 443 port.
-            # This causes Twitter to issue 301 redirect.
-            # See Issue http://github.com/joshthecoder/tweepy/issues/#issue/12
-            self.headers['Host'] = self.host
-
-        def build_parameters(self, args, kargs):
-            self.parameters = {}
-            for idx, arg in enumerate(args):
-                if arg is None:
-                    continue
-
-                try:
-                    self.parameters[self.allowed_param[idx]] = convert_to_utf8_str(arg)
-                except IndexError:
-                    raise TweepError('Too many parameters supplied!')
-
-            for k, arg in kargs.items():
-                if arg is None:
-                    continue
-                if k in self.parameters:
-                    raise TweepError('Multiple values for parameter %s supplied!' % k)
-
-                self.parameters[k] = convert_to_utf8_str(arg)
-
-        def build_path(self):
-            for variable in re_path_template.findall(self.path):
-                name = variable.strip('{}')
-
-                if name == 'user' and 'user' not in self.parameters and self.api.auth:
-                    # No 'user' parameter provided, fetch it from Auth instead.
-                    value = self.api.auth.get_username()
-                else:
-                    try:
-                        value = urllib.quote(self.parameters[name])
-                    except KeyError:
-                        raise TweepError('No parameter value found for path variable: %s' % name)
-                    del self.parameters[name]
-
-                self.path = self.path.replace(variable, value)
-
-        def execute(self):
-            # Build the request URL
-            url = self.api_root + self.path
-            if len(self.parameters):
-                url = '%s?%s' % (url, urllib.urlencode(self.parameters))
-
-            # Query the cache if one is available
-            # and this request uses a GET method.
-            if self.api.cache and self.method == 'GET':
-                cache_result = self.api.cache.get(url)
-                # if cache result found and not expired, return it
-                if cache_result:
-                    # must restore api reference
-                    if isinstance(cache_result, list):
-                        for result in cache_result:
-                            result._api = self.api
-                    else:
-                        cache_result._api = self.api
-                    return cache_result
-
-            # Continue attempting request until successful
-            # or maximum number of retries is reached.
-            retries_performed = 0
-            while retries_performed < self.retry_count + 1:
-                # Open connection
-                # FIXME: add timeout
-                if self.api.secure:
-                    conn = httplib.HTTPSConnection(self.host)
-                else:
-                    conn = httplib.HTTPConnection(self.host)
-
-                # Apply authentication
-                if self.api.auth:
-                    self.api.auth.apply_auth(
-                            self.scheme + self.host + url,
-                            self.method, self.headers, self.parameters
-                    )
-
-                # Execute request
-                try:
-                    conn.request(self.method, url, headers=self.headers, body=self.post_data)
-                    resp = conn.getresponse()
-                except Exception, e:
-                    raise TweepError('Failed to send request: %s' % e)
-
-                # Exit request loop if non-retry error code
-                if self.retry_errors:
-                    if resp.status not in self.retry_errors: break
-                else:
-                    if resp.status == 200: break
-
-                # Sleep before retrying request again
-                time.sleep(self.retry_delay)
-                retries_performed += 1
-
-            # If an error was returned, throw an exception
-            self.api.last_response = resp
-            if resp.status != 200:
-                try:
-                    error_msg = self.api.parser.parse_error(resp.read())
-                except Exception:
-                    error_msg = "Twitter error response: status code = %s" % resp.status
-                raise TweepError(error_msg, resp)
-
-            # Parse the response payload
-            result = self.api.parser.parse(self, resp.read())
-
-            conn.close()
-
-            # Store result into cache if one is available.
-            if self.api.cache and self.method == 'GET' and result:
-                self.api.cache.store(url, result)
-
-            return result
-
-
-    def _call(api, *args, **kargs):
-
-        method = APIMethod(api, args, kargs)
-        return method.execute()
-
-
-    # Set pagination mode
-    if 'cursor' in APIMethod.allowed_param:
-        _call.pagination_mode = 'cursor'
-    elif 'page' in APIMethod.allowed_param:
-        _call.pagination_mode = 'page'
-
-    return _call
-

tweepy/cache.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-import time
-import threading
-import os
-import cPickle as pickle
-
-try:
-    import hashlib
-except ImportError:
-    # python 2.4
-    import md5 as hashlib
-
-try:
-    import fcntl
-except ImportError:
-    # Probably on a windows system
-    # TODO: use win32file
-    pass
-
-
-class Cache(object):
-    """Cache interface"""
-
-    def __init__(self, timeout=60):
-        """Initialize the cache
-            timeout: number of seconds to keep a cached entry
-        """
-        self.timeout = timeout
-
-    def store(self, key, value):
-        """Add new record to cache
-            key: entry key
-            value: data of entry
-        """
-        raise NotImplementedError
-
-    def get(self, key, timeout=None):
-        """Get cached entry if exists and not expired
-            key: which entry to get
-            timeout: override timeout with this value [optional]
-        """
-        raise NotImplementedError
-
-    def count(self):
-        """Get count of entries currently stored in cache"""
-        raise NotImplementedError
-
-    def cleanup(self):
-        """Delete any expired entries in cache."""
-        raise NotImplementedError
-
-    def flush(self):
-        """Delete all cached entries"""
-        raise NotImplementedError
-
-
-class MemoryCache(Cache):
-    """In-memory cache"""
-
-    def __init__(self, timeout=60):
-        Cache.__init__(self, timeout)
-        self._entries = {}
-        self.lock = threading.Lock()
-
-    def __getstate__(self):
-        # pickle
-        return {'entries': self._entries, 'timeout': self.timeout}
-
-    def __setstate__(self, state):
-        # unpickle
-        self.lock = threading.Lock()
-        self._entries = state['entries']
-        self.timeout = state['timeout']
-
-    def _is_expired(self, entry, timeout):
-        return timeout > 0 and (time.time() - entry[0]) >= timeout
-
-    def store(self, key, value):
-        self.lock.acquire()
-        self._entries[key] = (time.time(), value)
-        self.lock.release()
-
-    def get(self, key, timeout=None):
-        self.lock.acquire()
-        try:
-            # check to see if we have this key
-            entry = self._entries.get(key)
-            if not entry:
-                # no hit, return nothing
-                return None
-
-            # use provided timeout in arguments if provided
-            # otherwise use the one provided during init.
-            if timeout is None:
-                timeout = self.timeout
-
-            # make sure entry is not expired
-            if self._is_expired(entry, timeout):
-                # entry expired, delete and return nothing
-                del self._entries[key]
-                return None
-
-            # entry found and not expired, return it
-            return entry[1]
-        finally:
-            self.lock.release()
-
-    def count(self):
-        return len(self._entries)
-
-    def cleanup(self):
-        self.lock.acquire()
-        try:
-            for k, v in self._entries.items():
-                if self._is_expired(v, self.timeout):
-                    del self._entries[k]
-        finally:
-            self.lock.release()
-
-    def flush(self):
-        self.lock.acquire()
-        self._entries.clear()
-        self.lock.release()
-
-
-class FileCache(Cache):
-    """File-based cache"""
-
-    # locks used to make cache thread-safe
-    cache_locks = {}
-
-    def __init__(self, cache_dir, timeout=60):
-        Cache.__init__(self, timeout)
-        if os.path.exists(cache_dir) is False:
-            os.mkdir(cache_dir)
-        self.cache_dir = cache_dir
-        if cache_dir in FileCache.cache_locks:
-            self.lock = FileCache.cache_locks[cache_dir]
-        else:
-            self.lock = threading.Lock()
-            FileCache.cache_locks[cache_dir] = self.lock
-
-        if os.name == 'posix':
-            self._lock_file = self._lock_file_posix
-            self._unlock_file = self._unlock_file_posix
-        elif os.name == 'nt':
-            self._lock_file = self._lock_file_win32
-            self._unlock_file = self._unlock_file_win32
-        else:
-            print 'Warning! FileCache locking not supported on this system!'
-            self._lock_file = self._lock_file_dummy
-            self._unlock_file = self._unlock_file_dummy
-
-    def _get_path(self, key):
-        md5 = hashlib.md5()
-        md5.update(key)
-        return os.path.join(self.cache_dir, md5.hexdigest())
-
-    def _lock_file_dummy(self, path, exclusive=True):
-        return None
-
-    def _unlock_file_dummy(self, lock):
-        return
-
-    def _lock_file_posix(self, path, exclusive=True):
-        lock_path = path + '.lock'
-        if exclusive is True:
-            f_lock = open(lock_path, 'w')
-            fcntl.lockf(f_lock, fcntl.LOCK_EX)
-        else:
-            f_lock = open(lock_path, 'r')
-            fcntl.lockf(f_lock, fcntl.LOCK_SH)
-        if os.path.exists(lock_path) is False:
-            f_lock.close()
-            return None
-        return f_lock
-
-    def _unlock_file_posix(self, lock):
-        lock.close()
-
-    def _lock_file_win32(self, path, exclusive=True):
-        # TODO: implement
-        return None
-
-    def _unlock_file_win32(self, lock):
-        # TODO: implement
-        return
-
-    def _delete_file(self, path):
-        os.remove(path)
-        if os.path.exists(path + '.lock'):
-            os.remove(path + '.lock')
-
-    def store(self, key, value):
-        path = self._get_path(key)
-        self.lock.acquire()
-        try:
-            # acquire lock and open file
-            f_lock = self._lock_file(path)
-            datafile = open(path, 'wb')
-
-            # write data
-            pickle.dump((time.time(), value), datafile)
-
-            # close and unlock file
-            datafile.close()
-            self._unlock_file(f_lock)
-        finally:
-            self.lock.release()
-
-    def get(self, key, timeout=None):
-        return self._get(self._get_path(key), timeout)
-
-    def _get(self, path, timeout):
-        if os.path.exists(path) is False:
-            # no record
-            return None
-        self.lock.acquire()
-        try:
-            # acquire lock and open
-            f_lock = self._lock_file(path, False)
-            datafile = open(path, 'rb')
-
-            # read pickled object
-            created_time, value = pickle.load(datafile)
-            datafile.close()
-
-            # check if value is expired
-            if timeout is None:
-                timeout = self.timeout
-            if timeout > 0 and (time.time() - created_time) >= timeout:
-                # expired! delete from cache
-                value = None
-                self._delete_file(path)
-
-            # unlock and return result
-            self._unlock_file(f_lock)
-            return value
-        finally:
-            self.lock.release()
-
-    def count(self):
-        c = 0
-        for entry in os.listdir(self.cache_dir):
-            if entry.endswith('.lock'):
-                continue
-            c += 1
-        return c
-
-    def cleanup(self):
-        for entry in os.listdir(self.cache_dir):
-            if entry.endswith('.lock'):
-                continue
-            self._get(os.path.join(self.cache_dir, entry), None)
-
-    def flush(self):
-        for entry in os.listdir(self.cache_dir):
-            if entry.endswith('.lock'):
-                continue
-            self._delete_file(os.path.join(self.cache_dir, entry))
-
-class MemCacheCache(Cache):
-    """Cache interface"""
-
-    def __init__(self, client, timeout=60):
-        """Initialize the cache
-            client: The memcache client
-            timeout: number of seconds to keep a cached entry
-        """
-        self.client = client
-        self.timeout = timeout
-
-    def store(self, key, value):
-        """Add new record to cache
-            key: entry key
-            value: data of entry
-        """
-        self.client.set(key, value, time=self.timeout)
-
-    def get(self, key, timeout=None):
-        """Get cached entry if exists and not expired
-            key: which entry to get
-            timeout: override timeout with this value [optional]. DOES NOT WORK HERE
-        """
-        return self.client.get(key, key)
-
-    def count(self):
-        """Get count of entries currently stored in cache. RETURN 0"""
-        return 0
-
-    def cleanup(self):
-        """Delete any expired entries in cache. NO-OP"""
-        pass
-
-    def flush(self):
-        """Delete all cached entries. NO-OP"""
-        pass

tweepy/cursor.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-from tweepy.error import TweepError
-
-class Cursor(object):
-    """Pagination helper class"""
-
-    def __init__(self, method, *args, **kargs):
-        if hasattr(method, 'pagination_mode'):
-            if method.pagination_mode == 'cursor':
-                self.iterator = CursorIterator(method, args, kargs)
-            else:
-                self.iterator = PageIterator(method, args, kargs)
-        else:
-            raise TweepError('This method does not perform pagination')
-
-    def pages(self, limit=0):
-        """Return iterator for pages"""
-        if limit > 0:
-            self.iterator.limit = limit
-        return self.iterator
-
-    def items(self, limit=0):
-        """Return iterator for items in each page"""
-        i = ItemIterator(self.iterator)
-        i.limit = limit
-        return i
-
-class BaseIterator(object):
-
-    def __init__(self, method, args, kargs):
-        self.method = method
-        self.args = args
-        self.kargs = kargs
-        self.limit = 0
-
-    def next(self):
-        raise NotImplementedError
-
-    def prev(self):
-        raise NotImplementedError
-
-    def __iter__(self):
-        return self
-
-class CursorIterator(BaseIterator):
-
-    def __init__(self, method, args, kargs):
-        BaseIterator.__init__(self, method, args, kargs)
-        self.next_cursor = -1
-        self.prev_cursor = 0
-        self.count = 0
-
-    def next(self):
-        if self.next_cursor == 0 or (self.limit and self.count == self.limit):
-            raise StopIteration
-        data, cursors = self.method(
-                cursor=self.next_cursor, *self.args, **self.kargs
-        )
-        self.prev_cursor, self.next_cursor = cursors
-        if len(data) == 0:
-            raise StopIteration
-        self.count += 1
-        return data
-
-    def prev(self):
-        if self.prev_cursor == 0:
-            raise TweepError('Can not page back more, at first page')
-        data, self.next_cursor, self.prev_cursor = self.method(
-                cursor=self.prev_cursor, *self.args, **self.kargs
-        )
-        self.count -= 1
-        return data
-
-class PageIterator(BaseIterator):
-
-    def __init__(self, method, args, kargs):
-        BaseIterator.__init__(self, method, args, kargs)
-        self.current_page = 0
-
-    def next(self):
-        self.current_page += 1
-        items = self.method(page=self.current_page, *self.args, **self.kargs)
-        if len(items) == 0 or (self.limit > 0 and self.current_page > self.limit):
-            raise StopIteration
-        return items
-
-    def prev(self):
-        if (self.current_page == 1):
-            raise TweepError('Can not page back more, at first page')
-        self.current_page -= 1
-        return self.method(page=self.current_page, *self.args, **self.kargs)
-
-class ItemIterator(BaseIterator):
-
-    def __init__(self, page_iterator):
-        self.page_iterator = page_iterator
-        self.limit = 0
-        self.current_page = None
-        self.page_index = -1
-        self.count = 0
-
-    def next(self):
-        if self.limit > 0 and self.count == self.limit:
-            raise StopIteration
-        if self.current_page is None or self.page_index == len(self.current_page) - 1:
-            # Reached end of current page, get the next page...
-            self.current_page = self.page_iterator.next()
-            self.page_index = -1
-        self.page_index += 1
-        self.count += 1
-        return self.current_page[self.page_index]
-
-    def prev(self):
-        if self.current_page is None:
-            raise TweepError('Can not go back more, at first page')
-        if self.page_index == 0:
-            # At the beginning of the current page, move to next...
-            self.current_page = self.page_iterator.prev()
-            self.page_index = len(self.current_page)
-            if self.page_index == 0:
-                raise TweepError('No more items')
-        self.page_index -= 1
-        self.count -= 1
-        return self.current_page[self.page_index]
-

tweepy/error.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-class TweepError(Exception):
-    """Tweepy exception"""
-
-    def __init__(self, reason, response=None):
-        self.reason = str(reason)
-        self.response = response
-
-    def __str__(self):
-        return self.reason
-

tweepy/models.py

-# Tweepy
-# Copyright 2009-2010 Joshua Roesslein
-# See LICENSE for details.
-
-from tweepy.error import TweepError
-from tweepy.utils import parse_datetime, parse_html_value, parse_a_href, \
-        parse_search_datetime, unescape_html
-
-
-class ResultSet(list):
-    """A list like object that holds results from a Twitter API query."""
-
-
-class Model(object):
-
-    def __init__(self, api=None):
-        self._api = api
-
-    def __getstate__(self):
-        # pickle
-        pickle = dict(self.__dict__)
-        try:
-            del pickle['_api']  # do not pickle the API reference
-        except KeyError:
-            pass
-        return pickle
-
-    @classmethod
-    def parse(cls, api, json):
-        """Parse a JSON object into a model instance."""
-        raise NotImplementedError
-
-    @classmethod
-    def parse_list(cls, api, json_list):
-        """Parse a list of JSON objects into a result set of model instances."""
-        results = ResultSet()
-        for obj in json_list:
-            if obj:
-                results.append(cls.parse(api, obj))
-        return results
-
-
-class Status(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        status = cls(api)
-        for k, v in json.items():
-            if k == 'user':
-                user = User.parse(api, v)
-                setattr(status, 'author', user)
-                setattr(status, 'user', user)  # DEPRECIATED
-            elif k == 'created_at':
-                setattr(status, k, parse_datetime(v))
-            elif k == 'source':
-                if '<' in v:
-                    setattr(status, k, parse_html_value(v))
-                    setattr(status, 'source_url', parse_a_href(v))
-                else:
-                    setattr(status, k, v)
-                    setattr(status, 'source_url', None)
-            elif k == 'retweeted_status':
-                setattr(status, k, Status.parse(api, v))
-            else:
-                setattr(status, k, v)
-        return status
-
-    def destroy(self):
-        return self._api.destroy_status(self.id)
-
-    def retweet(self):
-        return self._api.retweet(self.id)
-
-    def retweets(self):
-        return self._api.retweets(self.id)
-
-    def favorite(self):
-        return self._api.create_favorite(self.id)
-
-
-class User(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        user = cls(api)
-        for k, v in json.items():
-            if k == 'created_at':
-                setattr(user, k, parse_datetime(v))
-            elif k == 'status':
-                setattr(user, k, Status.parse(api, v))
-            elif k == 'following':
-                # twitter sets this to null if it is false
-                if v is True:
-                    setattr(user, k, True)
-                else:
-                    setattr(user, k, False)
-            else:
-                setattr(user, k, v)
-        return user
-
-    @classmethod
-    def parse_list(cls, api, json_list):
-        if isinstance(json_list, list):
-            item_list = json_list
-        else:
-            item_list = json_list['users']
-
-        results = ResultSet()
-        for obj in item_list:
-            results.append(cls.parse(api, obj))
-        return results
-
-    def timeline(self, **kargs):
-        return self._api.user_timeline(user_id=self.id, **kargs)
-
-    def friends(self, **kargs):
-        return self._api.friends(user_id=self.id, **kargs)
-
-    def followers(self, **kargs):
-        return self._api.followers(user_id=self.id, **kargs)
-
-    def follow(self):
-        self._api.create_friendship(user_id=self.id)
-        self.following = True
-
-    def unfollow(self):
-        self._api.destroy_friendship(user_id=self.id)
-        self.following = False
-
-    def lists_memberships(self, *args, **kargs):
-        return self._api.lists_memberships(user=self.screen_name, *args, **kargs)
-
-    def lists_subscriptions(self, *args, **kargs):
-        return self._api.lists_subscriptions(user=self.screen_name, *args, **kargs)
-
-    def lists(self, *args, **kargs):
-        return self._api.lists(user=self.screen_name, *args, **kargs)
-
-    def followers_ids(self, *args, **kargs):
-        return self._api.followers_ids(user_id=self.id, *args, **kargs)
-
-
-class DirectMessage(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        dm = cls(api)
-        for k, v in json.items():
-            if k == 'sender' or k == 'recipient':
-                setattr(dm, k, User.parse(api, v))
-            elif k == 'created_at':
-                setattr(dm, k, parse_datetime(v))
-            else:
-                setattr(dm, k, v)
-        return dm
-
-    def destroy(self):
-        return self._api.destroy_direct_message(self.id)
-
-
-class Friendship(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        relationship = json['relationship']
-
-        # parse source
-        source = cls(api)
-        for k, v in relationship['source'].items():
-            setattr(source, k, v)
-
-        # parse target
-        target = cls(api)
-        for k, v in relationship['target'].items():
-            setattr(target, k, v)
-
-        return source, target
-
-
-class SavedSearch(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        ss = cls(api)
-        for k, v in json.items():
-            if k == 'created_at':
-                setattr(ss, k, parse_datetime(v))
-            else:
-                setattr(ss, k, v)
-        return ss
-
-    def destroy(self):
-        return self._api.destroy_saved_search(self.id)
-
-
-class SearchResult(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        result = cls()
-        for k, v in json.items():
-            if k == 'created_at':
-                setattr(result, k, parse_search_datetime(v))
-            elif k == 'source':
-                setattr(result, k, parse_html_value(unescape_html(v)))
-            else:
-                setattr(result, k, v)
-        return result
-
-    @classmethod
-    def parse_list(cls, api, json_list, result_set=None):
-        results = ResultSet()
-        results.max_id = json_list.get('max_id')
-        results.since_id = json_list.get('since_id')
-        results.refresh_url = json_list.get('refresh_url')
-        results.next_page = json_list.get('next_page')
-        results.results_per_page = json_list.get('results_per_page')
-        results.page = json_list.get('page')
-        results.completed_in = json_list.get('completed_in')
-        results.query = json_list.get('query')
-
-        for obj in json_list['results']:
-            results.append(cls.parse(api, obj))
-        return results
-
-
-class List(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        lst = List(api)
-        for k,v in json.items():
-            if k == 'user':
-                setattr(lst, k, User.parse(api, v))
-            else:
-                setattr(lst, k, v)
-        return lst
-
-    @classmethod
-    def parse_list(cls, api, json_list, result_set=None):
-        results = ResultSet()
-        for obj in json_list['lists']:
-            results.append(cls.parse(api, obj))
-        return results
-
-    def update(self, **kargs):
-        return self._api.update_list(self.slug, **kargs)
-
-    def destroy(self):
-        return self._api.destroy_list(self.slug)
-
-    def timeline(self, **kargs):
-        return self._api.list_timeline(self.user.screen_name, self.slug, **kargs)
-
-    def add_member(self, id):
-        return self._api.add_list_member(self.slug, id)
-
-    def remove_member(self, id):
-        return self._api.remove_list_member(self.slug, id)
-
-    def members(self, **kargs):
-        return self._api.list_members(self.user.screen_name, self.slug, **kargs)
-
-    def is_member(self, id):
-        return self._api.is_list_member(self.user.screen_name, self.slug, id)
-
-    def subscribe(self):
-        return self._api.subscribe_list(self.user.screen_name, self.slug)
-
-    def unsubscribe(self):
-        return self._api.unsubscribe_list(self.user.screen_name, self.slug)
-
-    def subscribers(self, **kargs):
-        return self._api.list_subscribers(self.user.screen_name, self.slug, **kargs)
-
-    def is_subscribed(self, id):
-        return self._api.is_subscribed_list(self.user.screen_name, self.slug, id)
-
-
-class JSONModel(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        return json
-
-
-class IDModel(Model):
-
-    @classmethod
-    def parse(cls, api, json):
-        if isinstance(json, list):
-            return json
-        else:
-            return json['ids']
-
-
-class ModelFactory(object):
-    """
-    Used by parsers for creating instances
-    of models. You may subclass this factory
-    to add your own extended models.
-    """
-
-    status = Status
-    user = User
-    direct_message = DirectMessage
-    friendship = Friendship
-    saved_search = SavedSearch
-    search_result = SearchResult
-    list = List
-
-    json = JSONModel
-    ids = IDModel
-

tweepy/oauth.py

-"""
-The MIT License
-
-Copyright (c) 2007 Leah Culver
-
-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.
-"""
-
-import cgi
-import urllib
-import time
-import random
-import urlparse
-import hmac
-import binascii
-
-
-VERSION = '1.0' # Hi Blaine!
-HTTP_METHOD = 'GET'
-SIGNATURE_METHOD = 'PLAINTEXT'
-
-
-class OAuthError(RuntimeError):
-    """Generic exception class."""
-    def __init__(self, message='OAuth error occured.'):
-        self.message = message
-
-def build_authenticate_header(realm=''):
-    """Optional WWW-Authenticate header (401 error)"""
-    return {'WWW-Authenticate': 'OAuth realm="%s"' % realm}
-
-def escape(s):
-    """Escape a URL including any /."""
-    return urllib.quote(s, safe='~')
-
-def _utf8_str(s):
-    """Convert unicode to utf-8."""
-    if isinstance(s, unicode):
-        return s.encode("utf-8")
-    else:
-        return str(s)
-
-def generate_timestamp():
-    """Get seconds since epoch (UTC)."""
-    return int(time.time())
-
-def generate_nonce(length=8):
-    """Generate pseudorandom number."""
-    return ''.join([str(random.randint(0, 9)) for i in range(length)])
-
-def generate_verifier(length=8):
-    """Generate pseudorandom number."""
-    return ''.join([str(random.randint(0, 9)) for i in range(length)])
-
-
-class OAuthConsumer(object):
-    """Consumer of OAuth authentication.
-
-    OAuthConsumer is a data type that represents the identity of the Consumer
-    via its shared secret with the Service Provider.
-
-    """
-    key = None
-    secret = None
-
-    def __init__(self, key, secret):
-        self.key = key
-        self.secret = secret
-
-
-class OAuthToken(object):
-    """OAuthToken is a data type that represents an End User via either an access
-    or request token.
-    
-    key -- the token
-    secret -- the token secret
-
-    """
-    key = None
-    secret = None
-    callback = None
-    callback_confirmed = None
-    verifier = None
-
-    def __init__(self, key, secret):
-        self.key = key
-        self.secret = secret
-
-    def set_callback(self, callback):
-        self.callback = callback
-        self.callback_confirmed = 'true'
-
-    def set_verifier(self, verifier=None):
-        if verifier is not None:
-            self.verifier = verifier
-        else:
-            self.verifier = generate_verifier()
-
-    def get_callback_url(self):
-        if self.callback and self.verifier:
-            # Append the oauth_verifier.
-            parts = urlparse.urlparse(self.callback)
-            scheme, netloc, path, params, query, fragment = parts[:6]
-            if query:
-                query = '%s&oauth_verifier=%s' % (query, self.verifier)
-            else:
-                query = 'oauth_verifier=%s' % self.verifier
-            return urlparse.urlunparse((scheme, netloc, path, params,
-                query, fragment))
-        return self.callback
-
-    def to_string(self):
-        data = {
-            'oauth_token': self.key,
-            'oauth_token_secret': self.secret,
-        }
-        if self.callback_confirmed is not None:
-            data['oauth_callback_confirmed'] = self.callback_confirmed
-        return urllib.urlencode(data)
- 
-    def from_string(s):
-        """ Returns a token from something like:
-        oauth_token_secret=xxx&oauth_token=xxx
-        """
-        params = cgi.parse_qs(s, keep_blank_values=False)
-        key = params['oauth_token'][0]
-        secret = params['oauth_token_secret'][0]
-        token = OAuthToken(key, secret)
-        try:
-            token.callback_confirmed = params['oauth_callback_confirmed'][0]
-        except KeyError:
-            pass # 1.0, no callback confirmed.
-        return token
-    from_string = staticmethod(from_string)
-
-    def __str__(self):
-        return self.to_string()
-
-
-class OAuthRequest(object):
-    """OAuthRequest represents the request and can be serialized.
-
-    OAuth parameters:
-        - oauth_consumer_key 
-        - oauth_token
-        - oauth_signature_method
-        - oauth_signature 
-        - oauth_timestamp 
-        - oauth_nonce
-        - oauth_version
-        - oauth_verifier
-        ... any additional parameters, as defined by the Service Provider.
-    """
-    parameters = None # OAuth parameters.
-    http_method = HTTP_METHOD
-    http_url = None
-    version = VERSION
-
-    def __init__(self, http_method=HTTP_METHOD, http_url=None, parameters=None):
-        self.http_method = http_method
-        self.http_url = http_url
-        self.parameters = parameters or {}
-
-    def set_parameter(self, parameter, value):
-        self.parameters[parameter] = value
-
-    def get_parameter(self, parameter):
-        try:
-            return self.parameters[parameter]
-        except:
-            raise OAuthError('Parameter not found: %s' % parameter)
-
-    def _get_timestamp_nonce(self):
-        return self.get_parameter('oauth_timestamp'), self.get_parameter(
-            'oauth_nonce')
-
-    def get_nonoauth_parameters(self):