<commit_before>#! /usr/bin/env python3

from configparser import ConfigParser
from datetime import datetime
import subprocess
import readline

import requests
from bs4 import BeautifulSoup


_STREAMS = 'http://music.cbc.ca/streams.aspx'


def get_playlists():
    r = requests.get(_STREAMS)
    r.raise_for_status()
    s = BeautifulSoup(r.text, 'lxml')
    return {li.select_one('a.playStream').text:
            li.select_one('.streamUrl > a[href]')['href']
            for li
            in s.select('ul.streams > li')}


def new_completer(playlist):
    def completer(text, state):
        nonlocal playlist
        options = [playlist for playlist in playlists if text in playlist]
        if state < len(options):
            return options[state]
        else:
            return None
    return completer


def play(playlist_url, tee=False):
    r = requests.get(playlist_url)
    r.raise_for_status()

    # Play first working stream
    p = ConfigParser()
    p.read_string(r.text)
    for key, value in p['playlist'].items():
        if (key.startswith('file') and
            subprocess.call(['ffmpeg',
                             '-nostdin',
                             '-i', 'async:' + value] +
                            # Yes, check by identity
                            ([] if not tee else
                             ['-f', 'mp3',
                              '-c', 'copy',
                              './' + datetime.now().replace(microsecond=0).isoformat() + '.mp3']) +
                             ['-f', 'alsa',
                              'default']) == 0):
            break


if __name__ == '__main__':
    from argparse import ArgumentParser
    from sys import exit

    ap = ArgumentParser('Play/download CBC Radio playlists')
    ap.add_argument('-l', '--list', action='store_true')
    ap.add_argument('-t', '--tee', action='store_true')
    ap.add_argument('playlist', nargs='?')
    args = ap.parse_args()

    playlists = get_playlists()

    if args.list:
        print(*sorted(playlists), sep='\n')
        exit()

    if args.playlist is None:
        print(*sorted(playlists), sep='\n')
        readline.parse_and_bind('tab: complete')
        readline.set_completer(new_completer(playlists))
        playlist_url = playlists[input('Playlist: ')]<commit_msg>CBCRadio.py: Crude hack to keep same container format.<commit_after>#! /usr/bin/env python3

from configparser import ConfigParser
from datetime import datetime
import subprocess
import readline

import requests
from bs4 import BeautifulSoup


_STREAMS = 'http://music.cbc.ca/streams.aspx'


def get_playlists():
    r = requests.get(_STREAMS)
    r.raise_for_status()
    s = BeautifulSoup(r.text, 'lxml')
    return {li.select_one('a.playStream').text:
            li.select_one('.streamUrl > a[href]')['href']
            for li
            in s.select('ul.streams > li')}


def new_completer(playlist):
    def completer(text, state):
        nonlocal playlist
        options = [playlist for playlist in playlists if text in playlist]
        if state < len(options):
            return options[state]
        else:
            return None
    return completer


def play(playlist_url, tee=False):
    r = requests.get(playlist_url)
    r.raise_for_status()

    # Play first working stream
    p = ConfigParser()
    p.read_string(r.text)
    for key, value in p['playlist'].items():
        if (key.startswith('file') and
            subprocess.call(['ffmpeg',
                             '-nostdin',
                             '-i', 'async:' + value] +
                            # Yes, check by identity
                            ([] if not tee else
                             ['-f', 'mpegts',
                              '-c', 'copy',
                              './' + datetime.now().replace(microsecond=0).isoformat() + '.m2ts']) +
                             ['-f', 'alsa',
                              'default']) == 0):
            break


if __name__ == '__main__':
    from argparse import ArgumentParser
    from sys import exit

    ap = ArgumentParser('Play/download CBC Radio playlists')
    ap.add_argument('-l', '--list', action='store_true')
    ap.add_argument('-t', '--tee', action='store_true')
    ap.add_argument('playlist', nargs='?')
    args = ap.parse_args()

    playlists = get_playlists()

    if args.list:
        print(*sorted(playlists), sep='\n')
        exit()

    if args.playlist is None:
        print(*sorted(playlists), sep='\n')
        readline.parse_and_bind('tab: complete')
        readline.set_completer(new_completer(playlists))
        playlist_url = playlists[input('Playlist: ')]
{'fraction_non_alphanumeric': 0.12862357741035002, 'fraction_numerical': 0.002362035645265192, 'mean_word_length': 1.993573264781491, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6380947', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1372, 'n_words': 319}