for any info/changes follow me: @nickmilon
twtPyCurl.twt.clients module¶
module clients
-
class
twtPyCurl.twt.clients.
ClientTwtRest
(credentials, **kwargs)[source]¶ Bases:
twtPyCurl.py.requests.Client
- client for Twitter REST API
- examples require a credentials.json in user’s home directory see
CredentialsProviderFile
Parameters: - credentials (Credentials) – an instance of
Credentials
- kwargs (dict) – for acceptable kwargs see
Client
Example: -
request_ep
(end_point, method='GET', parms={}, multipart=False)[source]¶ request end point
Parameters: - end_point (str) – twitter REST end point sortcut ie ‘users/search’
- method (str) – request method one of GET or POST (defaults to GET)
- parms (dict) – parameters dictionary to pass to twitter
Returns: an instance of
Response
Warning
doesn’t check end_point’s validity will raise a twitter API error if not valid
-
_request_ep_media
(parms_dict)[source]¶ this is a special case see a post request with media(binary file(s) content or media_data (base64 encoded content) upload content and modify parameters with media_ids there is a faster but complicated endpoint
-
on_request_error_http
(err)[source]¶ we got an http error, if error < 500 twitter’s error message is in data i.e: {‘errors’: [{‘message’: ‘Invalid or expired token.’, ‘code’: 89}]}
-
_adHocCmd_
(element, *args, **kwargs)[source]¶ this makes the trick of issuing requests against endpoints using dot notation syntax it is provided only for ease of use when issuing requests from command line. Applications should not use dot notation but instead call
request_ep()
method
-
class
twtPyCurl.twt.clients.
ClientTwtStream
(credentials=None, stats_every=1, **kwargs)[source]¶ Bases:
twtPyCurl.py.requests.ClientStream
A client for twitter stream API disconnect can be initiated by a message to disconnect from twitter or by the program by setting request_abort property to a tuple (code,message)
Parameters: - credentials (Credentials) – an instance of
Credentials
- stats_every (int) – print statististics every n data packets defaults to 0 (disables statics)
- kwargs (dict) – for acceptable kwargs see
Client
andClientStream
Example: -
format_stream_stats
= '|{name:8s}|{DHMS:12s}|{chunks:15,d}|{data:14,d}|{avg_per_sec:12,.2f}|{t_data:14,d}|{t_msgs:8,d}|'¶
-
format_stream_stats_header
= '...........................................................................................\n| name | DHMS | chunks | data |avg_per_sec | t_data | t_msgs |\n...........................................................................................'¶
-
on_request_error_curl
(err)[source]¶ default error handling, for curl (connection) Errors override method for any special handling see curl error codes and twitter streaming message types return True to retry request raise an exception or return False to abort remember! after 1st unsuccessful retry probably the error will be E_COULDNT_CONNECT
-
on_request_error_http
(err)[source]¶ default error handling, for HTTP Errors override method for any special handling return True to retry request raise an exception or return False to abort
-
classmethod
wait_seconds
(try_cnt, initial, maximum, tries_max=5, exponential=False)[source]¶ see: https://dev.twitter.com/streaming/overview/connecting
Parameters: - try_cnt successive retries count starting with 0
- initial float (seconds or fraction)
- maximum float (seconds or fraction)
- exponential back off exponentially if True else linearly
Returns: False or backoff value
-
on_data_default
(data)[source]¶ this is where actual stream data comes after chunks are merged, if we don’t specify an on_data_cb function on class initialization
-
on_twitter_data
(data)[source]¶ this is where actual twitter data comes unless you specify on_twitter_data_cb on class initialization, override in descendants or provide a on_twitter_data_cb
-
on_twitter_msg_base
(msg)[source]¶ twitter messages come here first so we can handle some cases here see message types and error codes here
-
request_ep
(end_point, method, test_server=False, **kwargs)[source]¶ shortcut to request constructs url from end_point
Parameters: - end_point (str) – twitter REST end point sortcut ie ‘stream/statuses/filter’
- method (str) – request method one of GET or POST (defaults to GET)
- test_server (bool) – if True channels request to test server
- kwargs (dict) – parameters dictionary to pass to twitter
Returns: an instance of
Response
Raises: see request method
Usage: >>> client.request_ep("stream/statuses/filter","POST", track="breaking, news")
Warning
doesn’t check end_point’s validity will raise a twitter API error if not valid
-
help
(*args, **kwargs)[source]¶ delegate help to endpoints
Usage: >>> h = client.help("userstream/user") see at: ( https://dev.twitter.com/streaming/overview ) ...
-
_adHocCmd_
(element, *args, **kwargs)[source]¶ this makes the trick of issuing requests against endpoints using dot notation syntax it is provided only for ease of use when issuing requests from command line. Applications should not use dot notation but instead call
request_ep()
method
- credentials (Credentials) – an instance of