Topic Closed
Tobias
40 Posts

Hi Guys!

One plugin problem solved and here is the next: I have added the MyTwitter gadget to the website and it works as expected.

Using the MyTwitter Gadget which makes use of the "http://search.twitter.com" just contains a few / random tweets. Maybe this is due to server overload, but when i use the official Widget from Twitter all the content is there.

I then tried twitterjs-1.13.1 and that works also fine, but just collects all my tweets, but none of the retweets. So basically: The only way to get all the data seems to be to use the official Twitter Widget (which looks gruesome on a narrow side-panel).

Does anybody know a different JavaScript, or jQuery or PHP script or some other trick i could try?

Bests, Tobias

P.S.: Feels strange having a "normal" weekend when you watch the news about Japan. Just noticed that the Gadget or rather the Twitter site isn't doing what i expected after retweeting a few news items, which didn't show up.

 

11 years ago#1637

Tobias
40 Posts

Hi there... Found my own solution by rewriting the Gadget like this:

  function showTweet()
  {
    // TSE: Modified using code from http://blog.ninanet.com/2010/04/19/twitter-feed-with-php

    $link = 'http://twitter.com/statuses/user_timeline/'.$this->username.'.atom?include_rts=true&count=1';
    $feed = file_get_contents($link);
    ereg("<content type=\"html\">(.*)</content>", $feed, $tweet);

    echo '<h3><a href="http://twitter.com/'.$this->username.'" target="_blank">@'.$this->username.'</a></h3>'."\n";

    if (!empty($tweet[1])) {
      // Remove Username
      $tweet[1] = str_replace($this->username.": ", "", $tweet[1]);
      // Add Links
      $tweet[1] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", "<a href=\"http://$2$3\" target=\"_blank\">$1$2$4</a>", $tweet[1]);
      // Add Usernames
      $tweet[1] = preg_replace("/@(\w+)/", "@<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">\\1</a>", $tweet[1]);
      // Add Keywords
      $tweet[1] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweet[1]);
      // Cleanup Long Dash
      $tweet[1] = str_replace("&#8211;", "&mdash;", $tweet[1]);

      echo htmlspecialchars_decode($tweet[1])."\n";
    }
    else {
      echo 'No tweets available...';
    }

Seems to work so far, though maybe a simple file caching should be added. And you want to get really serious, use tmhOAuth for OAuth authentification :)

Bests, Tobias

11 years ago#1644

Tobias
40 Posts

...Added simple file caching to MyTwitter Gadget. Yeah performance boost on my webpage loading time! Things are getting a bit difficult to post on the forum. Anybody interested in reviewing my clumsy PHP ?

Bests, Tobias  

11 years ago#1655

Stano
483 Posts
16.6K Downloads
1 Themes
1 Plugins

nice...

Edited: 11 years ago#1656

Josh S.
2K Posts
304K Downloads
16 Themes
18 Plugins

Did you ever get the updates to the twitter addon author? If not, here's his website: http://www.opusonline.at/

11 years ago#1691

Tobias
40 Posts

Just sent it to the Author via Mail... Tobias  

P.S.: That you do not define a data type in PHP for variables gives me a woozy feeling in the stomach

11 years ago#1696

Josh S.
2K Posts
304K Downloads
16 Themes
18 Plugins

P.S.: That you do not define a data type in PHP for variables gives me a woozy feeling in the stomach

You mean like "int a_number = 12345" in java syntax? That sort of strong variable typing isn't even supported by PHP.

11 years ago#1702

Tobias
40 Posts

Hi Josh!

Did you ever get my changes for the MyTwitter gadget reviewed & redelivered from the original author?
If not i can also send you a zip, if you drop me a short E-Mail.

Bests,
Tobias
 

11 years ago#2132

Josh S.
2K Posts
304K Downloads
16 Themes
18 Plugins

Tobias,

I've only seen the code you posted to the forum here. If opusonline.at isn't updating the MyTwitter plugin anymore, a new plugin (say "MyTwitter2" ) could be created with your improvements.

11 years ago#2217

Tobias
40 Posts

Well here are the complete changes in MyTwitterGadget.php:

<?php
defined('is_running') or die('Not an entry point...');

class MyTwitterGadget
{
    // Modified by TSE using code from
    // http://blog.ninanet.com/2010/04/19/twitter-feed-with-php   and
    // http://return-true.com/2010/02/adding-file-caching-to-your-twitter-api-script/
   
    var $header;
    var $username;
    var $is_working = true;
   
    const CACHE_EMPTY = 1;
    const CACHE_EXPIRED = 2;
    const CACHE_OK = 3;
   
    function MyTwitterGadget()
    {
        $this->getData();
       
        if ($this->is_working and !common::LoggedIn())
        {
            $this->showTweet();
        }
       
        if (common::LoggedIn())
        {
            echo '<h3>'.$this->header.'</h3>'."\n";
            echo common::Link('Admin_MyTwitter', 'Twitter Admin')."\n";
        }
    }
   
    function getData()
    {
        global $addonPathData;
        $configFile = $addonPathData.'/index.php';
       
        if (file_exists($configFile))
        {
            require($configFile);
            $this->header = $twitterConfig['gadget_head'];
            $this->username = $twitterConfig['twitter_username'];
           
            if ($twitterConfig['gadget_enable'] != 'enable')
                $this->is_working = false;
        }
        else
        {
            $this->is_working = false;
        }
    }
   
    function checkCache($cPath)
    {
        if (file_exists($cPath))
        {
            $modtime = filemtime($cPath);
            // 20 minutes ago in Unix timestamp format (no. seconds since 1st Jan 1970)
            $timeago = time() - 1200;
           
            if($modtime < $timeago)
                $cache_state = self::CACHE_EXPIRED;
            else
                $cache_state = self::CACHE_OK;
        }
        else
        {
            $cache_state = self::CACHE_EMPTY;
        }
       
        return $cache_state;
    }
   
    function updateCache($cPath)
    {
        $link = 'http://twitter.com/statuses/user_timeline/'.$this->username.'.atom?include_rts=true&count=1';
        $feed = file_get_contents($link);
        ereg("<content type=\"html\">(.*)</content>", $feed, $tweet);
       
        if (!empty($tweet[1]))
        {
            // Remove Username
            $tweet[1] = str_replace($this->username.": ", "", $tweet[1]);
            // Add Links
            $tweet[1] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", "<a href=\"http://$2$3\" target=\"_blank\">$1$2$4</a>", $tweet[1]);
            // Add Usernames
            $tweet[1] = preg_replace("/@(\w+)/", "@<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">\\1</a>", $tweet[1]);
            // Add Keywords
            $tweet[1] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweet[1]);
            // Cleanup Long Dash
            $tweet[1] = str_replace("&#8211;", "&mdash;", $tweet[1]);
           
            //Let's save our data into the cache
            $fp = fopen($cPath, 'w');
            if(flock($fp, LOCK_EX))
            {
                fwrite($fp, $tweet[1]);
                flock($fp, LOCK_UN);
            }
            fclose($fp);
           
            return $tweet[1];
        }
        else
        {
            return '';
        }
    }
   
    function showTweet()
    {
        global $addonPathData;
        $cPath = $addonPathData.'/'.$this->username.'.cache';
       
        // Check Cache
        $cache_state = $this->checkCache($cPath);
       
        // Cache is empty or expired
        if ( self::CACHE_OK != $cache_state )
        {
            // Try to update the cache
            $tweet = $this->updateCache($cPath);
           
            if ($tweet == '')
            {
                // If that failed and we have an expired cache, use the old one
                if ($cache_state == self::CACHE_EXPIRED )
                    $tweet = file_get_contents($cPath);
                // If we don't have an expired cache either, show an error message
                else
                    $tweet = 'No tweets available...';
            }
        }
        // Cache is ok, so read it
        else
        {
            $tweet = file_get_contents($cPath);
        }
       
        // Display the tweet
        echo '<h3><a href="http://twitter.com/'.$this->username.'" target="_blank">'.$this->header.'</a></h3>'."\n";
        echo htmlspecialchars_decode($tweet)."\n";
    }
}

 

Sorry i am no professional PHP programmer, hope it's not too bad :)

Bests,

Tobias

 

11 years ago#2447

Topic Closed

 

News

elFinder 2.1.50 in Upcoming Release
12/28/2019

A new release for Typesetter is in the works with a lot of improvements including the ... Read More

Typesetter 5.1
8/12/2017

Typesetter 5.1 is now available for download. 5.1 includes bug fixes, UI/UX improvements, ... Read More

More News

creisi productions

Dienstleistungen von creisi productions, Ennetb├╝rgen (Schweiz): * Konzeption, Planung und Erstellung Ihres Internet-Auftritts * Betreuung und Aktualisierung/Pflege Ihrer Websi...

Find out more about our Provider Spotlight

Log In

  Register