Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created December 19, 2010 14:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lindenb/747381 to your computer and use it in GitHub Desktop.
Save lindenb/747381 to your computer and use it in GitHub Desktop.
Save bookmarks from delicious to Diigo.com using the Diigo API.
/**
* Delicious2Diigo
*
* Author:
* Pierre Lindenbaum
* http://plindenbaum.blogspot.com
*
* Motivation:
* save bookmarks from delicious to diigo using the Diigo API.
*
*
* Usage:
* javac Delicious2Diigo
*
* curl -s "https://deliciouslogin:deliciouspassword@api.del.icio.us/v1/posts/all" |\
* java Delicious2Diigo | tac |\
* sed -e 's/__LOGIN__/mydiigologin/' -e 's/__PASSWORD__/mydiigopassword/' > file.sh
* sh file.sh
*
*/
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;
public class Delicious2Diigo
{
private void run(InputStream in) throws Exception
{
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
XMLEventReader reader= xmlInputFactory.createXMLEventReader(in);
while(reader.hasNext())
{
XMLEvent evt=reader.nextEvent();
if(!evt.isStartElement()) continue;
if(!evt.asStartElement().getName().getLocalPart().equals("post")) continue;
Attribute hrefAtt=evt.asStartElement().getAttributeByName(new QName("href"));
Attribute tagAtt=evt.asStartElement().getAttributeByName(new QName("tag"));
Attribute descriptionAtt=evt.asStartElement().getAttributeByName(new QName("description"));
Attribute extendedAtt=evt.asStartElement().getAttributeByName(new QName("extended"));
Attribute sharedAtt=evt.asStartElement().getAttributeByName(new QName("shared"));
if(descriptionAtt==null) descriptionAtt=hrefAtt;
if(extendedAtt==null ) extendedAtt=descriptionAtt;
StringBuilder url=new StringBuilder();
url.append("url=");
url.append(URLEncoder.encode(hrefAtt.getValue(), "UTF-8"));
url.append("&title=");
if(descriptionAtt!=null)
{
url.append(URLEncoder.encode(descriptionAtt.getValue(), "UTF-8"));
}
else
{
url.append(URLEncoder.encode(hrefAtt.getValue(), "UTF-8"));
}
if(sharedAtt!=null && sharedAtt.getValue().equals("no"))
{
url.append("&shared=no");
}
else
{
url.append("&shared=yes");
}
url.append("&desc=");
if(extendedAtt!=null)
{
url.append(URLEncoder.encode(extendedAtt.getValue(), "UTF-8"));
}
else if(descriptionAtt!=null)
{
url.append(URLEncoder.encode(descriptionAtt.getValue(), "UTF-8"));
}
url.append("&tags=");
if(tagAtt!=null)
{
url.append(URLEncoder.encode(tagAtt.getValue(), "UTF-8"));
}
System.out.print("sleep 10; ");
System.out.println("curl -d \""+ url+"\" -u \"__LOGIN__:__PASSWORD__\" \"https://secure.diigo.com/api/v2/bookmarks\" ; echo ;");
}
reader.close();
}
public static void main(String[] args)
{
try
{
Delicious2Diigo app=new Delicious2Diigo();
int optind=0;
if(optind==args.length)
{
app.run(System.in);
}
else
{
while(optind< args.length)
{
java.io.FileInputStream r= new FileInputStream(args[optind++]);
app.run(r);
r.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment