@@ -36,172 +36,98 @@ function __construct( $type = "newest", $user = null ) {
3636 }
3737 }
3838
39+ private function initialize_stream_common ($ url , $ cache_id , $ is_hits = true ) {
40+
41+ $ cached_thing = Thingiverse::get_object_from_cache ($ cache_id );
42+
43+ if (false === $ cached_thing ) {
44+ $ obj = Thingiverse::get_authorized_url_json ($ url );
45+ $ items = $ is_hits ? $ obj ->hits : $ obj ;
46+
47+ foreach ($ items as $ key => $ lock )
48+ {
49+ $ thing = new ThingiverseThing ();
50+ $ thing ->initialize_from_json ($ lock );
51+ array_push ($ this ->things , $ thing );
52+ }
53+
54+ Thingiverse::log_message ("Renewing KEY: " . $ cache_id );
55+ set_transient ($ cache_id , $ this ->things , Thingiverse::CACHE_TTL_WIDGET );
56+ } else {
57+ $ this ->things = $ cached_thing ;
58+ }
59+ }
60+
3961 function initialize_stream_newest () {
40- $ this ->url = Thingiverse::BASE_URL . "/rss/newest " ;
4162 $ this ->title = "Newest Things " ;
42- $ this ->load_stream_from_rss_url ();
63+ $ this ->url = Thingiverse::BASE_URL . "/api/v2/search/things?sort=newest " ;
64+ $ cache_id = "thingiverse-press-stream-newest-global " ;
65+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
4366 }
4467
4568 function initialize_stream_featured () {
46- $ this ->url = Thingiverse::BASE_URL . "/rss/featured " ;
4769 $ this ->title = "Featured Things " ;
48- $ this ->load_stream_from_rss_url ();
70+ $ this ->url = Thingiverse::BASE_URL . "/api/v2/search/things?sort=featured " ;
71+ $ cache_id = "thingiverse-press-stream-featured-global " ;
72+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
4973 }
5074
5175 function initialize_stream_popular () {
52- $ this ->url = Thingiverse::BASE_URL . "/rss/popular " ;
5376 $ this ->title = "Popular Things " ;
54- $ this ->load_stream_from_rss_url ();
77+ $ this ->url = Thingiverse::BASE_URL . "/api/v2/search/things?sort=popular&posted_after=now-30d " ;
78+ $ cache_id = "thingiverse-press-stream-popular-global " ;
79+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
5580 }
5681
5782 function initialize_stream_derivatives () {
58- $ this ->url = Thingiverse::BASE_URL . "/rss/derivatives " ;
5983 $ this ->title = "Newest Derivatives " ;
60- $ this ->load_stream_from_rss_url ();
84+ $ this ->url = Thingiverse::BASE_URL . "/api/v2/search/things?sort=derivatives " ;
85+ $ cache_id = "thingiverse-press-stream-derivatives-global " ;
86+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
6187 }
6288
6389 // Newest instances has the thing creator, not the instance creator as author.
6490 // Fall back to HTML parsing.
6591 function initialize_stream_instances () {
66- $ this ->url = Thingiverse::BASE_URL . "/made-things " ;
6792 $ this ->title = "Newest Instances " ;
68- $ this ->load_stream_from_instances_url ();
93+ $ this ->url = Thingiverse::BASE_URL . "/api/v2/search/things?sort=makes " ;
94+ $ cache_id = "thingiverse-press-stream-instances-global " ;
95+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
6996 }
7097
7198 function initialize_stream_designed () {
72- $ this ->user_id = Thingiverse::user_id_from_name ( $ this ->user );
73- $ this ->url = Thingiverse::BASE_URL . "/rss/user: $ this ->user_id " ;
74- $ this -> title = "Newest Things " ;
75- $ this ->load_stream_from_rss_url ( );
99+ $ this ->user_url = ( is_null ( $ this -> user ) ? null : Thingiverse::BASE_URL . " / $ this ->user /designs " );
100+ $ this ->url = Thingiverse::BASE_API_URL . "/users/ $ this ->user /search/?type=designs&sort=newest " ;
101+ $ cache_id = "thingiverse-press-stream-designs- $ this -> user " ;
102+ $ this ->initialize_stream_common ( $ this -> url , $ cache_id , true );
76103 }
77104
78105 function initialize_stream_likes () {
79- $ this ->user_id = Thingiverse::user_id_from_name ( $ this ->user );
80- $ this ->url = Thingiverse::BASE_URL . "/rss/user: $ this ->user_id /likes " ;
81- $ this -> title = "Newest Likes " ;
82- $ this ->load_stream_from_rss_url ( );
106+ $ this ->user_url = ( is_null ( $ this -> user ) ? null : Thingiverse::BASE_URL . " / $ this ->user /likes " );
107+ $ this ->url = Thingiverse::BASE_URL . "/api/users/ $ this ->user /likes " ;
108+ $ cache_id = "thingiverse-press-stream-likes- $ this -> user " ;
109+ $ this ->initialize_stream_common ( $ this -> url , $ cache_id , false );
83110 }
84111
85112 function initialize_stream_made () {
86-
87113 $ this ->user_url = (is_null ($ this ->user ) ? null : Thingiverse::BASE_URL . "/ $ this ->user /makes " );
88114 $ this ->url = Thingiverse::BASE_API_URL . "/users/ $ this ->user /search/?type=makes&sort=newest " ;
89115 $ cache_id = "thingiverse-press-stream-makes- $ this ->user " ;
90- $ cached_thing = Thingiverse::get_object_from_cache ($ cache_id );
91-
92- if (false === $ cached_thing ){
93- $ obj = Thingiverse::get_authorized_url_json ($ this ->url );
94-
95- foreach ($ obj ->hits as $ key => $ lock )
96- {
97- $ thing = new ThingiverseThing ();
98- $ thing -> initialize_from_json ($ lock );
99- array_push ($ this ->things , $ thing );
100- }
101-
102- Thingiverse::log_message ("Renewing KEY: " .$ cache_id );
103- set_transient ($ cache_id , $ this ->things , Thingiverse::CACHE_TTL_WIDGET );
104- } else {
105- $ this -> things = $ cached_thing ;
106- }
116+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
107117 }
108118
109-
110119 function initialize_stream_collections () {
111-
112120 $ this ->user_url = (is_null ($ this ->user ) ? null : Thingiverse::BASE_URL . "/ $ this ->user /collections " );
113121 $ this ->url = Thingiverse::BASE_API_URL . "/users/ $ this ->user /search/?type=collections&sort=newest " ;
114122 $ cache_id = "thingiverse-press-stream-collections- $ this ->user " ;
115- $ cached_thing = Thingiverse::get_object_from_cache ($ cache_id );
116-
117- if (false === $ cached_thing ){
118- $ obj = Thingiverse::get_authorized_url_json ($ this ->url );
119-
120- foreach ($ obj ->hits as $ key => $ lock )
121- {
122- $ thing = new ThingiverseThing ();
123- $ thing -> initialize_from_json ($ lock );
124- $ thing -> url = $ lock -> absolute_url ;
125- array_push ($ this ->things , $ thing );
126- }
127-
128- Thingiverse::log_message ("Renewing KEY: " .$ cache_id );
129- set_transient ($ cache_id , $ this ->things , Thingiverse::CACHE_TTL_WIDGET );
130- } else {
131- $ this -> things = $ cached_thing ;
132- }
123+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,true );
133124 }
134125
135-
136126 function initialize_stream_favorites () {
137127 $ this ->user_url = (is_null ($ this ->user ) ? null : Thingiverse::BASE_URL . "/ $ this ->user /favorites " );
138128 $ this ->url = Thingiverse::BASE_API_URL . "/users/ $ this ->user /favorites " ;
139129 $ cache_id = "thingiverse-press-stream-favorites- $ this ->user " ;
140- $ cached_thing = Thingiverse::get_object_from_cache ($ cache_id );
141-
142- if (false === $ cached_thing ){
143- $ obj = Thingiverse::get_authorized_url_json ($ this ->url );
144-
145- foreach ($ obj as $ key => $ lock )
146- {
147- $ thing = new ThingiverseThing ();
148- $ thing -> initialize_from_json ($ lock );
149- array_push ($ this ->things , $ thing );
150- }
151-
152- Thingiverse::log_message ("Renewing KEY: " .$ cache_id );
153- set_transient ($ cache_id , $ this ->things , Thingiverse::CACHE_TTL_WIDGET );
154- } else {
155- $ this -> things = $ cached_thing ;
156- }
157- }
158-
159- // Returns a DOM object for the specified URL. Pulls it from the transient
160- // cache if it is available, otherwise fetches it.
161- function get_dom_for_url ($ url ){
162- $ dom = new DomDocument ("1.0 " );
163- // cache key - chop off "http://www.thingiverse.com" and sluggify
164- $ t_key = "thingiverse-stream- " . sanitize_title (substr ($ url ,27 ));
165- $ dom_str = Thingiverse::get_object_from_cache ($ t_key );
166- if (false === $ dom_str ){
167- @$ dom ->load ($ url ); // use @ to suppress parser warnings
168- $ xml_data = $ dom ->saveXML ();
169- set_transient ($ t_key , $ xml_data , Thingiverse::CACHE_TTL_WIDGET );
170- } else {
171- @$ dom ->loadXML ($ dom_str ); // use @ to suppress parser warnings
172- }
173- return $ dom ;
174- }
175-
176- function load_stream_from_rss_url () {
177- $ dom = $ this ->get_dom_for_url ($ this ->url );
178- // FIXME: check for parse error. set some kind of thing status!
179- $ this ->parse_things_from_rss_dom ($ dom );
180- }
181-
182- function load_stream_from_instances_url () {
183- $ dom = $ this ->get_dom_for_url ($ this ->url );
184- // FIXME: check for parse error. set some kind of thing status!
185- $ this ->parse_thing_instances_from_html_dom ($ dom );
186- }
187-
188- function parse_things_from_rss_dom ($ dom ) {
189- $ xp = new DomXpath ($ dom );
190- $ thing_nodes = $ xp ->query ("//item " );
191- foreach ($ thing_nodes as $ thing_node ){
192- $ thing = ThingiverseThing::from_rss_item_dom ($ thing_node );
193- array_push ($ this ->things , $ thing );
194- }
130+ $ this ->initialize_stream_common ($ this ->url ,$ cache_id ,false );
195131 }
196-
197- function parse_thing_instances_from_html_dom ($ dom ) {
198- $ xp = new DomXpath ($ dom );
199- $ thing_nodes = $ xp ->query ("//div[@class= \"instance_float \"] " );
200- foreach ( $ thing_nodes as $ thing_node ) {
201- $ thing = ThingiverseThing::from_html_instance_dom ($ thing_node );
202- array_push ($ this ->things , $ thing );
203- }
204- }
205-
206132}
207133?>
0 commit comments