Topic: newsbot - post RSS news
hey there...
I wanted to have rss news in my forums, one per topic so they can be commented and discussed. I think this is cool cause it allows you to keep a newsarchive on your forums.
ok... originaly i wrote the code to use myself with some settings mixed up inside the code, bad programing practive i know. Also the variable names, wich are very descriptive were writen in portuguese.
i went through the script and adapted it, the original version is working like a charm on my forums. I didnt test the modified version though so its possible that it has some kind of bug, i think not
If you need it just try it out, if you get some errors report them and i fix the problem.
You have to create a new table on your database and define some setings. The feed urls should be inserted in that table. The sql to create the table is in the code as a comment
OK, so this is how i put this working:
i created a folder in punbbroot and put this file there, you can name it whatever you feel like. I also put magpie and a modified copy of search_idx.php.
You can dload magpie here.
since this only works with mysql and i didnt had access to the database class, my modified search_idx.php has all the $db->function() replaced by the equivalent nativve mysql function. Like, $db->query() is replaced by mysql_query().
Ok... i know this is bad programing practice, i just wanted to have it working.
Anyway... any questions or trouble reply to this message.
<?php
//configuration=================================================================
//database===========================================
$db_host = '';
$db_name = '';
$db_username = '';
$db_password = '';
//other===============================================
$newstbot_name=''; //name of the user that make the news post
$newsbot_id=''; //id of that user
$newsforum_id=''; //the id of the forum where you wan the news to go
$follow_link_text='Read more at'; //change this if you want to display some other text
//==============================================================================
/*
sql for creating the extra table you need
create table feedsources(
id int,
feed_url varchar(255),
updated int DEFAULT 0
);
*/
require('magpierss/rss_fetch.inc');
define(constantebot,1);
require "search_idx.php";
mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name);
$sql_get_feeds="select id, feed_url, updated from feedsources";
$resultado=mysql_query($sql_get_feeds);
$ii=0;
while ( $row=mysql_fetch_array($resultado) ){
$feed_array[$ii]["feed"]=$row["feed_url"];
$feed_array[$ii]["id"]=$row["id"];
$feed_array[$ii]["updated"]=$row["updated"];
$ii++;
}
$num_posts=0;
foreach ($feed_array as $feed_array_element){
$feed_last_update = $feed_array_element["updated"];
$url = $feed_array_element["feed"];
$feed_id = $feed_array_element["id"];
$rss = fetch_rss($url);
$new_last_update = $feed_last_update;
foreach ( $rss->items as $item ) {
$timenow = time();
$date_string = $item["pubdate"];
$date_posted = strtotime($date_string);
if($date_posted > $new_last_update){
$new_last_update=$date_posted;
}
if($date_posted <= $feed_last_update){
break;
}
$num_posts++;
$topic_subject = $item['title'];
$poster = $newstbot_name;
$topic_forum_id = $newsforum_id;
$poster_id = $newsbot_id;;
$post_message = mysql_escape_string($item['description']);
$post_message = $post_message.
"<br /><br /><a href=\"".$item["link"]."\">$follow_link_text</a>";
$sql="
insert into topics
(subject,
poster,
posted,
forum_id,
last_post_id,
last_poster)
values
('$topic_subject',
'$poster',
$timenow,
$topic_forum_id,
$timenow,
'$poster')";
mysql_query($sql);
$topic_id = mysql_insert_id();
$sql="
insert into posts
(poster,
poster_id,
message,
hide_smilies,
posted,
topic_id)
values
('$poster',
$poster_id,'"
."$post_message"
."',
1,
$timenow,
$topic_id)";
mysql_query($sql);
$last_post_id = mysql_insert_id();
$sql="update topics set last_post_id=$last_post_id, last_post=$date_posted where id=$topic_id";
echo "$sql <br>";
mysql_query($sql);
$sql="
update forums
set
num_posts=num_posts+1,
num_topics=num_topics+1,
last_post=$timenow,
last_post_id=$last_post_id,
last_poster='$poster'
where
id=$topic_forum_id";
mysql_query($sql);
$post_message=strip_tags($post_message);
update_search_index('post', $last_post_id, $post_message);
}
$sql2="update feedsources set updated=$new_last_update where id=$feed_id";
mysql_query($sql2);
echo "$sql2 <br>";
}
$sql3="update users set num_posts=num_posts+$num_posts where id=$newsbot_id";
mysql_query($sql3);
?>