-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibroadcast-uploader3.py
executable file
·235 lines (189 loc) · 6.56 KB
/
ibroadcast-uploader3.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
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
#!/usr/bin/env python
import requests
import json
import glob
import os
import hashlib
import sys
sys.tracebacklimit = 0
class ServerError(Exception):
pass
class ValueError(Exception):
pass
class Uploader(object):
"""
Class for uploading content to iBroadcast.
"""
VERSION = '.1'
CLIENT = 'python uploader script'
def __init__(self, username, password):
self.username = username
self.password = password
# Initialise our variables that each function will set.
self.user_id = None
self.token = None
self.supported = None
self.files = None
self.md5 = None
def process(self):
try:
self.login()
except ValueError as e:
print('Login failed: %s' % e)
return
self.files = []
self.load_files()
if self.confirm():
self.upload()
def login(self, username=None, password=None):
"""
Login to iBroadcast with the given username and password
Raises:
ValueError on invalid login
"""
# Default to passed in values, but fallback to initial data.
username = username or self.username
password = password or self.password
print('Logging in...')
# Build a request object.
post_data = json.dumps({
'mode' : 'status',
'email_address': username,
'password': password,
'version': self.VERSION,
'client': self.CLIENT,
'supported_types' : 1,
})
response = requests.post(
"https://json.ibroadcast.com/s/JSON/status",
data=post_data,
headers={'Content-Type': 'application/json'}
)
if not response.ok:
raise ServerError('Server returned bad status: ',
response.status_code)
jsoned = response.json()
if 'user' not in jsoned:
raise ValueError('Invalid login.')
print('Login successful - user_id: ', jsoned['user']['id'])
self.user_id = jsoned['user']['id']
self.token = jsoned['user']['token']
self.supported = []
for filetype in jsoned['supported']:
self.supported.append(filetype['extension'])
def load_files(self, directory=None):
"""
Load all files in the directory that match the supported extension list.
directory defaults to present working directory.
raises:
ValueError if supported is not yet set.
"""
if self.supported is None:
raise ValueError('Supported not yet set - have you logged in yet?')
if not directory:
directory = os.getcwd()
for full_filename in glob.glob(os.path.join(directory, '*')):
filename = os.path.basename(full_filename)
# Skip hidden files.
if filename.startswith('.'):
continue
# Make sure it's a supported extension.
dummy, ext = os.path.splitext(full_filename)
if ext in self.supported:
self.files.append(full_filename)
# Recurse into subdirectories.
# XXX Symlinks may cause... issues.
if os.path.isdir(full_filename):
self.load_files(full_filename)
def confirm(self):
"""
Presents a dialog for the user to either list all files, or just upload.
"""
print("Found %s files. Press 'L' to list, or 'U' to start the " \
"upload." % len(self.files))
response = input('--> ')
print()
if response == 'L'.upper():
print('Listing found, supported files')
for filename in self.files:
print(' - ', filename)
print()
print("Press 'U' to start the upload if this looks reasonable.")
response = input('--> ')
if response == 'U'.upper():
print('Starting upload.')
return True
print('Aborting')
return False
def __load_md5(self):
"""
Reach out to iBroadcast and get an md5.
"""
post_data = "user_id=%s&token=%s" % (self.user_id, self.token)
# Send our request.
response = requests.post(
"https://sync.ibroadcast.com",
data=post_data,
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
if not response.ok:
raise ServerError('Server returned bad status: ',
response.status_code)
jsoned = response.json()
self.md5 = jsoned['md5']
def calcmd5(self, filePath="."):
with open(filePath, 'rb') as fh:
m = hashlib.md5()
while True:
data = fh.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
def upload(self):
"""
Go and perform an upload of any files that haven't yet been uploaded
"""
self.__load_md5()
fcount=len(self.files)
ccount=1
for filename in self.files:
print("%d / %d" %(ccount,fcount), 'Uploading ', filename)
ccount+=1
# Get an md5 of the file contents and compare it to whats up
# there already
file_md5 = self.calcmd5(filename)
if file_md5 in self.md5:
print('Skipping - already uploaded.')
continue
upload_file = open(filename, 'rb')
file_data = {
'file': upload_file,
}
post_data = {
'user_id': self.user_id,
'token': self.token,
'file_path' : filename,
'method': self.CLIENT,
}
response = requests.post(
"https://sync.ibroadcast.com",
post_data,
files=file_data,
)
upload_file.close()
if not response.ok:
raise ServerError('Server returned bad status: ',
response.status_code)
jsoned = response.json()
result = jsoned['result']
if result is False:
raise ValueError('File upload failed.')
print('Done')
if __name__ == '__main__':
# NB: this could use parsearg
if len(sys.argv) != 3:
print("Usage: ibroadcast-uploader.py <username> <password>")
sys.exit(1)
uploader = Uploader(sys.argv[1], sys.argv[2])
uploader.process()