tushare.py


Overview

tushare.py implements a component for integrating with the TuShare API, a financial data service providing news and market information. This component is part of the InfiniFlow system, designed to fetch and filter financial news articles from various sources within a specified date range. It processes the fetched data and returns a filtered result based on a keyword search.

The file defines two main classes:

This component is likely used within a larger data processing or AI pipeline to enrich input data with relevant financial news fetched dynamically from TuShare.


Classes and Methods

Class: TuShareParam

class TuShareParam(ComponentParamBase):

Description

Defines the parameters for configuring the TuShare component. It extends from ComponentParamBase, inheriting parameter validation features.

Properties

Property

Type

Default Value

Description

token

string

"xxx"

API authentication token for TuShare.

src

string

"eastmoney"

Source of the news data; must be one of the allowed sources.

start_date

string

"2024-01-01 09:00:00"

Start datetime for news filtering, format YYYY-MM-DD HH:MM:SS.

end_date

string

Current time (default)

End datetime for news filtering, defaults to current local time.

keyword

string

""

Keyword to filter news content; case-insensitive search.

Methods

Usage Example

params = TuShareParam()
params.token = "your_real_token"
params.src = "sina"
params.start_date = "2024-05-01 00:00:00"
params.end_date = "2024-05-10 23:59:59"
params.keyword = "stock"
params.check()  # Validates parameters

Class: TuShare

class TuShare(ComponentBase, ABC):

Description

The main component class that fetches financial news data from the TuShare API, filters the results based on a keyword, and outputs the filtered news in markdown table format.

It inherits from ComponentBase and ABC (Abstract Base Class), indicating it is designed to integrate with a component-based pipeline framework.

Class Attributes

Methods

Usage Example

ts_component = TuShare()
ts_component._param.token = "your_token"
ts_component._param.src = "sina"
ts_component._param.start_date = "2024-05-01 00:00:00"
ts_component._param.end_date = "2024-05-10 23:59:59"
ts_component._param.keyword = "earnings"

# Assume input content is provided via the pipeline framework
output_df = ts_component._run(history=None)
print(output_df)

Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

classDiagram
    class TuShareParam {
        +token: str
        +src: str
        +start_date: str
        +end_date: str
        +keyword: str
        +__init__()
        +check()
    }
    
    class TuShare {
        +component_name: str = "TuShare"
        +_run(history, **kwargs) pandas.DataFrame or output string
    }
    
    ComponentParamBase <|-- TuShareParam
    ComponentBase <|-- TuShare
    
    TuShare ..> TuShareParam : uses
    TuShare --> requests : HTTP POST
    TuShare --> pandas.DataFrame : processes data

Summary

This file provides a modular, parameter-driven component to fetch financial news from TuShare API, filter it by keyword, and return structured results. It is designed to fit into a larger pipeline system (InfiniFlow), making it reusable and configurable for different data sources, time ranges, and search keywords.

The design emphasizes error resilience, simplicity in API interaction, and leveraging pandas for data management. It abstracts away API specifics behind a component interface, facilitating integration into automated workflows requiring timely financial news data.


End of Documentation for tushare.py