Permalink
Browse files

initial version, seemed to work over here - lets see what you think!

  • Loading branch information...
1 parent b78a251 commit 28b1eadf30bd929927e347244c99f301f10a348f @mrimann committed Apr 6, 2012
Showing with 162 additions and 0 deletions.
  1. +26 −0 README.rst
  2. +136 −0 kipptBackup.php
View
@@ -0,0 +1,26 @@
+What's this all about?
+----------------------
+I started playing arount with kippt.com which looks promising as a successor for the beloved del.icio.us which (since acquired by that big company) lost a lot of the power that I liked. And since I already had a del.icio.us to MySQL-Backup script in place, I thought something like that needs to be possible with kippt.com, too.
+
+And here we go: A rough first draft of a kippt.com backup script - maybe a migration tool from del.icio.us to kippt.com will follow.
+
+
+Backing up all your kippt.com bookmarks to MySQL:
+-------------------------------------------------
+
+How to do?
+..........
+Just clone this repository, enter your local database credentials together with your kippt.com username and API-Token in the bottom of the script called "kipptBackup.php" and then run the following code on your shell:
+
+ php -f kipptBackup.php
+
+You can find your kipp.com API-Token by visiting the following "page" with your browser while being logged in: https://kippt.com/api/account
+
+Feedback?
+---------
+Please yes - just send it to mario@rimann.org and let's see what I can make out of it. Of course nice "thank you" mails are appreciated most :-)
+
+
+Found a bug or want an additional feature?
+------------------------------------------
+Don't worry, just open an issue on GitHub (https://github.com/mrimann/kippt-tools/issues) or even better: Fix it and contribute it back to the project. You can fork the sourcecode from the Git repository and send me a pull request as soon as you've finished (I have to find out how to work with this first, you might be the first contributor)
View
@@ -0,0 +1,136 @@
+<?php
+/**
+ */
+
+
+class kipptBackup {
+ var $userName;
+ var $apiToken;
+
+ var $dbName;
+ var $dbUser;
+ var $dbPass;
+
+ public function setUsername($userName) {
+ $this->userName = $userName;
+ }
+
+ public function setApiToken($apiToken) {
+ $this->apiToken = $apiToken;
+ }
+
+ public function setDatabaseCredentials($dbName, $dbUser, $dbPass) {
+ $this->dbName = $dbName;
+ $this->dbUser = $dbUser;
+ $this->dbPass = $dbPass;
+ }
+
+ public function createBackup() {
+ $this->connectToDatabase();
+
+ echo 'Trying with kipp.com User "' . $this->userName . '" and API-Token "' . $this->apiToken . '"';
+ echo "\n\n";
+
+
+ $ch = curl_init('https://kippt.com/api/clips/');
+
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ curl_setopt(
+ $ch,
+ CURLOPT_HTTPHEADER,
+ array(
+ 'X-Kippt-Username: ' . $this->userName,
+ 'X-Kippt-API-Token: ' . $this->apiToken
+ )
+ );
+
+ $apiResult = curl_exec($ch);
+ curl_close($ch);
+
+ print_r($apiResult);
+
+ // decode the JSON data
+ $apiData = json_decode($apiResult);
+ print_r($apiData);
+
+ foreach ($apiData->objects as $clip) {
+ $this->compareOrImportClip($clip);
+ }
+
+
+ }
+
+ private function compareOrImportClip($clip) {
+ echo 'checking clip with ID=' . $clip->id;
+
+ $dbResult = mysql_query('SELECT id,updated FROM clips WHERE id=' . $clip->id);
+ if (mysql_num_rows($dbResult) == 1) {
+ echo "\n" . 'Clip exists already';
+
+ $row = mysql_fetch_assoc($dbResult);
+ if ($row['updated'] < $clip->updated) {
+ echo "\n" . 'Clip exists, but seems to have changed, trying to update...';
+ $this->updateExistingClip($clip);
+ }
+
+ } else {
+ echo "\n" . 'New clip, inserting';
+ $this->insertNewClip($clip);
+ }
+
+ echo "\n\n";
+
+ }
+
+ private function updateExistingClip($clip) {
+ mysql_query('DELETE from clips WHERE id=' . $clip->id . ' LIMIT 1;');
+
+ $this->insertNewClip($clip);
+ }
+
+ private function insertNewClip($clip) {
+ $importResult = mysql_query('INSERT into clips SET id="' . $clip->id .
+ '", title="' . $clip->title .'"' .
+ ', url="' . $clip->url .'"' .
+ ', list="' . $clip->list .'"' .
+ ', notes="' . $clip->notes .'"' .
+ ', is_starred="' . (int)$clip->is_starred .'"' .
+ ', url_domain="' . $clip->url_domain .'"' .
+ ', created="' . $clip->created .'"' .
+ ', updated="' . $clip->updated .'"' .
+ ', resource_uri="' . $clip->resource_uri .'"'
+ );
+
+ if (!$importResult) {
+ echo 'INSERT error: ' . mysql_error() . "\n\n";
+ }
+ }
+
+ private function connectToDatabase() {
+ mysql_connect(
+ 'localhost',
+ $this->dbUser,
+ $this->dbPass
+ );
+ mysql_select_db($this->dbName);
+ mysql_query('SET NAMES utf8;');
+ }
+}
+
+
+$backup = new kipptBackup();
+
+// modify to suit your needs
+$backup->setUsername('johndoe');
+$backup->setApiToken('123456abcdef');
+$backup->setDatabaseCredentials(
+ 'name of your local database',
+ 'database username',
+ 'database password'
+);
+
+$backup->createBackup();
+
+
+?>

0 comments on commit 28b1ead

Please sign in to comment.