-
Notifications
You must be signed in to change notification settings - Fork 0
/
WimTiVoTestCode.cpp
1326 lines (1293 loc) · 60.1 KB
/
WimTiVoTestCode.cpp
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
using namespace std;
HRESULT WriteAttributes(IXmlReader* pReader)
{
const WCHAR* pwszPrefix;
const WCHAR* pwszLocalName;
const WCHAR* pwszValue;
HRESULT hr = pReader->MoveToFirstAttribute();
if (S_FALSE == hr)
return hr;
if (S_OK != hr)
{
wprintf(L"Error moving to first attribute, error is %08.8lx", hr);
return hr;
}
else
{
while (TRUE)
{
if (!pReader->IsDefault())
{
UINT cwchPrefix;
if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
{
wprintf(L"Error getting prefix, error is %08.8lx", hr);
return hr;
}
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
{
wprintf(L"Error getting local name, error is %08.8lx", hr);
return hr;
}
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
{
wprintf(L"Error getting value, error is %08.8lx", hr);
return hr;
}
if (cwchPrefix > 0)
wprintf(L"Attr: %s:%s=\"%s\" \n", pwszPrefix, pwszLocalName, pwszValue);
else
wprintf(L"Attr: %s=\"%s\" \n", pwszLocalName, pwszValue);
}
if (S_OK != pReader->MoveToNextAttribute())
break;
}
}
return hr;
}
void XML_Test_Read(const CString csFileName = CString(_T("WimTivoServer.0.xml")))
{
HRESULT hr = S_OK;
CComPtr<IXmlReader> pReader;
if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**) &pReader, NULL)))
std::cout << "[" << getTimeISO8601() << "] Error creating xml reader, error is: " << hex << hr << endl;
else
{
if (FAILED(hr = pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit)))
std::cout << "[" << getTimeISO8601() << "] Error setting XmlReaderProperty_DtdProcessing, error is: " << hex << hr << endl;
else
{
//Open read-only input stream
CComPtr<IStream> pFileStream;
if (FAILED(hr = SHCreateStreamOnFile(csFileName.GetString(), STGM_READ, &pFileStream)))
std::cout << "[" << getTimeISO8601() << "] Error creating file reader, error is: " << hex << hr << endl;
else
{
if (FAILED(hr = pReader->SetInput(pFileStream)))
std::cout << "[" << getTimeISO8601() << "] Error setting input for reader, error is: " << hex << hr << endl;
else
{
XmlNodeType nodeType;
const WCHAR* pwszPrefix;
const WCHAR* pwszLocalName;
const WCHAR* pwszValue;
UINT cwchPrefix;
//read until there are no more nodes
while (S_OK == (hr = pReader->Read(&nodeType)))
{
switch (nodeType)
{
case XmlNodeType_XmlDeclaration:
wprintf(L"XmlDeclaration\n");
if (FAILED(hr = WriteAttributes(pReader)))
wprintf(L"Error writing attributes, error is %08.8lx", hr);
break;
case XmlNodeType_Element:
if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
wprintf(L"Error getting prefix, error is %08.8lx", hr);
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
wprintf(L"Error getting local name, error is %08.8lx", hr);
if (cwchPrefix > 0)
wprintf(L"Element: %s:%s\n", pwszPrefix, pwszLocalName);
else
wprintf(L"Element: %s\n", pwszLocalName);
if (FAILED(hr = WriteAttributes(pReader)))
wprintf(L"Error writing attributes, error is %08.8lx", hr);
if (pReader->IsEmptyElement() )
wprintf(L" (empty)");
break;
case XmlNodeType_EndElement:
if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
wprintf(L"Error getting prefix, error is %08.8lx", hr);
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
wprintf(L"Error getting local name, error is %08.8lx", hr);
if (cwchPrefix > 0)
wprintf(L"End Element: %s:%s\n", pwszPrefix, pwszLocalName);
else
wprintf(L"End Element: %s\n", pwszLocalName);
break;
case XmlNodeType_Text:
case XmlNodeType_Whitespace:
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
wprintf(L"Error getting value, error is %08.8lx", hr);
wprintf(L"Text: >%s<\n", pwszValue);
break;
case XmlNodeType_CDATA:
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
wprintf(L"Error getting value, error is %08.8lx", hr);
wprintf(L"CDATA: %s\n", pwszValue);
break;
case XmlNodeType_ProcessingInstruction:
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
wprintf(L"Error getting name, error is %08.8lx", hr);
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
wprintf(L"Error getting value, error is %08.8lx", hr);
wprintf(L"Processing Instruction name:%s value:%s\n", pwszLocalName, pwszValue);
break;
case XmlNodeType_Comment:
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
wprintf(L"Error getting value, error is %08.8lx", hr);
wprintf(L"Comment: %s\n", pwszValue);
break;
case XmlNodeType_DocumentType:
wprintf(L"DOCTYPE is not printed\n");
break;
}
}
}
}
}
}
}
std::wstring Indent(const int count = 1)
{
std::wstring rval;
for (auto i = 0; i < count; i++)
rval.append(L" ");
return(rval);
}
void XML_Test_Read_ElementsOnly(CString csFileName = CString(_T("WimTivoServer.0.xml")))
{
HRESULT hr = S_OK;
CComPtr<IXmlReader> pReader;
if (FAILED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**) &pReader, NULL)))
std::cout << "[" << getTimeISO8601() << "] Error creating xml reader, error is: " << hex << hr << endl;
else
{
if (FAILED(hr = pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit)))
std::cout << "[" << getTimeISO8601() << "] Error setting XmlReaderProperty_DtdProcessing, error is: " << hex << showbase << setfill('0') << setw(8) << hr << endl;
else
{
//Open read-only input stream
CComPtr<IStream> pFileStream;
if (FAILED(hr = SHCreateStreamOnFile(csFileName.GetString(), STGM_READ, &pFileStream)))
std::cout << "[" << getTimeISO8601() << "] Error creating file reader, error is: " << hex << hr << endl;
else
{
if (FAILED(hr = pReader->SetInput(pFileStream)))
std::cout << "[" << getTimeISO8601() << "] Error setting input for reader, error is: " << hex << hr << endl;
else
{
int indentlevel = 0;
XmlNodeType nodeType;
const WCHAR* pwszPrefix;
const WCHAR* pwszLocalName;
const WCHAR* pwszValue;
UINT cwchPrefix;
//read until there are no more nodes
while (S_OK == (hr = pReader->Read(&nodeType)))
{
switch (nodeType)
{
case XmlNodeType_XmlDeclaration:
std::cout << "[ ] XmlDeclaration" << endl;
//wprintf(L"XmlDeclaration\n");
//if (FAILED(hr = WriteAttributes(pReader)))
// wprintf(L"Error writing attributes, error is %08.8lx", hr);
break;
case XmlNodeType_Element:
if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
std::cout << "[ ] Error getting prefix, error is " << hex << hr << endl;
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
std::cout << "[ ] Error getting local name, error is" << hex << hr << endl;
if (cwchPrefix > 0)
std::wcout << L"[ ] " << Indent(indentlevel) << L"Element: " << pwszPrefix << L":" << pwszLocalName << endl;
else
std::wcout << L"[ ] " << Indent(indentlevel) << L"Element: " << pwszLocalName << endl;
//if (FAILED(hr = WriteAttributes(pReader)))
// wprintf(L"Error writing attributes, error is %08.8lx", hr);
if (pReader->IsEmptyElement() )
std::cout << "[ ] (empty)" << endl;
indentlevel++;
break;
case XmlNodeType_EndElement:
if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
std::cout << "[ ] Error getting prefix, error is " << hex << hr << endl;
if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
std::cout << "[ ] Error getting local name, error is" << hex << hr << endl;
if (cwchPrefix > 0)
std::wcout << L"[ ] " << Indent(indentlevel) << L"End Element: " << pwszPrefix << L":" << pwszLocalName << endl;
else
std::wcout << L"[ ] " << Indent(indentlevel) << L"End Element: " << pwszLocalName << endl;
indentlevel--;
break;
case XmlNodeType_Text:
case XmlNodeType_Whitespace:
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
std::cout << "[ ] Error getting value, error is " << hex << hr << endl;
std::wcout << L"[ ] " << Indent(indentlevel) << L"Text: >" << pwszValue << L"<" << endl;
break;
case XmlNodeType_CDATA:
//if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
// wprintf(L"Error getting value, error is %08.8lx", hr);
//wprintf(L"CDATA: %s\n", pwszValue);
break;
case XmlNodeType_ProcessingInstruction:
//if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
// wprintf(L"Error getting name, error is %08.8lx", hr);
//if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
// wprintf(L"Error getting value, error is %08.8lx", hr);
//wprintf(L"Processing Instruction name:%s value:%s\n", pwszLocalName, pwszValue);
break;
case XmlNodeType_Comment:
if (FAILED(hr = pReader->GetValue(&pwszValue, NULL)))
std::cout << "[ ] Error getting value, error is " << hex << hr << endl;
std::wcout << L"[ ] " << Indent(indentlevel) << L"Comment: " << pwszValue << endl;
break;
case XmlNodeType_DocumentType:
std::cout << "[ ] DOCTYPE is not printed" << endl;
break;
}
}
}
}
}
}
}
void XML_Test_Write_InMemory(void)
{
HRESULT hr = S_OK;
CComPtr<IXmlWriter> pWriter;
if (FAILED(hr = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL)))
std::cout << "[" << getTimeISO8601() << "] Error creating xml writer, error is: " << hex << hr << endl;
else
{
// from: http://stackoverflow.com/questions/3037946/how-can-i-store-xml-in-buffer-using-xmlite
CComPtr<IStream> spMemoryStream(::SHCreateMemStream(NULL, 0));
//// Opens writeable output stream.
//spMemoryStream.Attach(::SHCreateMemStream(NULL, 0));
//std::unique_ptr<IStream> spMemoryStream(::SHCreateMemStream(NULL, 0));
if (spMemoryStream != NULL)
{
CComPtr<IXmlWriterOutput> pWriterOutput;
CreateXmlWriterOutputWithEncodingName(spMemoryStream, NULL, L"utf-16", &pWriterOutput);
pWriter->SetOutput(pWriterOutput);
//pWriter->SetOutput(spMemoryStream);
pWriter->SetProperty(XmlWriterProperty_Indent, TRUE);
pWriter->WriteStartDocument(XmlStandalone_Omit);
pWriter->WriteStartElement(NULL,_T("TiVoContainer"),_T("http://www.tivo.com/developer/calypso-protocol-1.6/"));
pWriter->WriteStartElement(NULL,_T("Details"),NULL);
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-server"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("SourceFormat"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-dvr"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Title"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("TotalItems"),NULL);
pWriter->WriteString(_T("1"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Details
pWriter->WriteStartElement(NULL,_T("ItemStart"),NULL);
pWriter->WriteString(_T("0"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("ItemCount"),NULL);
pWriter->WriteString(_T("1"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Item"),NULL);
pWriter->WriteStartElement(NULL,_T("Details"),NULL);
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-videos"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("SourceFormat"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-dvr"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Title"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("UniqueId"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Details
pWriter->WriteStartElement(NULL,_T("Links"),NULL);
pWriter->WriteStartElement(NULL,_T("Content"),NULL);
pWriter->WriteStartElement(NULL,_T("Url"),NULL);
pWriter->WriteString(_T("https://192.168.0.108:443/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-videos"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Content
pWriter->WriteFullEndElement(); // Links
pWriter->WriteFullEndElement(); // Item
pWriter->WriteFullEndElement(); // TiVoContainer
pWriter->WriteEndDocument();
pWriter->Flush();
// Allocates enough memeory for the xml content.
STATSTG ssStreamData = {0};
spMemoryStream->Stat(&ssStreamData, STATFLAG_NONAME);
SIZE_T cbSize = ssStreamData.cbSize.LowPart;
LPWSTR pwszContent = (WCHAR*) new BYTE[cbSize + sizeof(WCHAR)];
if (pwszContent != NULL)
{
// Copies the content from the stream to the buffer.
LARGE_INTEGER position;
position.QuadPart = 0;
spMemoryStream->Seek(position, STREAM_SEEK_SET, NULL);
ULONG cbRead;
spMemoryStream->Read(pwszContent, cbSize, &cbRead);
pwszContent[cbSize / sizeof(WCHAR)] = L'\0';
wprintf(L"%s", pwszContent);
delete[] pwszContent;
}
// Next I'm going to follow http://stackoverflow.com/questions/8804687/unable-to-read-xml-string-with-xmllite-using-memory-buffer
// and figure out how to read the XML out of an internal memory buffer.
}
}
}
void XML_Test_Write(const CString csFileName = CString(_T("WimTivoServer.8.xml")))
{
HRESULT hr = S_OK;
CComPtr<IXmlWriter> pWriter;
if (FAILED(hr = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL)))
std::cout << "[" << getTimeISO8601() << "] Error creating xml writer, error is: " << hex << hr << endl;
else
{
//Open writeable output stream
CComPtr<IStream> pOutFileStream;
if (FAILED(hr = SHCreateStreamOnFile(csFileName.GetString(), STGM_CREATE | STGM_WRITE, &pOutFileStream)))
std::cout << "[" << getTimeISO8601() << "] Error creating file writer, error is: " << hex << hr << endl;
else
{
if (FAILED(hr = pWriter->SetOutput(pOutFileStream)))
wprintf(L"Error, Method: SetOutput, error is %08.8lx", hr);
else
{
if (FAILED(hr = pWriter->SetProperty(XmlWriterProperty_Indent, TRUE)))
wprintf(L"Error, Method: SetProperty XmlWriterProperty_Indent, error is %08.8lx", hr);
else
{
if (FAILED(hr = pWriter->WriteStartDocument(XmlStandalone_Omit)))
wprintf(L"Error, Method: WriteStartDocument, error is %08.8lx", hr);
else
{
pWriter->WriteStartElement(NULL,_T("TiVoContainer"),_T("http://www.tivo.com/developer/calypso-protocol-1.6/"));
pWriter->WriteStartElement(NULL,_T("Details"),NULL);
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-server"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("SourceFormat"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-dvr"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Title"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("TotalItems"),NULL);
pWriter->WriteString(_T("1"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Details
pWriter->WriteStartElement(NULL,_T("ItemStart"),NULL);
pWriter->WriteString(_T("0"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("ItemCount"),NULL);
pWriter->WriteString(_T("1"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Item"),NULL);
pWriter->WriteStartElement(NULL,_T("Details"),NULL);
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-videos"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("SourceFormat"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-dvr"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("Title"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("UniqueId"),NULL);
pWriter->WriteString(_T("TivoHD"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Details
pWriter->WriteStartElement(NULL,_T("Links"),NULL);
pWriter->WriteStartElement(NULL,_T("Content"),NULL);
pWriter->WriteStartElement(NULL,_T("Url"),NULL);
pWriter->WriteString(_T("https://192.168.0.108:443/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying"));
pWriter->WriteFullEndElement();
pWriter->WriteStartElement(NULL,_T("ContentType"),NULL);
pWriter->WriteString(_T("x-tivo-container/tivo-videos"));
pWriter->WriteFullEndElement();
pWriter->WriteFullEndElement(); // Content
pWriter->WriteFullEndElement(); // Links
pWriter->WriteFullEndElement(); // Item
pWriter->WriteFullEndElement(); // TiVoContainer
// WriteEndDocument closes any open elements or attributes
if (FAILED(hr = pWriter->WriteEndDocument()))
wprintf(L"Error, Method: WriteEndDocument, error is %08.8lx", hr);
if (FAILED(hr = pWriter->Flush()))
wprintf(L"Error, Method: Flush, error is %08.8lx", hr);
}
}
}
}
}
}
void XML_Test_FileReformat(const CString & Source = _T("D:\\Videos\\chunk-01-0001.xml"), const CString Destination = _T("D:/Videos/Evening-1.xml"))
{
// crap to clean up a file...
HRESULT hr = S_OK;
CComPtr<IXmlReader> pReader;
if (SUCCEEDED(hr = CreateXmlReader(__uuidof(IXmlReader), (void**) &pReader, NULL)))
{
if (SUCCEEDED(hr = pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit)))
{
CComPtr<IStream> spFileStreamOne;
if (SUCCEEDED(hr = SHCreateStreamOnFile(Source.GetString(), STGM_READ, &spFileStreamOne)))
{
if (SUCCEEDED(hr = pReader->SetInput(spFileStreamOne)))
{
CComPtr<IXmlWriter> pWriter;
if (SUCCEEDED(hr = CreateXmlWriter(__uuidof(IXmlWriter), (void**) &pWriter, NULL)))
{
CComPtr<IStream> spFileStreamTwo;
if (SUCCEEDED(hr = SHCreateStreamOnFile(Destination.GetString(), STGM_CREATE | STGM_WRITE, &spFileStreamTwo)))
{
if (SUCCEEDED(hr = pWriter->SetOutput(spFileStreamTwo)))
{
pWriter->SetProperty(XmlWriterProperty_Indent, TRUE);
while (S_OK == (hr = pWriter->WriteNode(pReader, TRUE))); // loops over entire xml file, writing it out with indenting
pWriter->Flush();
}
}
}
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
#ifdef _WIM_TIVO_CONVERT_
std::queue<cTiVoFile> TiVoFilesToConvert;
UINT TiVoConvertFileThread(LPVOID lvp)
{
TRACE(__FUNCTION__ "\n");
CString m_csTDCatPath(FindEXEFromPath(_T("tdcat.exe")));
CString m_csTiVoDecodePath(FindEXEFromPath(_T("tivodecode.exe")));
CString m_csFFMPEGPath(FindEXEFromPath(_T("ffmpeg.exe")));
while (!TiVoFilesToConvert.empty())
{
cTiVoFile TiVoFile(TiVoFilesToConvert.front());
TiVoFilesToConvert.pop();
CString csTiVoFileName(TiVoFile.GetPathName());
CFileStatus status;
if (TRUE == CFile::GetStatus(csTiVoFileName, status)) // Test to make sure the .TiVo file exists!
{
CString csMPEGPathName(ReplaceExtension(csTiVoFileName, _T(".mpeg")));
CString csXMLPathName(ReplaceExtension(csTiVoFileName, _T(".xml")));
CString csMP4PathName(ReplaceExtension(csTiVoFileName, _T(".mp4")));
if (TRUE != CFile::GetStatus(csMPEGPathName, status))
{
if (!m_csTDCatPath.IsEmpty())
{
std::cout << "[" << getTimeISO8601() << "]\tspawn: " << CStringA(m_csTDCatPath).GetString() << " " << CStringA(m_csTDCatPath).GetString() << " --mak " << CStringA(TiVoFile.GetMAK()).GetString() << " --out " << CStringA(QuoteFileName(csXMLPathName)).GetString() << " " << CStringA(csXMLPathName).GetString() << " --chunk-2 " << CStringA(QuoteFileName(csTiVoFileName)).GetString() << std::endl;
if (-1 == _tspawnl(_P_WAIT, m_csTDCatPath.GetString(), m_csTDCatPath.GetString(), _T("--mak"), TiVoFile.GetMAK(), _T("--out"), QuoteFileName(csXMLPathName).GetString(), _T("--chunk-2"), QuoteFileName(csTiVoFileName).GetString(), NULL))
std::cout << "[ ] _tspawnlp failed: " /* << strerror(errno) */ << std::endl;
}
if (TRUE == CFile::GetStatus(csXMLPathName, status))
{
status.m_ctime = status.m_mtime = TiVoFile.GetCaptureDate();
CFile::SetStatus(csXMLPathName, status);
}
std::cout << "[" << getTimeISO8601() << "]\tspawn: " << CStringA(m_csTiVoDecodePath).GetString() << " " << CStringA(m_csTiVoDecodePath).GetString() << " --mak " << CStringA(TiVoFile.GetMAK()).GetString() << " --out " << CStringA(QuoteFileName(csMPEGPathName)).GetString() << " " << CStringA(QuoteFileName(csTiVoFileName)).GetString() << std::endl;
if (-1 == _tspawnl(_P_WAIT, m_csTiVoDecodePath.GetString(), m_csTiVoDecodePath.GetString(), _T("--mak"), TiVoFile.GetMAK(), _T("--out"), QuoteFileName(csMPEGPathName).GetString(), QuoteFileName(csTiVoFileName).GetString(), NULL))
std::cout << "[ ] _tspawnlp failed: " /* << strerror(errno) */ << std::endl;
if (TRUE == CFile::GetStatus(csMPEGPathName, status))
{
status.m_ctime = status.m_mtime = TiVoFile.GetCaptureDate();
CFile::SetStatus(csMPEGPathName, status);
DeleteFile(csTiVoFileName);
if (TRUE != CFile::GetStatus(csMP4PathName, status))
{
CString csTitle(TiVoFile.GetTitle()); while(0 < csTitle.Replace(_T("\""), _T("'"))); csTitle.Insert(0,_T("title=\""));csTitle.Append(_T("\""));
CString csShow(TiVoFile.GetTitle()); while(0 < csShow.Replace(_T("\""), _T("'"))); csShow.Insert(0,_T("show=\""));csShow.Append(_T("\""));
CString csDescription(TiVoFile.GetDescription()); while(0 < csDescription.Replace(_T("\""), _T("'"))); csDescription.Insert(0,_T("description=\""));csDescription.Append(_T("\""));
CString csEpisodeID(TiVoFile.GetEpisodeTitle()); while(0 < csEpisodeID.Replace(_T("\""), _T("'"))); csEpisodeID.Insert(0,_T("episode_id=\""));csEpisodeID.Append(_T("\""));
std::cout << "[" << getTimeISO8601() << "]\tspawn: " << CStringA(m_csFFMPEGPath).GetString() << " " << CStringA(m_csFFMPEGPath).GetString() << " -i " << CStringA(QuoteFileName(csMPEGPathName)).GetString() << " -metadata " << CStringA(csTitle).GetString() << " -metadata " << CStringA(csShow).GetString() << " -metadata " << CStringA(csDescription).GetString() << " -metadata " << CStringA(csEpisodeID).GetString() << " -vcodec copy -acodec copy -y " << CStringA(QuoteFileName(csMP4PathName)).GetString() << std::endl;
if (-1 == _tspawnlp(_P_WAIT, m_csFFMPEGPath.GetString(), m_csFFMPEGPath.GetString(), _T("-i"), QuoteFileName(csMPEGPathName).GetString(),
_T("-metadata"), csTitle.GetString(),
_T("-metadata"), csShow.GetString(),
_T("-metadata"), csDescription.GetString(),
_T("-metadata"), csEpisodeID.GetString(),
_T("-vcodec"), _T("copy"),
_T("-acodec"), _T("copy"),
_T("-y"), // Cause it to overwrite exiting output files
QuoteFileName(csMP4PathName).GetString(), NULL))
std::cout << "[ ] _tspawnlp failed: " /* << _sys_errlist[errno] */ << std::endl;
}
if (TRUE == CFile::GetStatus(csMP4PathName, status))
{
status.m_ctime = status.m_mtime = TiVoFile.GetCaptureDate();
CFile::SetStatus(csMP4PathName, status);
DeleteFile(csMPEGPathName);
}
}
}
}
}
TRACE(__FUNCTION__ " Exiting\n");
return(0);
}
#endif
UINT WatchTiVoDirectories(LPVOID lvp)
{
UINT rval = 0;
// Obtaining Directory Change Notifications
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261(v=vs.85).aspx
HANDLE dwChangeHandles[2];
TCHAR * lpDir = _T("//Acid/TiVo/");
dwChangeHandles[0] = FindFirstChangeNotification(
lpDir, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes
if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
{
std::cout << "ERROR: FindFirstChangeNotification function failed." << std::endl;
rval = GetLastError();
}
else if (dwChangeHandles[0] == NULL)
{
std::cout << "ERROR: Unexpected NULL from FindFirstChangeNotification." << std::endl;
rval = GetLastError();
}
else
{
while (TRUE)
{
// Wait for notification.
std::cout << "Waiting for notification..." << std::endl;
DWORD dwWaitStatus = WaitForMultipleObjects(1, dwChangeHandles, FALSE, INFINITE);
switch (dwWaitStatus)
{
case WAIT_OBJECT_0:
// A file was created, renamed, or deleted in the directory.
// Refresh this directory and restart the notification.
std::cout << "Directory (" << CStringA(lpDir).GetString() << ") changed." << std::endl;
if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
{
std::cout << "ERROR: FindNextChangeNotification function failed." << std::endl;
rval = GetLastError();
return(rval);
}
break;
//case WAIT_OBJECT_0 + 1:
// // A directory was created, renamed, or deleted.
// // Refresh the tree and restart the notification.
// RefreshTree(lpDrive);
// if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
// {
// std::cout << "ERROR: FindNextChangeNotification function failed." << std::endl;
// rval = GetLastError();
// return(rval);
// }
// break;
case WAIT_TIMEOUT:
// A timeout occurred, this would happen if some value other
// than INFINITE is used in the Wait call and no changes occur.
// In a single-threaded environment you might not want an
// INFINITE wait.
std::cout << "No changes in the timeout period." << std::endl;
break;
default:
std::cout << "ERROR: Unhandled dwWaitStatus." << std::endl;
rval = GetLastError();
return(rval);
break;
}
}
}
return(rval);
}
bool TiVoBeaconListen(SOCKADDR_IN &saServer)
{
bool rval = false;
// Create a UDP/IP datagram socket
SOCKET theSocket = socket(AF_INET, // Address family
SOCK_DGRAM, // Socket type
IPPROTO_UDP); // Protocol
if (theSocket == INVALID_SOCKET)
{
TRACE("%s: %d\n","socket()",WSAGetLastError());
}
else
{
// Fill in the address structure
SOCKADDR_IN saClient;
saClient.sin_family = AF_INET;
saClient.sin_addr.s_addr = INADDR_ANY; // Let WinSock assign address
saClient.sin_port = htons(2190); // Use port passed from user
// bind the name to the socket
int nRet = bind(theSocket, // Socket descriptor
(LPSOCKADDR)&saClient, // Address to bind to
sizeof(SOCKADDR_IN) // Size of address
);
if (nRet == SOCKET_ERROR)
{
TRACE("%s: %d\n","bind()",WSAGetLastError());
closesocket(theSocket);
}
else
{
char szBuf[2048];
int nLen = sizeof(SOCKADDR_IN);
nRet = recvfrom(theSocket, // Bound socket
szBuf, // Receive buffer
sizeof(szBuf), // Size of buffer in bytes
0, // Flags
(LPSOCKADDR)&saServer, // Buffer to receive client address
&nLen); // Length of client address buffer
if (nRet == INVALID_SOCKET)
{
TRACE("%s: %d\n","recvfrom()",WSAGetLastError());
}
else
{
// here's where I should look at what I recieve.
CStringA csServerBroadcast(szBuf, nRet);
csServerBroadcast.Replace("\n", " ");
csServerBroadcast.Trim();
std::cout << "[" << getTimeISO8601() << "] " << inet_ntoa(saServer.sin_addr) << " " << csServerBroadcast.GetString() << endl;
// printf("%s\t%s\n", inet_ntoa(saServer.sin_addr), csServerBroadcast.GetString());
rval = true;
//if (strncmp(szBuf,csServerBroadcast,nRet) == 0)
//{
// TRACE("Server at: %s\n",inet_ntoa(saServer.sin_addr));
// rval = true;
//}
//else
// TRACE("Someone not wanting to be decoded at: %s\n",inet_ntoa(saServer.sin_addr));
}
closesocket(theSocket);
}
}
return(rval);
}
UINT TiVoBeaconListenThread(LPVOID lvp)
{
//static CString csPutDecodedTags;
//if (csPutDecodedTags.IsEmpty())
// csPutDecodedTags.LoadString(IDS_PUT_DECODED_TAG);
//SOCKADDR_IN saServer;
//while ((theApp.UDPClientListen(saServer) == true) &&
// (theApp.m_bDecodeRemoteStopRequested == false))
//{
// bool bHappyWithCurrentServer = true;
// // Complete the address structure
// saServer.sin_family = AF_INET;
// saServer.sin_port = htons(4096);
// // these I want to optimize and only ask for if they changed
// CCorelateParameters D4Params;
// void * EPCParams = NULL;
// bool bD4Correlate = true;
// vector<CTagData> Hints;
// while ((bHappyWithCurrentServer == true) &&
// (theApp.m_bDecodeRemoteStopRequested == false))
// {
// CRawReadData RawData;
// bHappyWithCurrentServer = GetRawData(saServer,RawData);
// if (bHappyWithCurrentServer)
// {
// CTime OldTime(D4Params.m_ChangeTime.time);
// bHappyWithCurrentServer = GetParams(saServer,D4Params);
// if (!bHappyWithCurrentServer)
// {
// bHappyWithCurrentServer = GetParams(saServer,&EPCParams);
// if (bHappyWithCurrentServer)
// {
// bD4Correlate = false;
// D4Params.m_ChangeTime.time = theApp.EPCTagDLL.GetEPCModificationTime(EPCParams); // hack
// }
// }
// CTime NewTime(D4Params.m_ChangeTime.time);
// bool NewParams = (OldTime != NewTime);
// if (bHappyWithCurrentServer)
// {
// if (NewParams)
// bHappyWithCurrentServer = GetHints(saServer,Hints);
// if (bHappyWithCurrentServer)
// {
// if (NewParams)
// {
// D4Params.m_TagList.resize(Hints.size());
// for (int index = 0, stop = (int) Hints.size(); index < stop; index++)
// {
// D4Params.m_TagList[index].m_lTagNumber = Hints[index].EPC.i[3];
// bHappyWithCurrentServer = GetRefData(saServer,D4Params.m_TagList[index]);
// }
// }
// CTagData NewTags[30];
// // Then I decode the tags.
// int Count = 0;
// if (!Hints.empty())
// {
// if (bD4Correlate == true)
// Count = ReadTagD4(&RawData,NewTags,sizeof(NewTags),&(Hints[0]),sizeof(CTagData),&D4Params);
// else
// Count = theApp.EPCTagDLL.ReadTagEPC(&RawData,NewTags,sizeof(NewTags),&(Hints[0]),sizeof(CTagData),EPCParams);
// }
// else
// {
// if (bD4Correlate == true)
// Count = ReadTagD4(&RawData,NewTags,sizeof(NewTags),NULL,0,&D4Params);
// else
// Count = theApp.EPCTagDLL.ReadTagEPC(&RawData,NewTags,sizeof(NewTags),NULL,0,EPCParams);
// }
// TRACE("\nDetected %d Tags\n",Count);
// char xmltags[1024*25];
// int offset = sprintf(xmltags,"<RawDataInfo><TagCount %d><DataType %d><DateTime>%d.%03d %d %d</DateTime></RawDataInfo>\n",
// Count,
// RawData.DataType,
// RawData.m_FileTime.time,
// RawData.m_FileTime.millitm,
// RawData.m_FileTime.timezone,
// RawData.m_FileTime.dstflag);
// // Write the tags back to a buffer.
// for (int index = 0; index < Count; index++)
// offset += NewTags[index].XMLWrite(xmltags+offset,sizeof(xmltags)-offset);
// xmltags[offset] = '\0';
// CString csRequest;
// csRequest.Format("%s HTTP/1.1\r\nHost: %s\r\n",csPutDecodedTags,inet_ntoa(saServer.sin_addr));
// csRequest += "Connection: close\r\n";
// //csRequest += "User-Agent: EPCPalletReader\r\n";
// csRequest.Format("%sContent-Length: %d\r\n\r\n",csRequest,offset);
// csRequest += xmltags;
// // then I connect back to the server and upload the tagdata.
// // connect to the server
// // Create a TCP/IP stream socket
// SOCKET TagSocket = socket(AF_INET, // Address family
// SOCK_STREAM, // Socket type
// IPPROTO_TCP); // Protocol
// if (TagSocket == INVALID_SOCKET)
// {
// TRACE("%s: %d\n","socket()",WSAGetLastError());
// bHappyWithCurrentServer = false;
// }
// else
// {
// // connect to the server
// int nRet = connect(TagSocket, // Socket
// (LPSOCKADDR)&saServer, // Server address
// sizeof(struct sockaddr)); // Length of server address structure
// nRet = send(TagSocket,csRequest,csRequest.GetLength(),0);
// nRet = recv(TagSocket,xmltags,sizeof(xmltags),0);
// closesocket(TagSocket);
// #ifdef _DEBUG
// CString csTrace(csRequest,132);
// csTrace.Replace("\r\n"," ");
// TRACE("==> %s\n",csTrace);
// CString csResponse(xmltags,nRet);
// csResponse.Replace("\r\n"," ");
// TRACE("<== %s\n",csResponse);
// #endif
// }
// }
// }
// }
// }
// theApp.EPCTagDLL.DestroyHistoryEPC(EPCParams);
//}
//theApp.m_bDecodeRemoteRunning = false;
return(0);
}
#ifdef _DE_CHUNK_FILE_
std::ifstream FileToDeChunk;
FileToDeChunk.open("C:\\Users\\Wim\\Videos\\The.Daily.Show.2013.05.02.Eric.Greitens.HDTV.x264-EVOLVE (Recorded May 02, 2013).pytivo.TiVo", ios_base::in | ios_base::binary);
std::ofstream OutFile("C:\\Users\\Wim\\Videos\\The.Daily.Show.2013.05.02.Eric.Greitens.HDTV.x264-EVOLVE (Recorded May 02, 2013).pytivo.dechunked.TiVo", ios_base::out | ios_base::binary);
long long chunksize = 0;
char ChunkHeader[64];
if (FileToDeChunk.is_open() && OutFile.is_open())
{
do {
FileToDeChunk.getline(ChunkHeader, sizeof(ChunkHeader));
stringstream ss(ChunkHeader);
ss << hex;
ss >> chunksize;
char * ChunkData = new char[chunksize];
FileToDeChunk.read(ChunkData, chunksize);
OutFile.write(ChunkData, chunksize);
delete[] ChunkData;
FileToDeChunk.getline(ChunkHeader, sizeof(ChunkHeader)); // gets the chunk footer
}
while (chunksize > 0);
FileToDeChunk.close();
OutFile.close();
}
#endif
#ifdef _COMPARE_PYTIVO_FILE
std::ifstream FileOne, FileTwo;
FileOne.open("C:\\Users\\Wim\\Videos\\The.Daily.Show.2013.05.02.Eric.Greitens.HDTV.x264-EVOLVE (Recorded May 02, 2013).pytivo.dechunked.TiVo", ios_base::in | ios_base::binary);
FileTwo.open("C:\\Users\\Wim\\Videos\\The.Daily.Show.2013.05.02.Eric.Greitens.HDTV.x264-EVOLVE (Recorded May 02, 2013).wim.1.1.TiVo", ios_base::in | ios_base::binary);
if (FileOne.is_open() && FileTwo.is_open())
{
FileOne.seekg(0x3000); // Beginning of MPEG
FileTwo.seekg(0x1c00); // Beginning of MPEG
char ByteOne, ByteTwo;
unsigned long long Position = 0;
while (!FileOne.eof() && !FileTwo.eof())
{
FileOne.read(&ByteOne, 1);
FileTwo.read(&ByteTwo, 1);
if (ByteOne != ByteTwo)
{
std::cout << "Bytes Don't Match at MPEG Offset " << Position << std::endl;
break;
}
Position++;
}
FileOne.close();
FileTwo.close();
std::cout << "Finished Comparing " << Position << " bytes" << std::endl;
}
#endif
#ifdef _WIM_TIVO_CONVERT_
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("//Acid/TiVo/*.tivo"));
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
continue;
if (finder.IsDirectory())
continue;
if (finder.IsHidden())
continue;
if (finder.IsSystem())
continue;
if (finder.IsTemporary())
continue;
if (finder.GetLength() > 0)
{
cTiVoFile myFile;
myFile.SetPathName(finder);
myFile.SetMAK(_T("1760168186"));
TiVoFilesToConvert.push(myFile);
}
}
finder.Close();
AfxBeginThread(TiVoConvertFileThread, NULL);
#endif
#ifdef _Original_Download_Tests_
if (SUCCEEDED(CoInitializeEx(0, COINIT_MULTITHREADED))) // COINIT_APARTMENTTHREADED
{
//XML_Test_FileReformat(_T("D:\\Videos\\chunk-01-0001.xml"), _T("D:/Videos/Evening Magazine (Recorded Mar 26, 2010, KINGDT).1.xml"));
//XML_Test_FileReformat(_T("D:\\Videos\\chunk-02-0002.xml"), _T("D:/Videos/Evening Magazine (Recorded Mar 26, 2010, KINGDT).2.xml"));
//XML_Test_Read_ElementsOnly();
//XML_Test_Read();
//XML_Test_Write();
//XML_Test_Write_InMemory();
std::vector<cTiVoFile> FilesToGetFromTiVo;
std::vector<CTiVoContainer> TiVoContainers;
//XML_Parse_TiVoNowPlaying(_T("WimTivoServer.3.xml"), FilesToGetFromTiVo);
//std::sort(FilesToGetFromTiVo.begin(),FilesToGetFromTiVo.end(),cTiVoFileCompareDateReverse);
FilesToGetFromTiVo.clear();
CInternetSession serverSession0;
XML_Parse_TiVoNowPlaying(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying&Recurse=Yes&SortOrder=!CaptureDate")), FilesToGetFromTiVo, TiVoContainers, serverSession0);
std::sort(FilesToGetFromTiVo.begin(),FilesToGetFromTiVo.end(),cTiVoFileCompareDateReverse);
FilesToGetFromTiVo.clear(); // This line is temporary just to make sure that no files are downloaded or converted
for (auto TiVoFileToGet = FilesToGetFromTiVo.begin(); TiVoFileToGet != FilesToGetFromTiVo.end(); TiVoFileToGet++)
{
CString csPathName(_T("//Acid/TiVo/"));
csPathName.Append(TiVoFileToGet->GetPathName());
CFileStatus status;
if (TRUE != CFile::GetStatus(csPathName, status))
while (false == GetTiVoFile(*TiVoFileToGet, serverSession0, _T("1760168186"), _T("//Acid/TiVo/")));
if (TRUE == CFile::GetStatus(csPathName, status))
{
status.m_ctime = status.m_mtime = TiVoFileToGet->GetCaptureDate();
CFile::SetStatus(csPathName, status);
CString csMPEGPathName(csPathName);
csMPEGPathName.Replace(_T(".TiVo"), _T(".mpeg"));
CString csMP4PathName(csPathName);
csMP4PathName.Replace(_T(".TiVo"), _T(".mp4"));
if (TRUE != CFile::GetStatus(csMP4PathName, status)) // Only do a bunch of this stuff if the mp4 file doesn't already exist
{
// This is just demo code making sure I can create a temporary file properly for future use with tdcat or tivodecode
TCHAR lpTempPathBuffer[MAX_PATH];
DWORD dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer);
if ((dwRetVal > MAX_PATH) || (dwRetVal == 0))
{
std::cout << "[" << getTimeISO8601() << "] GetTempPath failed" << endl;
_tcscpy(lpTempPathBuffer,_T("."));
}
// Generates a temporary file name.
TCHAR szTempFileName[MAX_PATH];
if (0 != GetTempFileName(lpTempPathBuffer, AfxGetAppName(), 0, szTempFileName))
{
wcout << L"[ ] GetTempFileName: " << szTempFileName << endl;
if (-1 == _tspawnlp(_P_WAIT, _T("tdcat.exe"), _T("tdcat.exe"), _T("--mak"), _T("1760168186"), _T("--out"), szTempFileName, _T("--chunk-2"), QuoteFileName(csPathName).GetString(), NULL))
std::cout << "[" << getTimeISO8601() << "] _tspawnlp failed: " << _sys_errlist[errno] << endl;
if (TRUE != CFile::GetStatus(csMPEGPathName, status))
if (-1 == _tspawnlp(_P_WAIT, _T("tivodecode.exe"), _T("tivodecode.exe"), _T("--mak"), _T("1760168186"), _T("--out"), QuoteFileName(csMPEGPathName).GetString(), QuoteFileName(csPathName).GetString(), NULL))
std::cout << "[" << getTimeISO8601() << "] _tspawnlp failed: " << _sys_errlist[errno] << endl;
if (TRUE == CFile::GetStatus(csMPEGPathName, status))
{
status.m_ctime = status.m_mtime = TiVoFileToGet->GetCaptureDate();
CFile::SetStatus(csMPEGPathName, status);
if (TRUE != CFile::GetStatus(csMP4PathName, status))
{
CString csTitle(TiVoFileToGet->GetTitle()); csTitle.Insert(0,_T("title=\""));csTitle.Append(_T("\""));
CString csShow(TiVoFileToGet->GetTitle()); csShow.Insert(0,_T("show=\""));csShow.Append(_T("\""));
CString csDescription(TiVoFileToGet->GetDescription()); csDescription.Insert(0,_T("description=\""));csDescription.Append(_T("\""));
CString csEpisodeID(TiVoFileToGet->GetEpisodeTitle()); csEpisodeID.Insert(0,_T("episode_id=\""));csEpisodeID.Append(_T("\""));
if (-1 == _tspawnlp(_P_WAIT, _T("ffmpeg.exe"), _T("ffmpeg.exe"), _T("-i"), QuoteFileName(csMPEGPathName).GetString(),
_T("-metadata"), csTitle.GetString(),
_T("-metadata"), csShow.GetString(),
_T("-metadata"), csDescription.GetString(),
_T("-metadata"), csEpisodeID.GetString(),
_T("-vcodec"), _T("copy"),
_T("-acodec"), _T("copy"),
_T("-y"), // Cause it to overwrite exiting output files
QuoteFileName(csMP4PathName).GetString(), NULL))
std::cout << "[" << getTimeISO8601() << "] _tspawnlp failed: " << _sys_errlist[errno] << endl;
}
if (TRUE == CFile::GetStatus(csMP4PathName, status))
{
status.m_ctime = status.m_mtime = TiVoFileToGet->GetCaptureDate();
CFile::SetStatus(csMP4PathName, status);
DeleteFile(csMPEGPathName);
}
}
DeleteFile(szTempFileName); // I must delete this file because the zero in the unique field up above causes a file to be created.
}
}
}
}
std::vector<CString> myURLS;
//myURLS.push_back(CString(_T("http://192.168.0.108/TiVoConnect?Command=ResetServer")));
//myURLS.push_back(CString(_T("http://192.168.0.108/TiVoConnect?Command=QueryContainer&Container=/")));
myURLS.push_back(CString(_T("http://192.168.0.108/TiVoConnect?Command=QueryFormats&SourceFormat=video%2Fx-tivo-mpeg")));
//myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying")));
myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying&Recurse=Yes&SortOrder=!CaptureDate")));
myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying/9948")));
myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoVideoDetails?id=984268"))); // It appears that after a "Details" call it uses a "Connection: keep-alive" call, so I should probably make another call to the now playing list to see if that makes the tivo more stable.
myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying/9948")));
//myURLS.push_back(CString(_T("http://192.168.1.11/TiVoConnect?Command=QueryContainer&Container=/")));
//myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=QueryContainer&Container=/NowPlaying")));
//myURLS.push_back(CString(_T("http://192.168.0.5:8080/TiVoConnect?Command=QueryContainer&Container=/")));
//myURLS.push_back(CString(_T("http://192.168.0.5:8080/TiVoConnect?Command=QueryFormats&SourceFormat=video%2Fx-tivo-mpeg")));
//myURLS.push_back(CString(_T("https://tivo:[email protected]:4430/TiVoConnect?Command=QueryContainer&Container=/TivoNowPlaying")));
//myURLS.push_back(CString(_T("http://tivo:[email protected]/download/Evening Magazine.TiVo?Container=/NowPlaying&id=984268")));
//myURLS.push_back(CString(_T("http://tivo:[email protected]/download/Evening Magazine.TiVo?Container=/NowPlaying&id=879493")));
//myURLS.push_back(CString(_T("http://tivo:[email protected]/download/Community.TiVo?Container=/NowPlaying&id=3002485")));
//myURLS.push_back(CString(_T("https://tivo:[email protected]:443/TiVoConnect?Command=ResetServer")));
myURLS.push_back(CString(_T("http://tivo:[email protected]/TiVoConnect?Command=ResetServer")));
int FileIndex = 0;
CInternetSession serverSession;
for (auto csURL = myURLS.begin(); csURL != myURLS.end(); csURL++)
{
std::cout << "[" << getTimeISO8601() << "] Attempting: " << CStringA(*csURL).GetString() << endl;
DWORD dwServiceType;
CString strServer;