for any info/changes follow me: @nickmilon

twtPyCurl.twt.clients module

module clients

twtPyCurl.twt.clients.backoff(seconds)[source]
exception twtPyCurl.twt.clients.ErrorTwtStreamDisconnectReq(error_number, msg)[source]

Bases: twtPyCurl.py.requests.ErrorRq

__init__(error_number, msg)[source]
exception twtPyCurl.twt.clients.ErrorTwtMissingParameters(parm_names_lst)[source]

Bases: twtPyCurl.py.requests.ErrorRq

__init__(parm_names_lst)[source]
exception twtPyCurl.twt.clients.ErrorRqHttpTwt(response)[source]

Bases: twtPyCurl.py.requests.ErrorRq

__init__(response)[source]

i.e: {‘errors’: [{‘message’: ‘Query parameters are missing.’, ‘code’: 25}]} {‘errors’: [{‘message’: ‘Invalid or expired token.’, ‘code’: 89}]}

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:
Example:

check here

__init__(credentials, **kwargs)[source]
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}]}

on_request_end()[source]
help(*args, **kwargs)[source]

delegate help to be handled by endpoints object

_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 and ClientStream
Example:

check here

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...........................................................................................'
__init__(credentials=None, stats_every=1, **kwargs)[source]
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

classmethod wait_on_nw_error(current_try)[source]
classmethod wait_on_http_error(current_try)[source]
classmethod wait_on_http_420(current_try)[source]
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

on_twitter_msg(msg_type, msg)[source]

override it to handle twitter messages

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