Skip to content

Commit 7fde8f4

Browse files
committed
get_news(): keep news URL and query in base.py, Google-style docstring
Follow the repo layout: const.py holds base hosts; feature endpoints and request templates live in the module that uses them.
1 parent f5dea81 commit 7fde8f4

2 files changed

Lines changed: 34 additions & 30 deletions

File tree

‎yfinance/base.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
from . import utils, cache
34-
from .const import _MIC_TO_YAHOO_SUFFIX, _NEWS_GRAPHQL_QUERY_, _NEWS_GRAPHQL_URL_, _SENTINEL_
34+
from .const import _MIC_TO_YAHOO_SUFFIX, _SENTINEL_
3535
from .data import YfData
3636
from .config import YfConfig
3737
from .exceptions import YFDataException, YFEarningsDateMissing, YFRateLimitError
@@ -49,6 +49,33 @@
4949
from bs4 import BeautifulSoup
5050

5151

52+
_NEWS_GRAPHQL_URL_ = 'https://nexus-finance-prod.media.yahoo.com/'
53+
54+
# GraphQL query behind the news list on finance.yahoo.com/quote/<symbol>/news/.
55+
# Assets only resolve when a field takes $clientContext (previewUrl, clickthroughUrl),
56+
# otherwise Yahoo returns an "AssetError" for every item.
57+
_NEWS_GRAPHQL_QUERY_ = '''
58+
query FinanceTickerListNews($listInput: LightyearListInput!, $clientContext: ClientContext!, $mlRecsInput: MLRecsInput!, $gqlContext: [GqlContext] = [], $imageResize: [ImageResizeInput!]! = [], $mlRecsFirst: Int, $after: String) {
59+
lightyearList: financeTickerList(list_input: $listInput, cc: $clientContext) {
60+
main: mlRecsStream(mlRecsInput: $mlRecsInput, first: $mlRecsFirst, after: $after) {
61+
edges { node { asset(gqlContext: $gqlContext) {
62+
__typename
63+
... on Story { id: uuid title previewUrl(cc: $clientContext) isPremiumNews contentAttributes { ...Attrs } finance { ...Tickers } }
64+
... on Video { id: uuid title previewUrl(cc: $clientContext) contentAttributes { ...Attrs } finance { ...Tickers } }
65+
... on Outlink { id: uuid title: headline description displayTime url provider { displayName url } contentAttributes { thumbnail { ...Image } } }
66+
} } }
67+
pageInfo { hasNextPage endCursor }
68+
}
69+
}
70+
}
71+
fragment Image on Image { url height width resized(resizeInput: $imageResize) { url height width transformLabel } }
72+
fragment Attrs on ContentAttributes {
73+
description summary publishTime displayTime isHosted canonicalUrl clickthroughUrl(cc: $clientContext)
74+
provider { displayName url } thumbnail { ...Image }
75+
}
76+
fragment Tickers on Finance { stockTickers { symbol } }
77+
'''
78+
5279
_tz_info_fetch_ctr = 0
5380

5481
class TickerBase:
@@ -619,9 +646,12 @@ def get_news(self, count=10, tab="news") -> list:
619646
News follows ``yf.config.locale``. Yahoo only has news for some locales;
620647
for others an empty list is returned.
621648
622-
:param count: maximum number of articles to return
623-
:param tab: "news", "all" or "press releases"
624-
:return: list of articles, each ``{"id": ..., "content": {...}}``
649+
Args:
650+
count (int): Maximum number of articles to return
651+
tab (str): "news", "all" or "press releases"
652+
653+
Returns:
654+
list of articles, each ``{"id": ..., "content": {...}}``
625655
"""
626656
logger = utils.get_yf_logger()
627657

‎yfinance/const.py‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
11
_QUERY1_URL_ = 'https://query1.finance.yahoo.com'
22
_BASE_URL_ = 'https://query2.finance.yahoo.com'
33
_ROOT_URL_ = 'https://finance.yahoo.com'
4-
_NEWS_GRAPHQL_URL_ = 'https://nexus-finance-prod.media.yahoo.com/'
5-
6-
# GraphQL query behind the news list on finance.yahoo.com/quote/<symbol>/news/.
7-
# Assets only resolve when a field takes $clientContext (previewUrl, clickthroughUrl),
8-
# otherwise Yahoo returns an "AssetError" for every item.
9-
_NEWS_GRAPHQL_QUERY_ = '''
10-
query FinanceTickerListNews($listInput: LightyearListInput!, $clientContext: ClientContext!, $mlRecsInput: MLRecsInput!, $gqlContext: [GqlContext] = [], $imageResize: [ImageResizeInput!]! = [], $mlRecsFirst: Int, $after: String) {
11-
lightyearList: financeTickerList(list_input: $listInput, cc: $clientContext) {
12-
main: mlRecsStream(mlRecsInput: $mlRecsInput, first: $mlRecsFirst, after: $after) {
13-
edges { node { asset(gqlContext: $gqlContext) {
14-
__typename
15-
... on Story { id: uuid title previewUrl(cc: $clientContext) isPremiumNews contentAttributes { ...Attrs } finance { ...Tickers } }
16-
... on Video { id: uuid title previewUrl(cc: $clientContext) contentAttributes { ...Attrs } finance { ...Tickers } }
17-
... on Outlink { id: uuid title: headline description displayTime url provider { displayName url } contentAttributes { thumbnail { ...Image } } }
18-
} } }
19-
pageInfo { hasNextPage endCursor }
20-
}
21-
}
22-
}
23-
fragment Image on Image { url height width resized(resizeInput: $imageResize) { url height width transformLabel } }
24-
fragment Attrs on ContentAttributes {
25-
description summary publishTime displayTime isHosted canonicalUrl clickthroughUrl(cc: $clientContext)
26-
provider { displayName url } thumbnail { ...Image }
27-
}
28-
fragment Tickers on Finance { stockTickers { symbol } }
29-
'''
304

315
_SENTINEL_ = object()
326

0 commit comments

Comments
 (0)