Created
November 18, 2017 06:19
-
-
Save ChrisLinn/fba2402e6e9e1f76561df242a46d1c8c to your computer and use it in GitHub Desktop.
Parse sst json to md.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var util =require('util'); | |
| var fs = require('fs'); | |
| var urlencode = require('urlencode'); | |
| function init() { | |
| fs.writeFileSync('test.md', '# SST 2017\n\n'); | |
| } | |
| function append(str) { | |
| fs.appendFileSync('test.md', str); | |
| } | |
| function tryAlias(owner) { | |
| if(owner.hasOwnProperty('alias')) | |
| return owner['alias'] | |
| else | |
| return owner['name'] | |
| } | |
| function escapeNewLineInAComment(text) { | |
| return text.replace(/\n/g, '\\n'); | |
| } | |
| function handleHashtagAndUrl(text) { | |
| new_text = ''; | |
| text = text.split(/<e type=\"hashtag\" |<e type=\"web\" /); | |
| for (var t = 0; t < text.length; t++){ | |
| line = text[t]; | |
| // if(line != '' && line != ' ' && line != ' '){ | |
| // console.log("--------", line) | |
| if(line.includes('hid=')){ | |
| hashtag = line.match('title=\"(.+?)\"'); | |
| hashtag = urlencode.decode(hashtag[1], 'utf8'); | |
| hashtag = util.format('__%s__\n', hashtag); | |
| new_text += hashtag; | |
| new_text += '\n'; | |
| line = line.substring(line.indexOf('/>')+2); | |
| // console.log("++++++++", line) | |
| new_text += extractMention(line); | |
| new_text += '\n'; | |
| }else if(line.includes('href=')){ | |
| title = line.match('title=\"(.+?)\"'); | |
| title = urlencode.decode(title[1], 'utf8'); | |
| title = title.replace(/\+/g, ' '); | |
| url = line.match('href=\"(.+?)\"'); | |
| url = urlencode.decode(url[1], 'utf8'); | |
| url = util.format('[%s](%s)\n', title,url); | |
| new_text += url | |
| new_text += '\n' | |
| line = line.substring(line.indexOf('/>')+2); | |
| // console.log("++++++++", line) | |
| new_text += extractMention(line); | |
| new_text += '\n'; | |
| }else{ | |
| // console.log("++++++++", line) | |
| new_text += extractMention(line) | |
| new_text += '\n' | |
| } | |
| // } | |
| } | |
| // console.log(new_text) | |
| return new_text | |
| } | |
| function extractMention(line) { | |
| new_line = ''; | |
| parts = line.split(/<e type=\"mention\" /); | |
| for (var p = 0; p < parts.length; p++){ | |
| part = parts[p]; | |
| if(part.includes('title=')){ | |
| mention = part.match('title=\"(.+?)\"'); | |
| mention = urlencode.decode(mention[1], 'utf8'); | |
| new_line += mention; | |
| new_line += part.substring(part.indexOf('/>')+2); | |
| }else{ | |
| new_line += part; | |
| } | |
| } | |
| return new_line | |
| } | |
| function appendContent(content) { | |
| if(content.hasOwnProperty('text')){ | |
| append(util.format('%s\n', | |
| handleHashtagAndUrl(content['text']))); | |
| } | |
| if(content.hasOwnProperty('images')){ | |
| for(var m = 0; m < content['images'].length; m++) { | |
| img = content['images'][m] | |
| append(util.format('<img src="%s" width="50%" height="50%" align="middle"/>\n', | |
| img['large']['url'])); | |
| } | |
| } | |
| if(content.hasOwnProperty('files')){ | |
| append('\n__分享文件:__\n'); | |
| for(var f = 0; f < content['files'].length; f++) { | |
| file = content['files'][f] | |
| append(util.format('[%s](%s)\n', file['name'], 'fileulrxxxxxxxxxxxxxxxxxxxfileulr')); | |
| } | |
| } | |
| } | |
| var json = require('./raw.json'); | |
| init(); | |
| for(var i = 0; i < json.length; i++) { | |
| // for(var i = 0; i < 3; i++) { | |
| var j = json[i]; | |
| if(!([15288854518512, 51118552582544, 48412842412188, 15525241224552].includes(j['topic_id']))){ | |
| switch(j['type']){ | |
| case "talk": | |
| talk = j['talk'] | |
| append(util.format('<img src="%s" width="25px"/> __%s__ on %s:\n\n', | |
| talk['owner']['avatar_url'], | |
| tryAlias(talk['owner']), | |
| // talk['owner']['name'], | |
| j['create_time'].substring(0,10))); | |
| appendContent(talk); | |
| break; | |
| case "q&a": | |
| if(j.hasOwnProperty('answer')){ | |
| question = j['question']; | |
| answer = j['answer']; | |
| append(util.format('<img src="%s" width="25px"/> __%s__ on %s:\n\n', | |
| answer['owner']['avatar_url'], | |
| tryAlias(answer['owner']), | |
| // talk['owner']['name'], | |
| j['create_time'].substring(0,10))); | |
| if(question["anonymous"] == true){ | |
| append('> 匿名用户'); | |
| }else{ | |
| append(util.format('> %s', tryAlias(question['owner']))); | |
| } | |
| append(util.format(' 提问:\n%s\n\n', handleHashtagAndUrl(question['text']))); | |
| appendContent(answer); | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| append('\n'); | |
| if(j['comments_count']>0){ | |
| append('</br>'); | |
| for(var c = 0; c < j['show_comments'].length; c++) { | |
| cmt = j['show_comments'][c] | |
| if(cmt.hasOwnProperty('repliee')){ | |
| append(util.format('<img src="%s" width="25px"/> __%s__ to <img src="%s" width="25px"/> __%s__: %s\n', | |
| cmt['owner']['avatar_url'], | |
| tryAlias(cmt['owner']), | |
| // cmt['owner']['name'], | |
| cmt['repliee']['avatar_url'], | |
| tryAlias(cmt['repliee']), | |
| // cmt['repliee']['name'], | |
| // handleHashtagAndUrl(escapeNewLineInAComment(cmt['text'])))); | |
| handleHashtagAndUrl(cmt['text']))); | |
| }else{ | |
| append(util.format('<img src="%s" width="25px"/> __%s__: %s\n', | |
| cmt['owner']['avatar_url'], | |
| tryAlias(cmt['owner']), | |
| // cmt['owner']['name'], | |
| handleHashtagAndUrl(cmt['text']))); | |
| // handleHashtagAndUrl(escapeNewLineInAComment(cmt['text'])))); | |
| } | |
| } | |
| } | |
| append('\n---\n\n') | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment