-
Notifications
You must be signed in to change notification settings - Fork 2
/
unGlobalScraper.pas
362 lines (338 loc) · 14.2 KB
/
unGlobalScraper.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
unit unGlobalScraper;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,ZDataset,DBGrids,Controls,Grids,ExtCtrls,Forms,
strutils,contnrs, ghashmap,dialogs, OWideSupp;
type
{ TGlobalScraper }
TGlobalScraper = class
zQuery:TZquery;
dbGrid:TDBGrid;
timer1:TTimer; // pro zavření hlášky s nenalezením a možností vybrat scraper pro
// rescrape nebo pokračovat na další položku
dobaTimer1:Integer;
procedure scrapujVyber;
procedure createNfo(directory:String);
procedure onTimer(sender:TObject);
function notScrapedAction:Boolean;
constructor create(zq:TZquery;dbg:TDBGrid);
destructor destroy;override;
end;
var
globalScraper:TGlobalScraper;
implementation
uses unit1, // resource strings
unit7, // formNastaveni
unit8, // formScraper
unNotSraped, // form for rescrape
unit9, // formScrapujVyber
OXmlCDOM,OXmlUtils; // creating xml
{ TGlobalScraper }
procedure TGlobalScraper.scrapujVyber;
var
i: Integer;
pomStr,PomStr1: String;
Pom: Integer;
scrapovatZnovuToSame : Boolean;
begin
PomStr:='';
PomStr1:='';
{projdi výběr scrapuj položku naplň tabulkaVysledku v FormScrapujVyber unit9}
//nultý řádek v tabulkaVysledku jsou názvy sloupců proto +1
FormScrapujVyber.tabulkaVysledku.RowCount:=dbgrid.SelectedRows.Count+1;
for i:=0 to dbgrid.SelectedRows.Count-1 do
begin
ZQuery.GotoBookmark(dbgrid.SelectedRows.Items[i]);
if ZQuery.FieldByName('DRUH').AsString='series' then
begin // --- řádek je seriál
FormScrapujVyber.tabulkaVysledku.Cells[2,i+1]:='series';
// pro případ, že scraper nenajde jméno ----- begin -----
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]:=
ZQuery.FieldByName('NAZEV_SERIALU').AsString;
//FormScraper.vybranyNazev:=ZQuery.FieldByName('NAZEV_SERIALU').AsString;
// pro případ, že scraper nenajde jméno ----- end -------
if (i<>0) and (ZQuery.FieldByName('NAZEV_SERIALU').AsString=pomStr) then
begin
pomStr:= ZQuery.FieldByName('NAZEV_SERIALU').AsString;
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]:=
FormScrapujVyber.tabulkaVysledku.Cells[0,i];
FormScrapujVyber.tabulkaVysledku.Cells[1,i+1]:=
FormScrapujVyber.tabulkaVysledku.Cells[1,i]; //ZQuery.FieldByName('ROK').AsString;
continue;
end
else
begin
pomStr:= ZQuery.FieldByName('NAZEV_SERIALU').AsString;
{s prázdným názvem házi scrapování error 404}
If ZQuery.FieldByName('NAZEV_SERIALU').AsString ='' then continue;
repeat
PomStr1:= aktualniScraperSerial(ZQuery.FieldByName('NAZEV_SERIALU').AsString);
if PomStr1 <> 'nenalezeno' then
begin
FormScrapujVyber.tabulkaVysledku.Cells[1,i+1]:=PomStr1;
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]:=FormScraper.vybranyNazev;
end
else
scrapovatZnovuToSame:= self.notScrapedAction;
// tady někde bych se měl po změně scraperu vrátit a znovu scrapovat tu samou položku
until not(scrapovatZnovuToSame) or (pomStr<>'nenalezeno');
continue;
end;
end // --- řádek je seriál
else
begin // --- řádek je film
FormScrapujVyber.tabulkaVysledku.Cells[2,i+1]:='film';
// pro případ, že scraper nenajde jméno ----- begin -----
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]:=
ZQuery.FieldByName('NAZEV').AsString;
//FormScraper.vybranyNazev:=ZQuery.FieldByName('NAZEV').AsString;;
// pro případ, že scraper nenajde jméno ----- end -----
{s prázdným názvem házi scrapování error 404}
if ZQuery.FieldByName('NAZEV').AsString ='' then continue;
repeat
pomStr:=aktualniScraperFilm(ZQuery.FieldByName('NAZEV').AsString);
if pomStr <>'nenalezeno' then
begin
FormScrapujVyber.tabulkaVysledku.Cells[1,i+1]:=PomStr;
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]:=FormScraper.vybranyNazev;
end
else
scrapovatZnovuToSame:= self.notScrapedAction;
until not(scrapovatZnovuToSame) or (pomStr<>'nenalezeno');
continue;
end; // --- řádek je film
end;
FormScrapujVyber.mojeAutosize;
Pom:=FormScrapujVyber.ShowModal;
{tabulkaVysledku do položek výběru v DBgrid}
if pom=mrOK then
for i:=0 to dbgrid.SelectedRows.Count-1 do
begin
ZQuery.GotoBookmark(dbgrid.SelectedRows.Items[i]);
ZQuery.Edit;
ZQuery.FieldByName('ROK').AsString:= FormScrapujVyber.tabulkaVysledku.Cells[1,i+1];
if FormScrapujVyber.tabulkaVysledku.Cells[2,i+1]='series' then
ZQuery.FieldByName('NAZEV_SERIALU').AsString:=
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1]
else
ZQuery.FieldByName('NAZEV').AsString:=
FormScrapujVyber.tabulkaVysledku.Cells[0,i+1];
ZQuery.Post;
end;
if pom=mrCancel then FormScrapujVyber.tabulkaVysledku.Clean([gzNormal,gzFixedRows])
end;
procedure TGlobalScraper.createNfo(directory:String);
var
i: Integer;
pomStr,PomStr1,pomPath, pomString: String;
Pom: Integer;
scrapovatZnovuToSame : Boolean;
pomEpisodesInfo:TEpisodeInfoComplete;
procedure createNfoFile(itemType,path:String); // itemType = tvshow or movie
var
xmlNode: TXMLNode;
xmlDoc : IXMLDocument;
pomGenre: TCaption;
j: Integer;
begin
xmlDoc:=CreateXMLDoc(itemType,true);
xmlNode:=xmlDoc.DocumentElement;
// documentNode contains documentElement(so called root element) :-)
// below inserts comment before documentElement(root element)
xmlDoc.GetDocumentNode.InsertComment('created on '+ DateTimeToStr(now) +
' - MediaStub Kodi Creator',xmlNOde);
xmlNode.AddChild('title').AddText(FormScraper.vybranyNazev);
xmlNode.AddChild('year').AddText(FormScraper.vybranyRok);
xmlNode.AddChild('plot').AddText(FormScraper.memDej.Lines.Text);
xmlNode.AddChild('rating').AddText(FormScraper.edtHodnoceni.Text);
pomGenre:= FormScraper.edtZanry.Text;
for j:=1 to WordCount(pomGenre,[',']) do
xmlNode.AddChild('genre').AddText(ExtractWord(j,pomGenre,[',']));
pomPath:=directory+path;
If ForceDirectories(pomPath) then //utf8tosys
begin
xmlDoc.WriterSettings.IndentType:=itIndent;
xmlDoc.SaveToFile(pomPath+itemType+'.nfo');
if aktualniScraperSerial = @(SerialThetvdb) then
begin
FormScraper.imgObrazek.Picture.SaveToFile(pomPath + 'banner.jpg');
getPosterFanart(pomPath,FormScraper.idSerie);
end
else
FormScraper.imgObrazek.Picture.SaveToFile(pomPath+'poster.jpg');
end;
end;
procedure createNfoFileEpizode(path:String;episodesInfo:TEpisodeInfoComplete);
var
xmlNode, xmlNode1: TXMLNode;
xmlDoc : IXMLDocument;
k: Integer;
episodes, sezona, pomSezona, pomEpisode: String;
episodesCount: SizeInt;
pomTitle, pomPlot:string;
begin
episodes:=ZQuery.FieldByName('DILY_NA_DISKU').AsString;
sezona:= ZQuery.FieldByName('SEZONA').AsString;
pomSezona:=ExtractWord(1,sezona,['s']);
RemoveLeadingChars(pomSezona, ['0']);
episodesCount:= WordCount(episodes,['e']);
if episodesCount = 1 then
xmlDoc:=CreateXMLDoc('root',true)
else
xmlDoc:=CreateXMLDoc('multiepisodeinfo',true);
xmlNode:=xmlDoc.DocumentElement;
// documentNode contains documentElement(so called root element) :-)
// below inserts comment before documentElement(root element)
xmlDoc.GetDocumentNode.InsertComment('created on '+ DateTimeToStr(now) +
' - MediaStub Kodi Creator',xmlNOde);
for k:=1 to episodesCount do
begin
pomEpisode:= ExtractWord(k,episodes,['e']);
Removeleadingchars(pomEpisode,['0']);
xmlNode1:=xmlNode.AddChild('episodedetails');
//ShowMessage(episodesInfo.episodeInfoAll['1']['1']['jmeno']);
if episodesInfo.episodeInfoAll['1']['1']['jmeno'] = 'xxxxx' then
begin
pomTitle:= ''; // info neni
pomPlot:=''; // plot není
end
else
begin
pomTitle:= episodesInfo.episodeInfoAll[pomSezona][pomEpisode]['jmeno'];
pomPlot:= episodesInfo.episodeInfoAll[pomSezona][pomEpisode]['obsah'];
end;
xmlNode1.AddChild('title').AddText(pomTitle);
xmlNode1.AddChild('season').AddText(pomSezona);
xmlNode1.AddChild('episode').AddText(pomEpisode);
xmlNode1.AddChild('plot').AddText(pomPlot);
end;
pomPath:=directory+path;
If ForceDirectories(pomPath) then //utf8tosys
begin
xmlDoc.WriterSettings.IndentType:=itIndent;
xmlDoc.SaveToFile(pomPath+sezona+episodes+'.nfo');
end;
end;
begin
PomStr:='';
PomStr1:='';
// možná zbytečné, ještě někdy prověřit :-)
// vytváří se totiž ve funkci unit8.SerialThemoviedbEpisodes()
pomEpisodesInfo:=TEpisodeInfoComplete.create;
{projdi výběr a vytvoř nfo soubor v cestě path}
//nultý řádek v tabulkaVysledku jsou názvy sloupců proto +1
for i:=0 to dbgrid.SelectedRows.Count-1 do
begin
ZQuery.GotoBookmark(dbgrid.SelectedRows.Items[i]);
if ZQuery.FieldByName('DRUH').AsString='series' then
begin // --- řádek je seriál
if (i<>0) and (ZQuery.FieldByName('NAZEV_SERIALU').AsString=pomStr) then
begin
pomStr:= ZQuery.FieldByName('NAZEV_SERIALU').AsString;
pomPath:=ZQuery.FieldByName('DIRECTORY').AsString;
createNfoFileEpizode(pomPath,pomEpisodesInfo);
continue;
end
else
begin
pomStr:= ZQuery.FieldByName('NAZEV_SERIALU').AsString;
{s prázdným názvem házi scrapování error 404}
If ZQuery.FieldByName('NAZEV_SERIALU').AsString ='' then continue;
repeat
PomStr1:= aktualniScraperSerial(ZQuery.FieldByName('NAZEV_SERIALU').AsString);
if PomStr1 <> 'nenalezeno' then
begin
pomPath:=ZQuery.FieldByName('DIRECTORY').AsString;
pomString:= ZQuery.FieldByName('SEZONA').AsString+'\';
pomPath:=StringReplace(pomPath,pomString,'',[rfIgnoreCase]);
createNfoFile('tvshow',pomPath);
// there may be several TV series in selection
FreeAndNil(pomEpisodesInfo);
pomEpisodesInfo:=aktualniScraperEpisody(FormScraper.idSerie);
createNfoFileEpizode(ZQuery.FieldByName('DIRECTORY').AsString,
pomEpisodesInfo);
end
else
scrapovatZnovuToSame:= self.notScrapedAction;
// tady někde bych se měl po změně scraperu vrátit a znovu scrapovat tu samou položku
until not(scrapovatZnovuToSame) or (pomStr<>'nenalezeno');
continue;
end;
end // --- řádek je seriál
else
begin // --- řádek je film
{s prázdným názvem házi scrapování error 404}
if ZQuery.FieldByName('NAZEV').AsString ='' then continue;
repeat
pomStr:=aktualniScraperFilm(ZQuery.FieldByName('NAZEV').AsString);
if pomStr <>'nenalezeno' then
begin
pomPath:=ZQuery.FieldByName('DIRECTORY').AsString;
createNfoFile('movie',pomPath);
end
else
scrapovatZnovuToSame:= self.notScrapedAction;
until not(scrapovatZnovuToSame) or (pomStr<>'nenalezeno');
continue;
end; // --- řádek je film
end;
freeandnil(pomEpisodesInfo);
end;
constructor TGlobalScraper.create(zq: TZquery;dbg:TDBGrid);
begin
self.zQuery:=zq;
self.dbGrid:=dbg;
self.dobaTimer1:=3;
self.timer1:=TTimer.create(nil);
self.timer1.enabled:=False;
self.timer1.Interval:=1000;
self.timer1.OnTimer:=@self.onTimer; // více na: http://forum.lazarus.freepascal.org/index.php?topic=7046.0
end;
destructor TGlobalScraper.destroy;
begin
self.timer1.Destroy;
inherited;
end;
procedure TGlobalScraper.onTimer(sender: TObject);
begin
dobaTimer1:=dobaTimer1-1;
if dobaTimer1 = 0 then
begin
Screen.GetCurrentModalForm.Close;
timer1.Enabled:=False; // možná zbytečné :-)
end;
frmNotScraped.btnContinue.Caption:=rsPokraOvatBez +
' ('+inttostr(dobaTimer1)+')';
end;
function TGlobalScraper.notScrapedAction:Boolean;
begin
dobaTimer1:=3;
timer1.Interval:=1000;
timer1.Enabled:=True;
frmNotScraped.btnChange.Caption:=rsZmNitScraper;
frmNotScraped.btnContinue.Caption:= rsPokraOvatBez +
' ('+inttostr(dobaTimer1)+')';
case frmNotScraped.ShowModal of
mrRetry: begin
timer1.Enabled:=False;
FormNastaveni.ShowModal ;
FormScrapujVyber.mojeAutosize;
notScrapedAction:=True;
end;
mrOK: notScrapedAction:=False;
//mrClose:notScrapedAction:=False;
mrCancel:notScrapedAction:=False;
end;
timer1.Enabled:=False;
end;
initialization
begin
globalScraper:=TGlobalScraper.create(nil,nil); // v čase inicializace není DBGrid1 vytvořena
// !!!!! need to asign real dbgrid and zquery used in form1 in TForm1.create
// !!!!! globalScraper.dbGrid:=DBGrid1;
// !!!!! globalScraper.zQuery:=ZQuery1;
end;
finalization
globalScraper.destroy;
end.