I like to help people on StackOverflow, matter of fact nowadays it depends on how quick you are when answering question. With that being said, I can't always keep a tab and an eye to refresh the browser to view new questions.
I made a very basic script to send me a notification (OSX) whenever there's a new question.
import requests, os, threading, time
from bs4 import BeautifulSoup
from timeloop import Timeloop
from datetime import timedelta
tl = Timeloop()
SE_REALTIME = "https://stackexchange.com/questions?tab=realtime"
WATCHED_TAG = "swift"
QUE_LI = []
# The notifier function
def notify(title, subtitle, message, link):
t = '-title {!r}'.format(title)
s = '-subtitle {!r}'.format(subtitle)
m = '-message {!r}'.format(message)
l = '-open {!r}'.format(link)
os.system('terminal-notifier {}'.format(' '.join([m, t, s, l])))
def fetch():
# The SOUP
soup = BeautifulSoup(requests.get(SE_REALTIME).content, "html.parser")
# List of Questions with the sid: Stackoverflow.com
li = soup.find_all('div', {'data-sid': 'stackoverflow.com'})
# Loop Through
for child in li:
id = child['class'][2]
all_tags = child.find("span", attrs={'class': 'realtime-tags'}).text.strip()
tags = all_tags.split()
if WATCHED_TAG in tags:
if id not in QUE_LI:
question = child.find("a", attrs={'class': 'realtime-question-url realtime-title'})
QUE_LI.append(id)
notify(title = 'New Question {}'.format(' '.join([WATCHED_TAG])),
subtitle = 'Stackoverflow.com',
message = question.text.strip(),
link = question['href'])
@tl.job(interval=timedelta(seconds=2))
def fetch_questions_every_2s():
fetch()
if __name__ == "__main__":
tl.start(block=True)
Ok, This code works well. But I'm looking for:
- A way to listen for changes on the div tree (without making a request every 2 second)
- Less code.
Dependencies:
Terminal-Notifier
BeautifulSoup
TimeLoop
Requests