forked from prolificjones82/Redbubble-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.redbubble.php
171 lines (126 loc) · 4.14 KB
/
class.redbubble.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Redbubble - Homebrew Redbubble API
* NOTE: Requires PHP version 5 or later
* @package Redbubble
* @author Lee Jones
* @copyright 2012 Lee Jones
* @version v3.1
* @license MIT
*/
require_once('libs/html_dom_parser.php');
require_once('class.redbubble_config.php');
require_once('class.redbubble_cache.php');
class Redbubble
{
protected $rburl = 'http://www.redbubble.com';
protected $config;
public function getRedbubbleUrl() {
return $this->rburl;
}
public function getConfig() {
return $this->config;
}
public function getCache() {
return new RedbubbleCache();
}
public function __construct($config)
{
$this->config = $config;
}
private function generateCollectionLink($collection_id)
{
if ($this->getConfig()->prettyUrls()) {
$url = '/' . $this->getConfig()->getRedbubbleUser() . '/' . $collection_id . '/';
} else {
$url = '?rbu=' . $this->getConfig()->getRedbubbleUser() . '&cID=' . $collection_id;
}
return $url;
}
private function convertResponseType($data)
{
switch ($this->getConfig()->getResponseType())
{
case 'json':
$new_data = json_encode($data);
break;
case 'object':
default:
$new_data = (object) $data;
break;
}
return $new_data;
}
public function getCollections()
{
if ($this->getConfig()->cacheResponses()) {
$data = $this->getCache()->getCacheObject($this->getConfig()->getRedbubbleUser());
}
if (!$data) {
$url = sprintf($this->getRedbubbleUrl() . "/people/%s/portfolio/", $this->getConfig()->getRedbubbleUser());
$html = file_get_html($url);
$collection_elements = $html->find('a[class=collection-link]');
$data = array();
foreach ($collection_elements as $element) {
$item_array = array();
// get collection id
$collection_id = str_replace('/people/' . $this->getConfig()->getRedbubbleUser() . '/collections/', '', $element->href);
$item_array['collection_id'] = $collection_id;
// get collection image
$pattern = array('background-image: url(',');');
$item_array['image'] = str_replace($pattern, '', $element->style);
// get collection title
$spans = $element->find('span span[class=pc-title-background]');
$item_array['title'] = $spans[0]->innertext;
$item_array['url'] = $this->generateCollectionLink($collection_id);
$data[] = (object) $item_array;
}
if ($this->getConfig()->cacheResponses()) {
$cache = $this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser(), $data);
if (!$cache) {
return $cache;
}
}
}
return $this->convertResponseType($data);
}
public function getProducts($collection_id)
{
if ($this->getConfig()->cacheResponses()) {
$data = $this->getCache()->getCacheObject($this->getConfig()->getRedbubbleUser() . '-' . $collection_id);
}
if (!$data) {
$url = sprintf($this->getRedbubbleUrl() . "/people/%s/collections/%s", $this->getConfig()->getRedbubbleUser(), $collection_id);
$html = file_get_html($url);
$products = $html->find('a[class=grid-item]');
$data = array();
foreach ($products as $product) {
$item_array = array();
// get product link
$item_array['link'] = $this->getRedbubbleUrl() . $product->href;
// get product design image
$pattern = array('background-image: url(',');');
$item_array['design_image'] = str_replace($pattern, '', $product->style);
// get product image
$img_tag = $product->find('img[class=design]');
$item_array['product_image'] = $img_tag[0]->src;
// get product title
$item_array['title'] = $product->title;
$pricing = $product->find('div span span[class=price] span');
$pricing_array = array();
foreach ($pricing as $price_element) {
$pricing_array[] = $price_element->innertext;
}
$item_array['price'] = implode('', $pricing_array);
$data[] = (object) $item_array;
}
if ($this->getConfig()->cacheResponses()) {
$cache = $this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser() . '-' . $collection_id, $data);
if (!$cache) {
return $cache;
}
}
}
return $this->convertResponseType($data);
}
}