Job Search Engine


886 浏览 5 years, 8 months

1.3 定时调度和邮件推送

版权声明: 转载请注明出处 http://www.codingsoho.com/

定时调度和邮件推送

主程序

if __name__ == '__main__':
    notify()
    schedule.every().day.at("10:30").do(notify)
    while True:
        schedule.run_pending()
        time.sleep(1)

爬取

def notify():
    scrap_records = sql_helper.session.query(TriggerScrapRecord).all()
    for record in scrap_records:
        param = {'username': record.username, 'keyword': record.keyword, 'href': record.href}
        func_sync(*(param,))
        if record.email:
            oneDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 1))
            from sqlalchemy import Date, cast
            from datetime import date
            print(type(record.job_entries), type(record))
            job_entries_objs = record.job_entries.filter(JobEntry.created > oneDayAgo ).all() 
            if len(job_entries_objs):
                send_mail(job_entries_objs, [record.email, ])     
    sys.exit()

发送邮件

def send_mail(entries, destination):
    mail_msg = """
    <table style="border: solid 1px;">
    %s
    </table>
    """    
    strings = ""
    fields = ['title', 'salary', 'company', 'region', 'degree', 'experience', 'industry', 'description']
    max = len(entries) if len(entries) < 100 else 100
    for entry in entries[0:max]:
        for field in fields:
            strings += """
            <tr>
                <td style="background-color:grey; color:white;">{}</td>
                <td>{}</td>
            </tr>
            """.format(field, getattr(entry, field))
        strings += """
            <tr>
                <td style="background-color:grey; color:white;">href</td>
                <td><a href="{}">href</a></td>
            </tr>        
        """.format(entry.href)
        strings += """
        </table>
        <br>
        <table style="border: solid 1px;">
        """        
    mail_msg = """
    <table>
        %s
    </table>
    """ % (strings)     
    mailsending.sendmail("codingsoho job notification", mail_msg, destination=destination)