O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


BLOGGING HACK

ljmusic.py
Get history information (including a histogram) of Aritsts in your Livejournal posts

Contributed by:
Unknown User anonymous2
[06/04/04 | Discuss (0) | Link to this hack]

#!/usr/bin/python
#
# Script for generating a list and histogram of artists appearing in a
# user's livejournal music selections.  Assumes Current Music is stored
# in the format Song - Artist.  Requires Python 2.3 and Livejournal
# Python module.
#
# Copyright (C) 2004 Tague Griffith <tague@minion.net>.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL SOFTWARE IN THE PUBLIC INTEREST, INC. BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


import getpass
import optparse
import re
import sys

from livejournal import LiveJournal

histogram = {}
usage = "usage: %prog [-u username] [-g]"
music_re = re.compile("(?P<song>.+)-(?P<artist>.+)")

parser = optparse.OptionParser(usage=usage)
parser.add_option("-u", "--user", type="string", dest="username",
                  help="livejournal username", default=getpass.getuser())
parser.add_option("-g", "--histogram", action="store_true", dest="hist",
                  help="generate histogram", default=False)
(options, args) = parser.parse_args()

try:
    lj = LiveJournal("1")
    lj.login(options.username,getpass.getpass())

    events = lj.getevents_last(20)
    while len(events) > 0:
        for event in events:
            if event.props.has_key('current_music'):
                music = event.props['current_music']
                print "%s: %s" % (event.eventtime, music)
                m = music_re.match(music)
                if m:
                    song = m.group("song").strip()
                    artist = m.group("artist").strip()
                    if histogram.has_key(artist):
                        songs = histogram[artist]
                        songs.append(song)
                    else:
                        histogram[artist] = [song]
        events = lj.getevents_last(20,events[len(events)-1].eventtime)

    if options.hist:
        artists = histogram.keys()
        artists.sort(lambda x,y: -cmp(len(histogram[x]),len(histogram[y])))
        for artist in artists:
            s =  '%s: ' % artist
            for i in range(len(histogram[artist])):
                s = s+'*'
            s = s+"(%d)" % len(histogram[artist])
            print s
except:
    print "Problem commicating with the LiveJournal server"
    sys.exit(-1)

See also: http://www.minion.net/marginal_hacks/ljmusic.py


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.