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
4d85f40c
Commit
4d85f40c
authored
Jul 29, 2009
by
Josh Roesslein
Browse files
Implemented search endpoint.
parent
735b8688
Changes
4
Hide whitespace changes
Inline
Side-by-side
api.py
View file @
4d85f40c
...
...
@@ -6,7 +6,7 @@ import base64
from
binder
import
bind_api
from
parsers
import
*
from
models
import
User
,
Status
,
DirectMessage
,
Friendship
,
SavedSearch
from
models
import
User
,
Status
,
DirectMessage
,
Friendship
,
SavedSearch
,
SearchResult
from
error
import
TweepError
"""Twitter API"""
...
...
@@ -15,7 +15,7 @@ class API(object):
def
__init__
(
self
,
username
=
None
,
password
=
None
,
host
=
'twitter.com'
,
secure
=
False
,
classes
=
{
'user'
:
User
,
'status'
:
Status
,
'direct_message'
:
DirectMessage
,
'friendship'
:
Friendship
,
'saved_search'
:
SavedSearch
}):
'saved_search'
:
SavedSearch
,
'search_result'
:
SearchResult
}):
if
username
and
password
:
self
.
set_credentials
(
username
,
password
)
else
:
...
...
@@ -371,3 +371,13 @@ class API(object):
parser
=
parse_return_true
)(
self
)
"""Search API"""
def
search
(
self
,
*
args
,
**
kargs
):
return
bind_api
(
host
=
'search.'
+
self
.
host
,
path
=
'/search.json'
,
parser
=
parse_search_results
,
allowed_param
=
[
'q'
,
'lang'
,
'rpp'
,
'page'
,
'since_id'
,
'geocode'
,
'show_user'
],
)(
self
,
*
args
,
**
kargs
)
binder.py
View file @
4d85f40c
...
...
@@ -8,7 +8,7 @@ import urllib
from
parsers
import
parse_error
from
error
import
TweepError
def
bind_api
(
path
,
parser
,
allowed_param
=
None
,
method
=
'GET'
,
require_auth
=
False
):
def
bind_api
(
path
,
parser
,
allowed_param
=
None
,
method
=
'GET'
,
require_auth
=
False
,
host
=
None
):
def
_call
(
api
,
*
args
,
**
kargs
):
# If require auth, throw exception if credentials not provided
...
...
@@ -22,10 +22,14 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False)
parameters
=
None
# Open connection
if
host
:
_host
=
host
else
:
_host
=
api
.
host
if
api
.
secure
:
conn
=
httplib
.
HTTPSConnection
(
api
.
host
)
conn
=
httplib
.
HTTPSConnection
(
_
host
)
else
:
conn
=
httplib
.
HTTPConnection
(
api
.
host
)
conn
=
httplib
.
HTTPConnection
(
_
host
)
# Build url with parameters
if
parameters
:
...
...
models.py
View file @
4d85f40c
...
...
@@ -30,3 +30,7 @@ class Friendship(object):
class
SavedSearch
(
object
):
pass
class
SearchResult
(
object
):
pass
parsers.py
View file @
4d85f40c
...
...
@@ -17,6 +17,10 @@ def _parse_datetime(str):
return
datetime
.
strptime
(
str
,
'%a %b %d %H:%M:%S +0000 %Y'
)
def
_parse_search_datetime
(
str
):
return
datetime
.
strptime
(
str
,
'%a, %d %b %Y %H:%M:%S +0000'
)
def
_parse_user
(
obj
,
api
):
user
=
api
.
classes
[
'user'
]()
...
...
@@ -128,6 +132,24 @@ def parse_saved_searches(data, api):
saved_searches
.
append
(
_parse_saved_search
(
obj
,
api
))
return
saved_searches
def
_parse_search_result
(
obj
,
api
):
result
=
api
.
classes
[
'search_result'
]()
for
k
,
v
in
obj
.
items
():
if
k
==
'created_at'
:
setattr
(
result
,
k
,
_parse_search_datetime
(
v
))
else
:
setattr
(
result
,
k
,
v
)
return
result
def
parse_search_results
(
data
,
api
):
results
=
json
.
loads
(
data
)[
'results'
]
result_objects
=
[]
for
obj
in
results
:
result_objects
.
append
(
_parse_search_result
(
obj
,
api
))
return
result_objects
def
parse_json
(
data
,
api
):
return
json
.
loads
(
data
)
...
...
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