Jump to content

Twitter feed with multiple accounts - skips firts account do you know why?


Recommended Posts

Hello everyone,

 

I'm working on a twitter module that will display a feed with over 7 twitter feeds all in the same feed sorted by newest first.  I have it just about done, I paid a coder on GetAFreelancer and he has it mostly working but it's not displaying the first account and he is not sure why...

 

Project Outline:

I have a twitter module for Joomla that we need updated to pull more than 7 feeds at a time combine all of the tweets and then sort them by the newest date first. The module also needs to use a cache file when twitter feeds are down as they are some of the time at peak hours.

 

Cache: The application needs to check for a cache file then look at the cache file if the created date is older than 10 minutes then connect to twitter for a new list of tweets, but if there is no file and no connection to twitter can be made we need to have a custom error message (Twitter is not available at this time, please try back later).

 

Data: The application will need to loop through each of the provided twitter accounts feeds getting the set number of tweets and save them to the cache file.

 

Sorting: The application will need to sort all of the tweets from all of the accounts out of the cache file and display them by the newest date first.

 

The Code:

<?php
// Get all of the accounts from the module's parameters
$usernames = $params->get('account');
// Get the number of tweets to be displayed from the module's parameters
if (!is_numeric($params->get('tweets'))){
  $tweets = 10;
}else{
  $tweets = $params->get('tweets');
}
// This comes before the entire block of tweets.
$prefix = $params->get('prefix');
// This comes after the entire block of tweets.
$suffix = $params->get('suffix');

// Configure relative date and time
function relativeTime($time){
  define("SECOND", 1);
  define("MINUTE", 60 * SECOND);
  define("HOUR", 60 * MINUTE);
  define("DAY", 24 * HOUR);
  define("MONTH", 30 * DAY);

$delta = time() - $time;
if ($delta < 2 * MINUTE){
	return "About 1 min ago";
}
if ($delta < 45 * MINUTE){
	return "About ".floor($delta / MINUTE) . " min ago";
}
if ($delta < 90 * MINUTE){
	return "About 1 hour ago";
}
if ($delta < 24 * HOUR){
	return "About ".floor($delta / HOUR) . " hours ago";
}
if ($delta < 48 * HOUR){
	return "Yesterday";
}
if ($delta < 30 * DAY){
	return floor($delta / DAY) . " days ago";
}
if ($delta < 12 * MONTH){
	$months = floor($delta / DAY / 30);
	return $months <= 1 ? "1 month ago" : $months . " months ago";
}else{
	$years = floor($delta / DAY / 365);
	return $years <= 1 ? "1 year ago" : $years . " years ago";
}
}

// Check if the cache folder exists if not create it
if (!file_exists('cache/mod_twitter/')){
  mkdir("cache/mod_twitter", 0777);
}

// Set cache file name and path
$Tfile = getcwd() . "/cache/mod_twitter/twitter_feed.cashe";

$exploded = explode(' ', $usernames);
$ulength = count($exploded);

$tsections = 0;
$tsections = intval($ulength / 7);
if ($ulength > ($tsections * 7)) {
  $tsections = $tsections + 1;	
}

$fed = "";

for ($u = 0; $u < $tsections; $u++)
{
  $llimit = ($u * 7) + 1;
  if (($llimit + 6) < $ulength)
  {
    $ulimit = $llimit + 6;
  }
  else
  {
    $ulimit = $ulength;
  }
  $usernamed = $exploded[$llimit - 1];

  for ($o = $llimit; $o < $ulimit; $o++)
  {
  $usernamed = $usernamed . ' ' . $exploded[$o];
  }
  
  $usernames = str_replace(" ", "+OR+from%3A", $usernamed);
  $feed = "http://search.twitter.com/search.atom?q=from%3A" . $usernames . "&rpp=" . $tweets;

  $feded = file_get_contents($feed);

  $fedd = explode('<entry>', $feded, 2);
  $feddd = explode('</feed>', '<entry>' . $fedd[1]);
  $fed = $fed . $feddd[0];
}

$fed = '<feed>' . $fed . '</feed>';
$fed = str_replace('<twitter:source>', '<twittersource>', $fed);
$fed = str_replace('</twitter:source>', '</twittersource>', $fed);
$fed = str_replace('<twitter:lang>', '<twitterlang>', $fed);
$fed = str_replace('</twitter:lang>', '</twitterlang>', $fed);

$xml = simplexml_load_string($fed);
$i = 0;
foreach ($xml->entry as $entryed) {
  $published = $entryed->published;
  $published = str_replace(' ', '', $published);
  $published = str_replace(':', '', $published);
  $published = str_replace('-', '', $published);
  for ($j=65; $j<91; $j++)
  {
    $published = str_replace(chr($j), '', $published);	  
  }
  for ($j=97; $j<123; $j++)
  {
    $published = str_replace(chr($j), '', $published);	  
  }


  $entries[$i][0] = $published;
  $entries[$i][1] = $entryed->asXML();
  $i++;
}
sort($entries);
$fed = "";
for ($e=count($entries); $e>=0; $e--)
{
$fed = $fed . $entries[$e][1];
}


$fed = '<feed>' . $fed . '</feed>';


if (strpos($fed, '</entry>') !== false)
{
  $Handle = fopen($Tfile, 'w');
  fwrite($Handle, $fed); 
  fclose($Handle); 
}  
else
{
  $fed = file_get_contents($Tfile, FILE_USE_INCLUDE_PATH);
}

  $feed = $fed;
  $clean = explode("<entry>", $feed);
  $amount = count($clean) - 1;

  for ($i = 1; $i <= $tweets; $i++) {
    $entry_close = explode("</entry>", $clean[$i]);
    $clean_content_1 = explode("<content type=\"html\">", $entry_close[0]);
    $clean_content = explode("</content>", $clean_content_1[1]);
    $clean_name_2 = explode("<name>", $entry_close[0]);
    $clean_name_1 = explode("(", $clean_name_2[1]);
    $clean_name = explode(")</name>", $clean_name_1[1]);
    $clean_uri_1 = explode("<uri>", $entry_close[0]);
    $clean_uri = explode("</uri>", $clean_uri_1[1]);
    $clean_published_1 = explode("<published>", $entry_close[0]);
    $clean_published = explode("Z", $clean_published_1[1]);
$clean_published = str_replace("T", " ", $clean_published);
$clean_time_1 = explode("<published>", $entry_close[0]);
$clean_time = explode("</published>", $clean_time_1[1]);
$unix_time = strtotime($clean_time[0]);
$date = relativeTime($unix_time);
    $displayDate = date($params->get('dateformat'), strtotime($clean_published[0]));
    $accountname = explode(")", $clean_name_1[1]);



// twitter wrapper tag
$displaytweets  = '<'.$params->get('divspan').' class="tweetswrapper'.$params->get('moduleclass_sfx').'">';

// display tweet icon
if($params->get('showicon')== "1"):
  $displaytweets .= '<'.$params->get('divspan').' class="tweet_icon'.$params->get('moduleclass_sfx').'">';
  $displaytweets .= '<img src="modules/mod_twitter/images/twitter.png" />';
  $displaytweets .= '</'.$params->get('divspan').'>';
endif;

// tweets content wrapper
if($params->get('showicon')== "1"):
  $displaytweets .= '<'.$params->get('divspan').' class="tweet'.$params->get('moduleclass_sfx').'">';
endif;

// display tweet date
if($params->get('showdate')== "1"):
  $displaytweets .= '<'.$params->get('divspan').' class="tweet_date'.$params->get('moduleclass_sfx').'">';
  $displaytweets .= $date.' - '.$displayDate;
  $displaytweets .= '</'.$params->get('divspan').'>';
endif;

// tweet content
$displaytweets .= '<'.$params->get('divspan').' class="tweet_content'.$params->get('moduleclass_sfx').'">';
$displaytweets .= html_entity_decode(str_replace("#fb", "", $clean_content[0]));
$displaytweets .= '</'.$params->get('divspan').'>';

// display read more
if($params->get('displayreadmore') == "1"):
  $displaytweets .= '<'.$params->get('divspan').' class="tweet_readmore'.$params->get('moduleclass_sfx').'" align="'.$params->get('readmorealign').'">';
      $displaytweets .= '<a href="' . $clean_uri[0] . '" title="Read more over at the ' . $clean_name[0] . ' twitter page!" id="twitter_link" target="'.$params->get('twitterTarget').'">'.$params->get('readmoretext').' »</a>';
  $displaytweets .= '</'.$params->get('divspan').'>';
    endif;

// display tweet twitter
if($params->get('submittedby')== "1"):
  $displaytweets .= '<'.$params->get('divspan').' class="tweet_submitted_by'.$params->get('moduleclass_sfx').'">';
  $displaytweets .= 'by: <a href="http://twitter.com/'.$clean_name_1[0].'" target="'.$params->get('twitterTarget').'">'.$accountname[0].'</a>';
  $displaytweets .= '</'.$params->get('divspan').'>';
endif;

// closing tweet wrapper
if($params->get('showicon')== "1"):
  $displaytweets .= '</'.$params->get('divspan').'>';
endif;

// closing twitter wrapper
$displaytweets .= '</'.$params->get('divspan').'>';

// twitter clear any css floats
    $displaytweets .= '<div class="tweet_clear"></div>';

// display full tweet
echo $displaytweets;

}

// This comes after the entire block of tweets.
echo $suffix;


?>

I Just quickly skimmed the code,

 

first this

$clean = explode("<entry>", $feed);

returns an array

 

then you have this, which is wrong

for ($i = 1; $i <= $tweets; $i++) {

 

arrays start at 0

for ($i = 0; $i <= $tweets; $i++) {

 

 

PS I hope the programmer was cheap..

 

EDIT:

Just as a note, when I work on projects, if something didn't work, and I told the client. "I don't know why" I wouldn't expect to get paid (well not the full money at least!)

Is $250 for a 24 hour turn around time a good deal? not sure but this was out of my skill set, I know basic php and that's it.

 

Odd if I change my code like the above the first tweet looses it's content, the date and user name still show but the content is gone verry odd.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.