forked from box/box-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_player.py
40 lines (31 loc) · 1.16 KB
/
music_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding: utf-8
from __future__ import unicode_literals
from random import shuffle
import subprocess
import tempfile
from boxsdk.client import Client
from demo.auth import authenticate
class MusicPlayer(object):
def __init__(self, folder_path):
self._folder_path = folder_path
self._client = self._get_client()
self._mp3_files = self._get_all_mp3_files(self._client)
shuffle(self._mp3_files)
def _get_client(self):
oauth, _, _ = self._authenticate()
return Client(oauth)
def _authenticate(self):
return authenticate()
def _get_all_mp3_files(self, client):
# music_folder = client.folder(folder_id='0').get_subfolder('music')
return client.search(query='*.mp3', limit=100, offset=0, file_extensions=['mp3'])
def play(self):
for item in self._mp3_files:
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(item.content())
item_with_name = item.get()
print item_with_name.name
subprocess.check_call(['afplay', temp_file.name])
if __name__ == '__main__':
music_player = MusicPlayer('music')
music_player.play()