-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl.php
168 lines (145 loc) · 5.15 KB
/
cl.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
157
158
159
160
161
162
163
164
165
166
167
168
<?php
date_default_timezone_set('UTC');
set_time_limit(0);
$max = 100000;
$sub = "minecraft";
$getReplies = "false";
$count = 0;
$after = "fake";
$end = time();
$day = 86400;
$stopFile = "stop.txt";
$runFile = "running.txt";
if (file_exists($runFile)) {
$myfile = fopen($runFile, "r") or die("Unable to open file!");
$workingSub = fread($myfile,filesize($runFile));
fclose($myfile);
echo "Work is in progress on subreddit " . $workingSub . " check <a href='/?sub=" . $workingSub . "'>here</a> periodically to see if it has completed.";
exit();
}
$options = getopt("s:m:r:");
if (isset($options["m"])) {
$max = $options["m"];
}
if (isset($options["s"])) {
$sub = $options["s"];
}
if (isset($options["r"])) {
$getReplies = $options["r"];
}
$zipFile = $sub . "/" . $sub . ".zip";
if (file_exists($sub . "/done.txt")) {
echo "File exists, download it <a href='" . $zipFile . "'>here</a>";
exit();
}
if (file_exists($sub)) {
echo "Work is in progress on subreddit " . $sub . ", check back here for the zip file when it has completed.";
exit();
}
unlink("cat.out");
echo "Work started on subreddit " . $sub . ", check back here for the zip file when it has completed.";
$baseUrl = "http://www.reddit.com/r/" . $sub . "/search.json?sort=top&restrict_sr=on&syntax=cloudsearch&limit=100&q=timestamp%3A";
$replyBase = "http://www.reddit.com";
mkdir($sub);
$myfile = fopen($runFile, "w");
$txt = $sub;
fwrite($myfile, $txt);
fclose($myfile);
$myfile = fopen($sub . "/" . $sub . ".csv", "w");
$header = array("Reddit ID", "Author", "Title", "Content", "Upvotes", "Reply To");
fputcsv($myfile, $header);
fclose($myfile);
while ($count < $max && !empty($after)) {
$after = "";
if (file_exists($stopFile)) {
exec("zip -r " . $zipFile . " " . $sub . "/");
$myfile = fopen($sub . "/done.txt", "w") or die("Unable to open file!");
$txt = "Done";
fwrite($myfile, $txt);
fclose($myfile);
unlink($runFile);
unlink($stopFile);
exit();
}
$start = $end - $day;
sleep(5);
$json = file_get_contents($baseUrl . $start . ".." . $end);
$retry = 0;
while(empty($json) && $retry < 10) {
sleep(2);
error_log("Retrying # " . $retry . ": " . $baseUrl . $start . ".." . $end);
$json = file_get_contents($baseUrl . $start . ".." . $end);
$retry++;
}
$obj = json_decode($json);
$contents = $obj->data->children;
foreach($contents as $content) {
$data = $content->data;
$num = $data->num_comments;
if ($num >= 10) {
$postContent = $data->selftext;
if (empty($postContent)) {
$postContent = $data->url;
}
$fields = array($data->name, $data->author, $data->title, $postContent, $data->score, 'nothing');
$myfile = fopen($sub . "/" . $sub . ".csv", "a");
fputcsv($myfile, $fields);
fclose($myfile);
echo "Wrote post for: " . $data->title . "\n";
error_log(date("F j, Y, g:i a") . ": Wrote post for: " . $data->title . "\n",3, "cat.out");
if ($getReplies != "false") {
sleep(2);
$replyJson = file_get_contents($replyBase . $data->permalink . ".json");
$retry = 0;
while (empty($replyJson) && $retry < 10) {
sleep(2);
$replyJson = file_get_contents($replyBase . $data->permalink . ".json");
$retry++;
}
$robj = json_decode($replyJson);
$replyContents = $robj[1]->data->children;
foreach($replyContents as $replyContent) {
$replyData = $replyContent->data;
parseReplyTree($replyData, $sub);
}
}
$count++;
}
$after = $data->name;
}
$end = $start - 1;
}
exec("zip -r " . $zipFile . " " . $sub . "/");
$myfile = fopen($sub . "/done.txt", "w") or die("Unable to open file!");
$txt = "Done";
fwrite($myfile, $txt);
fclose($myfile);
unlink($runFile);
$message = "The run for " . $sub . " has completed. You can get your dumb file here: 198.101.158.143/" . $zipFile . "\n\nClean up after you've downloaded, why don't you? 198.101.158.143/?sub=" . $sub . "&delete=true";
mail('[email protected]', 'Reddit Runner Ran', $message);
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
function parseReplyTree($replyData, $sub) {
if (isset($replyData->body)) {
$replyFields = array($replyData->name, $replyData->author, "nothing", $replyData->body, $replyData->score, $replyData->parent_id);
$myfile = fopen($sub . "/" . $sub . ".csv", "a");
fputcsv($myfile, $replyFields);
fclose($myfile);
if ($replyData->replies != "") {
foreach ($replyData->replies->data->children as $reply) {
parseReplyTree($reply->data, $sub);
}
}
}
}
?>