most_recent.py 881 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/python
  2. from datetime import datetime
  3. from optparse import OptionParser
  4. from urllib2 import urlopen
  5. from BeautifulSoup import BeautifulSoup
  6. from gobble import get_parser
  7. if __name__ == '__main__':
  8. usage = "%prog [-s <WEB SERVER>] <USERNAME>"
  9. parser = get_parser(usage=usage)
  10. parser.set_defaults(server='alpha.libre.fm')
  11. opts,args = parser.parse_args()
  12. if len(args) != 1:
  13. parser.error("All arguments are required.")
  14. username, = args
  15. server = opts.server
  16. if server[:7] != "http://":
  17. server = "http://%s" % (server,)
  18. soup = BeautifulSoup(urlopen('%s/user/%s/recent-tracks'
  19. % (server, username)))
  20. gobbles_list = soup.find('ul', {'class': 'gobbles'})
  21. timestamp = gobbles_list.find('li')['about'].split('#')[1].split('.')[0]
  22. print datetime.fromtimestamp(float(timestamp))