I wrote a short script in Python 3 that connects to the Stack Exchange API, gets all questions on Programming Puzzles & Code Golf over the past two weeks, and determines the average number of questions per day as well as the average number of answers per question.
The number of questions per day is intended to match that on Area 51, which it does. Obviously it's much easier to just scrape Area 51 directly, but I wanted to figure it out myself for practice.
I'm not an expert with Python or with web APIs, so I was hoping you fine Code Review folks can help me improve my practices.
import requests, datetime, time
def seconds_since_epoch(dt):
epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
return int((dt - epoch).total_seconds())
today = datetime.datetime.now(datetime.timezone.utc)
params = {
"site": "codegolf",
"fromdate": seconds_since_epoch(today - datetime.timedelta(days=14)),
"todate": seconds_since_epoch(today),
"pagesize": 100,
"page": 1
}
base_url = "https://api.stackexchange.com/2.2"
results = []
while True:
req = requests.get(base_url + "/questions", params=params)
contents = req.json()
results.extend(contents["items"])
if not contents["has_more"]:
break
if "backoff" in contents:
time.sleep(contents["backoff"])
params["page"] += 1
questions_per_day = len(results) / 14
answers_per_question = sum([q["answer_count"] for q in results]) / len(results)
print("Over the past 2 weeks, PPCG has had...")
print(round(questions_per_day, 1), "questions per day")
print(round(answers_per_question, 1), "answers per question")
My approach is to build the query using a dict
and make the request to the API using the requests
module. I set the page size to the maximum to reduce the number of requests made so that the daily quota isn't exhausted quite so fast.
The code is hosted on GitHub, should you want to fork and adapt it for your own purposes, assuming it isn't too terrible.