0

I want to fetch the daily and weekly count of questions tagged with a particular tag.

For example, I need the daily & weekly count of the number of questions asked for the top 100 languages or tags:

daily_and_weekly

I was able to find an example for the total number of questions by tags

double-beep
  • 5,031
  • 17
  • 33
  • 41
Aniket Tiwari
  • 3,561
  • 4
  • 21
  • 61
  • 1
    Hmmm... It appears that the `fromdate` and/or `todate` for that endpoint limits which tags to return based on the tag's creation date. Those values don't limit the counts to the date range provided. Are you wanting this information for lots of tags, or just a few? It looks like you could get it from the `total` in the `.wrapper` when making a request to the `/questions` endpoint and specifying a single tag, but that would require a request for each combination of date range and single tag. – Makyen Oct 24 '21 at 21:38
  • @Makyen Could you give an example – Aniket Tiwari Oct 25 '21 at 05:56
  • It's also worth noting that "today" means the last 24 hours and "this week" the last 7 days (see the respective tooltips). [Here is an example in the API documentation](https://api.stackexchange.com/docs/questions#fromdate=1635083100&order=desc&sort=activity&tagged=javascript&filter=!-\)5fGp\*dqmLp&site=stackoverflow&run=true) based on what Makyen said above. Notice `tagged` is set to the tag name, the filter is changed to include `total` in `.wrapper` and `fromdate` is set to `currentDate - 24hours` (must be epoch seconds). – double-beep Oct 25 '21 at 13:47
  • @double-beep I can't see the wrapper. Could you share an example to fetch count for daily and weekly – Aniket Tiwari Oct 27 '21 at 05:00
  • @double-beep I tried to modify the date by 1 week but the data doesn't match https://stackoverflow.com/tags with the tag name – Aniket Tiwari Nov 01 '21 at 17:33
  • I can write in Ruby or Python – Aniket Tiwari Nov 02 '21 at 10:59

1 Answers1

1
  • Find the epoch seconds 24 hours ago and a week ago (168 hours).
  • For each of the list of tags you have, make two GET requests to /questions (to get both questions asked this day and this week). You should supply these parameters:
    • filter (see one of my other answers). You'll need to include total in the .wrapper object and exclude items.
    • tagged - the tag you want
    • fromdate - epoch seconds a day/week ago
  • Look for the total property of the JSON returned. It might differ from the count shown on the site because the latter is cached.

Here's an example in Python using StackAPI (docs):

from datetime import datetime, timedelta
from stackapi import StackAPI

now = datetime.now()
last_day = now - timedelta(days = 1)
last_week = now - timedelta(weeks = 1)

last_day_secs = int(last_day.timestamp())
last_week_secs = int(last_week.timestamp())

# replace accordingly
tags = [ "javascript", "python", "java", "c#" ]

SITE = StackAPI("stackoverflow")
# exclude items, include the "total" property of .wrapper
q_filter = "!-)5fGp*dqmLp"

def fetch_q_total(fromdate, tag):
  questions = SITE.fetch("questions",
                         tagged = tag,
                         fromdate = fromdate,
                         filter = q_filter)
  return questions["total"]

for tag in tags:
  q_day_total = fetch_q_total(last_day_secs, tag)
  q_week_total = fetch_q_total(last_week_secs, tag)

  print(f"{q_day_total} {tag} questions were posted in the last 24 hours")
  print(f"{q_week_total} {tag} questions were posted in the last week")
double-beep
  • 5,031
  • 17
  • 33
  • 41