Skip to content

Instantly share code, notes, and snippets.

@fffonion
Created June 13, 2019 02:52
Show Gist options
  • Save fffonion/e10b53b7dac555ca50d783b9a320091b to your computer and use it in GitHub Desktop.
Save fffonion/e10b53b7dac555ca50d783b9a320091b to your computer and use it in GitHub Desktop.
畅言导出评论转换成导入评论格式
import json
import time
cmt = open('export.json').read()
cmt = json.loads(cmt)
print(len(cmt['comments']))
blk = set((
'=======',
'~~~~~~~~~~~~'
))
articles = {}
for cmt in cmt['comments']:
ctime = int(time.mktime(time.strptime(cmt['ctime'], '%Y-%m-%d %H:%M:%S')) * 1000)
remove = False
for b in blk:
if b in cmt['content']:
remove = True
break
if remove:
continue
if cmt['topicSourceId'] not in articles:
url = cmt['topicUrl']
articles[cmt['topicSourceId']] = {
"title": cmt['topicTitle'],
"url": url,
"ttime": ctime, # use cmt time as ttime
"sourceid": cmt['topicSourceId'],
"comments": [],
}
articles[cmt['topicSourceId']]['comments'].append({
"cmtid": cmt['id'],
"ctime": ctime,
"content": cmt['content'],
"replyid": cmt['replyId'],
"user": {
"userid": cmt['referUserId'],
"nickname": cmt['nickname'],
"usericon": cmt['iconUrl'],
"userurl": cmt['userProfileUrl'],
},
"ip": cmt['ip'],
"channeltype": "1",
"spcount": cmt['sp'] if 'sp' in cmt else '0',
"opcount": cmt['op'] if 'op' in cmt else '0',
"attachment": [] if not cmt['attachs'] else cmt['attachs'],
})
with open('import.json', 'wb') as f:
for a in articles:
f.write(json.dumps(articles[a]))
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment