-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHourlyCronTest.py
executable file
·183 lines (144 loc) · 6.57 KB
/
HourlyCronTest.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
"""
Created on Wed May 11 15:58:25 2016
@author: lmadeo
HourlyCronTest.py
This version of HourlyCron only exists to test the archiver_script to see that
it doesn't break anything.
The simple goal is to let this job run for a while and send mp3s to the Audio4
folder. Let's hope it's easy!!!!
"""
import os
import local
import key
import SpinPapiLib as SPlib
import datetime as DT
from dateutil.relativedelta import *
import calendar
import pprint
from subprocess import call
from contextlib import contextmanager
import sys
import glob
import ftplib
import pysftp
import HourlyCron as HC
# example of sox and call usage:
# http://ymkimit.blogspot.com/2014/07/recording-sound-detecting-silence.html
# option other than subprocess.call -> os.system
#num2day has been modified to align with date.weekday() RTFM
num2day = { 7: 'SaturdayAFTER', -1: 'Sunday' , 0 : 'Monday' ,
1 : 'Tuesday' , 2 :'Wednesday', 3 : 'Thursday' ,
4 : 'Friday' , 5 :'Saturday', 6 : 'Sunday'}
num2dayShort = { 7: 'SatAFTER', -1: 'SunBEFORE' , 0 : 'Mon' ,
1 : 'Tue' , 2 :'Wed', 3 : 'Thu' ,
4 : 'Fri' , 5 :'Sat', 6 : 'Sun'}
day2shortDay = { 'Monday' : 'Mon', 'Tuesday' : 'Tue',
'Wednesday' : 'Wed', 'Thursday': 'Thu', 'Friday': 'Fri',
'Saturday': 'Sat', 'Sunday': 'Sun'}
day2num = {'Monday':0, 'Tuesday':1, 'Wednesday':2, 'Thursday':3,
'Friday':4, 'Saturday':5, 'Sunday':6}
DEBUGGING = False
if __name__ == '__main__':
tab = ' '
pp = pprint.PrettyPrinter(indent=4)
#Now,as defined below, is actually the start of today
Now = DT.datetime.now() + relativedelta(hour=0, minute=0, second=0, microsecond=0)
ThisHour = DT.datetime.now() + relativedelta( minute=0, second=0, microsecond=0)
print '===================================================================='
print 'HOURLYCRON-*test*.py ', str(DT.datetime.now() + relativedelta(microsecond=0))
print '===================================================================='
#print 'ThisHour -> ', str(ThisHour)
startDelta = local.startDelta
endDelta = local.endDelta
startSpinDay = local.startSpinDay
#grab most recentCharlieSched pickle out of designated folder
charlieSched = HC.getCharlieSched()
#pp.pprint( charlieSched )
#add any necessary remote folders
HC.addNewRemoteFolders(charlieSched)
print 'dud85'
#LastHour is one hour ago if EndDelta is greater than zero
LastHour, today = HC.getCurrentTime(endDelta)
#adjust time to Spinitron time
spinDay = HC.day2spinDay(today, LastHour, startSpinDay) #spinDay of *last archivable chunk*
print tab, 'LastHour -> ', LastHour
print tab, 'today -> ', today
print tab, 'spinDay -> ', spinDay
#======================================
# make list of shows to archive
#======================================
if DEBUGGING:
#changing LastHour & spinDay hijacks current time
#showToArchive will be the Euphonic Smorgasbord (as of June 2016)
LasthHour = 12
spinDay = 'Friday'
showsToArchive = HC.getShows2Archive(charlieSched, LastHour, spinDay)
print '==========================================='
print 'showsToArchive ->'
print tab, str(showsToArchive)
print '=========================================='
# if there's going to be something to archive, then open ftp client
if len(showsToArchive) > 0:
#ftp = ftplib.FTP(key.host, key.username, key.passwd)
sftp = pysftp.Connection(host=key.host, username=key.username,
password=key.passwd)
#ftp.connect(host = key.host, port=key.port)
#ftp.cwd(local.remote)
sftp.chdir(local.remoteTesting)
#================================================================
# build mp3 for each show in list
#================================================================
for show in showsToArchive:
# build list of audio archive chunks to concat
chunkList = HC.buildChunkList(show, spinDay)
print "====================="
print "PrettyPrint chunkList:"
pp.pprint(chunkList)
print "====== END: PrettyPrint chunkList ===="
# create correct mp3 for each chunk of show to archive
HC.createAudioChunks(chunkList, local.tmpMp3)
print 'AudioChunks created for: ', str(show)
# sox-concat the audio fles just put into tmpMp3 folder
HC.audioConcat(local.tmpMp3, local.Mp3Staging)
#if audioConcat was successful:
if True: # because success is the only option!
# send "new.mp3" to correct folder on webserver, using ftp
#build target folder name ex: remote file path + "Sun1300"
timeList = show['OnairTime'].split(':')
showStart = ''.join((timeList[0],timeList[1]))
subfolder = ''.join((day2shortDay[spinDay], showStart)) # ex: Sun1300
remoteTargetFolder = ''.join((local.remoteTesting, subfolder))
#ftp.cwd(remoteTargetFolder)
try:
print "remoteTargetFolder -> ",remoteTargetFolder
sftp.cwd(remoteTargetFolder)
except IOError:
sftp.mkdir(remoteTargetFolder)
sftp.cwd(remoteTargetFolder)
os.chdir(local.Mp3Staging)
#ftp magic upload
localMp3 = 'new.mp3'
#myfile = open(localMp3, 'rb')
print 'START: *SFTP* of audioArchive'
#ftp.storbinary('STOR ' + localMp3 , myfile)
sftp.put(localMp3)
#myfile.close()
# Using scp, er, ftp, mv "new.mp3" to "current.mp3"
#ftp.rename(localMp3, 'current.mp3') # not really "local" mp3 anymore ...
sourceMp3 = localMp3 # to increase readability of sftp.rename ...
try: # we will try to remove the target file before we move the sourceMp3 to it ...
sftp.remove('current.mp3')
except:
pass
finally:
sftp.rename(sourceMp3, 'current.mp3') # not really 'local' mp3 anymore ...
print '*SFTP* of audioArchive COMPLETE!!!'
if len(showsToArchive) > 0:
#ftp.close()
sftp.close()
print
print '++++++++++++++++++++++++++++++++++++++++++++++'
print 'END of HourlyCron-*test* -> ', str(DT.datetime.now() + relativedelta(microsecond=0))
print '++++++++++++++++++++++++++++++++++++++++++++++'
print