Skip to content

Instantly share code, notes, and snippets.

@adactio
Created October 21, 2021 12:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adactio/aa46506fc7c28f18f0feeffe0276a4a7 to your computer and use it in GitHub Desktop.
Save adactio/aa46506fc7c28f18f0feeffe0276a4a7 to your computer and use it in GitHub Desktop.
Scraping Bandcamp for artist info.
<?php
function curlURL($url) {
$return = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$return['header'] = substr($response, 0, $header_size);
$return['body'] = substr($response, $header_size);
curl_close($ch);
return $return;
}
function getBandcampArtist($artist="") {
$return = array();
trim($artist);
$artist = str_replace(
array(' ','á','é','í','ó','&','-','/','\''),
array('','a','e','i','o','and','','',''),
strtolower($artist)
);
$words = ['music','band'];
$url = 'https://'.$artist.'.bandcamp.com';
$response = curlURL($url);
if (!stristr($response['header'],'200 OK')) {
if (stristr($response['header'], 'Location: '.$url.'/releases')) {
$url.= '/releases';
$response = curlURL($url);
}
}
if (stristr($response['header'],'200 OK')) {
$return['url'] = $url;
$parser = new DOMDocument();
if (@$parser -> loadHTML($response['body'])) {
$xpath = new DOMXPath($parser);
foreach ($xpath -> query('//*[local-name(.) = "meta"][contains(@property, "og:video:secure_url")][@content]') as $node) {
$return['player'] = $node -> getAttribute('content');
}
foreach ($xpath -> query('//*[local-name(.) = "meta"][contains(@property, "og:image")][@content]') as $node) {
$return['image'] = $node -> getAttribute('content');
}
}
} else {
foreach ($words as $word) {
if (!stristr($artist, $word)) {
$return = getBandcampArtist($artist.$word);
break;
} else {
$artist = str_replace($word, '', $artist);
}
}
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment