Example #1
0
def read_posts(filename):
    line_list = None
    try:
        with filewithlock.open(filename, 'r', 'utf-8') as f:
            line_list = f.read().splitlines()
    except Exception as e:
        my_log('File {} read error: {}'.format(filename, e))

    if not line_list:
        return []

    post_list = []
    now_post = parse_metadata(line_list[0])
    now_comment = None
    for line in line_list[1:]:
        if line[:2] == '#p':
            if now_comment:
                now_post['comments'].append(now_comment)
                now_comment = None
            post_list.append(now_post)
            now_post = parse_metadata(line)
        elif line[:2] == '#c':
            if now_comment:
                now_post['comments'].append(now_comment)
            now_comment = parse_comment_metadata(line)
        else:
            if now_comment:
                now_comment['text'] += line + '\n'
            else:
                now_post['text'] += line + '\n'
    if now_comment:
        now_post['comments'].append(now_comment)
    post_list.append(now_post)

    return post_list
Example #2
0
def read_posts_dict(filename):
    with filewithlock.open(filename, 'r', 'utf-8') as f:
        line_list = f.read().splitlines()

    if not line_list:
        return {}

    post_dict = {}
    now_post = parse_metadata(line_list[0])
    now_comment = None
    for line in line_list[1:]:
        if line[:2] == '#p':
            if now_comment:
                now_post['comments'].append(now_comment)
                now_comment = None
            if not (post_dict.get(now_post['pid'])
                    and now_post['text'].splitlines()[0] == '#MISSED'):
                post_dict[now_post['pid']] = now_post
            now_post = parse_metadata(line)
        elif line[:2] == '#c':
            if now_comment:
                now_post['comments'].append(now_comment)
            now_comment = parse_comment_metadata(line)
        else:
            if now_comment:
                now_comment['text'] += line + '\n'
            else:
                now_post['text'] += line + '\n'
    if now_comment:
        now_post['comments'].append(now_comment)
    if not (post_dict.get(now_post['pid'])
            and now_post['text'].splitlines()[0] == '#MISSED'):
        post_dict[now_post['pid']] = now_post

    return post_dict
Example #3
0
def write_posts(filename, posts):
    dirname = os.path.dirname(filename)
    if dirname and not os.path.exists(dirname):
        os.makedirs(dirname)

    with filewithlock.open(filename, 'w', 'utf-8') as g:
        for post in posts:
            g.write('#p {} {} {} {}\n{}'.format(
                post['pid'],
                datetime.fromtimestamp(post['timestamp']).strftime(
                    '%Y-%m-%d %H:%M:%S'), post['likenum'], post['reply'], post[
                        'text']))
            for comment in post['comments']:
                g.write('#c {} {}\n{}'.format(
                    comment['cid'],
                    datetime.fromtimestamp(comment['timestamp']).strftime(
                        '%Y-%m-%d %H:%M:%S'), comment['text']))
Example #4
0
def read_posts_dict(filename):
    line_list = None
    try:
        with filewithlock.open(filename, 'r', encoding='utf-8') as f:
            line_list = f.read().splitlines()
    except Exception as e:
        my_log('File {} read error: {}'.format(filename, e))

    if not line_list:
        return {}

    post_dict = {}
    now_post = parse_metadata(line_list[0])
    now_comment = None
    for line in line_list[1:]:
        if line[:2] == '#p':
            if now_comment:
                now_post['comments'].append(now_comment)
                now_comment = None
            if not (post_dict.get(now_post['pid'])
                    and now_post['text'].splitlines()[0] == '#MISSED'):
                post_dict[now_post['pid']] = now_post
            now_post = parse_metadata(line)
        elif line[:2] == '#c':
            if now_comment:
                now_post['comments'].append(now_comment)
            now_comment = parse_comment_metadata(line)
        else:
            if now_comment:
                now_comment['text'] += line + '\n'
            else:
                now_post['text'] += line + '\n'
    if now_comment:
        now_post['comments'].append(now_comment)
    if not (post_dict.get(now_post['pid'])
            and now_post['text'].splitlines()[0] == '#MISSED'):
        post_dict[now_post['pid']] = now_post

    return post_dict