function fetch_stories($storyIDs, $mysqlStoryTable) { //the mysql_fetch_array keeps track of stories fetch so that I didn't have to make another variable to do that // now I do //get all stories in database //while still stories //if (numstories < 100) fetch numstories; else fetch 100 //get stories and load to array //get story data //for each story //save into database //$maximums = array( 'link' => 0 , 'href' => 0, 'title' => 0, 'description' => 0, 'tnsrc' => 0); try { $api = Services_Digg::factory('Stories'); /*$countStories = 0; //storyid counter while ($row = mysql_fetch_array($storyIDs, MYSQL_NUM)) { $storyIDs[$countStories] = $row[0]; //save ids into array $countStories++; } if ($numStories != $countStories) print "not matching counts";*/ $countStories = $numStories = count($storyIDs); $savedStories = 0; print "Saved ... "; while ($countStories > 0) { if ($countStories < $numStories) { sleep(2); } $fetchCount = $countStories < 100 ? $countStories : 100; //100 max from api //print $countStories; //extract /*$subStoryIDs = array (); //empty array for ($i = 0; $i < $fetchCount; $i++) { $row = mysql_fetch_array($storyIDs, MYSQL_NUM); $subStoryIDs[$i] = $row[0]; //extract to second array }*/ $subStoryIDs = array_slice($storyIDs, $numStories - $countStories, $fetchCount); //print_r($subStoryIDs); //print "\n" . count($subStoryIDs); //now storyids should have enough story ids //call api $params = array('count' => 100, 'sort' => 'submit_date-asc'); $stories = $api->getStoriesById($subStoryIDs, $params); //print $stories->total . " " . $stories->count . " " . $stories->offset ; if ($fetchCount != $stories->total) { print "Fetch did not match total\n"; } $x = 0; foreach ($stories as $story) { //$maximums = update_maximums($maximums, $story); //save each story $query = format_insert_story_query($story, $mysqlStoryTable); $status = mysql_query($query); if ($status) { $x++; //save story counter } else { //One problem was that I did not add slashes to quotes in link, title, description print "\n--- Did not save " . $story->title . "\n"; //var_dump($story); print $query . "\n"; //print $story->status . "\n"; } } $savedStories += $x; //print "Did not save " . (100-$x) . " stories\n"; print $savedStories . " ... "; $countStories -= $fetchCount; //decrement counter } } catch (PEAR_Exception $error) { echo $error->getMessage() . "\n"; } catch (Services_Digg_Exception $error) { echo $error . '\\n'; } //var_dump($maximums); return $savedStories; }
<?php $path = realpath('./../'); ini_set('include_path', $path . ':' . ini_get('include_path')); require_once 'Services/Digg.php'; Services_Digg::$appKey = 'http://www.example.com/Services_Digg_Proxy.php'; Services_Digg::$uri = 'http://services.digg.com';