新聞¶
Warning
除了 mock 以外,每個來源都會連網路。cnyes 抓的是非官方 API,隨時可能改變;NewsAPI 會用掉你的 API 額度。
cjtrade.pkgs.analytics.informational.news_client
¶
NewsProviderType
¶
Bases: Enum
Supported news providers.
NewsClient(provider_type, **config)
¶
An unified API to interact with different news providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_type
|
NewsProviderType or str
|
Provider to use; a string is converted with |
required |
**config
|
Any
|
Passed to the provider constructor (e.g. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider type is not supported. |
Warnings
All providers except MOCK make network calls; results vary over time.
Source code in src/cjtrade/pkgs/analytics/informational/news_client.py
37 38 39 40 41 | |
current_provider_type
property
¶
The provider type this client was created with.
fetch_headline_news_async(n=10)
async
¶
Fetch up to n headline news items asynchronously from the provider.
Source code in src/cjtrade/pkgs/analytics/informational/news_client.py
59 60 61 | |
fetch_headline_news(n=10)
¶
Fetch up to n headline news items from the provider (blocking).
Source code in src/cjtrade/pkgs/analytics/informational/news_client.py
63 64 65 | |
search_by_keyword(keyword, n=10)
¶
Search the provider for up to n news items matching keyword.
Source code in src/cjtrade/pkgs/analytics/informational/news_client.py
67 68 69 | |
get_provider_name()
¶
Return the provider name.
Source code in src/cjtrade/pkgs/analytics/informational/news_client.py
71 72 73 | |
cjtrade.pkgs.analytics.informational.news_providers
¶
News(title, content)
¶
A news item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Headline. |
required |
content
|
str
|
Body text or summary. |
required |
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
16 17 18 | |
NewsInterface(**config)
¶
Bases: ABC
An unified interface for different news providers. All news provider implementations must inherit from this class and implement all abstract methods.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
26 27 | |
fetch_headline_news_async(n=10)
abstractmethod
async
¶
Fetch latest news asynchronously.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
29 30 31 32 | |
fetch_headline_news(n=10)
abstractmethod
¶
Fetch latest news. (blocking call)
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
34 35 36 37 | |
search_by_keyword(keyword, n=10)
abstractmethod
¶
Search news by keyword.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
39 40 41 42 | |
get_provider_name()
abstractmethod
¶
Get the provider name.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
44 45 46 47 | |
MockNewsProvider(**config)
¶
Bases: NewsInterface
Offline news provider that returns three fixed Chinese news items.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/mock.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
fetch_headline_news(n=10)
¶
Return the first n mock news items.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/mock.py
70 71 72 | |
fetch_headline_news_async(n=10)
async
¶
Return the first n mock news items after a simulated delay.
Warnings
Sleeps 5 seconds to simulate network latency.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/mock.py
74 75 76 77 78 79 80 81 82 | |
search_by_keyword(keyword, n=10)
¶
Return up to n mock items whose title or content contains keyword.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/mock.py
85 86 87 88 89 | |
get_provider_name()
¶
Return "mock".
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/mock.py
92 93 94 | |
NewsAPIProvider(**config)
¶
Bases: NewsInterface
News provider backed by NewsAPI (newsapi.org).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**config
|
Must include |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Warnings
Requires the news extra. Methods call the NewsAPI web service; results
change over time and count against the API quota.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/news_api.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
fetch_headline_news_async(n=10)
async
¶
Fetch up to n top business headlines for "Taiwan" (bbc-news, the-verge).
Warnings
Makes a network call to NewsAPI. Sleeps 1 s first; the NewsAPI call itself is blocking.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/news_api.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
fetch_headline_news(n=10)
¶
Fetch up to n top headlines for "Taiwan" from bbc-news.
Warnings
Makes a network call to NewsAPI.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/news_api.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
search_by_keyword(keyword, n=10)
¶
Search the last 30 days of bbc-news and the-verge for keyword.
Warnings
Makes a network call to NewsAPI. The date range depends on the current date.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/news_api.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
get_provider_name()
¶
Return "news_api".
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/news_api.py
119 120 121 | |
StatementDogProvider(**config)
¶
Bases: NewsInterface
Placeholder provider for StatementDog (財報狗).
Warnings
Not implemented: the fetch and search methods return None.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/_base.py
26 27 | |
fetch_headline_news_async()
async
¶
Not implemented.
Warnings
Placeholder; returns None.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/statement_dog.py
13 14 15 16 17 18 19 20 | |
search_by_keyword(keyword)
¶
Not implemented.
Warnings
Placeholder; returns None.
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/statement_dog.py
22 23 24 25 26 27 28 29 | |
get_provider_name()
¶
Return "StatementDog".
Source code in src/cjtrade/pkgs/analytics/informational/news_providers/statement_dog.py
31 32 33 | |