Skip to content

Instantly share code, notes, and snippets.

@Xyene
Last active April 8, 2018 21:04
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 Xyene/e70e5dca0f0554379c74fc8ad00a3f76 to your computer and use it in GitHub Desktop.
Save Xyene/e70e5dca0f0554379c74fc8ad00a3f76 to your computer and use it in GitHub Desktop.
from piazza_api import Piazza
import time
import requests
import json
EMAIL = 'you@mail.utoronto.ca'
PASSWORD = 'hunter2'
WEBHOOK_URL = 'https://hooks.slack.com/services/#####/##########'
CLASS_NAME = 'CSC209'
NETWORK_ID = 'jbcmehb923110u'
while True:
print 'Logging into Piazza...'
try:
p = Piazza()
p.user_login(email=EMAIL, password=PASSWORD)
n = p.network(NETWORK_ID)
latest_post = None
next_latest_post = None
while True:
print 'Polling Piazza...'
for post in n.iter_all_posts():
if post['bucket_name'] == 'Pinned':
continue
nr = post['nr']
if latest_post is None:
print 'Most recent question ID:', nr
next_latest_post = nr
break
if nr > latest_post:
question_url = 'https://piazza.com/class/%s?cid=%s' % (NETWORK_ID, nr)
print 'New post:', question_url
requests.post(WEBHOOK_URL, {'payload': json.dumps({
'channel': '#general',
'username': 'Make Piazza Great Again',
'text': 'New post in %s: %s' % (CLASS_NAME, question_url),
'icon_emoji': ':trumpwrong:'
})})
next_latest_post = max(next_latest_post, nr)
latest_post = next_latest_post
time.sleep(1)
except KeyboardInterrupt:
break
except:
import traceback
traceback.print_exc()
print 'Restarting app...'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment