Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion piazza_api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def iter_all_posts(self, limit=None, sleep=0):
time.sleep(sleep)
yield self.get_post(cid)

def create_post(self, post_type, post_folders, post_subject, post_content, is_announcement=0, bypass_email=0, anonymous=False, is_private=False):
def create_post(self, post_type, post_folders, post_subject, post_content, is_announcement=0, bypass_email=0, anonymous=False, is_private=False, scheduled_time=None):
"""Create a post

It seems like if the post has `<p>` tags, then it's treated as HTML,
Expand All @@ -134,6 +134,8 @@ def create_post(self, post_type, post_folders, post_subject, post_content, is_an
:param anonymous:
:type is_private: bool
:param is_private: If True, post will be private to instructors only.
:type scheduled_time: int | None
:param scheduled_time: The scheduled time to make post as a Unix millisecond timestamp, or None if not scheduled. Note: Does not support poll posts
Comment on lines +137 to +138
:rtype: dict
:returns: Dictionary with information about the created post.
"""
Expand All @@ -158,6 +160,36 @@ def create_post(self, post_type, post_folders, post_subject, post_content, is_an
if bypass_email:
params["prof_override"] = True

if scheduled_time is not None:

if post_type == "poll":
raise NotImplementedError("Piazza API currently does not support posting scheduled polls")

# Create post draft params
draft_params = {
"draft": {
"content": post_content,
#"selectedPrivateUsers": {}, #TODO: figure out how to set this
"folders": post_folders,
"btn": {
"post_type_note": True if post_type == "note" else False,
"post_type_question": True if post_type == "question" else False,
"schedule_later": True,
"schedule_later_time": scheduled_time
},
"txt": {
"post_summary": post_subject,
},
}
}

# Submit the draft and get the id
draft_id = self._rpc.save_draft(draft_params)

params["draftId"] = draft_id
params["config"]["schedule_later"] = True
params["config"]["schedule_later_time"] = scheduled_time

return self._rpc.content_create(params)

def create_followup(self, post, content, anonymous=False, instructor=False):
Expand Down
17 changes: 17 additions & 0 deletions piazza_api/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,23 @@ def search(self, query, nid=None):
return self._handle_error(r, "Search with query '{}' failed."
.format(query))

def save_draft(self, params):
"""Save a post draft. Needed when scheduling posts.

:type params: dict
:param params: A dict of options to pass to the endpoint. Depends on
the specific type of content being created.
:returns: Python object containing returned data
"""
r = self.request(
method="network.save_draft",
data=params
)
return self._handle_error(
r,
"Could not save post draft {}.".format(repr(params))
)

def get_stats(self, nid=None):
"""Get statistics for class

Expand Down