With PHP XScreensaver displays news
A cobbled together script for displaying google news in XScreensaver.
1 //Needed for magpie:
2 define('MAGPIE_DIR', './magpierss/');
3 require_once(MAGPIE_DIR.'rss_fetch.inc');
4 //From the grabber script:
5 $config['start_tag'] = "<p>"; // where you want to start grabbing
6 $config['end_tag'] = "</p>"; // where you want to stop grabbing
7 $config['show_tags'] = 0; // do you want the tags to be shown when you show the html? 1 = yes, 0 = no
8 //from the magpie examples:
9
10 //Modified to use a single rss URL
11 $rss = fetch_rss('http://news.google.com/nwshp?hl=en&ned=us&output=rss');
12 //Modified to create an explicit array and shuffle it to get a random item.
13 $rss_array = $rss->items;
14 shuffle( $rss_array );
15 foreach ( $rss_array as $key => $item )
16 {
17 if ($key > 1) {
18 break;
19 }
20 $config['url'] = $rss_array[1]['link'];
21 //From grabber script:
22 $grab = new grabber;
23 $grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] );
24 if (!$grab->error) {
25 foreach( $grab->html[0] as $html )
26 {
27 echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br />";
28 }
29 }
30 }
31
32 //From grabber script:
33 class grabber
34 {
35 var $error = '';
36 var $html = '';
37
38 function grabhtml( $url, $start, $end )
39 {
40 $file = file_get_contents( $url );
41
42 if( $file )
43 {
44 if( preg_match_all( "#$start(.*?)$end#s", $file, $match ) )
45 {
46 $this->html = $match;
47 }
48 else
49 {
50 $this->error = "Tags cannot be found.";
51 }
52 }
53 else
54 {
55 $this->error = "Site cannot be found!";
56 }
57 }
58
59 function strip( $html, $show, $start, $end )
60 {
61 if( !$show )
62 {
63 $html = str_replace( $start, "", $html );
64 $html = str_replace( $end, "", $html );
65
66 //Added the PHP function strip_tags and strip any numeric html character references:
67 $pattern = '/^\&\#(\d+);$/u';
68 return preg_replace( $pattern, '', strip_tags( $html ) );
69 }
70 else
71 {
72 return $html;
73 }
74 }
75 }