Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Zahra Rajabi
tweepy
Commits
dcce34d9
Commit
dcce34d9
authored
Nov 06, 2009
by
Josh Roesslein
Browse files
Use versioned API and the api.twitter.com domain.
parent
f569633d
Changes
5
Hide whitespace changes
Inline
Side-by-side
CHANGES
View file @
dcce34d9
...
@@ -12,6 +12,7 @@ during upgrade will be listed here.
...
@@ -12,6 +12,7 @@ during upgrade will be listed here.
+ Removed 'email' parameter from API.update_profile. No longer supported.
+ Removed 'email' parameter from API.update_profile. No longer supported.
+ API.auth_handler -> API.auth
+ API.auth_handler -> API.auth
+ Moved memcache implementation to tweepy-more repository.
+ Moved memcache implementation to tweepy-more repository.
+ Tweepy now uses the versioned API and the new api.twitter.com subdomain
1.1 -> 1.2 [Current]
1.1 -> 1.2 [Current]
=====================
=====================
...
...
tests.py
View file @
dcce34d9
...
@@ -108,9 +108,9 @@ class TweepyAPITests(unittest.TestCase):
...
@@ -108,9 +108,9 @@ class TweepyAPITests(unittest.TestCase):
self
.
api
.
followers_ids
(
username
)
self
.
api
.
followers_ids
(
username
)
def
testverifycredentials
(
self
):
def
testverifycredentials
(
self
):
self
.
assertEqual
(
self
.
api
.
verify_credentials
(),
Tru
e
)
self
.
assert
Not
Equal
(
self
.
api
.
verify_credentials
(),
Fals
e
)
api
=
API
.
new
(
'basic'
,
'bad'
,
'password'
)
api
=
API
(
BasicAuthHandler
(
'bad'
,
'password'
)
)
self
.
assertEqual
(
api
.
verify_credentials
(),
False
)
self
.
assertEqual
(
api
.
verify_credentials
(),
False
)
def
testratelimitstatus
(
self
):
def
testratelimitstatus
(
self
):
...
@@ -139,24 +139,25 @@ class TweepyAPITests(unittest.TestCase):
...
@@ -139,24 +139,25 @@ class TweepyAPITests(unittest.TestCase):
self
.
assertEqual
(
updated
.
profile_sidebar_fill_color
,
'000'
)
self
.
assertEqual
(
updated
.
profile_sidebar_fill_color
,
'000'
)
self
.
assertEqual
(
updated
.
profile_sidebar_border_color
,
'000'
)
self
.
assertEqual
(
updated
.
profile_sidebar_border_color
,
'000'
)
"""
def testupateprofileimage(self):
def testupateprofileimage(self):
self.api.update_profile_image('examples/profile.png')
self.api.update_profile_image('examples/profile.png')
def testupdateprofilebg(self):
def testupdateprofilebg(self):
self.api.update_profile_background_image('examples/bg.png')
self.api.update_profile_background_image('examples/bg.png')
"""
def
testupdateprofile
(
self
):
def
testupdateprofile
(
self
):
original
=
self
.
api
.
me
()
original
=
self
.
api
.
me
()
profile
=
{
profile
=
{
'name'
:
'Tweepy test 123'
,
'name'
:
'Tweepy test 123'
,
'email'
:
'test@example.com'
,
'url'
:
'http://www.example.com'
,
'url'
:
'http://www.example.com'
,
'location'
:
'pytopia'
,
'location'
:
'pytopia'
,
'description'
:
'just testing things out'
'description'
:
'just testing things out'
}
}
updated
=
self
.
api
.
update_profile
(
**
profile
)
updated
=
self
.
api
.
update_profile
(
**
profile
)
self
.
api
.
update_profile
(
self
.
api
.
update_profile
(
name
=
original
.
name
,
email
=
'hi@example.com'
,
url
=
original
.
url
,
name
=
original
.
name
,
url
=
original
.
url
,
location
=
original
.
location
,
description
=
original
.
description
location
=
original
.
location
,
description
=
original
.
description
)
)
...
@@ -248,7 +249,7 @@ class TweepyAuthTests(unittest.TestCase):
...
@@ -248,7 +249,7 @@ class TweepyAuthTests(unittest.TestCase):
# test getting access token
# test getting access token
auth_url
=
auth
.
get_authorization_url
()
auth_url
=
auth
.
get_authorization_url
()
self
.
assert_
(
auth_url
.
startswith
(
'http://twitter.com/oauth/authorize?'
))
self
.
assert_
(
auth_url
.
startswith
(
'http://
api.
twitter.com/oauth/authorize?'
))
print
'Please authorize: '
+
auth_url
print
'Please authorize: '
+
auth_url
verifier
=
raw_input
(
'PIN: '
).
strip
()
verifier
=
raw_input
(
'PIN: '
).
strip
()
self
.
assert_
(
len
(
verifier
)
>
0
)
self
.
assert_
(
len
(
verifier
)
>
0
)
...
...
tweepy/api.py
View file @
dcce34d9
...
@@ -13,13 +13,16 @@ from tweepy.parsers import *
...
@@ -13,13 +13,16 @@ from tweepy.parsers import *
class
API
(
object
):
class
API
(
object
):
"""Twitter API"""
"""Twitter API"""
def
__init__
(
self
,
auth_handler
=
None
,
host
=
'twitter.com'
,
cache
=
None
,
def
__init__
(
self
,
auth_handler
=
None
,
secure
=
False
,
api_root
=
''
,
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
):
retry_count
=
0
,
retry_delay
=
0
,
retry_errors
=
None
):
# you may access these freely
# you may access these freely
self
.
auth
=
auth_handler
self
.
auth
=
auth_handler
self
.
host
=
host
self
.
host
=
host
self
.
search_host
=
search_host
self
.
api_root
=
api_root
self
.
api_root
=
api_root
self
.
search_root
=
search_root
self
.
cache
=
cache
self
.
cache
=
cache
self
.
secure
=
secure
self
.
secure
=
secure
self
.
retry_count
=
retry_count
self
.
retry_count
=
retry_count
...
@@ -572,53 +575,45 @@ class API(object):
...
@@ -572,53 +575,45 @@ class API(object):
return
False
return
False
""" search """
""" search """
search
=
bind_api
(
def
search
(
self
,
*
args
,
**
kargs
):
search_api
=
True
,
return
bind_api
(
path
=
'/search.json'
,
host
=
'search.'
+
self
.
host
,
parser
=
parse_search_results
,
path
=
'/search.json'
,
allowed_param
=
[
'q'
,
'lang'
,
'locale'
,
'rpp'
,
'page'
,
'since_id'
,
'geocode'
,
'show_user'
]
parser
=
parse_search_results
,
)
allowed_param
=
[
'q'
,
'lang'
,
'locale'
,
'rpp'
,
'page'
,
'since_id'
,
'geocode'
,
'show_user'
],
)(
self
,
*
args
,
**
kargs
)
search
.
pagination_mode
=
'page'
""" trends """
""" trends """
def
trends
(
self
):
trends
=
bind_api
(
return
bind_api
(
search_api
=
True
,
host
=
'search.'
+
self
.
host
,
path
=
'/trends.json'
,
path
=
'/trends.json'
,
parser
=
parse_json
parser
=
parse_json
)
)(
self
)
""" trends/current """
""" trends/current """
def
trends_current
(
self
,
*
args
,
**
kargs
):
trends_current
=
bind_api
(
return
bind_api
(
search_api
=
True
,
host
=
'search.'
+
self
.
host
,
path
=
'/trends/current.json'
,
path
=
'/trends/current.json'
,
parser
=
parse_json
,
parser
=
parse_json
,
allowed_param
=
[
'exclude'
]
allowed_param
=
[
'exclude'
]
)
)(
self
,
*
args
,
**
kargs
)
""" trends/daily """
""" trends/daily """
def
trends_daily
(
self
,
*
args
,
**
kargs
):
trends_daily
=
bind_api
(
return
bind_api
(
search_api
=
True
,
host
=
"search."
+
self
.
host
,
path
=
'/trends/daily.json'
,
path
=
'/trends/daily.json'
,
parser
=
parse_json
,
parser
=
parse_json
,
allowed_param
=
[
'date'
,
'exclude'
]
allowed_param
=
[
'date'
,
'exclude'
]
)
)(
self
,
*
args
,
**
kargs
)
""" trends/weekly """
""" trends/weekly """
def
trends_weekly
(
self
,
*
args
,
**
kargs
):
trends_weekly
=
bind_api
(
return
bind_api
(
search_api
=
True
,
host
=
"search."
+
self
.
host
,
path
=
'/trends/weekly.json'
,
path
=
'/trends/weekly.json'
,
parser
=
parse_json
,
parser
=
parse_json
,
allowed_param
=
[
'date'
,
'exclude'
]
allowed_param
=
[
'date'
,
'exclude'
]
)
)(
self
,
*
args
,
**
kargs
)
""" Internal use only """
""" Internal use only """
@
staticmethod
@
staticmethod
def
_pack_image
(
filename
,
max_size
):
def
_pack_image
(
filename
,
max_size
):
"""Pack image from file into multipart-formdata post body"""
"""Pack image from file into multipart-formdata post body"""
...
...
tweepy/auth.py
View file @
dcce34d9
...
@@ -37,10 +37,10 @@ class BasicAuthHandler(AuthHandler):
...
@@ -37,10 +37,10 @@ class BasicAuthHandler(AuthHandler):
class
OAuthHandler
(
AuthHandler
):
class
OAuthHandler
(
AuthHandler
):
"""OAuth authentication handler"""
"""OAuth authentication handler"""
REQUEST_TOKEN_URL
=
'http://twitter.com/oauth/request_token'
REQUEST_TOKEN_URL
=
'http://
api.
twitter.com/oauth/request_token'
AUTHORIZATION_URL
=
'http://twitter.com/oauth/authorize'
AUTHORIZATION_URL
=
'http://
api.
twitter.com/oauth/authorize'
AUTHENTICATE_URL
=
'http://twitter.com/oauth/authenticate'
AUTHENTICATE_URL
=
'http://
api.
twitter.com/oauth/authenticate'
ACCESS_TOKEN_URL
=
'http://twitter.com/oauth/access_token'
ACCESS_TOKEN_URL
=
'http://
api.
twitter.com/oauth/access_token'
def
__init__
(
self
,
consumer_key
,
consumer_secret
,
callback
=
None
):
def
__init__
(
self
,
consumer_key
,
consumer_secret
,
callback
=
None
):
self
.
_consumer
=
oauth
.
OAuthConsumer
(
consumer_key
,
consumer_secret
)
self
.
_consumer
=
oauth
.
OAuthConsumer
(
consumer_key
,
consumer_secret
)
...
...
tweepy/binder.py
View file @
dcce34d9
...
@@ -22,7 +22,7 @@ except ImportError:
...
@@ -22,7 +22,7 @@ except ImportError:
def
bind_api
(
path
,
parser
,
allowed_param
=
[],
method
=
'GET'
,
require_auth
=
False
,
def
bind_api
(
path
,
parser
,
allowed_param
=
[],
method
=
'GET'
,
require_auth
=
False
,
timeout
=
None
,
host
=
Non
e
):
timeout
=
None
,
search_api
=
Fals
e
):
def
_call
(
api
,
*
args
,
**
kargs
):
def
_call
(
api
,
*
args
,
**
kargs
):
# If require auth, throw exception if credentials not provided
# If require auth, throw exception if credentials not provided
...
@@ -66,10 +66,11 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
...
@@ -66,10 +66,11 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
parameters
=
None
parameters
=
None
# Build url with parameters
# Build url with parameters
api_root
=
api
.
api_root
if
search_api
is
False
else
api
.
search_root
if
parameters
:
if
parameters
:
url
=
'%s?%s'
%
(
api
.
api
_root
+
path
,
urllib
.
urlencode
(
parameters
))
url
=
'%s?%s'
%
(
api_root
+
path
,
urllib
.
urlencode
(
parameters
))
else
:
else
:
url
=
api
.
api_root
+
path
url
=
api_root
+
path
# Check cache if caching enabled and method is GET
# Check cache if caching enabled and method is GET
if
api
.
cache
and
method
==
'GET'
:
if
api
.
cache
and
method
==
'GET'
:
...
@@ -89,7 +90,7 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
...
@@ -89,7 +90,7 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
scheme
=
'https://'
scheme
=
'https://'
else
:
else
:
scheme
=
'http://'
scheme
=
'http://'
_
host
=
host
or
api
.
host
host
=
api
.
host
if
search_api
is
False
else
api
.
search_
host
# Continue attempting request until successful
# Continue attempting request until successful
# or maximum number of retries is reached.
# or maximum number of retries is reached.
...
@@ -98,14 +99,14 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
...
@@ -98,14 +99,14 @@ def bind_api(path, parser, allowed_param=[], method='GET', require_auth=False,
# Open connection
# Open connection
# FIXME: add timeout
# FIXME: add timeout
if
api
.
secure
:
if
api
.
secure
:
conn
=
httplib
.
HTTPSConnection
(
_
host
)
conn
=
httplib
.
HTTPSConnection
(
host
)
else
:
else
:
conn
=
httplib
.
HTTPConnection
(
_
host
)
conn
=
httplib
.
HTTPConnection
(
host
)
# Apply authentication
# Apply authentication
if
api
.
auth
:
if
api
.
auth
:
api
.
auth
.
apply_auth
(
api
.
auth
.
apply_auth
(
scheme
+
_
host
+
url
,
scheme
+
host
+
url
,
method
,
headers
,
parameters
method
,
headers
,
parameters
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment