use appdirs for cache

This commit is contained in:
Matt Behrens 2018-07-20 12:06:11 -04:00
parent 94f0c496d4
commit fc98eac239
3 changed files with 11 additions and 7 deletions

View file

@ -117,4 +117,4 @@ def stream(instance):
clientcred = ensure_clientcred(instance) clientcred = ensure_clientcred(instance)
usercred = ensure_usercred(instance) usercred = ensure_usercred(instance)
mastodon.stream(instance, clientcred, usercred) mastodon.stream(instance, clientcred, usercred, cache_dir=dirs.user_cache_dir)

View file

@ -59,11 +59,11 @@ def login(client, grant_code, usercred):
client.log_in(code=grant_code, scopes=['read'], to_file=usercred) client.log_in(code=grant_code, scopes=['read'], to_file=usercred)
umask(saved_umask) umask(saved_umask)
def stream(instance, clientcred, usercred): def stream(instance, clientcred, usercred, cache_dir='.'):
'''Stream statuses and add them to a queue.''' '''Stream statuses and add them to a queue.'''
client = build_client(instance, clientcred, usercred) client = build_client(instance, clientcred, usercred)
listener = StreamListener(Queue()) listener = StreamListener(Queue(cache_dir))
click.echo('==> Streaming from {}'.format(instance)) click.echo('==> Streaming from {}'.format(instance))
client.stream_user(listener) client.stream_user(listener)

View file

@ -1,11 +1,12 @@
'''The play queue.''' '''The play queue.'''
from os import path
import shlex import shlex
from subprocess import run from subprocess import run
from threading import Thread, Lock from threading import Thread, Lock
import click import click
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL, utils
import fediplay.env as env import fediplay.env as env
@ -15,15 +16,16 @@ class Queue(object):
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
def __init__(self): def __init__(self, cache_dir):
self.lock = Lock() self.lock = Lock()
self.playing = False self.playing = False
self.queue = [] self.queue = []
self.cache_dir = cache_dir
def add(self, url): def add(self, url):
'''Fetches the url and adds the resulting audio to the play queue.''' '''Fetches the url and adds the resulting audio to the play queue.'''
filename = Getter().get(url) filename = Getter(self.cache_dir).get(url)
with self.lock: with self.lock:
self.queue.append(filename) self.queue.append(filename)
@ -54,8 +56,9 @@ class Getter(object):
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
def __init__(self): def __init__(self, cache_dir):
self.filename = None self.filename = None
self.cache_dir = cache_dir
def _progress_hook(self, progress): def _progress_hook(self, progress):
if progress['status'] == 'finished': if progress['status'] == 'finished':
@ -67,6 +70,7 @@ class Getter(object):
options = { options = {
'format': 'mp3/mp4', 'format': 'mp3/mp4',
'nocheckcertificate': env.no_check_certificate(), 'nocheckcertificate': env.no_check_certificate(),
'outtmpl': path.join(self.cache_dir, utils.DEFAULT_OUTTMPL),
'progress_hooks': [self._progress_hook] 'progress_hooks': [self._progress_hook]
} }
with YoutubeDL(options) as downloader: with YoutubeDL(options) as downloader: