Collector

The class for collecting twits of Stocktwits

A collection of methods to simplify your downloading

Stocktwits Collector

stocktwits_collector.collector.Collector.get_history

get history from Stocktwist, default last 30 messages

stocktwits_collector.collector.Collector.save_history

save history from Stocktwist on files splitted by chunk per day, week or month

Detailed list

class stocktwits_collector.collector.Collector
clean_data(messages, event)

clean data

Arguments:
messages (list of dict):

list of messages

event (dict):

dictionary fully described in save_history() symbols (list of str): names of symbols to fetch users (list of str): names of users to fetch only_combo (bool): if True, fetches only messages of those symbols posted from those users

Returns:

list of unique dictionaries cleaned

clean_history(cursor, history, chunk='day')

clean history from messages with different chunk

Arguments:
cursor (dict):

dictionary with the keys oldest_date, min ID, earliest_date and max (ID)

history (list[dict]):

list of messages

chunk (str):

day, week or month, default day

Returns:

history cleaned

get_cursor(messages)

get cursor with oldest date, min ID and max ID

Arguments:
messages (list[dict]):

list of messages

Returns:

a dictionary with oldest_date, min ID, earliest_date and max (ID)

get_data(event)

get data from Stocktwits, default last 30 messages

Arguments:
event (dict):

dictionary fully described in save_history() symbols (list of str): names of symbols to fetch users (list of str): names of users to fetch min (int): optional, min ID max (int): optional, max ID limit (int): optional, defalt 30 messages

Returns:

list of messages

get_date(chunk='day', date=None, jump_chunk=False)

get date at midnight about chunk

Arguments:
chunk (str):

day, week or month, default day

date (str):

datetime with format %Y-%m-%dT%H:%M:%SZ

jump_chunk (bool):

True if you want to jump one chunk

Returns:

string of date at midnight about that chunk or next one

get_file_name(history, current_chunk, event)

get filename

Arguments:
history (list[dict]):

list of messages

current_chunk (dict):

dictionary like event

event (dict):

dictionary fully described in save_history()

Returns:

the file name

get_history(event)

get history from Stocktwist, default last 30 messages

Arguments:
event (dict):

dictionary fully described in save_history() start (datetime): optional, min datetime is_verbose (bool): optional, if True comments will be printed

Returns:

list of messages

get_temporary_event(messages, current_chunk, event)

get temporary chunk event from messages

Arguments:
messages (list[dict]):

list of messages

current_chunk (dict):

dictionary fully described in save_history()

event (dict):

dictionary fully described in save_history()

Returns:

the temporary chunk event updated with the partial start and new min

hold_output()

hold output

This method is temporary until PR approval: https://github.com/p-hiroshige/stockTwitsAPI/pull/1

Example:
with hold_output() as (out, err):

method_with_a_print()

captured_output = out.getvalue().strip()

is_same_chunk(first_date, second_date, chunk='day')

compare a date with a second date

Argument:
first_date (str):

datetime with format %Y-%m-%dT%H:%M:%SZ

second_date (str):

another date with format %Y-%m-%dT%H:%M:%SZ

chunk (str):

day, week or month, default day

Returns:

a boolean, True if the dates are of the same chunk

is_younger(first_date, second_date)

compare a date with a second date

Argument:
first_date (str):

datetime with format %Y-%m-%dT%H:%M:%SZ

second_date (str):

another date with format %Y-%m-%dT%H:%M:%SZ

Returns:

a boolean, True if first date is younger than second one

save_data(history, current_chunk, event)

save data

Arguments:
history (list[dict]):

list of messages

current_chunk (dict):

dictionary like event

event (dict):

dictionary fully described in save_history()

Returns:

the temporary chunk event updated with the partial start and new max

save_history(event)

save history from Stocktwist on files splitted by chunk per day, week or month

Arguments:
event (dict):

symbols (list[str]): names of symbols to fetch users (list[str]): names of users to fetch only_combo (bool): optional, if True, fetches only messages of those symbols posted from those users min (int): optional, min ID max (int): optional, max ID limit (int): optional, default 30 messages start (str): optional, min datetime chunk (str): optional (day, week or month), default day filename_prefix (str): optional, default “history.” filename_suffix (str): optional, default “.json” is_verbose (bool): optional, if True comments will be printed

Returns:

last temporary chunk event discarded

there_is_symbol(symbols_fetched, symbols_target)

check if in the message there are the symbols target

Arguments:
symbols_fetched (list of dict):

list of symbols

symbols_target (list of string):

list of symbols names

Returns:

a boolean, True if there is at least one symbol of target in the symbols fetched

update_event(key, value, event)

update a specific key of event

Arguments:
key (str):

attribute name of event

value (mix):

value you want to replace on that key

event (dict):

dictionary fully described in save_history()

Returns:

dictionary with the attribute named key changed with value

walk(event, cursor, history)

walk along the messages like a shrimp

Arguments:
event (dict):

dictionary fully described in save_history()

cursor (dict):

dictionary with the keys oldest_date, min ID, earliest_date and max (ID)

history (list[dict]):

list of messages

Returns:

cursor, history