Created
April 8, 2014 17:51
-
-
Save ptgolden/10162649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import sys | |
from blessings import Terminal | |
from lxml.html import parse | |
RECENTLY_PLAYED_URL = 'http://www.wxyc.info/playlists/recent.html' | |
def format_row(artist, song, release, label): | |
return u'{artist} :: {song} ({release}; {label})'.format(**locals()) | |
def get_recently_played(page, limit=5): | |
trs = page.cssselect('tr[bgcolor="#F3F3F3"]') | |
return [ tuple(td.text_content().strip() for td in tr.getchildren()[1:-1]) | |
for tr in trs[:limit] ] | |
def get_latest_show(page): | |
show_gen = (el.text_content().replace('START OF SHOW:', '').strip() | |
for el in page.cssselect('tr[bgcolor="#444444"]') | |
if el.text_content().strip().startswith('START OF SHOW')) | |
try: | |
show = next(show_gen) | |
except StopIteration: | |
show = '(could not determine show name)' | |
return show | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-n', '--number', type=int, default=5, | |
help='Number of recent tracks to show') | |
parser.add_argument('--raw', action='store_true', | |
help=('Do not format. Instead print tab-separated ' | |
'values for artist, track, album, label')) | |
args = parser.parse_args() | |
page = parse(RECENTLY_PLAYED_URL).getroot() | |
show_title = get_latest_show(page) | |
track_list = get_recently_played(page, args.number) | |
if args.raw: | |
print('\n'.join(['\t'.join(parts) for parts in track_list])) | |
else: | |
t = Terminal() | |
print('\n' + t.blue_bold(show_title)) | |
print(t.blue(u'═' * len(show_title))) | |
print('\n'.join([format_row(*parts) for parts in track_list]) + '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment