librescrape.pl 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/perl
  2. # librescrape.pl - proof of concept scraper for libre.fm.
  3. #
  4. # Note that we're not really scraping the pages at all, but querying
  5. # them like a database! Cool, huh?
  6. use LWP::Simple;
  7. use RDF::Redland;
  8. use RDF::RDFa::Parser::Redland '0.02';
  9. my $uri = shift @ARGV
  10. or die "Usage: librescrape.pl 'http://alpha.libre.fm/user/somebody'\n";
  11. if ($uri =~ /user-profile\.php/)
  12. {
  13. $uri =~ s/user-profile.php/user-recent-tracks.php/;
  14. $uri .= '&count=1000';
  15. }
  16. else
  17. {
  18. $uri .= '/recent-tracks?count=1000';
  19. }
  20. my $parser = RDF::RDFa::Parser::Redland->new(get($uri), $uri);
  21. $parser->consume;
  22. my $model = $parser->redland;
  23. my $query = <<SPARQL;
  24. PREFIX gob: <http://purl.org/ontology/last-fm/>
  25. PREFIX dc: <http://purl.org/dc/terms/>
  26. PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  27. SELECT *
  28. WHERE
  29. {
  30. <$uri> foaf:primaryTopic ?person .
  31. ?gobble a gob:ScrobbleEvent ;
  32. gob:track_played ?track ;
  33. dc:date ?timestamp ;
  34. gob:user ?person .
  35. ?track dc:title ?trackname ;
  36. foaf:maker ?artist .
  37. ?artist foaf:name ?artistname .
  38. }
  39. ORDER BY DESC(?timestamp)
  40. SPARQL
  41. my $query_obj = RDF::Redland::Query->new($query, undef, undef, 'sparql');
  42. my $results = $query_obj->execute($model);
  43. while((!$results->finished) && (my %row = $results->bindings))
  44. {
  45. $results->next_result;
  46. print sprintf("%s\t%s\t%s\n",
  47. $row{'artistname'}->literal_value,
  48. $row{'trackname' }->literal_value,
  49. $row{'timestamp' }->literal_value);
  50. }