You are here:  » Random RSS source


Random RSS source

Submitted by Demarco on Sun, 2009-07-26 18:56 in

Hi Dave,

i need your help because i'm struggling with this all nite long..

can u tell me how to randomize rss source?..

for example i have this txt file named "list.txt" in the server which look like this (contains all rss sources url):

site1.com/rss
site2.com/rss
site3.com/rss
site4.com/rss
site5.com/rss
site6.com/rss
site7.com/rss
site8.com/rss
site9.com/rss
site10.com/rss

so, how if i want to only show 5 random rss source each time?

and the second site:

is there any way i can categorize it based on my list.txt, for example here's the list.txt:

Golf
site1.com/rss
site2.com/rss
site3.com/rss
site4.com/rss
Music
site5.com/rss
site6.com/rss
site7.com/rss
Movie
site8.com/rss
site9.com/rss
site10.com/rss

is there any way to show the rss based on the category?

for example user go to these url: www.domain.com/?cat=music the user will be shown content source from that category.

Thanks

Submitted by support on Mon, 2009-07-27 08:17

Hi Demarco,

Sorry for the delay in your post appearing, posts and comments are not displayed until I publish them (in order to protect customer URLs, and other sensitive information) but they will normally appear quite quickly!

For random feeds, try this...

<?php
  
// read feed URLs into $feeds array from list.txt
  
$feeds = array();
  
$fp fopen("list.txt","r");
  while(!
feof($fp))
  {
    
$line trim(fgets($fp,1024));
    if (
$line$feeds[] = $line;
  }
  
// randomise the array
  
shuffle($feeds);
  
// now parse as many as required i.e. 5
  
for($i=0;$i<5;$i++)
  {
    
MagicParser_parse($feeds[$i],"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  }
?>

For the category feeds, I am assuming that the URLs will always begin with http and any line that doesn't begin with that is a category name, so this can be used to create a 2-dimensional array where the category name is the primary key; for example:

<?php
  
// read feed URLs into $feeds array from list.txt
  
$feeds = array();
  
$fp fopen("list.txt","r");
  while(!
feof($fp))
  {
    
$line trim(fgets($fp,1024));
    if (
$line)
    {
      if (
substr($line,0,4)=="http")
      {
       
$feeds[$category][] = $line;
      }
      else
      {
        
$category $line;
      }
    }
  }
  
// now parse feeds from $_GET["cat"]
  
foreach($feeds[$_GET["cat"]] as $feed)
  {
    
MagicParser_parse($feed,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  }
?>

Hope this helps,
Cheers,
David.

Submitted by Demarco on Tue, 2009-07-28 14:25

Thanks Dave :)

Submitted by Demarco on Thu, 2009-07-30 09:50

Hi Dave..

instead of using that random feed i'm make the script to show the link to the random keyword, so it will show feed based on that keyword

<?php
  
// read feed URLs into $feeds array from list.txt
  
$feeds = array();
  
$fp fopen("list.txt","r");
  while(!
feof($fp))
  {
    
$line trim(fgets($fp,1024));
    if (
$line$feeds[] = $line;
  }
  
// randomise the array
  
shuffle($feeds);
  
// now parse as many as required i.e. 5
  
for($i=0;$i<5;$i++)
  {
$navPlus str_replace(' ''+'$feeds[$i]);
$navUc ucwords($feeds[$i]);
print 
"<a href=?keyword=".$navPlus.">".$navUc."</a><br>";
  }
?>

the one i want to ask is what should i add at the top of the script so if user type the random KW that not in the list.txt, it will show 404?

so if user go to example.com?keyword=invalid (invalid is not in the list.txt) so it show the 404 error page.. is it need to load the list.txt 2 times? for checking the KW & randomize it?.. the one i'm scare how if my list.txt is quite huge (around thousand kw)and it load list.txt 2times? it will use much system resources?

Thanks

Submitted by support on Thu, 2009-07-30 10:06

Hi,

The code to do that would need to go in the section where the keyword is used rather then the code you posted, but is basically - after you have read the keywords into $someArray;

if (!$someArray[$_GET["keyword"])
{
  header("HTTP/1.0 404 Not Found");
  exit();
}

...however, since this uses the header() statement, it must come BEFORE your script has begun to generate any output; otherwise the header cannot be set.

Regarding the filesize, 1000 may be OK, but it's a question of how long the shuffle takes. If it's not practicle, it would be better to look at a database solution and then you can use database features like ORDER BY RAND() LIMIT 5 to select 5 random keywords rather than have to read a text file every time.

Cheers,
David.

Submitted by Demarco on Thu, 2009-07-30 10:26

you mean something like this in the top of the script?

<?php
  $feeds 
= array();
  
$fp fopen("list.txt","r");
  while(!
feof($fp))
  {
    
$line trim(fgets($fp,1024));
    if (
$line$feeds[] = $line;
  }
  if (!
$feeds[$_GET["keyword"])
{
  
header("HTTP/1.0 404 Not Found");
  exit();
}
?>

and at the randomize section :

<?php
  shuffle
($feeds);
  
// now parse as many as required i.e. 5
  
for($i=0;$i<2;$i++)
  {
$navPlus str_replace(' ''+'$feeds[$i]);
$navUc ucwords($feeds[$i]);
print 
"<a href=?ss=".$navPlus.">".$navUc."</a><br>";
  }
?>

Submitted by support on Thu, 2009-07-30 10:29

Hi Demarco,

If you're using ss as the parameter, then it must be $_GET["ss"] instead of $_GET["keyword"], i.e.

  if (!$feeds[$_GET["ss"])

Cheers,
David.

Submitted by Demarco on Thu, 2009-07-30 10:33

Hi DAve,

its not working..

i got :
Parse error: syntax error, unexpected ')', expecting ']' in /home/xxx/public_html/index.php on line 9

what does it mean?

Thanks

Submitted by support on Thu, 2009-07-30 10:35

Hi,

Sorry, there is a square bracket missing, it should be:

  if (!$feeds[$_GET["ss"]])

Cheers,
David.

Submitted by Demarco on Thu, 2009-07-30 10:45

ive test it, it only show blank page?..

and i change this part:

header("HTTP/1.0 404 Not Found");
exit();

to:

print "Not in list";

but it shows not in list for evrey keyword i typed, even its in the list.. is there something wrong?

Thanks

Submitted by support on Thu, 2009-07-30 10:53

Hi Demarco,

Could you perhaps post your entire script and I'll take a look? Or if it contains URLs, you could email it to me if you like and I'll have a look for you...

Cheers,
David.