Create an RSS feed of comments from myspace
September 8th, 2007 by Aaron
Lately, I’ve been trying to find ways to reduce the amount of time I spend on stupid sites like myspace (nevermind the fact that the time it took to reduce this amount took me enough time to visit myspace 1x a day for another month - heh). At any rate, I’ve been using Google Reader alot more (I’m up to 180 or so feeds) and I thought: Why don’t I make an RSS feed o my comments - then I don’t have to go back to the site when someone sends me a comment. (Mind you, myspace does send you an e-mail when you receive a comment, but doesn’t include the content. JEMDiary does, however ;)) I searched the internet and found a few sites that are doing that for a service, and one guy who was giving away a regular expression. So, I took his idea and wrote my own php script for cron. Check it out here:
You can take this script and modify the URL variable and run it on a PHP5 compatible host. You might also have to change the URL for your xml file. Then, just schedule the script to run every hour and feed the xml file URL to your reader. Do keep in mind that this will increase your profile views 1 for every hour (I myself don’t really care…)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | <?php /** * get a list of your comments on myspace * and then make an rss feed out of them * * @author Aaron Saray */ /*********************************************************************************** * Editable content here: **********************************************************************************/ /** * set url: most likely * www.myspace.com/username */ $URL = 'http://www.myspace.com/yourusername'; /*********************************************************************************** * END Editable content here: **********************************************************************************/ /** * class to parse out the comments and create to rss */ class myspaceCommentToRSS { /** * the URL of the page * * @var string */ protected $_URL = ''; /** * The commentee's username * * @var string */ protected $_username = ''; /** * the simplexml object * @var object */ protected $_rss = null; /** * the myspace details * @var array */ protected $_mySpaceData = array(); /** * output file */ protected $_outputFile = ''; /** * constructor takes in the URL * * @param string $url */ public function __construct($url) { /** set url **/ $this->_URL = $url; /** create output filename **/ $this->_outputFile = $_SERVER['DOCUMENT_ROOT'] . '/myspace/newestcomments.xml'; } /** * output the xml */ public function outputXML() { /** make sure we can get the data even **/ $this->_parseMySpaceData(); /** build the rss element **/ $this->_buildXML(); /** deposit it **/ $this->_writeXML(); } /** * parses out all of our data from my space that we'll need */ protected function _parseMySpaceData() { /** get the host/url/nick out of here, this is done very programmatically **/ $parts = explode('/', substr($this->_URL, strpos($this->_URL, '/')+2)); $this->_username = $parts[count($parts)-1]; /** open our connection **/ $fp = fsockopen($parts[0], 80, $errno, $errstr, 30); if (!$fp) { die(print "Unable to open host"); } else { /** make our request **/ $out = "GET /{$parts[1]} HTTP/1.1rn"; $out .= "Host: {$parts[0]}rn"; $out .= "Connection: Closernrn"; fwrite($fp, $out); /** gather our respsonses **/ $f = ''; while (!feof($fp)) { $f .= fgets($fp, 128); } /** close and be done **/ fclose($fp); } /** * $f now contains the response, so match out all the data * * Thanks to makedatamakesense.com for this regular expression - saved me * some time */ preg_match_all($REPLACEMESEEBOTTOM, $f, $matches, PREG_SET_ORDER); /** now sort out the data nicely **/ foreach ($matches as $match) { /** * notes: * 0 = the full match * 1 = posters profile link * 2 = posters current nick * 3 = posters current profile pic * 4 = posted date * 5 = post content * * Double htmlentities because I don't want to include a huge entity file * Finally, a hardset replace so i can get normal line breaks */ $this->_mySpaceData[] = array ( 'title'=>htmlentities(htmlentities(trim($match[2]) . ' - ' . trim($match[4]), ENT_NOQUOTES), ENT_NOQUOTES), 'description'=>str_replace('<br />', '', htmlentities(htmlentities(trim($match[5]), ENT_NOQUOTES), ENT_NOQUOTES)) ); } } /** * builds our xml element */ protected function _buildXML() { /** create new rss element **/ $this->_rss = new simpleXMLElement('<rss version="2.0"></rss>'); /** add our child and then add the standard identifiers to it **/ $channel = $this->_rss->addChild('channel'); $channel->addChild('title', "Comments for " . $this->_username); $channel->addChild('link', $this->_URL); $channel->addChild('description', 'RSS feed for MySpace comments'); $channel->addChild('language', 'en-us'); $channel->addChild('pubDate', date('r')); $channel->addChild('lastBuildDate', date('r')); /** loop through each myspace data array item and add an item for each **/ foreach ($this->_mySpaceData as $data) { $item = $channel->addChild('item'); $item->addChild('title', $data['title']); $item->addChild('description', $data['description']); } } /** * writes our xml document */ protected function _writeXML() { $fp = fopen($this->_outputFile, 'w'); fputs($fp, $this->_rss->asXML()); fclose($fp); } } /** new instance **/ $rss = new myspaceCommentToRss($URL); /** output the xml for the rss feed **/ $rss->outputXML(); ?> |
For whatever reason, WP keeps eating things, so see these two notes:
*replace the regular expression variable with this:
1 | #<tr>[^<]*?<td[^>]*?>[^<]*?<a href="([^"]*?)">([^<]*?)</a>[^<]*?<br>[^<]*?<br>[^<]*?<a[^>]*?>[^<]*<img src="([^"]*?)"[^>]*?>[^<]*?</a>.*?</td>[^<]*?<td[^>]*?>[^<]*<span[^>]*?>([^<]*?)</span>[^<]*<br>[^<]*<br>(.*?)</td>[^<]*?</tr>#is |
This entry was posted on Saturday, September 8th, 2007 at 5:18 pm and is filed under Misc Web Design • PHP • scripting. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.















September 14th, 2008 at 2:03 pm
I tried this out and get an error when I run the script, (even after doing the last step/fix you had posted at the bottom. I’ve changed the URL to my MySpace profile, everything was done correctly. At first the error was:
Warning: preg_match_all() [function.preg-match-all]: Empty regular expression in /home/redw1910/public_html/myspace/MS_comments.php on line 106
Warning: Invalid argument supplied for foreach() in /home/redw1910/public_html/myspace/MS_comments.php on line 109
Now it’s saying:
Parse error: syntax error, unexpected ‘;’, expecting T_VARIABLE or ‘$’ in /home/redw1910/public_html/myspace/MS_comments.php on line 106
What have I done incorrectly?
September 14th, 2008 at 4:24 pm
@Ojibraids: It sounds as if you have a typo somewhere around line 106 or 105. Make sure that you have all your variables with $ signs, that you don’t have any looking like constants, and that you match up every quote.
September 14th, 2008 at 4:40 pm
this is how the entire line 106 looks. I copied it straight from the code snippet you posted:
preg_match_all($#[^<]*?]*?>[^<]*?([^<]*?)[^<]*?[^<]*?[^<]*?]*?>[^<]*]*?>[^<]*?.*?[^<]*?]*?>[^<]*]*?>([^<]*?)[^<]*[^<]*(.*?)[^<]*?#is, $f, $matches, PREG_SET_ORDER);