KickRocksBish Posted November 2, 2010 Share Posted November 2, 2010 script I made to get the info for last.fm users <?php $lastfm[] = file_get_contents("http://www.last.fm/user/joffeman"); $lastfm[] = file_get_contents("http://www.last.fm/user/chaos2092"); for($a=0; $a<count($lastfm); $a++){ echo $lastfm[$a]; $filename = "lastfm.txt"; $b = fopen($filename, 'w'); fwrite($b, $lastfm[0]); fwrite($b, $lastfm[1]); fclose($b);} sleep(10000); ?> It spits out everything on the page, I need to parse out the username and plays and put it in a file in this format Username - Plays each one on a new line the info is in the contents of the page I just need to know how to parse it out <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head profile="http://purl.org/uF/2008/03/"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <title>joffeman’s Music Profile – Users at Last.fm</title> <link rel="search" type="application/opensearchdescription+xml" href="http://cdn.last.fm/opensearch.xml" title="Last.fm" /> <meta name="description" content="Listen to joffeman’s personal radio station (481,212 tracks played). Listen to Chaos2092’s personal radio station (38,123 tracks played) Now how could I parse that out and if I put another account in it will put the info in a file to I would need to parse out both and put it in format like joffeman - 481,212 tracks played Chaos2092 - 38,123 tracks played any help would be appreciated Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 2, 2010 Author Share Posted November 2, 2010 anyone? Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 2, 2010 Share Posted November 2, 2010 looks like the number of plays is set in tags like this: <span class="count"><span class="flip">1</span></span> So u could use regex to get the value $play_span = preg_match("/<span class=\/"count\/">(.*?)<\/span>/", $html, $matches); $play_span = $matches[1]; // NOW STRIP THE SPAN TAGS (i'm sure there is a better way but im too tired to think $erase_me = array("<span class=/"flip/">", "</span>"); $plays = str_replace($erase_me, "", $play_span); that should work.. Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 2, 2010 Author Share Posted November 2, 2010 Didnt work for me, I just include that somewhere in the script now it just doesnt work Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 2, 2010 Author Share Posted November 2, 2010 Like I wanna take this file http://dl.dropbox.com/u/2967480/lastfm.txt and I just wanna parse out the part on the 9th line " <meta name="description" content="Listen to Chaos2092’s personal radio station (38,123 tracks played). Chaos2092’s top artists: Converge, Dream Theater, Porcupine Tree. Favourite tags are alcoholics anonymous suite, awesome riff, doom metal. Get your own music profile at Last.fm, the world’s largest social music platform."/>" I wanna parse out just the Users name and the tracks played.. and put it in format Chaos2092's - 38,123 tracks played Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 2, 2010 Author Share Posted November 2, 2010 Ok should I use explode to remove all the data beside the username and tracks played? Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 This in your "for" loop should do the trick. I'm not sure if you ONLY wanted to save the username and plays in the text file?? $lastfm_info = preg_match("/<meta name=\"description\" content=\"Listen to (.*?)\)./", $lastfm[$a], $matches); $lastfm_info = $matches[1]; $lastfm_info = str_replace(" personal radio station (", " - ", $play_span); Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 3, 2010 Author Share Posted November 3, 2010 now this is what I have <?php $lastfm[] = file_get_contents("http://www.last.fm/user/joffeman"); $lastfm[] = file_get_contents("http://www.last.fm/user/chaos2092"); for($a=0; $a<count($lastfm); $a++){ echo $lastfm[$a]; $lastfm_info = preg_match("/<meta name=\"description\" content=\"Listen to (.*?)\)./", $lastfm[$a], $matches); $lastfm_info = $matches[0]; $lastfm_info = str_replace("’s personal radio station (", " - ", $play_span); $filename = "lastfm.txt"; $b = fopen($filename, 'w'); fwrite($b, $matches[$a]); fwrite($b, $matches[$a]); fclose($b);} sleep(10000); ?> It didnt return joffemans plays and it returned this in the file "Chaos2092’s personal radio station (38,123 tracks playedChaos2092’s personal radio station (38,123 tracks played" I want it to be like joffemans - 481,212 Chaos2092 - 38,123 Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 it wasn't taking out that extra stuff in it. sorry i forgot to correct some stuff in the code.. and as far is it repeating the info, its because you are saving the $matches[$a] .. replace all your for loop with this.. i'm sorry i keep giving u bad code lol. but this should work. i looked it over. foreach($lastfm as $value){ echo $value; $lastfm_info = preg_match("/<meta name=\"description\" content=\"Listen to (.*?)\)./", $value, $matches); $lastfm_info = str_replace("’s personal radio station (", " - ", $matches[0]); $filename = "lastfm.txt"; $b = fopen($filename, 'w'); fwrite($b, $lastfm_info]); fclose($b); } Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 3, 2010 Author Share Posted November 3, 2010 this goes in the for loop? Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 replaces the for loop Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 3, 2010 Author Share Posted November 3, 2010 ok sorry I keep replying with it not workin I appreciate you helping me tho now I have this <?php $lastfm[] = file_get_contents("http://www.last.fm/user/joffeman"); $lastfm[] = file_get_contents("http://www.last.fm/user/chaos2092"); foreach($lastfm as $value){ echo $value; $lastfm_info = preg_match("/<meta name=\"description\" content=\"Listen to (.*?)\)./", $value, $matches); $lastfm_info = str_replace("’s personal radio station (", " - ", $matches[0]); $filename = "lastfm.txt"; $b = fopen($filename, 'w'); fwrite($b, $lastfm_info); fclose($b); } sleep(10000); ?> and all iot returned in the file was "<meta name="description" content="Listen to Chaos2092’s personal radio station (38,123 tracks played)." skipped over joffeman again Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 ok I fought with this thing for a while. the encoding is messed up it appears and i can't figure out how to work with that. But i did find a work around.. SO Here is the new code: $lastfm[] = file_get_contents("http://www.last.fm/user/joffeman"); $lastfm[] = file_get_contents("http://www.last.fm/user/chaos2092"); foreach($lastfm as $value){ preg_match("/<meta name=\"description\" content=\"Listen to (.*?)\)./", $value, $matches); $lastfm_info = explode(" personal radio station (", $matches[1]); $lastfm_info = substr($lastfm_info[0], 0, -4) . " - " . $lastfm_info[1]; echo $lastfm_info; $filename = "lastfm.txt"; $b = fopen($filename, 'a'); // NEEDS TO BE 'a' FOR APPEND fwrite($b, $lastfm_info); fclose($b); } sleep(10000); Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 3, 2010 Author Share Posted November 3, 2010 awesome I really appreciate that!!! One last thing tho, can you make it write the accts on a new line instead of "joffeman - 481,212 tracks playedChaos2092 - 38,123 tracks played" in the text file. Thats all I need now. Thanks!!!! Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 change this: $lastfm_info = substr($lastfm_info[0], 0, -4) . " - " . $lastfm_info[1]; to this: $lastfm_info = substr($lastfm_info[0], 0, -4) . " - " . $lastfm_info[1] . "\r\n"; Quote Link to comment Share on other sites More sharing options...
KickRocksBish Posted November 3, 2010 Author Share Posted November 3, 2010 shit I didntt hink of that, youre awesome man,, Thanks for the help.. Im really just learning php, can you refer me to anywhere that would help me out in learning php? Thanks Quote Link to comment Share on other sites More sharing options...
Jocka Posted November 3, 2010 Share Posted November 3, 2010 I suggest just looking at random tutorials and using Google a lot. I've learned most of what I know thru those methods lol. It's hard to learn everything with PHP if you don't use everything in PHP. There is a lot I don't know but the basics become easier to remember as u use it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.