12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- def process_comment(res_data):
- result = {}
- def add_comment(root, father_user_id, user_id, comment):
- if father_user_id in root.keys():
- root[father_user_id]['comments'][user_id] = comment
- return root
- for key in root.keys():
- comments = root[key]['comments']
- new_comments = add_comment(
- comments, father_user_id, user_id, comment)
- if new_comments:
- root[key]['comments'] = new_comments
- return root
- return False
- for data in res_data:
- comment_id = data['id']
- host_user_id = data['user']['screen_name']
- if '//' in data['text_raw']:
- texts = data['text_raw'].split('//')
- father_id = ''
- for text in texts[::-1]:
- text = text.split(':')
- if len(text) == 1:
- content = text[0] if text[0] == '' else '快转微博'
- if father_id == '':
- result[host_user_id] = {
- 'comment_id': comment_id, 'content': content, 'comments': {}
- }
- else:
- result = add_comment(result, father_id, host_user_id, {
- 'content': content,
- 'comment_id': comment_id,
- 'comments': {}
- })
- else:
- user_id = text[0][1:]
- content = text[1] if text[1] != '' else '快转微博'
- if father_id == '':
- if user_id not in result.keys():
- result[user_id] = {
- 'content': content,
- 'comments': {}
- }
- father_id = user_id
- else:
- result = add_comment(result, father_id, user_id, {
- 'content': content,
- 'comments': {}
- })
- father_id = user_id
- else:
- content = data['text_raw']
- result[host_user_id] = {
- 'comment_id': comment_id, 'content': content, 'comments': {}
- }
- return result
|