-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
987 lines (870 loc) · 28.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
const qs = require('qs')
const dayjs = require('dayjs')
const request = require('request-promise')
const cheerio = require('cheerio')
const parseUrl = require('./parse-wechat-url')
const errors = require('./errors')
const unescape = require('lodash.unescape')
const {
getParameterByName,
normalizeUrl
} = require('./util')
const video = require('./video')
const defaultConfig = {
shouldReturnRawMeta: false,
shouldReturnContent: true,
shouldFollowTransferLink: true,
shouldExtractMpLinks: false,
shouldExtractTags: false,
shouldExtractRepostMeta: false
}
const basic = {}
basic.accountId = ''
basic.accountAvatar = ''
basic.accountBiz = null
basic.accountBizNumber = null
basic.accountName = null
const getError = function(code) {
return {
done: false,
code: code,
msg: errors[code]
}
}
const extract = async function(html, options = {}) {
const {
shouldReturnRawMeta,
shouldReturnContent,
shouldFollowTransferLink,
shouldExtractMpLinks,
shouldExtractTags,
shouldExtractRepostMeta
} = Object.assign({}, defaultConfig, options)
let paramType = 'HTML' // 参数为 URL 还是 HTML
let url = null
let rawUrl = null
if (options.url) {
url = normalizeUrl(options.url)
}
let type = 'post'
let hasCopyright = false
let shareContentTpl
if (!html) {
return getError(2001)
}
// 参数错误
// 支持地址
if (/^http/.test(html)) {
html = normalizeUrl(html)
if (!/http(s?):\/\/mp.weixin.qq.com/.test(html) && !/http(s?):\/\/weixin.sogou.com/.test(html)) {
return getError(2009)
}
paramType = 'URL'
rawUrl = html
if (!url) {
url = html
}
let host = 'mp.weixin.qq.com'
if (/http(s?):\/\/weixin.sogou.com/.test(html)) {
host = 'weixin.sogou.com'
}
try {
html = await request({
uri: html,
method: 'GET',
headers: {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': host
}
})
// unknown purpose
// if (html.includes('location.replace')) {
// const rs = html.match(/<script[\s\S]*?>([\s\S]*?)<\/script>/gi)
// if (rs && rs[0]) {
// const code = rs[0].split('\n').filter(one => {
// return !one.includes('location.replace') && !one.includes('script>')
// }).join('\n') + '\n return url;'
// try {
// const fn = new Function(code)
// return await extract(fn(), options)
// } catch (e) {
// return getError(1005)
// }
// }
// }
} catch (e) {
return getError(1002)
}
} else {
html = html.replace(/\\n/g, '')
}
if (!html) {
return getError(1003)
}
if (html.includes('访问过于频繁') && !html.includes('js_content')) {
return paramType === 'URL' ? getError(1004) : getError(2010)
} else if (html.includes('链接已过期') && !html.includes('js_content')) {
return getError(2002)
} else if (html.includes('被投诉且经审核涉嫌侵权,无法查看')) {
return getError(2003)
} else if (html.includes('该公众号已迁移')) {
const match = html.match(/var\stransferTargetLink\s=\s'(.*?)';/)
if (match && match[1]) {
if (shouldFollowTransferLink) {
return await extract(match[1])
} else {
return {
...getError(1006),
url: match[1]
}
}
} else {
return getError(2004)
}
} else if (html.includes('该内容已被发布者删除')) {
return getError(2005)
} else if (html.includes('此内容因违规无法查看')) {
return getError(2006)
} else if (html.includes('此内容发送失败无法查看')) {
return getError(2007)
} else if (html.includes('由用户投诉并经平台审核,涉嫌过度营销、骚扰用户')) {
return getError(2011)
} else if (html.includes('此帐号已被屏蔽') && !html.includes('id="js_content"')) {
return getError(2012)
} else if (html.includes('此帐号已自主注销') && !html.includes('id="js_content"')) {
return getError(2013)
} else if (!html.includes('id="js_content"') && html.includes('此帐号处于帐号迁移流程中')) {
return getError(2015)
} else if (html.includes('page_rumor') && !html.includes('id="js_content"')) {
return getError(2014)
} else if (html.includes('投诉类型') && html.includes('冒名侵权')) {
return getError(2016)
} else if (!html.includes('id="js_content"') && html.includes('参数错误') && html.includes('appmsg/error.html')) {
return getError(2009)
} else if (!html.includes('id="js_content"') && !html.includes('id=\\"js_content\\"')) {
// http://mp.weixin.qq.com/s?__biz=MjM5ODIyMTE0MA==&mid=2650971473&idx=1&sn=f529f2a74fac89ed2a8ca8f7a44d93b3&chksm=bd38396a8a4fb07ce4ebab564de2ef01c2d50d60a225328c987cbf66e6167d067bc45f1527d3#rd
// 图片类型但是没有 js_content 容器
if (html.includes('cover_url')) {
type = 'image'
} else {
return getError(1000)
}
}
html = html.replace('>微信号', ' id="append-account-alias">微信号')
.replace('>功能介绍', ' id="append-account-desc">功能介绍')
.replace(/\n\s+<script/g, '\n\n<script')
const $ = cheerio.load(html, {
decodeEntities: false
})
// 原创
if ($('#copyright_logo') && $('#copyright_logo').text().includes('原创')) {
hasCopyright = true
}
// 检查是否为视频类型
const hasVideo = /video/.test($('body').attr('class'))
if (hasVideo) {
type = 'video'
}
const hasImage = $('#js_content > #img_list')
if (hasImage.length) {
type = 'image'
}
const hasShare = $('#js_share_content')
if (hasShare.length) {
type = 'repost'
}
// https://mp.weixin.qq.com/s?__biz=MzIxNDEzNzI4Mg==&mid=2653326714&idx=5&sn=838a05d4d37b9b9cd286dab03b6b610a&chksm=8c7e0dd7bb0984c1eaba456084ca3accab603b102afe847087dbcdbb2842e91cb2de1142bb1a&scene=27#wechat_redirect
if ($('.page_share_audio').length || $('#voice_parent').length) {
type = 'voice'
}
if (/share_media_text/.test(html)) {
type = 'text'
}
// @todo 检查是否为图片类型
// @todo 链接已过期
const expire = $('.weui-msg .weui-msg__title').text()
if (expire.trim() === '链接已过期') {
return getError(2002)
}
const error = $('.global_error_msg.warn').text()
if (error.trim().includes('系统出错')) {
return getError(2008)
}
basic.accountName = $('.profile_nickname').text()
// alias
const accountAliasPrev = $('#append-account-alias')
let accountAlias = accountAliasPrev.siblings('span').text()
const accountDescPrev = $('#append-account-desc')
let accountDesc = accountDescPrev.siblings('span').text()
// 20221218 patch
if (!accountDesc) {
const $accountDesc = $('.profile_meta_value')
if ($accountDesc[1]) {
try {
const text = $accountDesc[1].children[0].data
if (text.length > 10) { // it may go wrong when html is changed
accountDesc = text
}
} catch (e) {
}
}
}
const post = {
msg_has_copyright: hasCopyright
}
post.msg_content = null
if (shouldReturnContent) {
post.msg_content = $('#js_content').html()
}
// 获取 block
const rs = html.match(/<script[\s\S]*?>([\s\S]*?)<\/script>/gi)
// 作者信息
let msgAuthor = null
// const $author = $('.rich_media_meta_text')
// if ($author.length) {
// let info = $author.text().trim()
// //20180622 布局变动
// if (info.includes('原创:')) {
// info = info.replace('原创:', '').trim()
// }
// info = info.replace('\n', '').replace('发表于', '')
// post.msg_author = info.trim()
// }
// 20221218 patch
// get from meta first
try {
const text = $("meta[name='author']").attr("content")
if (text) {
post.msg_author = text
}
} catch (e) {
const $author = $('#js_author_name')
if ($author.length) {
let info = $author.text().trim()
if (info.length) {
post.msg_author = info
}
}
}
let extractExtra = false
const extra = {
biz: null,
sn: null,
mid: null,
idx: null,
msg_title: null,
user_name: null,
nick_name: null,
hd_head_img: null
}
const extraFields = Object.keys(extra)
for (let i = 0; i < rs.length; i++) {
const script = rs[i]
// image type
if (script.includes('picture_page_info_list') && script.includes('https://mmbiz.qpic.cn')) {
const lines = script.split('\n')
const _script = lines.slice(1, lines.length - 2).join('\n').trim().replace(/^\(function\(\) {/, '')
.replace(/}\)\(\);$/, '')
try {
const code = `var x = {}; ${_script} \n return x;`
.replace(/window\./g, 'x.')
.replace('//g', '/\\n/g')
const fn = new Function(code)
const result = fn()
if (result.picture_page_info_list) {
extraFields.picture_page_info_list = result.picture_page_info_list
}
} catch (e) {
}
}
if (type === 'voice' && script.includes('voiceid')) {
const lines = script.split(/\n|\r/).filter(one => one.includes('voiceid')).sort((a, b) => a.length > b.length ? -1 : 1)
if (lines.length) {
const val = lines[0].replace(/'|"|:|voiceid|,/g, '')
if (val) {
post.msg_source_url = `https://res.wx.qq.com/voice/getvoice?mediaid=${val.trim()}`
}
}
}
if (!extractExtra) {
// biz
extraFields.forEach(field => {
const reg = new RegExp(`var\\s+${field}\\s*=`)
if (reg.test(script)) {
try {
const line = script.split('\n').filter(one => reg.test(one))
const fn = new Function(`${line} \n return ${field}`)
extra[field] = fn()
} catch (e) {
console.log('error', e)
}
if (!extractExtra) {
extractExtra = true
}
}
if (!extra[field]) {
const reg2 = new RegExp(`window\.${field}\\s*=`)
if (reg2.test(script)) {
try {
const line = script.split('\n').filter(one => reg2.test(one))
const code = `window = {}; ${line} \n return window.${field}`
const fn = new Function(code)
extra[field] = fn()
} catch (e) {
console.log(e)
}
if (!extractExtra) {
extractExtra = true
}
}
}
})
if (extractExtra) {
basic.accountBiz = extra.biz
if (basic.accountBiz) {
basic.accountBizNumber = Buffer.from(basic.accountBiz, 'base64').toString() * 1
}
post.msg_sn = extra.sn || null
post.msg_idx = extra.idx ? extra.idx * 1 : null
post.msg_mid = extra.mid ? extra.mid * 1 : null
}
}
extraFields.forEach(field => {
if (!extra[field]) {
const reg3 = new RegExp(`d\.${field}\\s*=`)
if (reg3.test(script)) {
let code
try {
let line = script.split('\n').filter(one => reg3.test(one))
if (line.length) {
line = line[0]
code = `d = {}; xml = false;
\nfunction getXmlValue (path) {
return false
}
\n${line} \n return d.${field}`
code = code.replace(/;,/g, ';')
const fn = new Function(code)
extra[field] = fn()
}
} catch (e) {
}
if (!extractExtra) {
extractExtra = true
}
}
}
})
// 视频
if (['video', 'text'].includes(type) && script.includes('d.title')) {
try {
video({
post,
basic,
script,
getError,
html,
$,
shouldReturnRawMeta
})
} catch (e) {
// skip, there is fallback in the end
// console.log('here')
// return getError(1005)
}
}
if ((type === 'image' || type === 'voice') && script.includes('d.title =')) {
const lines = script.split('\n').filter(line => !!line.trim())
let code = lines.filter((line, index) => /d\./.test(line) || (lines[index - 1] && lines[index - 1].includes('d.') && !line.includes('}')))
code = `var d = {};
\nfunction getXmlValue (path) {
return false
}\n` + code.join('\n').replace('var d = _g.cgiData;', 'var d = {}') + '\n return d;'
let data = {}
code = `var _g = {};` + code
try {
code = `var _g = {};` + code
const fn = new Function(code)
data = fn()
accountName = data.nick_name
basic.accountAvatar = data.hd_head_img
basic.accountId = data.user_name
// biz
if (!basic.accountBiz && data.biz) {
basic.accountBiz = data.biz
basic.accountBizNumber = Buffer.from(basic.accountBiz, 'base64').toString() * 1
}
// 标题
post.msg_title = data.title
post.msg_desc = null
post.msg_cover = null
post.msg_link = data.msg_link || null
post.msg_article_type = null
// sn, idx, mid
post.msg_sn = data.sn || null
post.msg_idx = data.idx ? data.idx * 1 : null
post.msg_mid = data.mid ? data.mid * 1 : null
// 视频链接赋值于 source_url
if (type === 'video') {
const vidMatch = html.match(/vid\s*:\s*'(.*?)'/)
if (vidMatch && vidMatch[1]) {
data.vid = vidMatch[1]
// 旧版 vid 已经不适用
// post.msg_source_url = 'http://v.qq.com/x/page/' + vid + '.html'
}
if (!post.msg_cover) {
// 旧版废弃
// post.msg_cover = `https://vpic.video.qq.com/60643382/${vid}.png`
post.msg_cover = $("meta[property='og:image']").attr("content")
}
}
// 视频只有标题 + 内容,内容直接从 meta 里取
if (type === 'video' || type === 'voice') {
const description = $("meta[name='description']").attr("content")
post.msg_content = description
}
// 发布时间
if (data.create_time) {
post.msg_publish_time = new Date(data.create_time * 1000)
post.msg_publish_time_str = dayjs(post.msg_publish_time).format('YYYY/MM/DD HH:mm:ss')
}
if (shouldReturnRawMeta) {
post.raw_data = data
}
} catch (e) {
return getError(1005)
}
}
// 图文
if ((type === 'post' || type === 'repost') && script.includes('var msg_link = ')) {
const lines = script.split('\n')
let code = lines.slice(1, lines.length - 1).filter(line => {
return !line.includes('var title')
}).map(line => {
// 特殊符号可能会导致解析出 bug
if (/var\s+msg_desc/.test(line)) {
line = line.replace(/`/g, "'")
line = line.replace(/"/g, '`')
}
return line
}).join('\n')
code = `var window = {
location: {
protocol: 'https'
}
};\nvar document={
addEventListener: function () {},
getElementById: function () {
return {
classList: {
remove: function () {},
add: function () {}
}
}
}
};\nvar location={protocol: "https"};\n` + code
let rs = ';\nvar rs = {'
code.match(/var\s(.*?)\s=/g).map(key => key.split(' ')[1]).forEach(key => {
if (key !== 'window') {
rs += `"${key}": typeof ${key} !== 'undefined' ? ${key} : null,`
}
})
rs += '\n}\n return rs \n'
code += rs
let data = {}
try {
code = ` String.prototype.html = function(encode) {
var replace =["'", "'", """, '"', " ", " ", ">", ">", "<", "<", "¥", "¥", "&", "&"];
var replaceReverse = ["&", "&", "¥", "¥", "<", "<", ">", ">", " ", " ", '"', """, "'", "'"];
var target;
if (encode) {
target = replaceReverse;
} else {
target = replace;
}
for (var i=0,str=this;i< target.length;i+= 2) {
str=str.replace(new RegExp(target[i],'g'),target[i+1]);
}
return str;
};
` + code
const fn = new Function(code)
data = fn()
} catch (e) {
return getError(1005)
}
// 20221218 patch
if (!basic.accountBiz) {
const reg = new RegExp(`var\\s+biz\\s*=`)
const matched = html.split('\n').find(line => reg.test(line) && line.length > 10)
if (matched) {
const fn = new Function(` ${matched}; return biz; `)
try {
const rs = fn()
if (rs) {
basic.accountBiz = rs
basic.accountBizNumber = Buffer.from(basic.accountBiz, 'base64').toString() * 1
}
} catch (e) {
console.log('warning', e)
}
}
}
const fields = ['msg_title', 'msg_desc', 'msg_link', 'msg_source_url']
fields.forEach(key => {
post[key] = data[key] || null
})
post.msg_cover = data.msg_cdn_url
post.msg_article_type = data['_ori_article_type'] || null
post.msg_publish_time = new Date(data.ct * 1000)
post.msg_publish_time_str = dayjs(post.msg_publish_time).format('YYYY/MM/DD HH:mm:ss')
if (shouldReturnRawMeta) {
post.raw_data = data
}
basic.accountId = data.user_name
basic.accountAvatar = data.ori_head_img_url
if (!basic.accountName && data.nickname) {
basic.accountName = data.nickname
}
}
}
// 有可能没有时间
if (!post.msg_publish_time) {
let date = $('#post-date').text()
if (date) {
post.msg_publish_time = new Date(date)
}
}
if (!post.msg_publish_time) {
let date = $('#publish_time').text()
if (date) {
post.msg_publish_time = new Date(date)
}
}
// 获取 .ct
if (!post.msg_publish_time) {
if (html.includes('.ct')) {
const line = html.split('\n').find(one => one.includes('.ct'))
const reg = /'(\d+)'/g;
const matched = reg.exec(line)
if (matched && matched[1].length >= 10) {
post.msg_publish_time = new Date(matched[1] * 1000)
}
}
}
// 有可能标题不存在
if (!post.msg_title) {
let title = $('.rich_media_title').text()
if (title) {
post.msg_title = title.trim()
}
}
// 转发类型可能 name 没有
if (!post.account_name) {
let name = $('.account_nickname_inner').text()
if (name) {
post.account_name = name.trim()
}
}
post.msg_type = type
// 有可能 ori_head_img_url 不存在,避免被设置成 /132
if (post.ori_head_img_url && post.ori_head_img_url.length < 10) {
post.ori_head_img_url = null
}
// 新注册公众号没有头像,置为 null
if (basic.accountAvatar.length < 10) {
basic.accountAvatar = null
}
// 有可能缺失 mid idx 等信息,从 url 中进行解析
if (post.msg_link && post.msg_link.includes('biz')) {
const parseParams = parseUrl(post.msg_link)
const list = ['mid', 'sn', 'idx']
list.forEach(field => {
if (!post[`msg_${field}`] && parseParams[field]) {
post[`msg_${field}`] = parseParams[field]
}
})
}
// 转载类型,内容不在 js_content 里
if (type === 'repost') {
let html = $('#content_tpl').html()
html = html.replace(/<img[^>]*>/g, '<p>[图片]</p>');
html = html.replace(/<iframe [^>]*?class=\"res_iframe card_iframe js_editor_card\"[^>]*?data-cardid=\"\"[^>]*?><\/iframe>/ig, '<p>[卡券]</p>');
html = html.replace(/<mpvoice([^>]*?)js_editor_audio([^>]*?)><\/mpvoice>/g, '<p>[语音]</p>');
html = html.replace(/<mpgongyi([^>]*?)js_editor_gy([^>]*?)><\/mpgongyi>/g, '<p>[公益]</p>');
html = html.replace(/<qqmusic([^>]*?)js_editor_qqmusic([^>]*?)><\/qqmusic>/g, '<p>[音乐]</p>');
html = html.replace(/<mpshop([^>]*?)js_editor_shop([^>]*?)><\/mpshop>/g, '<p>[小店]</p>');
html = html.replace(/<iframe([^>]*?)class=[\'\"][^\'\"]*video_iframe([^>]*?)><\/iframe>/g, '<p>[视频]</p>');
html = html.replace(/(<iframe[^>]*?js_editor_vote_card[^<]*?<\/iframe>)/gi, '<p>[投票]</p>');
html = html.replace(/<mp-weapp([^>]*?)weapp_element([^>]*?)><\/mp-weapp>/g, '<p>[小程序]</p>');
html = html.replace(/<mp-miniprogram([^>]*?)><\/mp-miniprogram>/g, '<p>[小程序]</p>');
html = html.replace(/<br\s*\/>/g, 'WEEXTRACT')
const $$ = cheerio.load(html, {
decodeEntities: false
})
let processedContent = $$.text()
.replace(/</g, '<')
.replace(/>/g, '>')
.trim().substr(0, 140)
const digest = processedContent.split('WEEXTRACT').map(function(line) {
return '<p>' + line + '</p>';
}).join('')
$('#js_content').append(digest)
const notice = $.html('.share_notice')
const content = $.html('#js_share_content')
post.msg_content = `<div>${notice}${content}</div>`
}
// 使用图片作为 cover
if (type === 'image' && !post.msg_cover) {
// old version
const image = $('#img_list > img').eq(0).attr('src')
if (image) {
post.msg_cover = image
}
if (!post.msg_cover) {
post.msg_cover = $("meta[property='og:image']").attr("content")
}
}
if (/document\.write/.test(post.msg_content)) {
const reg = /<script[\s\S]*?>([\s\S]*?)<\/script>/
const rs = post.msg_content.match(reg)
if (rs) { // 有可能只是正文里提到了 document.write
const script = rs[0]
if ((type === 'voice' || type === 'image') && /document\.write/.test(script)) {
try {
const code = script
.split('.replace')[0]
.split('\n')
.filter(one => !one.includes('<script') && !one.includes('script>'))
.join('\n')
.replace('document.write', 'return ') + ')'
const fn = new Function(code)
post.msg_content = post.msg_content.replace(reg, fn())
} catch (e) {
// 此处在 v1.2.0 之后不报错,因为不影响整体流程
// return getError(1005)
}
}
}
}
// 避免有换行符
if (post.msg_content) {
post.msg_content = post.msg_content.trim().replace(/\n/g, "<br>")
}
if (!basic.accountId && extra.user_name) {
basic.accountId = extra.user_name
}
if (!basic.accountName && extra.nick_name) {
basic.accountName = extra.nick_name
}
if (!basic.accountAvatar && extra.hd_head_img) {
basic.accountAvatar = extra.hd_head_img
}
if (!basic.accountName) {
if ($('.wx_follow_nickname')) {
const name = $('.wx_follow_nickname').text()
if (name) {
basic.accountName = name.trim()
}
}
}
const data = {
account_name: basic.accountName,
account_alias: accountAlias,
account_avatar: basic.accountAvatar,
account_description: accountDesc,
account_id: basic.accountId,
account_biz: basic.accountBiz,
account_biz_number: basic.accountBizNumber,
account_qr_code: `https://open.weixin.qq.com/qr/code?username=${basic.accountId || accountAlias}`,
...post
}
// 空字段置为 null
for (let i in data) {
if (data[i] === '') {
data[i] = null
}
}
// 文字类型
if (!data.msg_title && data.msg_type === 'post') {
data.msg_type = 'text'
const title = $("meta[property='og:title']").attr("content")
const desc = $("meta[property='og:description']").attr("content")
if (title) {
data.msg_title = title
const rawContent = $('#js_panel_like_title').html()
// 使用有换行格式
if (rawContent) {
data.msg_content = rawContent.trim().replace(/\n/g, '<br/>')
} else {
data.msg_content = title
}
}
// 可以没有标题,https://mp.weixin.qq.com/s?__biz=MjM5NDcwOTk3NQ==&mid=2651469811&idx=1&sn=21843009d7489a71597476b3fa59e6ca&chksm=bd7d3f4b8a0ab65d53371e763c1cd528df420aac9f3cbed2043d860b4bc7244207076a25a9b5#rd
if (!title && desc) {
data.msg_title = desc
}
}
// 时间参数兜底
if (!data.msg_publish_time) {
const matched = html.match(/d.ct.=."(\d+)"/)
if (matched && matched[1]) {
data.msg_publish_time = new Date(matched[1] * 1000)
data.msg_publish_time_str = dayjs(data.msg_publish_time).format('YYYY/MM/DD HH:mm:ss')
}
}
// 链接参数
if (!data.msg_mid || !data.msg_link) {
let url = null
if (options.url && /biz/.test(options.url)) {
url = options.url
}
if (!url) {
if (rawUrl) {
url = rawUrl
}
}
if (!url) {
url = $("meta[property='og:url']").attr("content")
}
if (url && /^http/.test(url) && /mid/.test(url) && /__biz/.test(url)) {
url = url.replace(/&/g, '&')
if (!data.msg_link) {
data.msg_link = url
}
if (!data.msg_mid) {
data.msg_mid = getParameterByName('mid', url)
}
if (!data.msg_idx) {
data.msg_idx = getParameterByName('idx', url)
}
if (!data.msg_sn) {
data.msg_sn = getParameterByName('sn', url)
}
}
}
// 标题 entities 处理
data.msg_title = unescape(data.msg_title)
// 视频
if (data.msg_type === 'video') {
if (!data.msg_content) {
data.msg_content = data.msg_title
} else {
data.msg_content = data.msg_content.replace(/\\x26/g, '&')
data.msg_content = data.msg_content.replace(/\\x0a/g, '<br/>')
data.msg_content = convertHtml(data.msg_content)
}
}
if (!data.msg_title) {
const title = $("meta[property='og:title']").attr("content")
if (title) {
data.msg_title = title
}
}
if (data.msg_content.includes('<script') && data.msg_content.includes('script>') && data.msg_content.includes('nonce=')) {
const desc = $("meta[property='og:description']").attr("content")
if (desc) {
data.msg_content = desc
}
}
if (!data.msg_title || !data.msg_publish_time) {
return getError(1001)
}
// 文字类型没有内容,使用标题
if (type === 'text' && !data.msg_content && data.msg_title) {
data.msg_content = data.msg_title
}
// 图片类型时使用图片+文字
// deprecated
if (type === 'image') {
data.msg_content = `<img src="${data.msg_cover}" style="max-width:100%"/><br>${data.msg_title}`
}
if (shouldExtractMpLinks) {
const mpLinks = []
const $links = $('a')
$links.each((i, ele) => {
const $one = $(ele)
const href = $one.attr('href')
if (href && href.includes('mp.weixin.qq.com')) {
mpLinks.push({
title: $one.text(),
href: href
})
}
})
data.mp_links_count = mpLinks.length
data.mp_links = mpLinks
}
if (shouldExtractTags) {
const tags = []
const $items = $('.article-tag__item-wrp')
if ($items.length) {
$items.each((i, ele) => {
const $this = $(ele)
try {
const url = $this.attr('data-url')
const name = $this.find('.article-tag__item').text()
let count = $this.find('.article-tag__item-num').text()
if (name) {
if (!count && $items.length === 1) {
const $count = $('.article-tag-card__right')
if ($count.length) {
count = $count.text().replace('个', '')
}
}
tags.push({
id: getParameterByName('album_id', url) || getParameterByName('tag_id', url) || null,
url,
name: name.replace(/^#/, ''),
count: count.replace(/\D/g,'') * 1
})
}
} catch (e) {
console.log(e)
}
})
}
data.tags = tags
}
if (shouldExtractRepostMeta) {
if (html.includes('copyright_info') && html.includes('original_primary_nickname')) {
const name = $('.original_primary_nickname').text()
if (name) {
data.repost_meta = {
account_name: name
}
}
}
}
if (data.msg_link && data.msg_link.includes('&')) {
data.msg_link = data.msg_link.replace(/&/g, '&')
}
// picture_page_info_list = image type
// https://mp.weixin.qq.com/s?__biz=MzAxNDQ4MzQzMQ==&mid=2649901351&idx=1&sn=033956c31beea4327b304f43a8ca5b5d&chksm=839443afb4e3cab9466f6a9e7b4752cdcfeac6111a620acff0a68a01bf1331afa987eba65995#rd
if (extraFields.picture_page_info_list) {
data.msg_type = 'image'
data.msg_content = `${data.msg_title}<br>`
for (const one of extraFields.picture_page_info_list) {
data.msg_content += `<img src="${one.cdn_url}" style="max-width:100%"/><br><br>`
}
}
return {
code: 0,
done: true,
data: data
}
}
function convertHtml(text) {
const target = ["'", "'", """, '"', " ", " ", ">", ">", "<", "<", "¥", "¥", "&", "&"]
for (var i = 0, str = text; i < target.length; i += 2) {
str = str.replace(new RegExp(target[i], 'g'), target[i + 1])
}
return str
}
module.exports = {
extract
}