Source

scripts / bookmark.py

#!/usr/bin/env python2
"""
Delicious CLI
=============

A simple script to easily create bookmarks in Delicious via command line.

It is recommended to put a wrapper like this to ``~/bin/b``::

    #!/bin/sh
    python2 ~/src/scripts/bookmark.py add my-user-name $@

The usage will be as follows::

    $ b google.com -t search,bigbrother
    Password:
    Added "Google" with tags "search,bigbrother".

Note that you don't have to specify the title. However, if you need to do it
(e.g. in case if the script fails to determine it), you can use the ``--title``
argument.
"""
from argh import arg, dispatch_command
import requests
from BeautifulSoup import BeautifulSoup
from pydelicious import DeliciousAPI
from getpass import getpass


@dispatch_command
@arg('username')
@arg('url')
@arg('--title')
@arg('-t', '--tags')
@arg('-d', '--description')
@arg('--private', default=False)
def add_bookmark(args):
    url = args.url
    if not url.startswith('http://'):
        url = 'http://' + url

    if args.title:
        title = args.title
    else:
        resp = requests.get(url)
        soup = BeautifulSoup(resp.content)
        title = soup.find('title').text.strip()

    tags = args.tags or 'unsorted'

    api = DeliciousAPI(args.username, getpass('Password:'))
    api.posts_add(url, title, tags=tags, extended=args.description,
                  shared=not args.private)

    print u'Added "{0}" with tags "{1}".'.format(title, tags)