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

Revision 1333, 3.6 kB (checked in by Torben Brodt, 2 years ago)

more updates for twitter 2.1, allow to display mentions, see demo page

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                while ($row = WCF::getDB()->fetchArray($result)) {
25                        $this->load($row);
26                }
27        }
28
29        public function updateAccount($user) {
30                $sql = 'SELECT  *
31                        FROM    wcf'.WCF_N.'_twitter_account
32                        WHERE   accountID = '.intval($user->id);
33                $row = WCF::getDB()->getFirstRow($sql);
34
35                if($row) {
36                        $update = array();
37                        foreach($row as $key => $val) {
38                                if($key == 'accountID') {
39                                        continue;
40                                }
41
42                                if($user->$key != $val) {
43                                        $update[] = $key.' = "'.escapeString($val).'"';
44                                }
45                        }
46
47                        // update
48                        if(count($update)) {
49                                $sql = "UPDATE          wcf".WCF_N."_twitter_account
50                                        SET             ".implode(",", $update)."
51                                        WHERE           accountID = ".intval($user->id);
52                                $result = WCF::getDB()->sendQuery($sql);
53                        }
54                } else {
55                        // insert
56                        $sql = "INSERT INTO     wcf".WCF_N."_twitter_account
57                                                (accountID, name, screen_name, profile_image_url)
58                                VALUES          (".intval($user->id).", '".escapeString($user->name)."',
59                                                '".escapeString($user->screen_name)."', '".escapeString($user->profile_image_url)."')";
60                        $result = WCF::getDB()->sendQuery($sql);
61                }
62        }
63
64        /**
65         *
66         */
67        public function load($me) {
68                $twitterObj = Twitter::getInstance();
69                $twitterObj->setToken($me['userToken'], $me['userSecret']);
70
71                $opts = array();
72
73                if($me['since_id']) {
74                        $opts['since_id'] = $me['since_id'];
75                }
76
77                try {
78                        $list = $twitterObj->get('/statuses/'.$me['action'].'.json', $opts);
79                } catch(Exception $e) {
80
81                        // increase fail count and stop
82                        $sql = 'UPDATE  wcf'.WCF_N.'_twitter_feed
83                                SET     failcount = failcount + 1
84                                WHERE   feedID = '.intval($me['feedID']);
85                        WCF::getDB()->sendQuery($sql);
86
87                        return;
88                }
89
90                $maxid = 0;
91                foreach($list as $message) {
92                        $maxid = max($maxid, $message->id);
93
94                        $this->updateAccount($message->user);
95
96                        // create message
97                        $sql = 'INSERT INTO
98                                        wcf'.WCF_N.'_twitter_message
99                                        (tweetID, accountID, username, message, time)
100                                VALUES ('.intval($message->id).', '.intval($message->user->id).',
101                                        "'.escapeString($message->user->screen_name).'", "'.escapeString($message->text).'",
102                                        '.intval(strtotime($message->created_at)).')';
103
104                        try  {
105                                WCF::getDB()->sendQuery($sql);
106                                $messageID = WCF::getDB()->getInsertID("wcf".WCF_N."_twitter_message", 'messageID');
107
108                        } catch(Exception $e) {
109                                $sql = 'SELECT  messageID
110                                        FROM    wcf'.WCF_N.'_twitter_message
111                                        WHERE   tweetID = '.intval($message->id);
112                                $row = WCF::getDB()->getFirstRow($sql);
113                                $messageID = $row['messageID'];
114                        }
115
116                        // create link to feed
117                        $sql = 'INSERT IGNORE INTO
118                                        wcf'.WCF_N.'_twitter_message_to_feed
119                                        (messageID, feedID)
120                                VALUES ('.intval($messageID).', '.intval($me['feedID']).')';
121                        WCF::getDB()->sendQuery($sql);
122                }
123
124                if($maxid) {
125
126                        // update max id and decrease fail count
127                        $sql = 'UPDATE  wcf'.WCF_N.'_twitter_feed
128                                SET     since_id = '.intval($maxid + 1).',
129                                        failcount = IF(failcount > 1, failcount - 1, 0)
130                                WHERE   feedID = '.intval($me['feedID']);
131                        WCF::getDB()->sendQuery($sql);
132                }
133        }
134}
135?>
Note: See TracBrowser for help on using the browser.