Jump to content

how to parse out plays from a last.fm page?


KickRocksBish

Recommended Posts

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

Link to comment
Share on other sites

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..

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

ok sorry I keep replying with it not workin :P 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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.