Skip to content

Commit

Permalink
moving some things around
Browse files Browse the repository at this point in the history
  • Loading branch information
Punkman committed Nov 9, 2011
1 parent 719a01f commit 9882a77
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
98 changes: 50 additions & 48 deletions Thingiverse.php
Expand Up @@ -11,16 +11,25 @@ public function log($msg) {
file_put_contents($this->log_path, $msg, FILE_APPEND);
}

/* Helpers */

public function get_range($start, $end, $get_files = true, $get_images = false) {

$this->get_things(range($start,$end), $get_files, $get_images);

}

public function get_user_things($user, $last_id = false) {

$this->get_list($last_id, $user . '/things');

}

public function get_things($thing_ids, $get_files = true, $get_images = false) {
$ttotal = 0;
foreach($thing_ids as $id) {
$tstart = time();
$ttotal = 0;

if($this->get_thing($id, $get_files, $get_images)) {
echo 'Got thing '. $id;
} else {
Expand All @@ -36,60 +45,23 @@ public function get_things($thing_ids, $get_files = true, $get_images = false) {
echo 'Total time: '. $ttotal . " sec \n";
}

public function get_user_things($user, $last_id = false, $get_things = true, $get_files = true, $get_images = false) {

$r = new HttpReq('http://www.thingiverse.com/' . $user . '/things');
$r->urlBase = 'http://www.thingiverse.com/' . $user . '/things';
$r->data = array();
$r->last_id = $last_id;
$r->attach('success', array($this, 'parse_things'));
$r->exec();
if($get_things && !empty($r->data)) {
$this->get_things(array_keys($r->data), $get_files, $get_images);
} else {
return $r->data;
}

}
/* Main methods */

public function get_newest($last_id) {
$type = 'newest';
public function get_list($last_id, $type = 'newest' ) {


$r = new HttpReq('http://www.thingiverse.com/' . $type);
$r->urlBase = 'http://www.thingiverse.com/' . $type;
$r->list_type = $type;
$r->data = array();
$r->last_id = $last_id;
$r->attach('success', array($this, 'parse_things'));
$r->singleFail = true;
$r->attach('success', array($this, 'thing_list'));
$r->attach('fail', array($this, 'list_fail'));
$r->exec();

return $r->data;
}

public function parse_things($r) {
$stop = false;

if(preg_match_all('@<a href="http://www.thingiverse.com/thing:([^"]+)">([^<]+)</a>@', $r->body, $things)) {

foreach($things[1] as $k => $id) {
if($r->last_id && $r->last_id == $id) {
$r->keep = false;
$stop = true;
break;
} else {
$r->data[$id] = $things[2][$k];
}
}
}

if(!$stop && preg_match('@/page:([0-9]+)">next@', $r->body, $page)) {
$r->keep = true;
$r->url = $r->urlBase . '/page:' . $page[1];
} else {
$r->keep = false;
}

}

public function get_thing($thing_id, $get_files = true, $get_images = false) {
$thing_id = (string) $thing_id;

Expand Down Expand Up @@ -170,8 +142,31 @@ public function get_image($image_id, $size = 'large') {

}

public function thing_fail($r, $msg) {
$this->log('thing '. $r->thing_id .' failed ' . $msg . "\n");
/* Parsing methods */

public function thing_list($r) {
$stop = false;

if(preg_match_all('@<a href="http://www.thingiverse.com/thing:([^"]+)">([^<]+)</a>@', $r->body, $things)) {

foreach($things[1] as $k => $id) {
if($r->last_id && $r->last_id == $id) {
$r->keep = false;
$stop = true;
break;
} else {
$r->data[$id] = $things[2][$k];
}
}
}

if(!$stop && preg_match('@/page:([0-9]+)">next@', $r->body, $page)) {
$r->keep = true;
$r->url = 'http://www.thingiverse.com/' . $r->list_type . '/page:' . $page[1];
} else {
$r->keep = false;
}

}

public function thing_meta($r) {
Expand Down Expand Up @@ -264,13 +259,20 @@ public function thing_meta($r) {

$r->data = $thing;
}

/* Error handlers */

public function thing_fail($r, $msg) {
$this->log('thing '. $r->thing_id .' failed ' . $msg . "\n");
}

public function file_fail($r,$msg) {
$this->log('file ' . $r->file_id . ' failed ' . $msg . "\n");
}


public function list_fail($r, $msg) {
$this->log('list '. $r->url .' failed ' . $msg . "\n");
}

}

Expand Down
29 changes: 18 additions & 11 deletions run.php
Expand Up @@ -4,28 +4,35 @@
include 'HttpReq.php';
include 'Thingiverse.php';


set_time_limit(0);
echo '<meta content="text/html; charset=UTF-8" http-equiv="Content-type">';
echo '<pre>';

$tv = new Thingiverse;

/* Print a list of all the things a user has uploaded
echo '<pre>';
print_r($tv->get_user_things('DrewPetitclerc', false));
echo '</pre>';
print_r($tv->get_user_things('DrewPetitclerc'));
*/

/* Get newest things up to last thing ID seen
echo '<pre>';
print_r($tv->get_newest(13366));
echo '</pre>';
print_r($tv->get_list(13366, 'newest'));
*/

/* Get a range of things */
echo '<pre>';
$tv->get_range(7001,8000);
echo '</pre>';
/* Get a range of things
$tv->get_range(12701,13000);
*/

/* Get a list of things
$failed = explode(' ', '5812 5814 5815 5829 5852 5853 5872 5875 5876 5880 5882 5918 5920 5921 5944 5989 5990');
$tv->get_things($failed);
*/

echo '</pre>';
?>

0 comments on commit 9882a77

Please sign in to comment.