Skip to content

Commit

Permalink
"makes", "favorites" and "collections" lists are getted from Thingive…
Browse files Browse the repository at this point in the history
…rse API
  • Loading branch information
dalacost committed May 26, 2023
1 parent a1ae4ba commit 269aba6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.1 - May, 2023
* "makes", "favorites" and "collections" lists are getted from Thingiverse API

1.0 - May, 2023

* Fork from original [thingiverse-embed](https://github.com/martymcguire/wp-thingiverse-embed) wordpress plugin.
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ There are two types of streams: *Global* and *User*. *User* streams require you
*User Streams*

- `designed` - content from http://www.thingiverse.com/< User >/designs
- `like` - content from http://www.thingiverse.com/< User >/favorites
- `like` - content from http://www.thingiverse.com/< User >/likes
- `made` - content from http://www.thingiverse.com/< User >/makes
- `favorites` - content from http://www.thingiverse.com/< User >/favorites
- `collections` - content from http://www.thingiverse.com/< User >/collections

*Global Streams*

Expand Down
28 changes: 19 additions & 9 deletions lib/thingiverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

class Thingiverse {

const BASE_URL = "https://www.thingiverse.com";
const TOKEN_URL = "https://cdn.thingiverse.com/site/js/app.bundle.js";
const BASE_URL = "https://www.thingiverse.com";
const BASE_API_URL = "https://api.thingiverse.com";
const TOKEN_URL = "https://cdn.thingiverse.com/site/js/app.bundle.js";
const CACHE_TTL_TOKEN = 86400; //24 h
const CACHE_TTL_THING = 3600; //1 h
const CACHE_TTL_WIDGET = 3600; //1 h
Expand All @@ -13,16 +14,13 @@ class Thingiverse {

public static function user_id_from_name( $user ) {

$user_key = 'user_id_from_name_'.$user;
$user_key = 'thingiverse-user-id-from-name-'.$user;

$cached_user_id_from_name = Thingiverse::get_object_from_cache($user_key);

if(false === $cached_user_id_from_name or $cached_user_id_from_name === ""){
$authorization_header = 'Authorization: Bearer '.Thingiverse::get_authorization_token();
$options = ['http' => ['header' => $authorization_header]];
$context = stream_context_create($options);
$json = file_get_contents('https://api.thingiverse.com/users/'.$user, false, $context);
$obj = json_decode($json);

$obj = Thingiverse::get_authorized_url_json('https://api.thingiverse.com/users/'.$user);
$id_from_name = $obj->id;
Thingiverse::log_message("Renewing KEY: ".$user_key."=".$id_from_name);
set_transient($user_key, $id_from_name, Thingiverse::CACHE_TTL_USER_ID);
Expand All @@ -36,7 +34,7 @@ public static function user_id_from_name( $user ) {

public static function get_authorization_token(){

$cached_token_key = 'thingiverse_authorization_token';
$cached_token_key = 'thingiverse-authorization-token';
$cached_token = Thingiverse::get_object_from_cache($cached_token_key);
if(false === $cached_token){
$js = file_get_contents(Thingiverse::TOKEN_URL);
Expand All @@ -52,6 +50,18 @@ public static function get_authorization_token(){
}
}

//Returns a Json object
public static function get_authorized_url_json($url){

$authorization_header = 'Authorization: Bearer '.Thingiverse::get_authorization_token();
$options = ['http' => ['header' => $authorization_header]];
$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);
$obj = json_decode($json);

return $obj;
}

public static function get_object_from_cache($key){

return Thingiverse::CACHE_ENABLE? get_transient($key):false;
Expand Down
82 changes: 75 additions & 7 deletions lib/thingiverse_stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ThingiverseStream {
* * designed (/<User>/designs, /rss/user:<id>)
* * likes (/<User>/favorites, /rss/user:<id>/likes)
* * made (/<User>/makes, /rss/user:<id>/made)
* * favorites (/<User>/favorites)
* * collections (/<User>/collections)
*/
public $title; // stream title
public $url;
Expand All @@ -25,7 +27,7 @@ class ThingiverseStream {

function __construct( $type = "newest", $user = null ) {
$this->user = $user;
$this->user_url = (is_null($user) ? null : Thingiverse::BASE_URL . "/user:$user");
$this->user_url = (is_null($user) ? null : Thingiverse::BASE_URL . "/$user");
$method_name = "initialize_stream_$type";
if(method_exists($this, $method_name)){
call_user_func( array($this, $method_name) );
Expand Down Expand Up @@ -80,13 +82,78 @@ function initialize_stream_likes() {
$this->load_stream_from_rss_url();
}

// Made things RSS has the thing creator, not the instance creator as author.
// Also shows the original creator's picture. Fall back to HTML parsing.
function initialize_stream_made() {
$this->user_id = Thingiverse::user_id_from_name($this->user);
$this->url = Thingiverse::BASE_URL . "/rss/user:$this->user_id/made";
$this->title = "Newest Instances";
$this->load_stream_from_rss_url();

$this->user_url = (is_null($this->user) ? null : Thingiverse::BASE_URL . "/$this->user/makes");
$this->url = Thingiverse::BASE_API_URL . "/users/$this->user/search/?type=makes&sort=newest";
$cache_id = "thingiverse-press-stream-makes-$this->user";
$cached_thing = Thingiverse::get_object_from_cache($cache_id);

if(false === $cached_thing){
$obj = Thingiverse::get_authorized_url_json($this->url);

foreach($obj->hits as $key => $lock)
{
$thing = new ThingiverseThing();
$thing -> initialize_from_json($lock);
array_push($this->things, $thing);
}

Thingiverse::log_message("Renewing KEY: ".$cache_id);
set_transient($cache_id, $this->things, Thingiverse::CACHE_TTL_WIDGET);
} else {
$this -> things = $cached_thing;
}
}


function initialize_stream_collections() {

$this->user_url = (is_null($this->user) ? null : Thingiverse::BASE_URL . "/$this->user/collections");
$this->url = Thingiverse::BASE_API_URL . "/users/$this->user/search/?type=collections&sort=newest";
$cache_id = "thingiverse-press-stream-collections-$this->user";
$cached_thing = Thingiverse::get_object_from_cache($cache_id);

if(false === $cached_thing){
$obj = Thingiverse::get_authorized_url_json($this->url);

foreach($obj->hits as $key => $lock)
{
$thing = new ThingiverseThing();
$thing -> initialize_from_json($lock);
$thing -> url = $lock -> absolute_url;
array_push($this->things, $thing);
}

Thingiverse::log_message("Renewing KEY: ".$cache_id);
set_transient($cache_id, $this->things, Thingiverse::CACHE_TTL_WIDGET);
} else {
$this -> things = $cached_thing;
}
}


function initialize_stream_favorites() {
$this->user_url = (is_null($this->user) ? null : Thingiverse::BASE_URL . "/$this->user/favorites");
$this->url = Thingiverse::BASE_API_URL . "/users/$this->user/favorites";
$cache_id = "thingiverse-press-stream-favorites-$this->user";
$cached_thing = Thingiverse::get_object_from_cache($cache_id);

if(false === $cached_thing){
$obj = Thingiverse::get_authorized_url_json($this->url);

foreach($obj as $key => $lock)
{
$thing = new ThingiverseThing();
$thing -> initialize_from_json($lock);
array_push($this->things, $thing);
}

Thingiverse::log_message("Renewing KEY: ".$cache_id);
set_transient($cache_id, $this->things, Thingiverse::CACHE_TTL_WIDGET);
} else {
$this -> things = $cached_thing;
}
}

// Returns a DOM object for the specified URL. Pulls it from the transient
Expand Down Expand Up @@ -135,5 +202,6 @@ function parse_thing_instances_from_html_dom($dom) {
array_push($this->things, $thing);
}
}

}
?>
26 changes: 11 additions & 15 deletions lib/thingiverse_thing.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ function __construct( $thing_url = "" ) {
$cached_thing = Thingiverse::get_object_from_cache($thing_cache_id);
if(false === $cached_thing){
$this->url = $thing_url;
$authorization_header = 'Authorization: Bearer '.Thingiverse::get_authorization_token();
$options = ['http' => ['header' => $authorization_header]];
$context = stream_context_create($options);
$json = file_get_contents('https://api.thingiverse.com/things/'.$thing_id, false, $context);
$obj = json_decode($json);
$obj = Thingiverse::get_authorized_url_json('https://api.thingiverse.com/things/'.$thing_id);
$this->initialize_from_json($obj);
//cache
Thingiverse::log_message("Renewing KEY: ".$thing_cache_id);
Expand All @@ -44,16 +40,16 @@ function __construct( $thing_url = "" ) {

function initialize_from_json($obj) {

$this->title = $obj-> name;
$this->creator_url = $obj-> creator -> public_url;
$this->creator = $obj-> creator -> name;
$this->creator_img = $obj-> creator -> thumbnail;
$this->main_image = $obj-> thumbnail;
$this->description = $obj-> description;
$this->instructions = $obj-> instructions;
$this->like_count = $obj-> like_count;
$this->create_date = $obj-> added;

@$this->title = $obj-> name;
@$this->creator_url = $obj-> creator -> public_url;
@$this->creator = $obj-> creator -> name;
@$this->creator_img = $obj-> creator -> thumbnail;
@$this->main_image = $obj-> thumbnail;
@$this->description = $obj-> description;
@$this->instructions = $obj-> instructions;
@$this->like_count = $obj-> like_count;
@$this->create_date = $obj-> added;
@$this->url = $obj-> public_url;
}

public static function from_rss_item_dom( $dom ) {
Expand Down
2 changes: 1 addition & 1 deletion templates/thing.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
<span class="thingiverse-thing-card-body-text"><?php echo $thing->description ?></span>
</div>
</div>
</div>
</div>
3 changes: 2 additions & 1 deletion thingiverse-stream-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct() {
);
}



/** @see WP_Widget::widget */
function widget($args, $instance) {
Expand Down Expand Up @@ -95,7 +96,7 @@ function form($instance) {
</label>
<br />
Global Streams: <em>newest, featured, popular, derivatives, instances</em><br />
User Streams: <em>designed, likes, made</em><br />
User Streams: <em>designed, likes, made, favorites, collections</em><br />
</p>
<p>
<label for="<?php echo $this->get_field_id('user'); ?>"><?php _e('User Name:'); ?>
Expand Down

0 comments on commit 269aba6

Please sign in to comment.