Thread Rating:
  • 3 Vote(s) - 4.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Release] MyTwitterBB 2.4
#21
I've made some modifications to your code in order to get it working for me. I have allow_url_fopen disabled on my hosting, so your script wouldn't work for me. I re-wrote the mytwitterbb_fetch() method to use the CURL PHP module and the twitter API and made some changes to the profile update section of task/mytwitterbb.php to reflect these changes. I started the mytwitterbb_fetch() method to be a drop-in replacement for your existing one but gave that up in the end and modified the task to use a different implementation of that method. I only use the profile section of mytwitterbb, so I've not made any amendment to the global twitter section.

It does rate-checking using the API - it stops making API requests if the server only has 30 requests left.

Requires CURL and PHP 5 with JSON module (included by default in 5.2).

I've included this code as I think the twitter grab is a cleaner way of grabbing tweets than your current implementation - you'll see its a lot less lines for starters! Feel free to amend and re-use as you like.

functions_mytwitterbb.php
function mytwitterbb_set_setting($name)
{

        //This setting is used to make sure that some settings are actually fetched from the database
        //For some reason some of the ones i was using were not being fetched.

        global $mybb, $db;

        if(!isset($mybb->setting[$name]))
        {

                $t = $db->fetch_array($db->query("SELECT value FROM ".TABLE_PREFIX."settings WHERE name='{$name}'"));

                if(is_array($t))
                {
                        $mybb->settings[$name] = $t['value'];
                }
                else
                {
                        $mybb->settings[$name] = "UNDEFINED";
                }
        }


} //End function: mytwitterbb_set_setting()


function mytwitterbb_grab($url) {
        return mytwitterbb_grab_curl($url);
}

function mytwitterbb_grab_curl($url) {
        $ch = curl_init($url);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_HEADER, 0 );
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
        $page = curl_exec($ch);
        curl_close($ch);
        return $page;
}

function mytwitterbb_get_rate_limit() {
        $data = mytwitterbb_grab("http://twitter.com/account/rate_limit_status.json");
        $json = json_decode($data);
        return $json->{"remaining_hits"};
}

function mytwitterbb_fetch($username) {
        global $mybb, $db;

        $remaining_requests = mytwitterbb_get_rate_limit();

        // dont break the remaining requests limit! stop working if there's 30 requests left
        if ($remaining_requests <= 30) {
                return "outoftime";
        }

        $url = "http://twitter.com/users/show/{$username}.json";
        $data = mytwitterbb_grab($url);
        $json = json_decode($data);

        if ($json->{"error"} == "Not found") {
                return "none";
        };

        $status = $json->{"status"}->{"text"};
        if ($status == "") {
                return "none";
        } else {
                return $status;
        };
}

tasks/mytwitterbb.php
function task_mytwitterbb()
{

        global $mybb, $template, $db;

        //Load settings
        mytwitterbb_set_setting("mytwitterbb_global");
        mytwitterbb_set_setting("mytwitterbb_profile");
        mytwitterbb_set_setting("mytwitterbb_profile_usertitle");

        if($mybb->settings['mytwitterbb_global'] == 1)
        {
                //Load the global settings
                mytwitterbb_set_setting("mytwitterbb_global_username");

                //Do the global twitter account
                $status = mytwitterbb_fetch($mybb->settings['mytwitterbb_global_username']);

                if(is_array($status))
                {
                        //Now update the data
                        $db->query("UPDATE ".TABLE_PREFIX."settings SET value='" . addslashes(utf8_decode($status['start'])) . "' WHERE name='mytwitterbb_global_tweet'");
                } //End if
        } //End if


        if($mybb->settings['mytwitterbb_profile'] == 1)
        {
                //Do the profile twitter accounts
                $userfields = $db->fetch_array($db->query("SELECT * FROM " . TABLE_PREFIX . "profilefields WHERE name='Twitter Username'"));
                $fid = $userfields['fid'];
                unset($userfields);

                $sql = $db->query("SELECT * FROM " . TABLE_PREFIX . "userfields WHERE fid{$fid}<>''");

                $sql = $db->query("SELECT * FROM " . TABLE_PREFIX . "userfields WHERE fid{$fid}<>''");

                while($row = $db->fetch_array($sql))
                {
                        //Fetch user data
                        $u = $db->fetch_array($db->query("SELECT * FROM ".TABLE_PREFIX."users WHERE uid='{$row['ufid']}'"));

                        //Update the information
                        if($u['mytwitterbb_use'] == "n")
                        {
                                $db->query("UPDATE ".TABLE_PREFIX."users SET mytwitterbb_backup_usertitle='{$u['usertitle']}', mytwitterbb_use='y' WHERE uid='{$u['uid']}'");
                        }

                        //Fetch their tweets
                        $twitter_username = $row['fid' . $fid];
                        $status = mytwitterbb_fetch($twitter_username);

                        if ($status == "outoftime") {
                                break;
                        }

                        if ($status == "none") {
                                $db->query("UPDATE ".TABLE_PREFIX."userfields SET fid{$fid}='' WHERE ufid='{$row['ufid']}'");
                        } else {
                                $status = addslashes($status);
                                //$status = mytwitterbb_remove_hashtag($status);
                                $status = htmlspecialchars_decode($status);
                                $status = '<a href="http://twitter.com/' . $twitter_username . '">@' . $twitter_username . '</a> ' . $status;
                                if ($status != "" && $status != $u['usertitle']) {
                                        if($mybb->settings['mytwitterbb_profile_usertitle'] == 1) {
                                                //Update their user title
                                                $db->query("UPDATE " . TABLE_PREFIX . "users SET usertitle='{$status}' WHERE uid='{$row['ufid']}'");
                                        }
                                        $db->query("UPDATE ".TABLE_PREFIX."users SET mytwitterbb_tweet='{$status}' WHERE uid='{$row['ufid']}'");
                                }
                        }
                        /*
                        //Make sure its valid.
                        if(is_array($status))
                        {
                                $pos = strpos($status['start'], ":");
                                $pos = $pos + 2;
                                $str = substr($status['start'], 0, $pos);
                                $stati = str_replace($str, "", $status['start']);
                                $stati = addslashes($stati);

                                //This appears sometimes for some reason, stop it!
                                $pos = strpos($stati, "http://static.twitter.com/images/arr2.gif");

                                $stati = mytwitterbb_remove_hashtag($stati);
                                $stati = htmlspecialchars_decode($stati);

                                if($stati != "" && $stati != $u['usertitle'] && !$pos)
                                {
                                        if($mybb->settings['mytwitterbb_profile_usertitle'] == 1)
                                        {
                                                //Update their user title
                                                $db->query("UPDATE " . TABLE_PREFIX . "users SET usertitle='{$stati}' WHERE uid='{$row['ufid']}'");
                                        }
                                        $db->query("UPDATE ".TABLE_PREFIX."users SET mytwitterbb_tweet='{$stati}' WHERE uid='{$row['ufid']}'");
                                } //End if
                        }
                        elseif($status == "none")
                        {
                                $db->query("UPDATE ".TABLE_PREFIX."userfields SET fid{$fid}='' WHERE ufid='{$row['ufid']}'");
                        } //End if
                        */

                        unset($status);

                } //End which

        } //End if statement

} //End function: task_mytwitterbb()

function mytwitterbb_remove_hashtag($str)
{

        global $mybb;

        mytwitterbb_set_setting("mytwitterbb_profile_hashtags");

        if($mybb->settings['mytwitterbb_profile_hashtag'] == 0)
        {
                //This adds a link to a tweet
                $words = explode(" ", $str);

                //Replace all of the links
                foreach($words as $id => $txt)
                {
                        $words[$id] = preg_replace("/\#(.*?)$/", "", $txt);
                }

                //Reconstruct and return
                return implode(" ", $words);
        }

        return $str;
}


I'm not going to support this, but letting you know that this works for me. Feel free to take what you want for any future versions.
If I make any further changes to this code, I'll reply with those also.
Reply
#22
An interesting list of changes. Now i will have to get my head around the curl and json things before i can fully understand it.

Thanks for proposing the changes i will have a look into them when i get a chance Smile
[Image: Supa-Comix-Signature.jpg]
Reply
#23
(2009-08-07, 11:02 AM)SupaComix Wrote: An interesting list of changes. Now i will have to get my head around the curl and json things before i can fully understand it.

Thanks for proposing the changes i will have a look into them when i get a chance Smile

Let me know if you setup a github with this code and I'll contribute some changes that way. I've noticed one or two issues with what I have currently so when I've got a spare hour I'll re-write the functions and the task manager parts.
Reply
#24
i installed this and then took it off. however, the usertitle is still the same as my twitter and not reverted back to the standard usertitle. i have tried to install it again, however, after activating the plugin and it says it is activated sucessfully, no options appear for it anymore in the settings tab.

can somebody help me so that i can revert my usertitle back to standard as it is driving me nuts as the twitter usertitle has upset my whole forums??
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)