-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_generate_tweets.php
81 lines (67 loc) · 2.49 KB
/
cron_generate_tweets.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
<?php
/*
* Custom script to pregenerate tweet msgs
*/
include('inc/include_top.php');
include('inc/class/class.cron.php');
$cron = new cronFuncs();
set_time_limit(0);
$run_cron = true;
//Check crpn key and if running
if ( ($argv[1] != CRON_KEY) and ($_GET['cron_key'] != CRON_KEY) ) {
echo mainFuncs::push_response(23);
$run_cron = false;
} else {
if ($cron->get_cron_state('gen_tweets') == 1) {
echo mainFuncs::push_response(24);
$run_cron = false;
}
}
if ($run_cron != true) {
exit();
}
$db->output_error = 1;
//Set cron status
$cron->set_cron_state('gen_tweets', 1);
$cron->set_log(1);
$configs = $db->query("
SELECT *
FROM " . DB_PREFIX . "users_config
WHERE tweet_bot_status=1;
");
while ($userConfig = mysql_fetch_array($configs, MYSQL_ASSOC)) {
$cron->set_user_id($userConfig['user_id']);
$queryLimit = $userConfig['tweet_generation_rate'] ? $userConfig['tweet_generation_rate'] : 0;
$queryOffset = $userConfig['tweet_generation_offset'] ? $userConfig['tweet_generation_offset'] : 0;
$tweetsCount = 0;
if (!empty($userConfig['tweet_template']) && !empty($userConfig['tweet_query'])) {
$tweetsData = $db->query($userConfig['tweet_query'] . " LIMIT {$queryLimit} OFFSET {$queryOffset};");
while ($data = mysql_fetch_array($tweetsData, MYSQL_ASSOC)) {
$tweet_content = bind_to_template($data, $userConfig['tweet_template']);
addTweetToQueue($tweet_content, $userConfig['user_id']);
$tweetsCount++;
}
}
$newOffset = $queryOffset + $queryLimit;
$db->query("UPDATE " . DB_PREFIX . "users_config
SET tweet_generation_offset={$newOffset}
WHERE user_id='{$userConfig['user_id']}';");
$cron->store_cron_log(6, "Generated {$tweetsCount} new tweets for user {$userConfig['user_id']}", '');
}
$cron->set_cron_state('gen_tweets',0);
function bind_to_template($replacements, $template)
{
$GLOBALS['replacements'] = $replacements;
$callback = create_function('$matches', 'return $GLOBALS["replacements"][$matches[1]];');
return preg_replace_callback('/{{(.+?)}}/', $callback , $template);
}
function addTweetToQueue($tweet_content, $user_id)
{
global $db;
$createDateTime = date('Y-m-d H:i:s');
$db->query("
INSERT INTO " . DB_PREFIX . "tweets_queue
(user_id, datetime_created, tweet_content)
VALUES ('{$db->prep($user_id)}', '{$db->prep($createDateTime)}', '{$db->prep($tweet_content)}');
");
}