forked from fduch2k/ypload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ypload
59 lines (49 loc) · 1.65 KB
/
ypload
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
#!/usr/bin/env python
import sys
import os
import ydisk
import mimetypes
YD_APP_ID = 'cb76e6135dc34947bcf7620e1ab62e54'
YD_APP_SECRET = 'e4dba0141d734e1d89a5333c26a44f46'
KEY_FILE = os.path.expanduser('~/.ypload-key')
def main():
if len(sys.argv) < 2:
sys.stderr.write('No filename given\n')
sys.exit(-2)
try:
key = ydisk.getKey(YD_APP_ID, YD_APP_SECRET, KEY_FILE)
except:
key = None
while not key or not ydisk.LoginAPI(key).getInfo():
print "Something wrong with the authorization token"
answ = raw_input(
'Would you like to retry authentication? (y/N): ').strip().lower()
if answ in ['y', "yes"]:
if os.path.isfile(KEY_FILE):
os.remove(KEY_FILE)
try:
key = ydisk.getKey(YD_APP_ID, YD_APP_SECRET, KEY_FILE)
except:
pass
else:
sys.exit(-5)
api = ydisk.DiskAPI(key)
api.mkdir('/JustShared')
for fname in sys.argv[1:]:
if os.path.isfile(fname):
fname = sys.argv[1]
newname = '/JustShared/' + os.path.basename(fname)
try:
tp, enc = mimetypes.guess_type(fname)
if not tp:
tp = 'application/facepalm'
api.put(newname, open(fname, 'r').read(), tp=tp)
print api.publish(newname)
except Exception, e:
sys.stderr.write('Something wrong with %s\n%s\n' % (fname, e))
sys.exit(-3)
else:
sys.stderr.write('No such file %s\n' % fname)
sys.exit(-4)
if __name__ == '__main__':
main()