Last active
August 27, 2018 00:06
-
-
Save criztovyl/e971ba59851f6f30ea98 to your computer and use it in GitHub Desktop.
Receives or displays a wishlist of a fan on bandcamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
A small PHP script to display a bandcamp wishlist | |
Copyright (C) 2015 Christoph "criztovyl" Schulz | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
// Set debug | |
define("BCWL_DEBUG", false); | |
// Constant(s) | |
// Constant for display_wishlist $opts | |
define("FULLHTML", 1); | |
define("STYLEDHTML", 2); | |
function debug($str){ | |
if(BCWL_DEBUG == true) echo "$str<br>\n"; | |
} | |
/** | |
* Returns an array of items on a bandcamp wishlist. | |
* @param $fanName the username of the fan | |
* @return an array | |
*/ | |
function bandcamp_wishlist($fanName){ | |
$dom = new DOMDocument; | |
// Load collection page | |
$dom->loadHTML(file_get_contents("https://bandcamp.com/$fanName")); | |
// Extract data that contains the collection data | |
$data = $dom->getElementById("pagedata")->getAttribute("data-blob"); | |
// Decode json | |
$json = json_decode($data, true)["item_cache"]["wishlist"]; | |
// Set up wishlist array | |
$wishlist = array(); | |
// Iterate over collections items | |
foreach($json as $item){ | |
// Whishlist items are non-sold items | |
if ($item["sale_item_type"] == NULL ){ | |
// Push wished item to wishlist | |
array_push($wishlist, $item); | |
} | |
} | |
// Return wishlist | |
return $wishlist; | |
} | |
/** | |
* Returns a bandcamp wishlist of a fan. | |
* @param $fan the fan | |
* @param $format the format. Currently supported: json, yaml, html. Defaults to json. | |
* For html you also can set opt FULLHTML to display a complete html page. | |
* HTML CSS class: bc_wishlist (div/ul/li) | |
* @param $opts options, currently on FULLHTML | |
*/ | |
function display_wishlist($fan, $format = "json", ...$opts){ | |
$classes = array("outer" => "bc_wishlist", "list" => "bc_wishlist", "listitem" => "bc_wishlist"); | |
debug("called"); | |
if(in_array(STYLEDHTML, $opts)) { | |
$style = $format; | |
$format = "html"; | |
$classes_ = $opts[array_search(STYLED_HTML, $otps)+2]; | |
foreach(array_keys($classes) as $key){ | |
if(isset($classes_[$key])) $classes[$key] = $classes_[$key]; | |
} | |
debug("is styled"); | |
} | |
else debug("not styled"); | |
debug("checked styled"); | |
// Receive data | |
$data = bandcamp_wishlist($fan); | |
debug("received data"); | |
// Return json | |
if($format == "json") return json_encode($data); | |
// Return yaml | |
if($format == "yaml") return yaml_emit($data); | |
// Return html | |
if($format == "html") { | |
// Set up empty string for HTML content | |
$html = ''; | |
debug("format html"); | |
if(isset($style)) $stylesheet = sprintf('<link rel="stylesheet" href="%s" type="text/css" />'."\n", $style) ; | |
// Receive if should generate complete HTML page | |
$fullHTML = in_array(FULLHTML, $opts); | |
// If should generate complete page, append HTML page begin to variable | |
if($fullHTML) $html .= '<!DOCTYPE html><html><head><title>Bandcamp wishlist of '.$fan.'</title><meta charset="utf-8">'.$stylesheet.'</head><body>'."\n".'<div class="'.$classes["outer"].'">'."\n"; | |
// Append unordered list | |
$html .= '<ul class="'.$classes["list"].'">'."\n"; | |
// Iterate over wishlist items | |
foreach ($data as $item) { | |
//Append list item with image, band name and title | |
$html .= sprintf('<li class="%5$s"><a href="%4$s"><img src="https://f1.bcbits.com/img/a%3$s_9.jpg" /></a><br>%1$s: <a href="%4$s">%2$s</a></li>'."\n", utf8_decode($item["band_name"]), utf8_decode($item["item_title"]), $item["item_art_id"], $item["item_url"], $classes["listitem"]); | |
} | |
// Close list | |
$html .= '</ul>'; | |
// If should generate complete page, append HTML page end | |
if($fullHTML) $html .= '</div></body></html>'; | |
// Return html | |
return $html; | |
} | |
debug("fall through"); | |
} | |
$_styled = isset($_GET["css"]); | |
$_full = isset($_GET["fullhtml"]); | |
$_classes = array("outer" => $_GET["outerclass"], "list" => $_GET["listclass"], "listitem" => $_GET["listitemclass"]); | |
echo display_wishlist($_GET["fan"], $_styled ? $_GET["css"] : $_GET["format"], $_full ? FULLHTML : NULL, $_styled ? STYLEDHTML : NULL, $_classes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment