-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKuaipan_File.py
More file actions
96 lines (75 loc) · 3.31 KB
/
Kuaipan_File.py
File metadata and controls
96 lines (75 loc) · 3.31 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2013 Deren Wu <deren.g@gmail.com> (author).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# @file: Kuaipan_File.py
# @date: 2013-11-8
'''
A demonstration for Kuaipan operation
'''
__author__ = 'deren.g@gmail.com (Deren Wu)'
import Kuaipan
import pprint
consumer_key = 'your_kuaipan_consumer_key'
consumer_secret = 'your_kuaipan_consumer_secret'
oauth_token = 'your_kuaipan_oauth_token'
oauth_token_secret = 'your_kuaipan_oauth_token_secret'
''' Make sure we are running in UTF-8 encoding by default '''
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
''' Get main instance for file operation '''
kuaipan_file = Kuaipan.KuaipanFile(consumer_key, consumer_secret, oauth_token, oauth_token_secret)
''' Create a dummy test file '''
myfile = 'demo_test.txt'
with open(myfile, 'wb') as fp:
fp.write(u'English: This a demostration for API usage!\n'+
u'繁體中文: 範例測試\n'+
u'简体中文: 范例测试\n')
mydir = 'testdir'
myfile_kuaipan = '/'+mydir+'/'+myfile
print ' API demostration start! '
print '\n 1. show information of your account '
pprint.pprint( kuaipan_file.account_info() )
print '\n 2. Create directory '
pprint.pprint( kuaipan_file.fileops_create_folder(mydir) )
print '\n 3. Upload our test file '
pprint.pprint( kuaipan_file.upload_file(myfile, kuaipan_path=myfile_kuaipan, ForceOverwrite=True) )
print '\n 4. Get file metadata '
pprint.pprint( kuaipan_file.metadata(myfile_kuaipan) )
print '\n 5. Get file reference info '
pprint.pprint( kuaipan_file.copy_ref(myfile_kuaipan) )
print '\n 6. Get share link (Response [{u\'msg\': u\'PSA_OUTLINK_STATUS_REVIEWING\'}] means the file is under review by Kuaipan office. You need to wait!)'
try:
pprint.pprint( kuaipan_file.shares(myfile_kuaipan) )
except:
pprint.pprint('Is your file with zero size or include invalid file name?')
print '\n 7. Get HTML document view '
pprint.pprint( kuaipan_file.fileops_documentView(myfile_kuaipan, zip=0) )
print '\n 8. Get thumbnail '
print( kuaipan_file.fileops_thumbnail(100, 100, myfile_kuaipan) )
print '\n 9. Get file history (If the file is never changed, the history would be 404 not found) '
pprint.pprint( kuaipan_file.upload_file(myfile, kuaipan_path=myfile_kuaipan, ForceOverwrite=True) ) # upload again to generate history
pprint.pprint( kuaipan_file.history(myfile_kuaipan) )
print '\n 10. Copy file (or directory) '
pprint.pprint( kuaipan_file.fileops_copy(myfile_kuaipan, myfile_kuaipan+'2') )
print '\n 11. Move file (or directory) '
pprint.pprint( kuaipan_file.fileops_move(myfile_kuaipan+'2', myfile_kuaipan+'3') )
print '\n 12. Delete file (or directory) '
pprint.pprint( kuaipan_file.fileops_delete(myfile_kuaipan+'3') )
print '\n 13. Download file to newfile.txt '
kuaipan_file.download_file(myfile_kuaipan, local_filepath="newfile.txt")