예제 #1
0
def forum_noti():
    parser = argparse.ArgumentParser('forum_noti')
    parser.add_argument('--redis-url', default='redis://*****:*****@localhost/ultrax?charset=utf8')
    parser.add_argument('--discuz-dbpre', default='pre_')
    parser.add_argument('--forums', default='2,36,38,40,78,82')
    parser.add_argument('--forums-thread-only', default='78')
    options = parser.parse_args()

    text = lambda t: sq_text(t.replace('cdb_', options.discuz_dbpre))

    engine = create_engine(
        options.connect_str,
        encoding='utf-8',
        convert_unicode=True,
    )

    forum_ids = map(int, options.forums.split(','))
    forums = engine.execute(text('''
        SELECT fid, name FROM cdb_forum_forum
        WHERE fid IN :fids
    '''),
                            fids=forum_ids).fetchall()
    forums = {i.fid: i.name for i in forums}

    r = redis.from_url(options.redis_url)
    interconnect = Interconnect('forum', options.redis_url)

    pid = int(r.get('aya:forum_lastpid') or 495985)

    post_template = u'|G{user}|r在|G{forum}|r发表了新主题|G{subject}|r:{excerpt}'
    reply_template = u'|G{user}|r回复了|G{forum}|r的主题|G{subject}|r:{excerpt}'

    threads_only = set(map(int, options.forums_thread_only.split(',')))

    while True:
        time.sleep(5)
        posts = engine.execute(text('''
            SELECT * FROM cdb_forum_post
            WHERE pid > :pid AND
                  fid IN :fids
            ORDER BY pid ASC
        '''),
                               pid=pid,
                               fids=forum_ids)

        if not posts:
            continue

        for p in posts:
            if not p.first and p.fid in threads_only:
                continue

            t = engine.execute(text('''
                SELECT * FROM cdb_forum_thread
                WHERE tid = :tid
            '''),
                               tid=p.tid).fetchone()

            template = post_template if p.first else reply_template

            excerpt = p.message.replace(u'\n', '')
            excerpt = HTMLParser.HTMLParser().unescape(excerpt)
            excerpt = re.sub(r'\[quote\].*?\[/quote\]', u'', excerpt)
            excerpt = re.sub(r'\[img\].*?\[/img\]', u'[图片]', excerpt)
            excerpt = re.sub(r'\[.+?\]', u'', excerpt)
            excerpt = re.sub(r'\{:.+?:\}', u'[表情]', excerpt)
            excerpt = re.sub(r' +', u' ', excerpt)
            excerpt = excerpt.strip()
            if len(excerpt) > 60:
                excerpt = excerpt[:60] + u'……'

            msg = template.format(
                user=p.author,
                forum=forums[t.fid],
                subject=t.subject,
                excerpt=excerpt,
            )
            interconnect.publish('speaker', [u'文文', msg])
            pid = p.pid

        r.set('aya:forum_lastpid', pid)
예제 #2
0
parser = argparse.ArgumentParser(sys.argv[0])
parser.add_argument('--host', default='127.0.0.1')
parser.add_argument('--port', type=int, default=7000)
parser.add_argument('--connect-str',
                    default='mysql://root@localhost/ultrax?charset=utf8')
parser.add_argument('--discuz-dbpre', default='pre_')
parser.add_argument('--discuz-authkey', default='Proton rocks')
options = parser.parse_args()

engine = create_engine(
    options.connect_str,
    encoding='utf-8',
    convert_unicode=True,
)

text = lambda t: sq_text(t.replace('cdb_', options.discuz_dbpre))


def md5(s):
    return hashlib.md5(s).hexdigest()


def _discuz_authcode(string, operation, key, expiry=0):
    try:
        ckey_length = 4
        key = md5(key)
        keya = md5(key[:16])
        keyb = md5(key[16:])
        if ckey_length:
            if operation == 'DECODE':
                keyc = string[:ckey_length]
예제 #3
0
# -- code --
parser = argparse.ArgumentParser(sys.argv[0])
parser.add_argument('--host', default='127.0.0.1')
parser.add_argument('--port', type=int, default=7000)
parser.add_argument('--connect-str', default='mysql://root@localhost/ultrax?charset=utf8')
parser.add_argument('--discuz-dbpre', default='pre_')
parser.add_argument('--discuz-authkey', default='Proton rocks')
options = parser.parse_args()

engine = create_engine(
    options.connect_str,
    encoding='utf-8',
    convert_unicode=True,
)

text = lambda t: sq_text(t.replace('cdb_', options.discuz_dbpre))


def md5(s):
    return hashlib.md5(s).hexdigest()


def _discuz_authcode(string, operation, key, expiry=0):
    try:
        ckey_length = 4
        key = md5(key)
        keya = md5(key[:16])
        keyb = md5(key[16:])
        if ckey_length:
            if operation == 'DECODE':
                keyc = string[:ckey_length]
예제 #4
0
def forum_noti():
    parser = argparse.ArgumentParser('forum_noti')
    parser.add_argument('--redis-url', default='redis://*****:*****@localhost/ultrax?charset=utf8')
    parser.add_argument('--discuz-dbpre', default='pre_')
    parser.add_argument('--forums', default='2,36,38,40,78,82')
    parser.add_argument('--forums-thread-only', default='78')
    options = parser.parse_args()

    text = lambda t: sq_text(t.replace('cdb_', options.discuz_dbpre))

    engine = create_engine(
        options.connect_str,
        encoding='utf-8',
        convert_unicode=True,
    )

    forum_ids = map(int, options.forums.split(','))
    forums = engine.execute(text('''
        SELECT fid, name FROM cdb_forum_forum
        WHERE fid IN :fids
    '''), fids=forum_ids).fetchall()
    forums = {i.fid: i.name for i in forums}

    r = redis.from_url(options.redis_url)
    interconnect = Interconnect('forum', options.redis_url)

    pid = int(r.get('aya:forum_lastpid') or 495985)

    post_template = u'|G{user}|r在|G{forum}|r发表了新主题|G{subject}|r:{excerpt}'
    reply_template = u'|G{user}|r回复了|G{forum}|r的主题|G{subject}|r:{excerpt}'

    threads_only = set(map(int, options.forums_thread_only.split(',')))

    while True:
        time.sleep(5)
        posts = engine.execute(text('''
            SELECT * FROM cdb_forum_post
            WHERE pid > :pid AND
                  fid IN :fids
            ORDER BY pid ASC
        '''), pid=pid, fids=forum_ids)

        if not posts:
            continue

        for p in posts:
            if not p.first and p.fid in threads_only:
                continue

            t = engine.execute(text('''
                SELECT * FROM cdb_forum_thread
                WHERE tid = :tid
            '''), tid=p.tid).fetchone()

            template = post_template if p.first else reply_template

            excerpt = p.message.replace(u'\n', '')
            excerpt = HTMLParser.HTMLParser().unescape(excerpt)
            excerpt = re.sub(r'\[quote\].*?\[/quote\]', u'', excerpt)
            excerpt = re.sub(r'\[img\].*?\[/img\]', u'[图片]', excerpt)
            excerpt = re.sub(r'\[.+?\]', u'', excerpt)
            excerpt = re.sub(r'\{:.+?:\}', u'[表情]', excerpt)
            excerpt = re.sub(r' +', u' ', excerpt)
            excerpt = excerpt.strip()
            if len(excerpt) > 60:
                excerpt = excerpt[:60] + u'……'

            msg = template.format(
                user=p.author,
                forum=forums[t.fid],
                subject=t.subject,
                excerpt=excerpt,
            )
            interconnect.publish('speaker', [u'文文', msg])
            pid = p.pid

        r.set('aya:forum_lastpid', pid)