most_recent.py 881 Bytes
Newer Older
1 2
#!/usr/bin/python

3 4 5 6 7
from datetime import datetime
from optparse import OptionParser
from urllib2 import urlopen

from BeautifulSoup import BeautifulSoup
8
from gobble import get_parser
9 10 11


if __name__ == '__main__':
12 13 14
    usage = "%prog [-s <WEB SERVER>] <USERNAME>"
    parser = get_parser(usage=usage)
    parser.set_defaults(server='alpha.libre.fm')
15
    opts,args = parser.parse_args()
16
    if len(args) != 1:
17 18
        parser.error("All arguments are required.")

19 20
    username, = args
    server = opts.server
21 22 23 24 25 26 27
    if server[:7] != "http://":
        server = "http://%s" % (server,)
    soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
                                    % (server, username)))
    gobbles_list = soup.find('ul', {'class': 'gobbles'})
    timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
    print datetime.fromtimestamp(float(timestamp))