/
utils.py
53 lines (40 loc) · 1.25 KB
/
utils.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
41
42
43
44
45
46
47
48
49
50
51
52
53
#soundcloud utilities
import soundcloud
import oauth_me
import re
import os
SOURCE_PATH = os.path.expanduser('~/Music/')
VALID_SUFFIX = '.mp3';
class SoundCloud:
def __init__( self ):
self.client = oauth_me.get_auth()
def upload_track( self, newTrack ):
track = self.client.post('/tracks', track={
'title': get_title(newTrack),
'sharing': 'private',
'asset_data': open(os.path.expanduser( newTrack ), 'rb')
})
print track.title + ' has been posted!'
print 'URL: ' + track.permalink_url + '/' + track.secret_token
sc = SoundCloud()
#fsevents class wrapper
from fsevents import Observer, Stream
class Monitor:
def __init__( self ):
self.obs = Observer()
self.stream = Stream ( self.callback, SOURCE_PATH, file_events=True )
def callback( self, event ): #file event callback function
if event.name.endswith( VALID_SUFFIX ) and os.path.isfile( event.name ):
sc.upload_track ( event.name )
def start( self ):
print "Starting monitor..."
self.obs.start()
self.obs.schedule( self.stream )
def stop( self ):
print "Stopping monitor..."
self.obs.unschedule( self.stream )
self.obs.stop()
monitor = Monitor()
def get_title( newTrack ):
exp = re.compile( '[\w-]*\.' )
return exp.search( newTrack ).group().strip('.')