summaryrefslogtreecommitdiffstats
path: root/index.php
blob: 77a29ee5f005551137cf538e4478bc951f225b7b (plain) (blame)
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
<?php
function runInBackground($command)
{
	$pid = shell_exec("nohup $command > lastRun.log 2>&1 & echo $!");
	file_put_contents("pidFile", $pid);
	return $pid;
}
function isProcessRunning($pid)
{
	exec("ps $pid", $state);
	return(count($state) >= 2);
}

$pid = @file_get_contents("pidFile");
$totalLines = @file_get_contents("totalLines");
$running = ($pid !== false && $totalLines !== false && isProcessRunning($pid));

if ($_GET["action"] == "progress") {
	if (!$running) {
		echo "Complete! Your spreadsheet has been e-mailed to you. (You may also <a href=\"MovieStats.csv\">download it from here</a>.)<br><a href=\"index.php\">Click here</a> to run again.";
		exit();
	}
	$statsFile = @fopen("MovieStats.csv", "r");
	if ($statsFile === false) {
		echo "Waiting to begin...";
		exit();
	}
	$lines = -2;
	while (!@feof($statsFile)) {
		@fgets($statsFile);
		$lines++;
	}
	if ($lines < 0)
		$lines = 0;
	if ($lines < $totalLines)
		echo "Progress: $lines movies out of $totalLines completed.";
	else
		echo "Finished... sending e-mail.";
	exit();
}
if ($running && $_GET["action"] == "terminate") {
	exec("kill $pid");
	$running = false;
}
if (!$running && $_GET["action"] == "start" && $_POST["forreal"] == "yeah") {
	@exec("kill $pid");
	file_put_contents("input.txt", $_POST['list']);
	file_put_contents("totalLines", (string)count(split("\n", trim($_POST['list']))));
	@unlink("MovieStats.csv");
	if ($_POST["type"] == "full")
		$script = "MovieSearchStats.py";
	elseif ($_POST["type"] == "blog")
		$script = "BlogSearchStats.py";
	runInBackground("/usr/bin/python $script input.txt MovieStats.csv '".$_POST['email']."'");
	$running = true;
}
?>
<html>
<head>
<title>AnyClip Movie Statistics Generator</title>
<style>
@font-face { font-family: 'Myriad Pro'; src: local('Myriad Pro Regular'), local('Myriad Pro'), url('http://static2.anyclipimg.com/fonts/MyriadPro-Regular.otf') format('opentype'); }
@font-face { font-family: 'Myriad Pro'; font-weight: bold; src: local('Myriad Pro Bold'), url('http://static2.anyclipimg.com/fonts/MyriadPro-Bold.otf') format('opentype'); }
@font-face { font-family: 'Myriad Pro'; font-style: italic; src: local('Myriad Pro Italic'), url('http://static2.anyclipimg.com/fonts/MyriadPro-It.otf') format('opentype'); }
@font-face { font-family: 'Myriad Pro'; font-weight: bold; font-style: italic; src: local('Myriad Pro Bold Italic'), url('http://static2.anyclipimg.com/fonts/MyriadPro-BoldIt.otf') format('opentype'); }
body,input,textarea { font-family: "Myriad Pro", Myriad, "Myriad Web Pro", Helvetica, Arial, sans-serif; }
</style>
</head>
<body>
<img src="/labs-logo.png" align="right">
<h2>AnyClip Movie Statistics Generator</h2>
<?php if ($running): ?>
<h4>Statistics are currently being generated. They will be e-mailed to you when complete. You may close this window, and statistics will continue to be generated; you may then return to this page at any time to check the progress.</h4>
<h3 id="progress"></h3>
<h5><br><br><br><br><a href="index.php?action=terminate">Click here</a> to terminate current stats job.</h5>
<script language="JavaScript">
var requestObj;
if(navigator.appName == "Microsoft Internet Explorer")
	requestObj = new ActiveXObject("Microsoft.XMLHTTP");
else
	requestObj = new XMLHttpRequest();

function updateProgress()
{
	requestObj.open('GET', "index.php?action=progress");
	requestObj.onreadystatechange = function()
	{
		if (requestObj.readyState == 4) {
			document.getElementById("progress").innerHTML = requestObj.responseText;
			setTimeout(updateProgress, 1000);
		}
	}
	requestObj.send(null);
}
updateProgress();
</script>
<?php else: ?>
<form action="index.php?action=start" method="POST">
<input type="hidden" name="forreal" value="yeah">
<p><label>E-mail: <input type="text" name="email" value="Matt@anyclip.com"></label></p>
<p><label>Movie list:<br>
<textarea name="list" rows="20" cols="25">
Gone with the Wind
Snow White
Star Wars New Hope
Bambi
Jaws
...fill me up...
</textarea></label></p>
<p><label><input type="radio" name="type" value="full" checked>YouTube &amp; Google Adwords Statistics</label><br>
<label><input type="radio" name="type" value="blog">Google Blog Search Statistics</label></p>
<input type="submit" value="Start">
</form>
<?php endif; ?>
</body>
</html>