root/twitter/files/lib/system/cronjob/TwitterUpdateJob.class.php @ 1335

Revision 1335, 4.3 kB (checked in by Torben Brodt, 2 years ago)

automatically add feeds for twitter system user

Line 
1<?php
2require_once(WCF_DIR.'lib/data/cronjobs/Cronjob.class.php');
3require_once(WCF_DIR.'lib/data/twitter/Twitter.class.php');
4
5/**
6 * query twitter retweets
7 *
8 * @author      Torben Brodt
9 * @package     de.easy-coding.wcf.twitter
10 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
11 */
12class TwitterUpdateJob implements Cronjob {
13
14        /**
15         * @see Cronjob::execute()
16         */
17        public function execute($data) {
18                $sql = "SELECT          *
19                        FROM            wcf".WCF_N."_twitter_feed ti
20                        INNER JOIN      wcf".WCF_N."_twitter_account ta ON ti.accountID = ta.accountID
21                        INNER JOIN      wcf".WCF_N."_user_to_twitter utt ON utt.accountID = ta.accountID
22                        WHERE           ti.failcount < 5";
23                $result = WCF::getDB()->sendQuery($sql);
24                $i = 0;
25                while ($row = WCF::getDB()->fetchArray($result)) {
26                        $this->load($row);
27                        $i++;
28                }
29
30                // try to active system user feeds
31                if($i == 0) {
32                        $user = $this->getSystemUser();
33                        if($user) {
34                                $sql = "INSERT INTO
35                                                        wcf".WCF_N."_twitter_feed
36                                                        (accountID, action)
37                                        VALUES          (".intval($user['accountID']).", 'user_timeline'),
38                                                        (".intval($user['accountID']).", 'mentions')
39                                        ON DUPLICATE KEY UPDATE
40                                                        failcount = IF(failcount > 1, failcount - 1, 0)";
41                                $result = WCF::getDB()->sendQuery($sql);
42                        }
43                }
44        }
45
46        protected function getSystemUser() {
47                $sql = "SELECT          *
48                        FROM            wcf".WCF_N."_twitter_account ta
49                        INNER JOIN      wcf".WCF_N."_user_to_twitter utt ON utt.accountID = ta.accountID
50                        WHERE           ta.screen_name = '".escapeString(TWITTER_USER)."'";
51                return WCF::getDB()->getFirstRow($sql);
52        }
53
54        public function updateAccount($user) {
55                $sql = 'SELECT  *
56                        FROM    wcf'.WCF_N.'_twitter_account
57                        WHERE   accountID = '.intval($user->id);
58                $row = WCF::getDB()->getFirstRow($sql);
59
60                if($row) {
61                        $update = array();
62                        foreach($row as $key => $val) {
63                                if($key == 'accountID') {
64                                        continue;
65                                }
66
67                                if($user->$key != $val) {
68                                        $update[] = $key.' = "'.escapeString($val).'"';
69                                }
70                        }
71
72                        // update
73                        if(count($update)) {
74                                $sql = "UPDATE          wcf".WCF_N."_twitter_account
75                                        SET             ".implode(",", $update)."
76                                        WHERE           accountID = ".intval($user->id);
77                                $result = WCF::getDB()->sendQuery($sql);
78                        }
79                } else {
80                        // insert
81                        $sql = "INSERT INTO     wcf".WCF_N."_twitter_account
82                                                (accountID, name, screen_name, profile_image_url)
83                                VALUES          (".intval($user->id).", '".escapeString($user->name)."',
84                                                '".escapeString($user->screen_name)."', '".escapeString($user->profile_image_url)."')";
85                        $result = WCF::getDB()->sendQuery($sql);
86                }
87        }
88
89        /**
90         *
91         */
92        public function load($me) {
93                $twitterObj = Twitter::getInstance();
94                $twitterObj->setToken($me['userToken'], $me['userSecret']);
95
96                $opts = array();
97
98                if($me['since_id']) {
99                        $opts['since_id'] = $me['since_id'];
100                }
101
102                try {
103                        $list = $twitterObj->get('/statuses/'.$me['action'].'.json', $opts);
104                } catch(Exception $e) {
105
106                        // increase fail count and stop
107                        $sql = 'UPDATE  wcf'.WCF_N.'_twitter_feed
108                                SET     failcount = failcount + 1
109                                WHERE   feedID = '.intval($me['feedID']);
110                        WCF::getDB()->sendQuery($sql);
111
112                        return;
113                }
114
115                $maxid = 0;
116                foreach($list as $message) {
117                        $maxid = max($maxid, $message->id);
118
119                        $this->updateAccount($message->user);
120
121                        // create message
122                        $sql = 'INSERT INTO
123                                        wcf'.WCF_N.'_twitter_message
124                                        (tweetID, accountID, username, message, time)
125                                VALUES ('.intval($message->id).', '.intval($message->user->id).',
126                                        "'.escapeString($message->user->screen_name).'", "'.escapeString($message->text).'",
127                                        '.intval(strtotime($message->created_at)).')';
128
129                        try  {
130                                WCF::getDB()->sendQuery($sql);
131                                $messageID = WCF::getDB()->getInsertID("wcf".WCF_N."_twitter_message", 'messageID');
132
133                        } catch(Exception $e) {
134                                $sql = 'SELECT  messageID
135                                        FROM    wcf'.WCF_N.'_twitter_message
136                                        WHERE   tweetID = '.intval($message->id);
137                                $row = WCF::getDB()->getFirstRow($sql);
138                                $messageID = $row['messageID'];
139                        }
140
141                        // create link to feed
142                        $sql = 'INSERT IGNORE INTO
143                                        wcf'.WCF_N.'_twitter_message_to_feed
144                                        (messageID, feedID)
145                                VALUES ('.intval($messageID).', '.intval($me['feedID']).')';
146                        WCF::getDB()->sendQuery($sql);
147                }
148
149                if($maxid) {
150
151                        // update max id and decrease fail count
152                        $sql = 'UPDATE  wcf'.WCF_N.'_twitter_feed
153                                SET     since_id = '.intval($maxid + 1).',
154                                        failcount = IF(failcount > 1, failcount - 1, 0)
155                                WHERE   feedID = '.intval($me['feedID']);
156                        WCF::getDB()->sendQuery($sql);
157                }
158        }
159}
160?>
Note: See TracBrowser for help on using the browser.