def my_favour(remove, truncate): if truncate: set_userinfo(USER_FAVOUR, {}) output.warn('your favour list truncated!') sys.exit(0) data = get_json(USER_FAVOUR) if remove: k1 = f'article_{remove}' if k1 in data: del_json(USER_FAVOUR, k1) output.warn('{} {} removed'.format(k1.split('_')[0], remove)) sys.exit(0) k2 = f'comment_{remove}' if k2 in data: del_json(USER_FAVOUR, k2) output.warn('{} {} removed'.format(k2.split('_')[0], remove)) sys.exit(0) summary = {'comment': [], 'article': []} for key in data.keys(): if key.startswith('comment'): summary['comment'].append(key[8:]) elif key.startswith('article'): summary['article'].append(key[8:]) output.info(f'your favour list summary below: \n') output.warn('[comment] total {}, [article] total {}'.format(len(summary['comment']), len(summary['article']))) output.succeed('comment list: {}'.format(', '.join(summary['comment']))) output.succeed('article list: {}'.format(', '.join(summary['article']))) output.color_print('\n you can view article via: jandan-py article-detail [article_id]\n view comment via: jandan-py comment-detail [comment_id]', fg='blue', bold=True)
def render_comment_detail(obj): if 'text' in obj: obj = _parse_comment_detail(obj['text']) output.succeed('{author}: {content} \n {pics}'.format(**obj)) elif 'errcode' in obj: output.error(obj['msg']) else: output.error('error unknow')
def post_tucao(author, email, content, comment_id): last_user = get_userinfo(USER_CONFIG) data = { 'content': content, 'comment_id': comment_id } data = _make_user_info(author, email, data, last_user) if 'author' not in data or 'email' not in data: output.error(f'author and email must set') sys.exit(0) r = start_request(JD_TUCAO, headers=make_dict(HEADERS, 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), method='post', data=data) if 'code' in r and r['code'] == 0: output.succeed(f'tucao {r["msg"]}, [tucao_id] {r["data"]["comment_ID"]}, [author] {r["data"]["comment_author"]}') else: print(r)
def render_article_detail(obj, silence=False): if 'status' in obj and obj['status'] == 'ok': output.info('id: {}, author: {}, date: {}, category: {}'.format( obj['post']['id'], obj['post']['author']['name'], obj['post']['date'], obj['post']['categories'][0]['description'])) output.warn('title: {}'.format(obj['post']['title'])) output.prompt('url: {}\n'.format(obj['post']['url'])) output.succeed('content:\n {}\n\n'.format(obj['post']['content'])) if silence == False and obj['post']['comments'] and len( obj['post']['comments']) > 0: output.echo('comments below: \n') for comment in obj['post']['comments']: output.echo( '------------------------------\n[{index}] {name} {date}:\n {content} \n [comment_id] {id} [oo] {vote_positive} [xx] {vote_negative}\n' .format(**comment)) elif 'errcode' in obj: output.color_print(obj['msg'], fg='red') else: output.color_print('error unknow', fg='red')
def post_comment(author, email, comment, category, post_id): data = {'comment': comment} last_user = get_userinfo(USER_CONFIG) data = _make_user_info(author, email, data, last_user) if 'author' not in data or 'email' not in data: output.error(f'author and email must set') sys.exit(0) if category: r = start_request(COMMENT_LIST.format(category)) comment_post_ID = parse_post_id(r['text']) else: if post_id == 0: output.error('post_id must be an integer not equal 0', bold=True) sys.exit(0) comment_post_ID = post_id data['comment_post_ID'] = comment_post_ID r = start_request(JD_COMMENT, headers=make_dict(HEADERS, 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), method='post', data=data) if isinstance(r, int): output.succeed(f'post comment succeed, [comment_id] {r}') else: print(r)
def _add_favour(id, category='comment'): key = f'{category}_{id}' set_json(USER_FAVOUR, {key: 1}) output.succeed(f'{category} {id} favoured')