Пример #1
0
def main():
    uname, up = util.get_enabled_upload()
    t = task.fetch_task('import_post')
    if t:
        data = json.loads(t.task_data)
        pubDate = data['pubDate']
        site = data['site']
        content = data['content']
        print site, content
        urls = _extract_link(content, site)
        print urls
        replacedict = dict()
        for u in urls:
            url = str(u[1:-1])
            if os.path.splitext(url)[1]=='':
                continue
            fname = os.path.split(url)[1]
            content_type, fcontent = urlfetch.fetch(url)
            mime, body = _encode_multipart(fname, fcontent, name=os.path.splitext(fname)[0])
            newurl = _upload(data['authorization'], mime, body)
            print newurl
            replacedict[u] = ur'"%s"' % newurl
        print replacedict
        newcontent = content
        for k, v in replacedict.iteritems():
            newcontent = newcontent.replace(k, v)
        newcontent = _fix_content(newcontent)
        #print newcontent
        r = _post(data['authorization'], name=data['title'], category_id=do_get_categories()[0].id, content=newcontent, creation_time=_parse_date(pubDate))
        print 'task done!'
        task.set_task_result(t.id, True)
Пример #2
0
def cron_job():
    print 'cron job start...'
    t = task.fetch_task(QUEUE_MAIL_HIGH)
    if t is None:
        t = task.fetch_task(QUEUE_MAIL_LOW)
    if t is None:
        return
    logging.info('task loaded: id=%s, execution_id=%s' % (t['id'], t['execution_id']))
    d = json.loads(t.task_data)
    # send mail:
    ss = setting.get_smtp_settings()
    conf = (ss[setting.SMTP_HOST], int(ss[setting.SMTP_PORT]), ss[setting.SMTP_USERNAME], ss[setting.SMTP_PASSWD], bool(ss[setting.SMTP_USE_TLS]))
    from_addr = ss[setting.SMTP_FROM_ADDR]
    to_addr = d['to']
    try:
        mail.send_mail(conf, from_addr, d['to'], d['subject'], d['body'])
    except Exception, e:
        logging.exception('Send mail failed.')
        task.set_task_result(t['id'], t['execution_id'], False)
Пример #3
0
def cron_job():
    print 'cron job start...'
    t = task.fetch_task(QUEUE_MAIL_HIGH)
    if t is None:
        t = task.fetch_task(QUEUE_MAIL_LOW)
    if t is None:
        return
    logging.info('task loaded: id=%s, execution_id=%s' %
                 (t['id'], t['execution_id']))
    d = json.loads(t.task_data)
    # send mail:
    ss = setting.get_smtp_settings()
    conf = (ss[setting.SMTP_HOST], int(ss[setting.SMTP_PORT]),
            ss[setting.SMTP_USERNAME], ss[setting.SMTP_PASSWD],
            bool(ss[setting.SMTP_USE_TLS]))
    from_addr = ss[setting.SMTP_FROM_ADDR]
    to_addr = d['to']
    try:
        mail.send_mail(conf, from_addr, d['to'], d['subject'], d['body'])
    except Exception, e:
        logging.exception('Send mail failed.')
        task.set_task_result(t['id'], t['execution_id'], False)
Пример #4
0
def main():
    uname, up = util.get_enabled_upload()
    t = task.fetch_task('import_post')
    if t:
        data = json.loads(t.task_data)
        pubDate = data['pubDate']
        site = data['site']
        content = data['content']
        print site, content
        urls = _extract_link(content, site)
        print urls
        replacedict = dict()
        for u in urls:
            url = str(u[1:-1])
            if os.path.splitext(url)[1] == '':
                continue
            fname = os.path.split(url)[1]
            content_type, fcontent = urlfetch.fetch(url)
            mime, body = _encode_multipart(fname,
                                           fcontent,
                                           name=os.path.splitext(fname)[0])
            newurl = _upload(data['authorization'], mime, body)
            print newurl
            replacedict[u] = ur'"%s"' % newurl
        print replacedict
        newcontent = content
        for k, v in replacedict.iteritems():
            newcontent = newcontent.replace(k, v)
        newcontent = _fix_content(newcontent)
        #print newcontent
        r = _post(data['authorization'],
                  name=data['title'],
                  category_id=do_get_categories()[0].id,
                  content=newcontent,
                  creation_time=_parse_date(pubDate))
        print 'task done!'
        task.set_task_result(t.id, True)
Пример #5
0
    d = json.loads(t.task_data)
    # send mail:
    ss = setting.get_smtp_settings()
    conf = (ss[setting.SMTP_HOST], int(ss[setting.SMTP_PORT]),
            ss[setting.SMTP_USERNAME], ss[setting.SMTP_PASSWD],
            bool(ss[setting.SMTP_USE_TLS]))
    from_addr = ss[setting.SMTP_FROM_ADDR]
    to_addr = d['to']
    try:
        mail.send_mail(conf, from_addr, d['to'], d['subject'], d['body'])
    except Exception, e:
        logging.exception('Send mail failed.')
        task.set_task_result(t['id'], t['execution_id'], False)
    else:
        task.set_task_result(
            t['id'], t['execution_id'], True,
            'sent at %s' % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))


def cron_loop():
    while True:
        time.sleep(10)
        try:
            cron_job()
        except BaseException, e:
            logging.exception('Cron error.')


if __name__ == '__main__':
    import conf_prod
    db.init(db_type = conf_prod.db.get('type', 'mysql'), \
Пример #6
0
    if t is None:
        return
    logging.info('task loaded: id=%s, execution_id=%s' % (t['id'], t['execution_id']))
    d = json.loads(t.task_data)
    # send mail:
    ss = setting.get_smtp_settings()
    conf = (ss[setting.SMTP_HOST], int(ss[setting.SMTP_PORT]), ss[setting.SMTP_USERNAME], ss[setting.SMTP_PASSWD], bool(ss[setting.SMTP_USE_TLS]))
    from_addr = ss[setting.SMTP_FROM_ADDR]
    to_addr = d['to']
    try:
        mail.send_mail(conf, from_addr, d['to'], d['subject'], d['body'])
    except Exception, e:
        logging.exception('Send mail failed.')
        task.set_task_result(t['id'], t['execution_id'], False)
    else:
        task.set_task_result(t['id'], t['execution_id'], True, 'sent at %s' % datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

def cron_loop():
    while True:
        time.sleep(10)
        try:
            cron_job()
        except BaseException, e:
            logging.exception('Cron error.')

if __name__=='__main__':
    import conf_prod
    db.init(db_type = conf_prod.db.get('type', 'mysql'), \
            db_schema = conf_prod.db.get('schema', 'itranswarp'), \
            db_host = conf_prod.db.get('host', 'localhost'), \
            db_port = conf_prod.db.get('port', 3306), \