-
Notifications
You must be signed in to change notification settings - Fork 0
/
piazza.py
82 lines (66 loc) · 2.94 KB
/
piazza.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
73
74
75
76
77
78
79
80
81
82
import discord
from piazza_api import Piazza
from bs4 import BeautifulSoup
from urllib.parse import urlparse
ZERO_WHITE_SPACE = "\u200b"
def piazza_parse(pi_url, email, passwd, author):
'''
Called by connect.py to get post from Piazza.
It will format the post json into a more readable message for server.
'''
# Create Piazza object, login, and get the users classes
data = {}
p = Piazza()
p.user_login(email=email, password=passwd)
classes = p.get_user_classes()
# Parse the piazza url into components
piazza_url = urlparse(pi_url)
# Get class id and the post number from piazza_url
class_id = piazza_url.path.split('/')[2]
post_num = piazza_url.query.split('=')[1]
# Returns a class network
class_net = p.network(class_id)
# Get the piazza post from the post number and class network
post = class_net.get_post(post_num)
# Get class name
class_name = "None"
for i in classes:
if i['nid'] == class_id:
class_name = i['num']
# Get question and subject of the post
question = post["history"][0]["content"]
subject = post["history"][0]["subject"]
# Format data to return
data.setdefault("class_name", class_name)
if not data['class_name']:
data['class_name'] = ZERO_WHITE_SPACE
data.setdefault("question", subject)
if not data['question']:
data['question'] = ZERO_WHITE_SPACE
# data.setdefault("subject", subject)
data.setdefault("question_text", BeautifulSoup(question, features='lxml').text)
if not data['question_text']:
data['question_text'] = ZERO_WHITE_SPACE
if not data['question_text']:
data['question'] = ZERO_WHITE_SPACE
data.setdefault("answer", {'instructor': '', 'student': ''})
# Get answers json
answers = post["children"]
# Assign student and instructor answers if they exist
for answer in answers:
if answer['type'] == 's_answer':
data['answer']['student'] = BeautifulSoup(answer['history'][0]['content'], features='lxml').text
elif answer['type'] == 'i_answer':
data['answer']['instructor'] = BeautifulSoup(answer['history'][0]['content'], features='lxml').text
return embed_creator(data, pi_url, author)
def embed_creator(data, pi_url, author):
embed=discord.Embed(title=data['question'].capitalize(), url=pi_url, description=data['question_text'], color=0x00fffb)
if data['answer']['student']:
embed.add_field(name="Student(s) Answer:", value=data['answer']['student'], inline=False)
if data['answer']['instructor']:
embed.add_field(name="Instructor(s) Answer:", value=data['answer']['instructor'], inline=False)
pre_footer = f"""Generated By: {author}\nRead full post with comments: [here]({pi_url})
"""
embed.add_field(name="More info:", value=pre_footer, inline=False)
embed.set_footer(text=f"Piazza bot by daffychuy https://github.com/daffychuy/PiazzaBot")
return embed