hczhu / TickerTick-API

TickerTick API: stock news API
MIT License
76 stars 6 forks source link

Is it possible to get the news for the last 3 months for example? #8

Closed Louvivien closed 2 months ago

Louvivien commented 1 year ago

Thanks for the great work. With hours ago you can get the news for a specific date back in time. But what would be cool would be to add a query parameter to get the news for the last 2190 hours (3 months) for example

hczhu commented 1 year ago

Thank you, @Louvivien. I hope this project helps you. The API already supports getting news for the last 3 months, though the API has to be called multiple times continually. The calls are necessary because a large amount of news for the last 3 months would blow up the HTTP response. There is a field last_id in the response json to help fetch news page by page. hours_ago is not needed at all. It's not for this use case. The following pseudo-code is an example of exhausting all news for the last 3 months, from the latest news to the oldest.

last_id = null
done = false
while (!done) {
  url = https://api.tickertick.com/feed?q=tt:aapl
  if last_id != null {
    url += "&last_id=" + last_id
  }
  json_response = fetch url
  last_id=json_response.last_id
  // All fetched news from this API call is an array json_respons.stories
  // Iterate on this array and throw away all news older than 3 months
  for (story in json_response.stories) {
    if story.time older than 3 months ago {
      // break out of the outer for loop
      done = true
      break
    } 
  }
}