Skip to content

Instantly share code, notes, and snippets.

@guoguo12
Created January 21, 2018 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guoguo12/2bfd71c7744830ff3b9608347a7b128f to your computer and use it in GitHub Desktop.
Save guoguo12/2bfd71c7744830ff3b9608347a7b128f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Created by Allen Guo <allenguo@berkeley.edu> for CS 61A Fall 2017.
Dependencies: Selenium configured for Chrome (see https://goo.gl/WBXMhf).
Before running:
- Fill out the constants below. Double check all of them!
- Open Piazza and switch to the CS 61A Piazza. Make sure you're not on a
Piazza for a different class.
- Also set your text editor to the plain-text (HTML) editor, i.e., not the
editor with lots of fancy buttons.
After running:
- Open Piazza (or refresh if you already have it open).
- Check that the generated drafts are okay.
- Add the drafts to the correct folder ("hw" or "project").
- Publish the drafts in order (master thread, then Q1, then Q2, etc.).
- Make sure the post IDs (e.g., @1234) are all correct.
"""
from time import sleep
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
# Fill these out (but don't commit them!)
USERNAME = ''
PASSWORD = ''
# Fill these out too
ASSIGNMENT = 'HW 11'
ASSIGNMENT_FULL = 'Homework 11'
ASSIGNMENT_DUE_DATE = 'Tuesday, November 14, by 11:59 PM'
QUESTION_NAME = 'Question' # For projects this is usually 'Problem'
QUESTION_COUNT = 3
MASTER_THREAD_POST_ID = 1585 # TODO: Compute this automatically
def make_post(d, title, text):
d.find_element_by_id('new_post_button').click()
sleep(1)
title_field = d.find_element_by_id('post_summary')
title_field.send_keys('[%s] %s' % (ASSIGNMENT, title))
title_field = d.find_element_by_id('rich_old_new_post')
title_field.send_keys(text)
d.find_element_by_id('save_draft').click()
sleep(1)
def make_master_thread(d):
text = "%s has been released! It's due %s." % (ASSIGNMENT_FULL, ASSIGNMENT_DUE_DATE)
text += '\n\n'
text += ('Please post question specific questions in their respective '
'threads and post any general questions below '
'(submitting, autograder, etc.). '
'Come to office hours if you have any further questions.')
text += '\n\n'
for q in range(1, QUESTION_COUNT + 1):
text += 'Q%d: @%d<br>' % (q, MASTER_THREAD_POST_ID + q)
text += '\n#pin'
make_post(d, 'Master Thread', text)
def make_thread(d, question_number):
text = ('Please post all questions you have below in the follow-ups. '
'If you are stuck on this question, feel free to read through the '
'follow-up responses to see if you can draw any ideas from them.')
text += '\n\n'
text += 'Back to main thread: @%d' % MASTER_THREAD_POST_ID
make_post(d, '%s %d' % (QUESTION_NAME, question_number), text)
def main():
chrome_options = Options()
chrome_options.add_argument('--window-size=1920,1080')
d = Chrome(chrome_options=chrome_options)
d.implicitly_wait(5)
d.get('https://piazza.com/')
sleep(5)
# Log in
d.find_element_by_id('login_button').click()
sleep(1)
username_field = d.find_element_by_id('email_field')
username_field.send_keys(USERNAME + '\t' + PASSWORD + '\n')
sleep(5)
make_master_thread(d)
for question_number in range(1, QUESTION_COUNT + 1):
make_thread(d, question_number)
d.quit()
# d.service.process.send_signal(signal.SIGTERM)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment