Sutanu... (sutanu) wrote in php,
Sutanu...
sutanu
php

  • Mood:
  • Music:

Code for Fetching LJ Community Contents through RSS

I wrote a code for Fetching LJ content of a community. Am using RSS here. I wrote one XML Parser, It displays the entries in proper format (User can modify the code for his/her choicable format). It also displays the user name and userpic of the person who posted the entry in the community, also it counts the number of comments for that post and displays the comment count.
Also it displays the current music, current mood and the mood icon used for that entry.

This code will be helpful when someone is designing a website for a community and wants to fetch the LJ contents in the site...

I've not yet tested the code for all types of design. If anyone can test the code and find the errors and fix them, it'll be helpful for everyone...



from SPHINX |
// | Website: http://www.wearesphinx.net |
// +----------------------------------------------------------------------+
// | File: fetch.php |
// | Description: Contains php code and XML parser to fetch |
// | Livejournal Entries from Community LJ Page |
// | Last Update: 22/04/2005 |
// +----------------------------------------------------------------------+

// Basic Requirements
$community="wearesphinx"; // Mention your community name here
$backend = "http://www.livejournal.com/community/".$community."/data/rss/";

// End

// variables needed later
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";

$mood = "";
$music = "";
$comment = "";
$datetime = "";
// end

function startElement($parser, $tagName, $attrs) {

// The function used when an element is encountered

global $insideitem, $tag;

if ($insideitem) {

$tag = $tagName;

} elseif ($tagName == "ITEM") {

$insideitem = true;
}

}

function characterData($parser, $data) {

// The function used to parse all other data than tags

global $insideitem, $tag, $title, $description, $link, $mood, $music, $comment, $datetime;

if ($insideitem) {

switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
case "COMMENTS":
$comment .= $data;
break;
case "LJ:MUSIC":
$music .= $data;
break;
case "LJ:MOOD":
$mood .= $data;
break;
case "PUBDATE":
$datetime .= $data;
break;
}

}

}

function endElement($parser, $tagName) {

// This function is used when an end-tag is encountered.

global $insideitem, $tag, $title, $description, $link, $mood, $music, $comment, $datetime, $community;

if ($tagName == "ITEM") {

/*Code for finding the entry ID*/
$linkid=preg_split('[/]',trim($link));
$entryfile=$linkid[count($linkid)-1];
list($entryname,$dummy)=split('[\.]',$entryfile);

// Find the Date and time of Post
$datearray=preg_split('[ ]',$datetime);
if(empty($title))
{
$title="Nothing to say Baby";
}
printf("

%s

", // make our title into an actual link
htmlspecialchars(trim($title))); // remove html characters from the title

//--------------------------------------------------------------------
// Find out the user who made the post

print "
";
$ljfile="http://www.livejournal.com/community/".$community."/".$entryname.".html";

$content=file_get_contents($ljfile);
chop($content);
$val=strstr($content,"
[Error: Irreparable invalid markup ('<img [...] $pos1>') in entry. Owner must fix manually. Raw contents below.]

I wrote a code for Fetching LJ content of a community. Am using RSS here. I wrote one XML Parser, It displays the entries in proper format (User can modify the code for his/her choicable format). It also displays the user name and userpic of the person who posted the entry in the community, also it counts the number of comments for that post and displays the comment count.
Also it displays the current music, current mood and the mood icon used for that entry.

This code will be helpful when someone is designing a website for a community and wants to fetch the LJ contents in the site...

I've not yet tested the code for all types of design. If anyone can test the code and find the errors and fix them, it'll be helpful for everyone...


<lj-cut>
<?php
// +----------------------------------------------------------------------+
// | CommunityLJcatch version 1.1 |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
// | 02111-1307, USA. |
// | |
// +----------------------------------------------------------------------+
// | Author: Sutanu Mandal <sutanu@gmail.com> from SPHINX |
// | Website: http://www.wearesphinx.net |
// +----------------------------------------------------------------------+
// | File: fetch.php |
// | Description: Contains php code and XML parser to fetch |
// | Livejournal Entries from Community LJ Page |
// | Last Update: 22/04/2005 |
// +----------------------------------------------------------------------+

// Basic Requirements
$community="wearesphinx"; // Mention your community name here
$backend = "http://www.livejournal.com/community/".$community."/data/rss/";

// End

// variables needed later
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";

$mood = "";
$music = "";
$comment = "";
$datetime = "";
// end

function startElement($parser, $tagName, $attrs) {

// The function used when an element is encountered

global $insideitem, $tag;

if ($insideitem) {

$tag = $tagName;

} elseif ($tagName == "ITEM") {

$insideitem = true;
}

}

function characterData($parser, $data) {

// The function used to parse all other data than tags

global $insideitem, $tag, $title, $description, $link, $mood, $music, $comment, $datetime;

if ($insideitem) {

switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
case "COMMENTS":
$comment .= $data;
break;
case "LJ:MUSIC":
$music .= $data;
break;
case "LJ:MOOD":
$mood .= $data;
break;
case "PUBDATE":
$datetime .= $data;
break;
}

}

}

function endElement($parser, $tagName) {

// This function is used when an end-tag is encountered.

global $insideitem, $tag, $title, $description, $link, $mood, $music, $comment, $datetime, $community;

if ($tagName == "ITEM") {

/*Code for finding the entry ID*/
$linkid=preg_split('[/]',trim($link));
$entryfile=$linkid[count($linkid)-1];
list($entryname,$dummy)=split('[\.]',$entryfile);

// Find the Date and time of Post
$datearray=preg_split('[ ]',$datetime);
if(empty($title))
{
$title="Nothing to say Baby";
}
printf("<p align=\"justify\"><h3 class='page-header'><b>%s</b></h3></p>", // make our title into an actual link
htmlspecialchars(trim($title))); // remove html characters from the title

//--------------------------------------------------------------------
// Find out the user who made the post

print "<div class=\"LJContents\">";
$ljfile="http://www.livejournal.com/community/".$community."/".$entryname.".html";

$content=file_get_contents($ljfile);
chop($content);
$val=strstr($content,"<table><tr valign='middle'><td><img src=");
$str1=strstr($val,"<img");
$pos1=strpos($str1,'>');
print "<table border=\"0\"><tr><td>";
for($index=0;$index<=$pos1;$index++)
print($str1{$index});
echo "</td><td><b>&nbsp;&nbsp;&nbsp;$datearray[1]&nbsp;$datearray[2]&nbsp;$datearray[3]&nbsp;$datearray[4]&nbsp;$datearray[5]</b>";
echo "<br>";
$pos1=false;
$str1=false;
$str1=strstr($val,"http://www.livejournal.com/userinfo.bml?user=");
$pos1=strpos($str1,'>');
$pos2=0;
$username=trim(substr($str1,45,$pos1-46));
print("<span class='ljuser' style='white-space: nowrap;'>
<a href='http://www.livejournal.com/userinfo.bml?user=$username'>
<img src='http://stat.livejournal.com/img/userinfo.gif' alt='[info]' width='17' height='17' style='vertical-align: bottom; border: 0;' />
</a><a href='http://www.livejournal.com/users/$username/'><b>$username</b></a></span><br></td></tr></table>\n");
$pos1=false;
$pos2=false;
$str1=false;
$str1=strstr($content,"Current mood:");
$pos1=strpos($str1,"<img src=");
$pos2=strpos($str1,"</tr>");
if($pos1<$pos2)
{
$str1=strstr($str1,"<img src=");
$pos1=strpos($str1,">");
$moodpic=trim(substr($str1,0,$pos1));
}
else
$moodpic="";



// Find out number of comments in that post
$commentcount=count(explode("talk-comment",$content))-1;

#fclose($LJ);
$val=false;


//-------------------------------------------------------------------------
// Continue with the formatting of the entry

if(!empty($mood))
print("<b>Current Mood:</b> $mood&nbsp;$moodpic<br>");
if(!empty($music))
print("<b>Current Music:</b> $music<br>");

printf("<p align=\"juatify\">%s</p>",$description); // Print out the live journal entry # Original


/* Code for display number of comments, Link to comment page, Edit Entry, Add Memory*/
if($commentcount>0)
{
if($commentcount>1)
printf("<a href=\"%s?nc=$commentcount\">$commentcount Comments</a>",trim($link));
else
printf("<a href=\"%s?nc=$commentcount\">$commentcount Comment</a>",trim($link));
}
else
printf("<a href=\"%s\">Permanent Link</a>",trim($link));
$comment=trim($comment);
if(!empty($comment))
print(",&nbsp;&nbsp;<a href=\"$comment?mode=reply\">Leave Comments</a>,&nbsp;&nbsp;");


print("<a href=\"http://www.livejournal.com/editjournal.bml?journal=wearesphinx&amp;itemid=$entryname\">
<img border='0' width=\"22\" height=\"20\" alt=\"Edit Entry\" title=\"Edit Entry\" src=\"http://stat.livejournal.com/img/btn_edit.gif\" /></a>");

print ("&nbsp;&nbsp;<a href=\"http://www.livejournal.com/tools/memadd.bml?journal=wearesphinx&amp;itemid=$entryname\">
<img border='0' width=\"22\" height=\"20\" alt=\"Add to Memories\" title=\"Add to Memories\"
src=\"http://stat.livejournal.com/img/btn_memories.gif\" /></a></div>");


printf ("<HR>\n"); // Make a line inbetween entries.
$title = $description = $link = $insideitem = $mood = $music = $comment = false;
}

}

// Now to the parsing itself. Starts by creating it:

$xml_parser = xml_parser_create();

// Then setup the handlers:

xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// Open the actual datafile:

$fp = fopen($backend, "r");

// Run through it line by line and parse:

while ($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

// Close the datafile

fclose($fp);

// Free any memmory used

xml_parser_free($xml_parser);

?>

</lj-cut>

But in this code the problem is that it doesn't recognise LJ-cut and doesn't do caching.

If anyone modifies the code and make it efficient then it'll be helpful :)
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 3 comments