Digicted

Ruby: backups maken van je del.icio.us bookmarks

Net las ik “Backup delicious to gmail” op Foobr. Aaron Bassett beschrijft hier een handige manier om een backup te maken van je del.icio.us-bookmarks door de XML-file te emailen naar je GMail-account door middel van een PHP-script.

Dit is naar mijn mening een prima manier om een backup te maken van je del.icio.us-bookmarks, maar ik gebruik liever Ruby (al was het maar omdat ik het taaltje nog steeds een beetje aan het leren ben, maar geweldig vind voor dergelijke problemen) en ik wil het niet in GMail hebben maar op de lokale harddisk. Eventueel een backup op deze website.

Eerst heb ik even gespiekt in zijn code en toen ben ik zelf begonnen.

Eerst maar even de hele code:

#!/usr/bin/env ruby
require 'net/https'

delicous_username = ""
delicous_password = ""
recent = true #only fetch recent posts? useful for debugging
xmlfile = "/fullpath/to/xmlfile"

https = Net::HTTP.new( 'api.del.icio.us', 443 )
https.use_ssl = true

https.start do |http|
  req = Net::HTTP::Get.new('/v1/posts/' + ( recent ? "recent" : "all" ) )
  req.basic_auth delicous_username, delicous_password
  xmlData = ""
  insertXslLine = true

  https.request( req ).read_body.each do |line|
    xmlData += line
    # add the reference to the xsl-file for some nice HTML-formatting
    xmlData += "<?xml-stylesheet type=\"text/xsl\" href=\"delicious.xsl\"?>\\n" if insertXslLine
    insertXslLine = false
  end
  File.new( xmlfile, "w+" ).write( xmlData )
end

De eerste paar regels zijn vooral voor de configuratie. Vul hier je del.icio.us-gebruikersnaam en -wachtwoord in, of je alleen de meest recente bookmarks wilt (zodat je niet de hele XML-file hoeft te fetchen) en de locatie voor het XML-bestand.

Dan maken we verbinding met api.del.icio.us via HTTPS en authen met onze gebruikersnaam en wachtwoord van de service. De rest is op zich best “simpel”:

  1. XML-file binnenhalen, regel voor regel;
  2. Een verwijzing naar het XSL-bestand invoegen op de 2e regel (XSL-bestand zullen we zo zien en is optioneel);
  3. De data van de XML-file met XSL-verwijzing wegschrijven naar het gekozen bestand.

Als we dat doen hebben we een kale XML-file, dus die gaan we iets mooier maken met een beetje XSLT zodat er iets uitkomt wat voor HTML door moet gaan.

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  <xsl:template match="/">
    <html>
      <head>
        <title>del.icio.us bookmarks backup</title>
      </head>
      <body>
        <h2>del.icio.us bookmarks of <xsl:value-of select="posts/@user" /></h2>
        <ol>
          <xsl:for-each select="posts/post">
            <li>
              <a>
                <xsl:attribute name="href">
                  <xsl:value-of select="@href"></xsl:value-of>
                </xsl:attribute>
                <xsl:value-of select="@description"></xsl:value-of>
              </a>
            </li>
          </xsl:for-each>
        </ol>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

De output hiervan zo iets moeten zijn als:

<html>
  <head>
    <title>del.icio.us bookmarks backup</title>
  </head>
  <body>
    <h2>del.icio.us bookmarks of martijnengler</h2>
    <ol>
      <li><a href="http://pinderkent.blogsavy.com/archives/88">Sometimes it's best to leave old software systems alone.</a></li>
      <li><a href="http://www.codinghorror.com/blog/archives/000920.html">Coding Horror: Google's Number One UI Mistake</a></li>
      <li><a href="http://blog.angrypets.com/2007/07/the-case-for-ra.html">The Case for Desktop RAID 0 - Debunking the Myths</a></li>
      <li><a href="http://www.deadharddrive.com/">My dead hard drive story</a></li>
      <li><a href="http://weblog.oomph.nl/2007/07/blogger-typepad.html">Blogger, Typepad en Wordpress. Welk platform kies je voor je weblog?</a></li>
      <li><a href="http://lifehacker.com/software/how-to/turn-mailing-lists-into-an-rss-feed-283353.php">How To: Turn mailing lists into an RSS feed - Lifehacker</a></li>
      <li><a href="http://www.silverware.nl/">Silverware</a></li>
      <li><a href="http://www.youtube.com/watch?v=Xo-rm95ZBcg">Plain White T's - Hey There Delilah</a></li>
      <li><a href="http://lifehacker.com/software/how-to/hide-files-inside-of-jpeg-images-282119.php">How To: Hide files inside of JPEG images - Lifehacker</a></li>
      <li><a href="http://www.quietbay.net/Science/astronomy/nightsky/">Learn: Identify constellations, stars, planets and how to navigate at night</a></li>
      <li><a href="http://weblogs.sqlteam.com/jeffs/archive/2006/02/10/9002.aspx">Data belongs in your tables -- not in your code</a></li>
      <li><a href="http://www.ibm.com/developerworks/web/library/wa-ltwebserv/">Lightweight Web servers</a></li>
      <li><a href="http://www.dynamicajax.com/fr/AJAX_Suggest_Tutorial-271_290_312.html">Creating a Google AJAX Suggest Like Website Search</a></li>
      <li><a href="http://www.codeandcoffee.com/2007/06/27/how-to-make-a-password-strength-meter-like-google/">How to Make a Password Strength Meter Like Google - Code and Coffee</a></li>
      <li><a href="http://www.wikihow.com/Share-Your-Obituary-With-Your-Online-Friends">How to Share Your Obituary With Your Online Friends - wikiHow</a></li>
    </ol>
  </body>
</html>

2 reacties to “Ruby: backups maken van je del.icio.us bookmarks”

  1. Bram.us » Back up your del.icio.us bookmarks (PHP/Ruby):

    [...] script which takes a backup of your del.icio.us bookmarks and mails it to you. Martijn then created a likewise script, but then in Ruby. Enjoy! Spread the [...]

  2. CedEtevaGeads:

    Hey,
    I am, Edward
    verry cool site
    my site:

    http://gXXBEOOv.spaces.live.com/

Reageer