Code for Fetching LJ Community Contents through RSS

    Date: 05/09/05 (PHP Community)    Keywords: rss, xml, web

    [Error: Irreparable invalid markup ('') 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...



    // +----------------------------------------------------------------------+
    // | 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 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("

    ", // 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,"");
    if($pos1<$pos2)
    {
    $str1=strstr($str1," $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("Current Mood: $mood&nbsp;$moodpic
    ");
    if(!empty($music))
    print("Current Music: $music
    ");

    printf("

    %s

    ",$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("$commentcount Comments",trim($link));
    else
    printf("$commentcount Comment",trim($link));
    }
    else
    printf("Permanent Link",trim($link));
    $comment=trim($comment);
    if(!empty($comment))
    print(",&nbsp;&nbsp;Leave Comments,&nbsp;&nbsp;");


    print("
    \"Edit
    ");

    print ("&nbsp;&nbsp;
    \"Add src=\"http://stat.livejournal.com/img/btn_memories.gif\" />
    ");


    printf ("
    \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);

    ?>



    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 :)

    Source: http://www.livejournal.com/community/php/296862.html

    $str1=strstr($val," $pos1=strpos($str1,'>');
    print "
    ";
    for($index=0;$index<=$pos1;$index++)
    print($str1{$index});
    echo "
    &nbsp;&nbsp;&nbsp;$datearray[1]&nbsp;$datearray[2]&nbsp;$datearray[3]&nbsp;$datearray[4]&nbsp;$datearray[5]";
    echo "
    ";
    $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("

    [info]
    $username

    \n");
    $pos1=false;
    $pos2=false;
    $str1=false;
    $str1=strstr($content,"Current mood:");
    $pos1=strpos($str1," $pos2=strpos($str1,"
    « PHP Syntax Exam || Manipulating time »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home