-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathufe-ftp.py
executable file
·129 lines (127 loc) · 3.82 KB
/
ufe-ftp.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
#!/usr/bin/python
# coding: utf-8
from lib.config import *
from lib.functions import *
import os
import sys
import ftplib
import time
# Check PID file
pid = str(os.getpid())
pidfile = "%s/ftp.pid" % tmppath
if os.path.isfile(pidfile):
logthis('%s already exists. The process should be running.' % pidfile)
sys.exit()
else:
# Create PID
file(pidfile, 'w').write(pid)
# Get list of videos to upload
ftp_list, next_ftp_video_list = select_next_ftp()
next_ftp_video_full_list = []
# Fill list with connection data
for registry in next_ftp_video_list :
encode_status = registry[0]
encode_file = registry[1]
vhash = registry[2]
vpid = registry[3]
ftp_path = registry[4]
site_id = registry[5]
for row in ftp_list :
if site_id == row[0] :
ftp_host = row[1]
ftp_user = row[2]
ftp_pass = row[3]
break
next_ftp_video_full_row = encode_status, encode_file, vhash, vpid, ftp_path, site_id, ftp_host, ftp_user, ftp_pass
next_ftp_video_full_list.append(next_ftp_video_full_row)
# Read list and upload
for registry in next_ftp_video_full_list :
encode_status = registry[0]
encode_file = registry[1]
vhash = registry[2]
vpid = registry[3]
ftp_path = registry[4]
site_id = registry[5]
ftp_host = registry[6]
ftp_user = registry[7]
ftp_pass = registry[8]
# Let's try to upload some videos!
try :
# Open FTP
ftp = ftplib.FTP(ftp_host, ftp_user, ftp_pass)
did_ftp=1
# Actualizo estado
update_ftp_status(2, vhash, vpid)
# Traigo el path con "/" y lo separo en año, dia y mes
ftp_path_split=ftp_path.split('/')
year=ftp_path_split[0]
month=ftp_path_split[1]
day=ftp_path_split[2]
# Busco si existe la carpeta del año
yearlist = []
ftp.retrlines('NLST',yearlist.append)
# Year exists?
if year in yearlist:
ftp.cwd(year)
monthlist = []
ftp.retrlines('NLST',monthlist.append)
# Month exists?
if month in monthlist:
ftp.cwd(month)
daylist = []
ftp.retrlines('NLST',daylist.append)
# Day exists?
if day in daylist:
ftp.cwd(day)
vhashlist = []
ftp.retrlines('NLST',vhashlist.append)
# Vhash exists?
if vhash in vhashlist:
ftp.cwd(vhash)
else:
ftp.mkd(vhash)
ftp.cwd(vhash)
else:
ftp.mkd(day)
ftp.cwd(day)
ftp.mkd(vhash)
ftp.cwd(vhash)
else:
ftp.mkd(month)
ftp.cwd(month)
ftp.mkd(day)
ftp.cwd(day)
ftp.mkd(vhash)
ftp.cwd(vhash)
else:
ftp.mkd(year)
ftp.cwd(year)
ftp.mkd(month)
ftp.cwd(month)
ftp.mkd(day)
ftp.cwd(day)
ftp.mkd(vhash)
ftp.cwd(vhash)
#
encode_file_name, encode_file_ext = os.path.splitext(encode_file)
encode_file_log = "%s.log" % encode_file_name
logthis('Uploading %s/%s/%s/%s/%s' % (year, month, day, vhash, encode_file))
# Upload video ...
ftp_file = open( '%s%s/%s' % (encoded, vhash, encode_file),'rb')
ftp.storbinary('STOR ' + encode_file, ftp_file)
# Upload video log ...
ftp_file_log = open( '%s%s/%s' % (encoded, vhash, encode_file_log),'rb')
ftp.storbinary('STOR ' + encode_file_log, ftp_file_log)
logthis('Uploaded %s/%s/%s/%s/%s' % (year, month, day, vhash, encode_file))
# Close FTP
ftp_file.close()
# Update status
update_ftp_status(3, vhash, vpid)
update_vp_quantity(1, 'vp_done', vhash)
# Did i open FTP?
if did_ftp==1 :
ftp.quit()
except :
pass
# Clean PID
os.unlink(pidfile)