Skip to content

Instantly share code, notes, and snippets.

@pieropalevsky
Created October 2, 2014 20:14
Show Gist options
  • Save pieropalevsky/d95a2c84767a887696c1 to your computer and use it in GitHub Desktop.
Save pieropalevsky/d95a2c84767a887696c1 to your computer and use it in GitHub Desktop.
Stackoverflow Job Alert
"""This module sends a stack overflow job alert email.
It can be scheduled with python sched library, cron, PaaS providers such as
pythonanywhere or heroku, or anything with similar functionality
"""
import smtplib
from datetime import date, datetime, timedelta
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import feedparser
def stack_overflow_jobs(feeds, days_ago):
"""Return a string of the urls of the jobs separated by a newline in all
the given stackoverflow.com RSS job searches within the given time frame
"""
jobs = ''
for feed in feeds:
for entry in feedparser.parse(feed).entries:
pub = datetime.strptime(entry.published[5:-2], '%d %b %Y %H:%M:%S')
if datetime.utcnow() - pub < timedelta(days=int(days_ago)):
jobs += entry.link + '\n'
return jobs
def send_gmail(sender, password, recipient, message):
"""Sends an email using GMail, the GMail settings may need to be adjusted
to allow for python to send mail
"""
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = recipient
msg['Subject'] = str(date.today()) + ' Jobs'
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(sender, password)
text = msg.as_string()
server.sendmail(sender, recipient, text)
server.close()
def job_alert(feeds, days_ago, sender, password, recipient):
"""Sends the job alert"""
send_gmail(sender, password, recipient, stack_overflow_jobs(feeds,
days_ago))
# Enter your own job searches here http://careers.stackoverflow.com/ and get
# the corresponding RSS feed
sample_feed_one = 'http://careers.stackoverflow.com/jobs/sample_feed_one'
sample_feed_two = 'http://careers.stackoverflow.com/jobs/sample_feed_two'
sample_feed_three = 'http://careers.stackoverflow.com/jobs/sample_feed_three'
# Add your searches to the searches tuple
searches = (sample_feed_one,
sample_feed_two,
sample_feed_three)
# Provide the correct arguments to the job alert
job_alert(searches,
1,
'sender@gmail.com',
'password',
'recipient@anymail.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment