forked from hfaran/piazza-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
surveys.py
72 lines (59 loc) · 2.16 KB
/
surveys.py
1
2
3
4
5
6
7
8
9
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
68
69
70
71
72
# Author: Yolanda Shen
# CS375 Summer 2021
from piazza_api.network import Network
from typing import Generator
from piazza_api import Piazza
import csv
from time import sleep
# Survey data
survey_csv = "surveys.csv"
survey_tags = ["nr", "folders"]
survey_metadata = ["student uid", "title"]
survey_titles = survey_metadata + survey_tags
"""
Surveys
"""
def process_all_surveys(surveys : list) -> None:
""" Processes and writes surveys to csv
:param survey: list of processed survey metadata
"""
try:
with open(survey_csv, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=survey_titles)
writer.writeheader()
for s in surveys:
try:
processed = process_surveys(s)
if processed:
writer.writerow(processed)
print(processed["nr"])
sleep(0.5)
except Exception as e:
print(s["nr"], " ", e)
except IOError:
print("I/O error")
def process_surveys(survey : dict) -> dict:
""" Processes an individual post
:param survey: survey metadata
:returns: processed post metadata based on tags in posts_titles
"""
metadata = {tag: survey[tag] for tag in survey_tags}
metadata.update(process_survey_hist(survey["children"], survey["history"]))
# for k,v in metadata.items():
# print(k, v)
folders = metadata["folders"]
if "survey" in folders or "survey" in metadata["title"].lower():
if "logistics" not in folders and "other" not in folders:
return metadata
raise Exception(metadata["title"], " is in folders ", metadata["folders"], " and was not included in csv")
def process_survey_hist(survey_children : dict, survey_hist : list) -> dict:
"""Parses history metadata for title, date, student uid, and creation date.
:param post_hist: post history
:returns: dictionary with relevant history data pulled.
"""
result = {}
latest = survey_hist[0]
result["student uid"] = survey_hist[-1]["uid"]
subject = latest["subject"]
result.update({"title": subject})
return result