/
verify.php
156 lines (132 loc) · 4.65 KB
/
verify.php
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
<?php
$argumentList = $argv;
if (count($argumentList) < 2) {
print(" > You need to supply your MAK as an argument.");
finishRun();
} else {
$mak = $argumentList[1];
}
// Require the Composer autoload file.
require 'vendor/autoload.php';
// Setup the ProcessBuilder.
validateClass('Symfony\Component\Process\ProcessBuilder');
$builder = new Symfony\Component\Process\ProcessBuilder();
// Setup the File Logger
$logFile = '/tmp/tivo_verify.log';
$logger = new Apix\Log\Logger();
$logger->add(
new Apix\Log\Logger\File($logFile)
);
// Locate the TiVo.
validateClass('JimLind\TiVo\TiVoFinder');
$finder = new JimLind\TiVo\TiVoFinder($builder);
$finder->setLogger($logger);
$ipAddress = $finder->find();
// Report the results of locating the TiVo.
if (empty($ipAddress) === false) {
print(" > Your TiVo was automatically found at `{$ipAddress}` on your network.\n");
} else {
print(" > Your TiVo could not be automatically located.\n");
print(" > Do you have an odd network setup like virtual machines or the like?\n");
}
// Exit if no IP can be used.
if (empty($ipAddress) && count($argumentList) < 3) {
print(" > No IP available. You can supply one manually if neccessary.\n");
finishRun($logFile);
}
// Use the manually entered IP.
if (count($argumentList) > 2) {
$ipAddress = $argumentList[2];
print(" > Using the manually entered IP of `{$ipAddress}` for your TiVo.\n");
}
// Setup the Guzzle client.
validateClass('GuzzleHttp\Client');
$guzzle = new GuzzleHttp\Client();
// Download a NowPlaying list.
validateClass('JimLind\TiVo\XmlDownloader');
print(" > Downloading the Now Playing list from your TiVo.\n");
$xmlDownloader = new JimLind\TiVo\XmlDownloader($ipAddress, $mak, $guzzle);
$xmlDownloader->setLogger($logger);
$xmlList = $xmlDownloader->download();
if (empty($xmlList)) {
print(" > No proper response from your TiVo. Something is wrong.\n");
finishRun($logFile);
} else {
$xmlCount = count($xmlList);
print(" > Now Playing list of {$xmlCount} shows downloaded.\n");
}
// Translate an XML list to a Show list.
validateClass('JimLind\TiVo\Factory\ShowListFactory');
$listFactory = new JimLind\TiVo\Factory\ShowListFactory();
$showList = $listFactory->createShowListFromXmlList($xmlList);
$showCount = count($showList);
print(" > Now Playing list of {$showCount} shows translated.\n");
// Grab a random Show.
$key = rand(1, $showCount - 1);
$show = $showList->offsetGet($key);
$showTitle = $show->getShowTitle();
$episodeTitle = $show->getEpisodeTitle();
$showURL = $show->getURL();
print(" > Picked a random show from the list.\n");
print(" > - {$showTitle} {$episodeTitle}\n");
print(" > - {$showURL}\n");
// Identify local file locations to be deleted later.
$tivoFile = '/tmp/' . rand() . '.tivo';
$mpegFile = '/tmp/' . rand() . '.mpeg';
// Download a preview file.
validateClass('JimLind\TiVo\VideoDownloader');
print(" > Downloading a preview of the show locally.\n");
$videoDownloader = new JimLind\TiVo\VideoDownloader($mak, $guzzle);
$videoDownloader->setLogger($logger);
$videoDownloader->downloadPreview($showURL, $tivoFile);
//readfile($tivoFile);
// Verify file exists and has contents.
if (file_exists($tivoFile) && filesize($tivoFile) > 0) {
print(" > Preview file was sucessfully downloaded.\n");
} else {
print(" > Preview file was not sucessfully downloaded.\n");
finishRun($logFile);
}
// Decode the preview file.
validateClass('JimLind\TiVo\VideoDecoder');
print(" > Decoding the preview of the local show file.\n");
$videoDecoder = new JimLind\TiVo\VideoDecoder($mak, $builder);
$videoDecoder->setLogger($logger);
$videoDecoder->decode($tivoFile, $mpegFile);
// Verify file exists and has contents.
if (file_exists($mpegFile) && filesize($mpegFile) > 0) {
print(" > Preview file was sucessfully decoded.\n");
print(" > A decoding failure may be logged due to the size of the preview.\n");
} else {
print(" > Preview file was not sucessfully decoded.\n");
finishRun($logFile);
}
// Delete video files.
unlink($tivoFile);
unlink($mpegFile);
print(" > Verification completed successfully.\n");
finishRun($logFile);
/*
* Helper function to make sure everything is properly autoloaded.
*/
function validateClass($className)
{
if (class_exists($className) === false) {
print(" > Can't load {$className}.\n");
print(" > Did you run `composer install` already?\n");
finishRun();
}
}
/**
* Helper function to display log and exit early.
*/
function finishRun($logFile = false)
{
if ($logFile) {
print("\n::::Log Contents::::\n");
readfile($logFile);
print("\n");
unlink($logFile);
}
die;
}