Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Jones committed Oct 17, 2018
1 parent 3532fb5 commit d947f04
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 28 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,67 @@ A homebrew API style page scraper designed to put your Redbubble products on you
## Quick Use
Upload the ```redbubble``` folder to your server.

Include the file in your page ```require "redbubble/redbubble.php";```
Include the file in your page ```require_once("class.redbubble.php");```

Initiante the class with your Redbubble username:
Use the `RedbubbleConfig` object to create your settings:

```php
$redbubble = new Redbubble('username', false, 'array');
$config = new RedbubbleConfig('username', 'object', false, true);
```

You can then instantiate the `Redbubble` class using this config object:

```php
$connection = new Rebubble($config);
```

There are now two functions you can use, ```getCollections()``` and ```getProducts($collection_id)```.

The ```getProducts()``` function requires a ```$collection_id``` as a parameter this is returned as part of the ```GetCollections()``` function.

## Response Types

Returned responses are a simple PHP ```object``` by default, however an option in the class is to specify the returned data type.

Options are ```object```, ```json```

## Pretty URLS

By default the functions return links as query strings using ```rbu``` and ```cID``` as query parameters. By setting the ```$pretty_urls``` variable to ```true``` when the Redbubble class is initiated these will be rewritten in ```/rbu/cID/``` format, you will need to modify your ```htaccess``` file to compensate.

## Response Types
Example:

```
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /?rbu=$1&cID=$2 [L]
```

Returned responses are a simple PHP ```array``` by default, however a third option in the class is to specify the returned data type.
## Caching Responses

Options are ```array```, ```object```, ```json```
By default response caching is turned on. As this is an unofficial Redbubble API connection the calls can take quite a long time, with this in mind responses are cached for _**two days**_ by default, this value can be edited in `class.redbubble_config.php`.

Responses are cached to a folder in called `redbubble_cache` which will require folder permissions of `777`.

## Example

To view a brief example of how you could structure your code take a look at ```index.php``` in the repository.
To view a brief example of how you could structure your code take a look at ```examples/index.php``` in the repository.

## Available Functions

| Name | Parameters | Description |
|------------------|----------------------|----------------------------------------------------------------------------------------------|
| getCollections() | none | Retrieves a list of all collections belonging to the current ```rbuser``` |
| getCollections() | none | Retrieves a list of all collections belonging to the current ```$rbuser``` |
| getProducts() | ```$collection_id``` | Retrieves a list of products from a particular collection, ```$collection_id``` is required. |

## Options

These options need to be set when the class is initiated with ```new Redbubble()```.
These options need to be set when the class is initiated with ```new RedbubbleConfig()```.

| Name | Type | Default | Description |
|---------------------|---------|---------------|-----------------------------------------------------------------------------------------------------------------|
| ```rbuser``` | String | ```null``` | This is **required** allows the class to scrape your Redbubble page. |
| ```response_type``` | String | ```object``` | Defines response type, possible values are ```object``` and ```json``` |
| ```pretty_urls``` | Boolean | ```false``` | For SEO purposes you may wish to use pretty URLS, set this to ```true``` and ensure to edit your htaccess file (example below).|
| ```response_type``` | String | ```array``` | Defines response type, possible values are ```array```, ```object``` and ```json``` |

## Htaccess

Example:

```
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /shop?rbu=$1&cID=$2 [L]
```

## Licence

Expand Down
26 changes: 21 additions & 5 deletions class.redbubble.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct($config)

private function generateCollectionLink($collection_id)
{
if ($this->getConfig()->getPrettyUrls()) {
if ($this->getConfig()->prettyUrls()) {
$url = '/' . $this->getConfig()->getRedbubbleUser() . '/' . $collection_id . '/';
} else {
$url = '?rbu=' . $this->getConfig()->getRedbubbleUser() . '&cID=' . $collection_id;
Expand All @@ -64,7 +64,9 @@ private function convertResponseType($data)

public function getCollections()
{
$data = $this->getCache()->getCacheObject($this->getConfig()->getRedbubbleUser());
if ($this->getConfig()->cacheResponses()) {
$data = $this->getCache()->getCacheObject($this->getConfig()->getRedbubbleUser());
}

if (!$data) {
$url = sprintf($this->getRedbubbleUrl() . "/people/%s/portfolio/", $this->getConfig()->getRedbubbleUser());
Expand Down Expand Up @@ -93,7 +95,13 @@ public function getCollections()
$data[] = (object) $item_array;
}

$this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser(), $data);
if ($this->getConfig()->cacheResponses()) {
$cache = $this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser(), $data);

if (!$cache) {
return $cache;
}
}

}

Expand All @@ -102,7 +110,9 @@ public function getCollections()

public function getProducts($collection_id)
{
$data = $this->getCache()->getCacheObject($this->getConfig()->getRedbubbleUser() . '-' . $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);
Expand Down Expand Up @@ -140,7 +150,13 @@ public function getProducts($collection_id)
$data[] = (object) $item_array;
}

$this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser() . '-' . $collection_id, $data);
if ($this->getConfig()->cacheResponses()) {
$cache = $this->getCache()->setCacheObject($this->getConfig()->getRedbubbleUser() . '-' . $collection_id, $data);

if (!$cache) {
return $cache;
}
}
}

return $this->convertResponseType($data);
Expand Down
11 changes: 9 additions & 2 deletions class.redbubble_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

class RedbubbleCache
{
protected $cache_path = '/cache/';
protected $cache_path = '/redbubble_cache/';
protected $duration = 7200; // two hours


public function getPath()
{
Expand Down Expand Up @@ -36,6 +37,12 @@ public function getCacheObject($name)
public function setCacheObject($name, $array)
{
$file = $this->getPath() . $name . '.cache';
file_put_contents($file, serialize($array));

if (is_writable($file)) {
file_put_contents($file, serialize($array));
return true;
}

return false;
}
}
11 changes: 9 additions & 2 deletions class.redbubble_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class RedbubbleConfig
protected $rbuser;
protected $response_type;
protected $pretty_urls;
protected $cache_responses;

public function getRedbubbleUser()
{
Expand All @@ -22,15 +23,21 @@ public function getResponseType()
return $this->response_type;
}

public function getPrettyUrls()
public function prettyUrls()
{
return $this->pretty_urls;
}

public function __construct($username, $response_type = 'object', $pretty_urls = false)
public function cacheResponses()
{
return $this->cache_responses;
}

public function __construct($username, $response_type = 'object', $pretty_urls = false, $cache_responses = true)
{
$this->rbuser = $username;
$this->response_type = $response_type;
$this->pretty_urls = $pretty_urls;
$this->cache_responses = $cache_responses;
}
}
File renamed without changes.

0 comments on commit d947f04

Please sign in to comment.