/
feedNextData.pas
441 lines (413 loc) · 12.2 KB
/
feedNextData.pas
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
unit feedNextData;
interface
uses eaterReg, jsonDoc;
type
TNextDataFeedProcessor=class(TFeedProcessor)
private
FFeedURL:WideString;
procedure ProcessArticle(Handler:IFeedHandler;jdata:IJSONDocument);
procedure ProcessCompositions(Handler:IFeedHandler;jcompos:IJSONDocArray);
public
function Determine(Store: IFeedStore; const FeedURL: WideString;
var FeedData: WideString; const FeedDataType: WideString): Boolean;
override;
procedure ProcessFeed(Handler: IFeedHandler; const FeedData: WideString);
override;
end;
implementation
uses SysUtils, Variants, eaterUtils, eaterSanitize;
{ TNextDataFeedProcessor }
function TNextDataFeedProcessor.Determine(Store: IFeedStore;
const FeedURL: WideString; var FeedData: WideString;
const FeedDataType: WideString): Boolean;
begin
Result:=Store.CheckLastLoadResultPrefix('NextData') and
FindPrefixAndCrop(FeedData,'<script id="__NEXT_DATA__" type="application/json">');
if Result then FFeedURL:=FeedURL;
end;
procedure TNextDataFeedProcessor.ProcessFeed(Handler: IFeedHandler;
const FeedData: WideString);
var
jdoc,jd1,jd2,jn0,jn1:IJSONDocument;
jcontent,jzones,jarticles,jcompos,jimg,jbody,jcats:IJSONDocArray;
je:IJSONEnumerator;
ci,cj,ck:integer;
itemid,itemurl,p1:string;
pubDate:TDateTime;
title,content:WideString;
tags,v:Variant;
begin
jd1:=JSON;
jcontent:=JSONDocArray;
jzones:=JSONDocArray;
jarticles:=JSONDocArray;
jcompos:=JSONDocArray;
jn0:=JSON(
['homepageBuilder',jarticles
//,'heroArticle',jarticles
,'stickyArticle',jarticles
,'featuredArticles',jarticles
]);
jdoc:=JSON(['props',JSON(['pageProps',
JSON(
['contentState',jd1
,'data',JSON(
['heroHome',jcontent
,'defaultFeedItems',jcontent
//birthdays,series,episodes,popuplarCategories?
,'highlightedContent',jcontent
,'compositions',jcompos
])
,'pageData',JSON(['zones',jzones])
,'entry',jn0
,'latestArticles',jarticles
])
])]);
try
jdoc.Parse(FeedData);
except
on EJSONDecodeException do
;//ignore "data past end"
end;
//old style
je:=JSONEnum(jd1);
while je.Next do
begin
jn0:=JSON(je.Value);
if jn0<>nil then
begin
itemid:=VarToStr(jn0['_id']);
itemurl:=VarToStr(jn0['canonical_url']);
end;
if (jn0<>nil) and (itemid<>'') and (itemurl<>'') then
begin
try
pubDate:=ConvDate1(VarToStr(jn0['display_date']));//publish_date?
except
pubDate:=UtcNow;
end;
if Handler.CheckNewPost(itemid,itemurl,pubDate) then
begin
title:=JSON(jn0['headlines'])['basic'];
jn1:=JSON(jn0['description']);
if jn1=nil then content:='' else content:=HTMLEncode(jn1['basic']);
p1:='';//default;
jn1:=JSON(jn0['promo_items']);
if jn1<>nil then
begin
jn1:=JSON(jn1['basic']);
if jn1<>nil then p1:=VarToStr(jn1['url']);
end;
if p1<>'' then
content:=
'<img class="postthumb" referrerpolicy="no-referrer" src="'+
HTMLEncode(JSON(JSON(jn0['promo_items'])['basic'])['url'])+
'" /><br />'#13#10+content;
Handler.RegisterPost(title,content);
end;
end;
end;
//new style
if jcontent.Count<>0 then
begin
try
jn1:=JSON(JSON(JSON(JSON(JSON(
jdoc['props'])['pageProps'])['seo'])['seomatic'])['metaTitleContainer']);
Handler.UpdateFeedName(JSON(jn1['title'])['title']);
except
//silent
end;
jimg:=JSONDocArray;
jbody:=JSONDocArray;
jcats:=JSONDocArray;
jn0:=JSON(['image',jimg,'body',jbody,'feedCategories',jcats]);
jn1:=JSON;
for ci:=0 to jcontent.Count-1 do
begin
jcontent.LoadItem(ci,jn0);
itemid:=jn0['id'];
itemurl:=FFeedURL+'feed/'+itemid+'-'+jn0['slug']+'/';
try
p1:=VarToStr(jn0['dateUpdated']);
if p1='' then p1:=VarToStr(jn0['postDate']);
pubDate:=ConvDate1(p1);
except
pubDate:=UtcNow;
end;
if Handler.CheckNewPost(itemid,itemurl,pubDate) then
begin
title:=SanitizeTitle(jn0['title']);
if jbody.Count=0 then
content:=VarToStr(jn0['excerpt'])
else
begin
content:='';
for cj:=0 to jbody.Count-1 do
begin
jbody.LoadItem(cj,jn1);
if not(VarIsNull(jn1['intro'])) then
content:=content+'<div class="intro">'+
VarToStr(jn1['intro'])+'</div>'#13#10;
if not(VarIsNull(jn1['text'])) then
content:=content+VarToStr(jn1['text'])+#13#10;
end;
end;
if jimg.Count<>0 then
begin
jimg.LoadItem(0,jn1);
jd1:=JSON(jn1['heroOptimized']);
if jd1=nil then jd1:=JSON(jn1['coverOptimized']);
if jd1<>nil then content:=
'<img class="postthumb" referrerpolicy="no-referrer" src="'+
HTMLEncode(jd1['src'])+
'" /><br />'#13#10+content;
end;
if jcats.Count<>0 then
begin
tags:=VarArrayCreate([0,jcats.Count-1],varOleStr);
for cj:=0 to jcats.Count-1 do
begin
jcats.LoadItem(cj,jn1);
tags[cj]:=jn1['title'];
end;
Handler.PostTags('category',tags);
end;
Handler.RegisterPost(title,content);
end;
end;
end;
//homepagebuilder
if jarticles.Count<>0 then
begin
if (FFeedURL<>'') and (FFeedURL[Length(FFeedURL)]<>'/') then
FFeedURL:=FFeedURL+'/';
try
jn1:=JSON(JSON(jn0['seomatic'])['metaTitleContainer']);
Handler.UpdateFeedName(JSON(jn1['title'])['title']);
except
//silent
end;
//ProcessArticle(Handler,JSON(jn0['heroArticle']));
//ProcessArticle(Handler,JSON(jn0['stickyArticle']));
jn1:=JSON(['cards',jcontent]);
jd2:=JSON;
for ci:=0 to jarticles.Count-1 do
begin
jarticles.LoadItem(ci,jn1);
if jcontent.Count=0 then
begin
v:=jn1['article'];
if VarIsArray(v) then
for cj:=VarArrayLowBound(v,1) to VarArrayHighBound(v,1) do
ProcessArticle(Handler,JSON(v[cj]))
else
begin
jd1:=JSON(v);
if jd1=nil then
ProcessArticle(Handler,jn1)
else
ProcessArticle(Handler,jd1);
end;
end
else
for cj:=0 to jcontent.Count-1 do
begin
jcontent.LoadItem(cj,jd2);
ProcessArticle(Handler,jd2);
end;
end;
end;
//zones
if jzones.Count<>0 then
begin
if (FFeedURL<>'') and (FFeedURL[Length(FFeedURL)]<>'/') then
FFeedURL:=FFeedURL+'/';
try
Handler.UpdateFeedName(JSON(JSON(JSON(JSON(
jdoc['props'])['pageProps'])['pageData'])['pageMetas'])['title']);
except
//ignore
end;
jn0:=JSON(['blocks',jcontent]);
jarticles:=JSONDocArray;
jn1:=JSON(['articles',jarticles]);
jimg:=JSONDocArray;
jbody:=JSONDocArray;
jd1:=JSON(['media',jimg,'body',jbody]);
jd2:=JSON;
for ci:=0 to jzones.Count-1 do
begin
jzones.LoadItem(ci,jn0);
for cj:=0 to jcontent.Count-1 do
begin
jcontent.LoadItem(cj,jn1);
//if not(VarIsNull(jcontent['articleUrl']))?
for ck:=0 to jarticles.Count-1 do
begin
jarticles.LoadItem(ck,jd1);
itemid:=jd1['id'];//?
itemurl:=FFeedURL+jd1['url'];
pubDate:=int64(jd1['pubDate'])/SecsPerDay+UnixDateDelta;
if Handler.CheckNewPost(itemid,itemurl,pubDate) then
begin
title:=SanitizeTitle(jd1['title']);
content:=jd1['chapo'];//??!!
//TODO: if jbody.Count<>0?
if jimg.Count<>0 then
begin
jimg.LoadItem(0,jd2);
content:=
'<img class="postthumb" referrerpolicy="no-referrer" src="'+
HTMLEncode(jd2['source'])+ //jd2['default']?
'" /><br />'#13#10+content;
//alt=" 'title'? 'credit'?
end;
//TODO: 'destinations' Handler.PostTags?
Handler.RegisterPost(title,content);
end;
end;
end;
end;
end;
//compositions
if jcompos.Count<>0 then
begin
try
jn1:=JSON(JSON(JSON(JSON(JSON(
jdoc['props'])['pageProps'])['data'])['pageProperties'])['seo']);
Handler.UpdateFeedName(jn1['title']);
except
//silent
end;
ProcessCompositions(Handler,jcompos);
end;
Handler.ReportSuccess('NextData');
end;
procedure TNextDataFeedProcessor.ProcessArticle(Handler: IFeedHandler;
jdata: IJSONDocument);
var
d1:IJSONDocument;
v:Variant;
i,i1,i2:integer;
itemid,itemurl:string;
pubDate:TDateTime;
title,content:WideString;
tags:Variant;
begin
if (jdata=nil) then itemid:='' else itemid:=VarToStr(jdata['id']);
if itemid<>'' then
begin
itemurl:=VarToStr(jdata['href']);
if (itemurl='') or (itemurl[1]='/') then itemurl:=Copy(itemurl,2,Length(itemurl)-1);
itemurl:=FFeedURL+itemurl;
try
d1:=JSON(jdata['date']);
if d1=nil then
pubDate:=ConvDate1(jdata['dateIso'])
else
pubDate:=ConvDate1(d1['iso']);
except
pubDate:=UtcNow;
end;
if Handler.CheckNewPost(itemid,itemurl,pubDate) then
begin
title:=SanitizeTitle(jdata['title']);
content:=HTMLEncode(jdata['excerpt']);
//image
v:=jdata['image'];
if VarIsArray(v) then d1:=JSON(v[0]) else d1:=nil;
if d1<>nil then
begin
content:=
'<img class="postthumb" referrerpolicy="no-referrer" src="'+
HTMLEncode(d1['src'])+
'" alt="'+HTMLEncode(VarToStr(d1['caption']))+'"'+
'" /><br />'#13#10+content;
end;
v:=jdata['category'];
if VarIsArray(v) then //if VarType(v) = varArray or varUnknown then
begin
d1:=JSON;
i1:=VarArrayLowBound(v,1);
i2:=VarArrayHighBound(v,1);
tags:=VararrayCreate([i1,i2],varOleStr);
for i:=i1 to i2 do
begin
d1:=JSON(v[i]);
tags[i]:=d1['title'];
end;
Handler.PostTags('category',tags);
end;
Handler.RegisterPost(title,content);
end;
end;
end;
procedure TNextDataFeedProcessor.ProcessCompositions(Handler: IFeedHandler;
jcompos: IJSONDocArray);
var
ci,ai:integer;
jc,jm:IJSONDocArray;
jn1,jn2:IJSONDocument;
je:IJSONEnumerator;
itemid,itemurl:string;
pubDate:TDateTime;
title,content:WideString;
v:Variant;
begin
jm:=JSONDocArray;
jc:=JSONDocArray;
jn1:=JSON(['metadata',jm,'compositions',jc]);
for ci:=0 to jcompos.Count-1 do
begin
jcompos.LoadItem(ci,jn1);
jn2:=JSON(jn1['layouts']);
if (jc.Count=0) and (jn2<>nil) then
begin
je:=JSONEnum(jn2);
while je.Next do
begin
v:=je.Value;
if VarIsArray(v) then
begin
for ai:=VarArrayLowBound(v,1) to VarArrayHighBound(v,1) do
jc.Add(JSON(v[ai]));
ProcessCompositions(Handler,jc);
end;
//else raise?
end;
end
else
begin
if jc.Count<>0 then
ProcessCompositions(Handler,jc);
//else?
jn2:=JSON(jn1['action']);
if (jn2<>nil) and (jm.Count<>0) then
begin
itemurl:=jn2['uri'];
itemid:=itemurl;
jm.LoadItem(0,jn2);
pubDate:=int64(jn2['timestamp'])/(SecsPerDay*1000)+UnixDateDelta;
if Handler.CheckNewPost(itemid,itemurl,pubDate) then
begin
content:=HTMLEncode(JSON(jn1['tag'])['text']);//?
title:=SanitizeTitle(JSON(jn1['title'])['text']);
//TODO: Handler.PostTags(... JSON(jn1['tag'])['text']?
jn2:=JSON(jn1['image']);
if jn2<>nil then
content:=
'<img class="postthumb" referrerpolicy="no-referrer" src="'+
HTMLEncode(jn2['url'])+
//'" alt="'+HTMLEncode(jn2['alt'])+
'" /><br />'#13#10+content;
//alt=" 'title'? 'credit'?
//TODO: 'destinations' Handler.PostTags?
Handler.RegisterPost(title,content);
end;
end;
end;
end;
end;
initialization
RegisterFeedProcessor(TNextDataFeedProcessor.Create);
end.