-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.py
More file actions
executable file
·317 lines (251 loc) · 11.2 KB
/
uploader.py
File metadata and controls
executable file
·317 lines (251 loc) · 11.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import vimeo
import asyncio
import os
import shutil
import sys
import time
import datetime
import re
import subprocess
from uuid import uuid4
import logging
# Import smtplib for the actual sending function
import smtplib
import yaml
# Import the email modules we'll need
from email.message import EmailMessage
# Need to change this to point to conf file. e.g /etc/uploader.conf
SETTINGS_FILE = './uploader.conf'
TEST_MODE = False
# Custom exception/error classes
class SettingsFileNotFound(Exception):
pass
class CannotConvertVideoFile(Exception):
pass
class CannotUploadVideoFile(Exception):
pass
class InvalidCredentialsError(Exception):
pass
class Uploader:
def __init__(self, settings_file):
logging.info('Initialising from settings: ' + settings_file)
if not os.path.exists(settings_file):
raise SettingsFileNotFound()
with open(settings_file, 'rt') as y:
settings = yaml.load(y)
self.CLIENT_ID = settings.get('client_id', '')
self.CLIENT_SECRET = settings.get('client_secret', '')
self.CLIENT_ACCESS_TOKEN = settings.get('client_access_token', '')
self.VIDEO_PASSWORD = settings.get('video_password', '')
self.EMAIL_USER = settings.get('email_user', '')
self.EMAIL_PASSWORD = settings.get('email_password', '')
self.REPORT_EMAIL = settings.get('email_address', '')
self.EMAIL_ORIGIN = settings.get('email_origin', '')
self.SMTP_SERVER = settings.get('smtp_server', '')
self.SMTP_PORT = settings.get('smtp_port', 0)
self.ENCODER = settings.get('encoder', '')
self.OUTPUT_EXT = settings.get('output_ext', '')
self.PRESET = settings.get('preset', '')
self.MONITOR_FOLDER = settings.get('monitor_folder', '')
self.PROCESSED = settings.get('processed_folder', '')
self.UPLOADED = settings.get('uploaded_folder', '')
self.ORIGINALS = settings.get('originals_folder', '')
# Number of seconds to wait before processing the file.
# This is a simple check to determine if the we are done recording.
# A video file modified within less that a second means that it is currently being edited/recorded
self.UPLOAD_DELAY = settings.get('upload_delay', 5)
self.SLEEP_DELAY = settings.get('sleep_delay', 1)
self.v = vimeo.VimeoClient(
token=self.CLIENT_ACCESS_TOKEN,
key=self.CLIENT_ID,
secret=self.CLIENT_SECRET
)
os.makedirs(self.PROCESSED, exist_ok=True)
os.makedirs(self.UPLOADED, exist_ok=True)
os.makedirs(self.ORIGINALS, exist_ok=True)
def __get_encoder_command(self, input_file, output_file):
return [
a.replace(
'{input_file}', input_file).replace(
'{output_file}', output_file).replace(
'{preset}', self.PRESET) for a in self.ENCODER.split(' ') if a and a != '']
def __check(self):
if self.CLIENT_ACCESS_TOKEN == '' or \
self.CLIENT_SECRET == '' or \
self.CLIENT_ID == '':
raise InvalidCredentialsError()
async def copy_file(self, source, destination):
try:
# If the file is present in the destination,
# Delete it first
if os.path.exists(destination):
logging.info(
'Before copying, we must remove file {}'.format(destination))
os.remove(destination)
logging.info('Copying file from {} to {}'.format(
source, destination))
shutil.copyfile(source, destination)
return destination
except:
pass
async def move_file(self, source, destination):
try:
# If the file is present in the destination,
# Delete it first
if os.path.exists(destination):
logging.info(
'Before moving, we must remove file {}'.format(destination))
os.remove(destination)
logging.info('Moving file from {} to {}'.format(
source, destination))
os.rename(source, destination)
return destination
except:
pass
def __can_email(self):
return self.EMAIL_PASSWORD and self.EMAIL_USER and \
self.REPORT_EMAIL and self.SMTP_SERVER and \
self.SMTP_PORT > 0 and self.EMAIL_ORIGIN
async def send_email(self, subject, message):
if TEST_MODE:
logging.info("Email sent: {}".format(message))
return
if not self.__can_email():
logging.warning('Cannot send email. No email credentials found.')
return
try:
logging.info('Sending email message \"{}\" to \"{}\"'.format(
message, self.REPORT_EMAIL))
msg = EmailMessage()
msg.set_content(message)
msg['subject'] = subject
msg['From'] = self.EMAIL_ORIGIN
msg['To'] = self.REPORT_EMAIL
# Send the message via our own SMTP server.
s = smtplib.SMTP_SSL(self.SMTP_SERVER, self.SMTP_PORT)
s.ehlo()
s.login(self.EMAIL_USER, self.EMAIL_PASSWORD)
s.send_message(msg)
s.quit()
except Exception as ex:
logging.error('Unable to send email. ' + str(ex))
async def convert(self, source_video):
logging.info('Converting video: ' + source_video)
basename = os.path.basename(source_video)
fname, ext = os.path.splitext(basename)
destination_if_success = os.path.join(
self.PROCESSED, fname + self.OUTPUT_EXT)
# remove existing file in the destination
if os.path.exists(destination_if_success):
logging.info('Removing file {}'.format(destination_if_success))
os.remove(destination_if_success)
encoder_command = self.__get_encoder_command(
source_video, destination_if_success)
logging.info('Running encoder {}'.format(encoder_command))
completed_proc = subprocess.run(encoder_command)
# Do convert, if success
# return await self.move_file(source_video, destination_if_success)
# Else
# If the file is not in the destination,
# it means it was not converted
if not os.path.exists(destination_if_success):
logging.error('Cannot convert file: ' + source_video)
else:
return destination_if_success
async def upload(self, source_video):
title = os.path.basename(source_video)
logging.info('Uploading video: \"{}\"'.format(source_video))
try:
privacy = {
'view': 'password'
}
r = self.v.upload(
source_video, data={
'name': title,
'description': title,
'password': self.VIDEO_PASSWORD,
'privacy': privacy
})
# If success
# Move the file to uploaded folder
basename = os.path.basename(source_video)
destination = os.path.join(self.UPLOADED, basename)
d = await self.move_file(source_video, destination)
return d
except Exception as e:
logging.error('Video was not uploaded [{}]'.format(source_video))
logging.error('Reason: ' + str(e))
async def last_modified(self, original):
last_mod = os.path.getmtime(original)
now_time = time.time()
return now_time - last_mod
async def run(self):
logging.info('Running Vimeo uploader.')
logging.info('Monitoring folder: {}'.format(
os.path.abspath(self.MONITOR_FOLDER)))
self.__check()
while True:
try:
logging.info('I am alive ! ' + str(datetime.datetime.now()))
await asyncio.sleep(self.SLEEP_DELAY)
# list all files inside the MONITOR_FOLDER directory
for f in os.listdir(self.MONITOR_FOLDER):
fname, ext = os.path.splitext(f)
# Support only the following video file types
if not ext in ['.ogv', '.mkv', '.mp4', '.flv', '.wmv']:
logging.info('Skipping file {}'.format(str(f)))
continue
logging.info('found file: %s' % (fname, ))
original = os.path.join(self.MONITOR_FOLDER, f)
past_seconds = await self.last_modified(original)
if past_seconds < self.UPLOAD_DELAY:
logging.info(
'Waiting to complete recording of {}. Time left is {:.0f} sec/s: '.format(
f, self.UPLOAD_DELAY - past_seconds)
)
continue
converted = await self.convert(original)
if not converted:
await self.send_email(
"VIMEO VIDEO NOT CONVERTED",
'A video with filename {} was not converted'.format(
f)
)
continue
UPLOADED = await self.upload(converted)
if not UPLOADED:
await self.send_email(
"VIMEO - VIDEO NOT UPLOADED",
'A video with filename {} was not UPLOADED'.format(
f)
)
continue
logging.info(
'Video \"{}\" successfully UPLOADED.'.format(f))
await self.send_email(
"VIMEO UPLOAD SUCCESS!",
'A video with filename {} was successfully UPLOADED to vimeo'.format(
f))
await self.move_file(original, os.path.join(
self.ORIGINALS, os.path.basename(original)))
except Exception as ex:
logging.warning('Loop error.\n' + str(ex))
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
try:
if len(sys.argv) > 1:
uploader = Uploader(sys.argv[1])
else:
uploader = Uploader(SETTINGS_FILE)
loop = asyncio.get_event_loop()
loop.run_until_complete(uploader.run())
except SettingsFileNotFound:
logging.warning('Settings file not found!')
except KeyboardInterrupt:
logging.warning('Program interrupted by user [KeyboardInterrupt]')
except Exception as ee:
logging.warning('Main error')
uploader.send_email(
"VIMEO UPLOADER",
'Main program ended with reason\n' + str(ee)
)