-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcron.php
162 lines (134 loc) · 4.31 KB
/
cron.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
<?php
use \RedBeanPHP\R as R;
use \League\OAuth2\Client\Token\AccessToken;
use \League\OAuth2\Client\Provider\Exception\IdentityProviderException;
require_once __DIR__."/vendor/autoload.php";
require_once __DIR__."/Cronnit.php";
$cronnit = new Cronnit();
$cronnit->connect();
$pending = R::find('post', '(`url` is null) and (`error` is null) and (`when` < ?) order by `when` asc', [time()]);
if (empty($pending)) {
echo "No posts ready looking for backlog\n";
$pending = R::find('post', '
(`url` is null) and
(`error` is null) and
(`when_original` < ?)
order by `when_original` asc
limit 5', [time()]);
}
$reddit = $cronnit->getReddit();
$account_sums = [];
foreach ($pending as $post) {
echo "posting {$post->id} for {$post->account->name}\n";
if ($post->account->banned) {
$post->error = $post->account->banned;
R::store($post);
continue;
}
$limit = @$post->account->dailyLimit ?? 5;
if ($cronnit->countDailyPosts($post->account, $post->when) > $limit) {
$post->error = "Post exceeded daily limit of $limit";
R::store($post);
continue;
}
try {
$accessToken = $cronnit->getAccessToken($post->account);
} catch (IdentityProviderException $e) {
$post->error = "Unable to get an access token - did you revoke access?";
R::store($post);
continue;
}
$data = [
'title' => $post->title,
'sr' => $post->subreddit,
'api_type' => 'json',
'sendreplies' => intval($post->sendreplies),
'nsfw' => intval($post->nsfw),
'resubmit' => true
];
if (preg_match('#^http[s]?://#i', $post->body)) {
$data['kind'] = 'link';
$data['url'] = trim($post->body);
} else {
$data['kind'] = 'self';
$data['text'] = $post->body;
}
try {
$response = $cronnit->api($accessToken, 'POST', 'api/submit', [
'body' => http_build_query($data)
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$post->error = "HTTP error: {$e->getMessage()}";
R::store($post);
continue;
}
if (isset($response->json->data->url)) {
$post->when_posted = time();
$post->url = $response->json->data->url;
R::store($post);
} else if ($response->json->errors) {
if (isset($response->json->ratelimit)) {
$post->ratelimit_count += 1;
$post->ratelimit_sum += (int)ceil($response->json->ratelimit) + @$account_sums[$post->account->id];
$post->when_original = $post->when_original ?: $post->when;
$post->when = time() + (int)ceil($response->json->ratelimit);
R::store($post);
@$account_sums[$post->account->id] += (int)$response->json->ratelimit;
}
if (count($response->json->errors) == 1 && $response->json->errors[0][0] == 'RATELIMIT') {
var_dump($response->json);
continue;
}
$errors = [];
foreach ($response->json->errors as $error) {
if ($error[0] != 'RATELIMIT') {
$errors[] = $error[1];
}
}
if (empty($errors)) {
$post->error = "A strange error happened while posting";
$post->errorResponse = json_encode($response->json);
} else {
$post->error = implode(',', $errors);
}
R::store($post);
var_dump($response->json);
}
}
$pendingComments = R::convertToBeans('comment', R::getAll("
select `comment`.*
from `comment`
join `post` on (`post`.`id` = `comment`.`post_id`)
where
(`comment`.`error` is null) and
(`comment`.`url` is null) and
((`post`.`when` + `comment`.`delay`) < ?)
", [time()]));
foreach ($pendingComments as $comment) {
if (!preg_match('#comments/([^/]+)/#i', $comment->post->url, $match)) {
$comment->error = "Malformed link";
R::store($comment);
trigger_error("Cannot extract thing ID for post #{$comment->post->id}");
continue;
}
$thingID = "t3_{$match[1]}";
$accessToken = $cronnit->getAccessToken($comment->post->account);
$response = $cronnit->api($accessToken, 'POST', 'api/comment', [
'body' => http_build_query([
'thing_id' => $thingID,
'text' => $comment->body,
'return_rtjson' => true
])
]);
if (isset($response->permalink)) {
$comment->url = $response->permalink;
if ($comment->url[0] === '/') {
$comment->url = "https://www.reddit.com{$comment->url}";
}
R::store($comment);
} else {
$comment->error = "Problem while posting comment";
R::store($comment);
var_dump(json_encode($response));
}
}