├── Kuaipan.py ├── Kuaipan_File.py ├── Kuaipan_OAuth.py ├── README.md ├── config.py ├── fabfile.py ├── kuaipan.png ├── requirement.txt └── 快盘.png /Kuaipan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2013 Deren Wu (author). 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # 19 | # @file: Kuaipan.py 20 | # @date: 2013-11-7 21 | 22 | ''' 23 | 支援金山快盘 Open API 操作 24 | 25 | Core lib for Kuaipan implementation. (http://www.kuaipan.cn/developers) 26 | 27 | Requirement : 28 | * Python oauth2 (https://pypi.python.org/pypi/oauth2/) 29 | * Python poster (https://pypi.python.org/pypi/poster/) 30 | 31 | ''' 32 | 33 | __author__ = 'deren.g@gmail.com (Deren Wu)' 34 | 35 | import os, sys 36 | import oauth2 as oauth 37 | import urllib 38 | import urllib2 39 | import poster 40 | import json 41 | 42 | class Kuaipan(object): 43 | """docstring for Kuaipan""" 44 | def __init__(self): 45 | super(Kuaipan, self).__init__() 46 | 47 | class KuaipanFile(object): 48 | """ 49 | Support all functions for Kuaipan fileops. You may add/delete/remove/edit your file on Kuaipan with simple functino calls 50 | 51 | Example: 52 | 53 | If you want to get your account info, write down codes as below. 54 | 55 | kuaipan_file = Kuaipan.KuaipanFile(consumer_key, consumer_secret, oauth_token, oauth_token_secret) 56 | print kuaipan_file.account_info() 57 | 58 | PS : Before file operation, please get your oauth_token/oauth_token_secret first. (Refer to Kuaipan_OAuth.py) 59 | 60 | """ 61 | def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret): 62 | super(KuaipanFile, self).__init__() 63 | self.signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() 64 | self.token = oauth.Token(oauth_token, oauth_token_secret) 65 | self.consumer = oauth.Consumer(consumer_key, consumer_secret) 66 | 67 | def SetUrlHandler(self, UsePoster=False, UseCookie=False, PacketDebug=False): 68 | opener = urllib2._opener 69 | if opener == None: 70 | #opener = urllib2.OpenerDirector() 71 | opener = urllib2.build_opener() 72 | ''' 73 | opener.add_handler(urllib2.ProxyHandler()) 74 | opener.add_handler(urllib2.UnknownHandler()) 75 | opener.add_handler(urllib2.HTTPHandler()) 76 | opener.add_handler(urllib2.HTTPDefaultErrorHandler()) 77 | opener.add_handler(urllib2.HTTPSHandler()) 78 | opener.add_handler(urllib2.HTTPErrorProcessor()) 79 | ''' 80 | 81 | if UseCookie is True: 82 | opener.add_handler(urllib2.HTTPCookieProcessor()) 83 | #opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 84 | 85 | if PacketDebug is True: 86 | opener.add_handler(urllib2.HTTPHandler(debuglevel=1)) 87 | opener.add_handler(urllib2.HTTPSHandler(debuglevel=1)) 88 | #opener = urllib2.build_opener(httpHandler, httpsHandler) 89 | 90 | if UsePoster is True: 91 | from poster.streaminghttp import StreamingHTTPHandler, StreamingHTTPRedirectHandler, StreamingHTTPSHandler 92 | opener.add_handler(StreamingHTTPHandler()) 93 | opener.add_handler(StreamingHTTPRedirectHandler()) 94 | opener.add_handler(StreamingHTTPSHandler()) 95 | urllib2.install_opener(opener) 96 | return opener 97 | 98 | def GetOauthUrl(self, url, method='GET', parameters=None): 99 | oauth_request = oauth.Request.from_consumer_and_token(self.consumer, 100 | token=self.token, 101 | http_method=method, 102 | http_url=url, 103 | parameters=parameters, 104 | is_form_encoded=True) 105 | oauth_request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) 106 | return oauth_request.to_url() 107 | 108 | def DoHttpRequst(self, url): 109 | response = urllib2.urlopen(url) 110 | if response.code != 200: 111 | raise Exception("Invalid response %s." % vars(response)) 112 | content = response.read() 113 | return content 114 | 115 | def oauth_http_and_json_parse(self, url, method='GET', parameters=None): 116 | oauth_url = self.GetOauthUrl(url, method=method, parameters=parameters) 117 | content = self.DoHttpRequst(oauth_url) 118 | return KuaipanUtil.json_decoder(content) 119 | 120 | 121 | def account_info(self): 122 | """ 123 | Description : 查看用户信息 124 | Reference link : 125 | http://www.kuaipan.cn/developers/document_apiinfo.htm 126 | """ 127 | url = 'http://openapi.kuaipan.cn/1/account_info' 128 | return self.oauth_http_and_json_parse(url, method='GET', parameters=None) 129 | 130 | def metadata(self, filepath, root='app_folder', \ 131 | list=None, \ 132 | file_limit=None, \ 133 | page=None, \ 134 | page_size=None, \ 135 | filter_ext=None, \ 136 | sort_by=None): 137 | """ 138 | Description : 获取单个文件,文件夹信息 139 | Reference link : 140 | http://www.kuaipan.cn/developers/document_apimetadata.htm 141 | """ 142 | url = 'http://openapi.kuaipan.cn/1/metadata/%s/%s' 143 | url = url % (root, filepath) 144 | parameters = {} 145 | if list != None: 146 | parameters['list'] = list 147 | if file_limit != None: 148 | parameters['file_limit'] = file_limit 149 | if page != None: 150 | parameters['page'] = page 151 | if page_size != None: 152 | parameters['page_size'] = page_size 153 | if filter_ext != None: 154 | parameters['filter_ext'] = filter_ext 155 | if sort_by != None: 156 | parameters['sort_by'] = sort_by 157 | return self.oauth_http_and_json_parse(url, method='GET', parameters=None) 158 | 159 | def history(self, path, root='app_folder'): 160 | """ 161 | Description : 文件的历史版本 162 | Reference link : 163 | http://www.kuaipan.cn/developers/document_apihistory.htm 164 | """ 165 | url = 'http://openapi.kuaipan.cn/1/history/%s/%s' 166 | url = url % (root, path) 167 | return self.oauth_http_and_json_parse(url, method='GET', parameters=None) 168 | 169 | def fileops_create_folder(self, path, root='app_folder'): 170 | """ 171 | Description : 新建文件夹 172 | Reference link : 173 | http://www.kuaipan.cn/developers/document_apicreate.htm 174 | """ 175 | url = 'http://openapi.kuaipan.cn/1/fileops/create_folder' 176 | parameters = {} 177 | parameters['root'] = root 178 | parameters['path'] = path 179 | return self.oauth_http_and_json_parse(url, method='GET', parameters=parameters) 180 | 181 | def fileops_delete(self, path, root='app_folder', to_recycle=True): 182 | """ 183 | Description : 删除文件,文件夹,以及文件夹下所有文件到回收站。删除有风险,操作需谨慎。 184 | Reference link : 185 | http://www.kuaipan.cn/developers/document_apidelete.htm 186 | """ 187 | url = 'http://openapi.kuaipan.cn/1/fileops/delete' 188 | parameters = {} 189 | parameters['root'] = root 190 | parameters['path'] = path 191 | parameters['to_recycle'] = to_recycle 192 | return self.oauth_http_and_json_parse(url, method='GET', parameters=parameters) 193 | 194 | def fileops_move(self, from_path, to_path, root='app_folder'): 195 | """ 196 | Description : 移动文件,文件夹,不能移动带共享的文件,当然也不支持其他不能移动的情况(如形成环路) 197 | Reference link : 198 | http://www.kuaipan.cn/developers/document_apimove.htm 199 | """ 200 | url = 'http://openapi.kuaipan.cn/1/fileops/move' 201 | parameters = {} 202 | parameters['root'] = root 203 | parameters['from_path'] = from_path 204 | parameters['to_path'] = to_path 205 | return self.oauth_http_and_json_parse(url, method='GET', parameters=parameters) 206 | 207 | def fileops_copy(self, from_path, to_path, root='app_folder', from_copy_ref=None): 208 | """ 209 | Description : 复制文件,文件夹 210 | Reference link : 211 | http://www.kuaipan.cn/developers/document_apicopy.htm 212 | """ 213 | url = 'http://openapi.kuaipan.cn/1/fileops/copy' 214 | parameters = {} 215 | parameters['root'] = root 216 | parameters['from_path'] = from_path 217 | parameters['to_path'] = to_path 218 | if from_copy_ref != None: 219 | parameters['from_copy_ref'] = from_copy_ref 220 | return self.oauth_http_and_json_parse(url, method='GET', parameters=parameters) 221 | 222 | def fileops_thumbnail(self, width, height, path, root='app_folder'): 223 | """ 224 | Description : 缩略图 225 | Reference link : 226 | http://www.kuaipan.cn/developers/document_apithumbnail.htm 227 | """ 228 | url = 'http://conv.kuaipan.cn/1/fileops/thumbnail' 229 | parameters = {} 230 | parameters['root'] = root 231 | parameters['path'] = path 232 | parameters['width'] = width 233 | parameters['height'] = height 234 | oauth_url = self.GetOauthUrl(url, method='GET', parameters=parameters) 235 | self.SetUrlHandler(UseCookie=True) 236 | content = self.DoHttpRequst(oauth_url) 237 | return content 238 | 239 | def fileops_documentView(self, path, root='app_folder', view='normal', type=None, zip=0): 240 | """ 241 | Description : 文档转换產生網頁預覽圖 242 | Reference link : 243 | http://www.kuaipan.cn/developers/document_apidocview.htm 244 | """ 245 | url = 'http://conv.kuaipan.cn/1/fileops/documentView' 246 | parameters = {} 247 | parameters['root'] = root 248 | parameters['path'] = path 249 | parameters['view'] = view 250 | parameters['zip'] = zip 251 | parameters['type'] = type 252 | if parameters['type']==None: 253 | parameters['type'] = os.path.splitext(path)[1].split('.')[-1] 254 | oauth_url = self.GetOauthUrl(url, method='GET', parameters=parameters) 255 | self.SetUrlHandler(UseCookie=True) 256 | content = self.DoHttpRequst(oauth_url) 257 | return content 258 | 259 | def copy_ref(self, filepath, root='app_folder'): 260 | """ 261 | Description : 产生一个复制引用(ref),别的帐号可以使用这个引用作为copy接口的参数来复制文件。复制的版本是文件的最新版本,与引用产生的时间无关。 262 | Reference link : 263 | http://www.kuaipan.cn/developers/document_apicopyref.htm 264 | """ 265 | url = 'http://openapi.kuaipan.cn/1/copy_ref/%s/%s' 266 | url = url % (root, filepath) 267 | return self.oauth_http_and_json_parse(url, method='GET', parameters=None) 268 | 269 | def shares(self, filepath, root='app_folder', name=None, access_code=None): 270 | """ 271 | Description : 创建并获取一个文件的分享链接。发布外链的文件会接受审核。 272 | Reference link : 273 | http://www.kuaipan.cn/developers/document_apishare.htm 274 | """ 275 | url = 'http://openapi.kuaipan.cn/1/shares/%s/%s' 276 | url = url % (root, filepath) 277 | parameters = {} 278 | if name != None: 279 | parameters['name'] = name 280 | if access_code != None: 281 | parameters['access_code'] = access_code 282 | return self.oauth_http_and_json_parse(url, method='GET', parameters=parameters) 283 | 284 | def upload_file(self, localfile, kuaipan_path, ForceOverwrite=True): 285 | """ 286 | Description : 上传文件 287 | Args: 288 | localfile: 本地文件路径 289 | kuaipan_path: 快盘的路径 290 | Reference link : 291 | http://www.kuaipan.cn/developers/document_apiupload.htm 292 | """ 293 | url = '%s1/fileops/upload_file' 294 | url = url % (self.upload_locate()['url']) 295 | parameters = {'path': kuaipan_path, 296 | 'root': 'app_folder', 297 | 'overwrite' : ForceOverwrite 298 | } 299 | target_url = self.GetOauthUrl(url, method='POST', parameters=parameters) 300 | #poster.streaminghttp.register_openers() 301 | self.SetUrlHandler(UsePoster=True) 302 | data, headers = poster.encode.multipart_encode({"file": open(localfile, "rb")}) 303 | request = urllib2.Request(target_url, data=data, headers=headers) 304 | try: 305 | response = urllib2.urlopen(request) 306 | content = response.read() 307 | if response.code != 200: 308 | raise Exception("Invalid response %s." % vars(response)) 309 | except urllib2.HTTPError, error: 310 | content = error.read() 311 | raise Exception("urllib2.HTTPError %s." % content) 312 | return KuaipanUtil.json_decoder(content) 313 | 314 | def upload_locate(self): 315 | """ 316 | Description : 获取文件上传地址 317 | Reference link : 318 | http://www.kuaipan.cn/developers/document_apiupload.htm 319 | """ 320 | url = 'http://api-content.dfs.kuaipan.cn/1/fileops/upload_locate' 321 | return self.oauth_http_and_json_parse(url, method='GET', parameters=None) 322 | 323 | def download_file(self, kuaipan_filepath, root="app_folder", local_filepath=None, show_status=False): # root = app_folder or kuaipan 324 | """ 325 | Description : 下载文件(支持HTTP Range Retrieval Requests) 326 | Reference link : 327 | http://www.kuaipan.cn/developers/document_apidownload.htm 328 | """ 329 | url = 'http://api-content.dfs.kuaipan.cn/1/fileops/download_file' 330 | filename = local_filepath 331 | if filename is None: 332 | filename = os.path.basename(kuaipan_filepath) 333 | parameters = {'path': kuaipan_filepath, 334 | 'root': root} 335 | oauth_url = self.GetOauthUrl(url, method='GET', parameters=parameters) 336 | #opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 337 | opener = self.SetUrlHandler(UseCookie=True) 338 | response = opener.open(oauth_url) 339 | if response.code != 200: 340 | raise Exception("Invalid response %s." % vars(response)) 341 | total_size = int(response.headers['content-length']) 342 | if show_status is True: 343 | print 'File length : '+str(total_size) 344 | sys.stdout.write('Downdloading') 345 | with open(filename, 'wb') as f: 346 | while 1: 347 | content = response.read(1024*8) 348 | if not content: 349 | response.close() 350 | #print 351 | break 352 | if show_status is True: 353 | sys.stdout.write('.') 354 | f.write(content) 355 | if show_status is True: 356 | print 'Success!' 357 | 358 | class KuaipanUtil(object): 359 | """docstring for KuaipanUtil""" 360 | def __init__(self): 361 | super(KuaipanUtil, self).__init__() 362 | @staticmethod 363 | def json_decoder(value): 364 | json_acceptable_string = value.replace("'", "\"") 365 | d = json.loads(json_acceptable_string) 366 | return d 367 | 368 | 369 | class KuaipanOAuth(object): 370 | """docstring for KuaipanOAuth""" 371 | request_token_url = 'https://openapi.kuaipan.cn/open/requestToken' 372 | access_token_url = 'https://openapi.kuaipan.cn/open/accessToken' 373 | kuaipanApiUrl = 'https://www.kuaipan.cn/api.php' 374 | 375 | def __init__(self, consumer_key, consumer_secret): 376 | super(KuaipanOAuth, self).__init__() 377 | self.consumer_key = consumer_key 378 | self.consumer_secret = consumer_secret 379 | self.consumer = oauth.Consumer(consumer_key, consumer_secret) 380 | self.client = oauth.Client(self.consumer) 381 | 382 | def GetRequestToken(self): 383 | resp, content = self.client.request(self.request_token_url, "GET") 384 | if resp['status'] != '200': 385 | raise Exception("Invalid response %s." % resp['status']) 386 | self.request_token = KuaipanUtil.json_decoder(content) 387 | self.AuthenticationUrl = "%s?ac=open&op=authorise&oauth_token=%s" % (self.kuaipanApiUrl, self.request_token['oauth_token']) 388 | return self.request_token 389 | 390 | def GetAccessToken(self, oauth_verifier): 391 | token = oauth.Token(self.request_token['oauth_token'], self.request_token['oauth_token_secret']) 392 | token.set_verifier(oauth_verifier) 393 | client = oauth.Client(self.consumer, token) 394 | resp, content = client.request(self.access_token_url, "GET") 395 | self.access_token = KuaipanUtil.json_decoder(content) 396 | return self.access_token 397 | -------------------------------------------------------------------------------- /Kuaipan_File.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import Kuaipan 5 | import pprint 6 | import platform 7 | import asyncore 8 | import pyinotify 9 | import time 10 | from config import * 11 | 12 | sysstr = platform.system() 13 | 14 | ''' Make sure we are running in UTF-8 encoding by default ''' 15 | import sys 16 | reload(sys) 17 | sys.setdefaultencoding("utf-8") 18 | 19 | ''' Get main instance for file operation ''' 20 | kuaipan_file = Kuaipan.KuaipanFile(consumer_key, consumer_secret, oauth_token, oauth_token_secret) 21 | 22 | wm = pyinotify.WatchManager() # Watch Manager 23 | mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_ATTRIB # watched events 24 | path_name = '' 25 | 26 | class EventHandler(pyinotify.ProcessEvent): 27 | def process_IN_CLOSE_WRITE(self, event): 28 | print "IN_CLOSE_WRITE:", event.pathname 29 | global path_name 30 | path_name = event.pathname 31 | def process_IN_ATTRIB(self, event): 32 | print "IN_ATTRIB:", event.pathname 33 | global path_name 34 | if path_name==event.pathname: 35 | time.sleep(0.5) 36 | myfilename = event.pathname.split('/')[-1]#文件名字 不包括路径 event.pathname是文件名字+路径 37 | myfile_kuaipan = '/'+kuaipan_dir+'/'+myfilename#myfile_kuaipan是在快盘里面要存储的路径+文件名 38 | pprint.pprint(kuaipan_file.upload_file(event.pathname, kuaipan_path=myfile_kuaipan, ForceOverwrite=True)) 39 | 40 | notifier = pyinotify.AsyncNotifier(wm, EventHandler()) 41 | wdd = wm.add_watch(download_dir, mask, rec=True) 42 | 43 | asyncore.loop() -------------------------------------------------------------------------------- /Kuaipan_OAuth.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # 4 | # Copyright 2013 Deren Wu (author). 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # 19 | # @file: Kuaipan_OAuth.py 20 | # @date: 2013-11-7 21 | 22 | ''' 23 | This example implements Kuaipan OAuth. (http://www.kuaipan.cn/developers/document_oauth.htm) 24 | 25 | Requirement : 26 | * Python oauth2 (https://pypi.python.org/pypi/oauth2/) 27 | * Python poster (https://pypi.python.org/pypi/poster/) 28 | 29 | Howto - 30 | Step 1: 31 | Set proper "consumer_key" and "consumer_secret" for your account 32 | You may get "consumer_key" and "consumer_secret" at URL "http://www.kuaipan.cn/developers/list.htm" 33 | 34 | Step 2: 35 | Run "python Kuaipan_OAuth.py" in your environment 36 | 37 | Step 3: 38 | If you work on Windows system, the authentication URL would be open in you default browser directly. Otherwise, you should copy/paste the URL printed on console screen. 39 | 40 | Step 4: 41 | If everything work well, you should get the autenticated key-paire at final step. 42 | 43 | ''' 44 | 45 | 46 | # For both Python 2.x and 3.x 47 | try: 48 | read_input = raw_input 49 | except NameError: 50 | read_input = input 51 | ############################################### 52 | 53 | import Kuaipan 54 | 55 | consumer_key = 'xcq3lc7bWR1RMI8A' 56 | consumer_secret = '8uKHM7lLyE9CCAFM' 57 | 58 | kuaipan_oauth = Kuaipan.KuaipanOAuth(consumer_key, consumer_secret) 59 | 60 | # Step 1: Get a request token. This is a temporary token that is used for 61 | # having the user authorize an access token and to sign the request to obtain 62 | # said access token. 63 | request_token = kuaipan_oauth.GetRequestToken() 64 | print "Request Token:" 65 | print " - oauth_token = %s" % request_token['oauth_token'] 66 | print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] 67 | print 68 | 69 | # Step 2: Redirect to the provider. Since this is a CLI script we do not 70 | # redirect. In a web application you would redirect the user to the URL 71 | # below. 72 | print "Go to the following link in your browser:" 73 | print kuaipan_oauth.AuthenticationUrl 74 | print 75 | 76 | # Opne URL in your default browser (For Windows only) 77 | import platform 78 | if cmp(platform.system(), "Windows")==0 : 79 | import webbrowser 80 | webbrowser.open(kuaipan_oauth.AuthenticationUrl) 81 | 82 | # After the user has granted access to you, the consumer, the provider will 83 | # redirect you to whatever URL you have told them to redirect to. You can 84 | # usually define this in the oauth_callback argument as well. 85 | accepted = 'n' 86 | while accepted.lower() == 'n': 87 | accepted = read_input('Have you authorized me? (y/n) ') 88 | oauth_verifier = read_input('What is the PIN? ') 89 | 90 | # Step 3: Once the consumer has redirected the user back to the oauth_callback 91 | # URL you can request the access token the user has approved. You use the 92 | # request token to sign this request. After this is done you throw away the 93 | # request token and use the access token returned. You should store this 94 | # access token somewhere safe, like a database, for future use. 95 | 96 | access_token = kuaipan_oauth.GetAccessToken(oauth_verifier) 97 | 98 | print "Access Token:" 99 | print " - oauth_token = %s" % access_token['oauth_token'] 100 | print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] 101 | print 102 | print "You may now access protected resources using the access tokens above." 103 | print 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | download4kuaipan 2 | 3 | ================== 4 | 写这个程序主要是最近刚买了个很便宜的vps,无聊中发现他下载youtube视频的速度特别快,能达到20M/s甚至更高。因此写了这个程序,用vps远程下载视频,然后远程监测下载文件夹的文件变化,如果发现有新增文件出现即把文件上传到金山快盘里面去。等到有空了从金山快盘下载视频或者文件,速度绝对比ssh代理下载的快。程序使用supervisor监控程序。 5 | 6 | ================== 7 | 1.使用下面的命令直接安装必要的包 8 | pip install -r requirement.txt 9 | 10 | 2.首先你要有金山快盘的账户 然后到这个[地址](http://www.kuaipan.cn/developers)申请快盘的开发平台的应用创建应用后就能得到consumer_key consumer_ecret。然后执行python Kuaipan_OAuth.py即可在命令行的提示下最终得到oauth_token oauth_token_secret这2个值。然后将config.py里面的相关配置修改性下。 11 | 至于kuaipan_dir 和download_dir的意义,看下图 12 | ![image](https://raw.githubusercontent.com/no13bus/download4kuaipan/master/kuaipan.png) 13 | 图中的kuaipan_dir = 'testdir' 14 | download_dir指的是vps上你设置的下载文件的路径 15 | 16 | 3.supervisor的跟本程序有关的配置如下 17 | [program:youtube] 18 | command=python /root/python-kuaipan/Kuaipan_File.py 19 | directory=/root/downloadfiles/ 20 | autorestart=true 21 | redirect_stderr=true 22 | 23 | 4.当然也可以在windows上面使用fabric远程连接vps 直接将文件下载至download_dir里面。下载之后,照样还是能将文件即刻上传金山快盘 24 | fab dl:url="视频地址" 或者 fab wget:url="网络文件地址" 25 | 26 | =================== 27 | ### 疑问: 28 | 1.fab dl:url="url地址" 29 | youtube的网址里面出现的=要变成\= 否则fabric无法解析 不知道为什么 30 | 31 | 2.supervisord监控应用的时候 这个应用如果有一些print输出的时候,监控里面并看不到任何输出,只是看到是否在running。不知道这个大家都是咋解决的 32 | 33 | =================== 34 | TO-List 35 | 对百度网盘的支持 36 | ================== 37 | 38 | 用到的开源项目 39 | * [金山快盘API](https://github.com/deren/python-kuaipan) 40 | * [监控linux文件变化 pyinotify](https://github.com/seb-m/pyinotify) 41 | * [supervisord](https://github.com/Supervisor/supervisor) 42 | * [fabric部署利器](https://github.com/fabric/fabric) 43 | 44 | 45 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | consumer_key = '*******' 4 | consumer_secret = '*******' 5 | oauth_token = '*******' 6 | oauth_token_secret = '*******' 7 | 8 | kuaipan_dir = 'testdir' #你的快盘的自己建立的文件夹 9 | download_dir = '/root/downloadfiles/' #这里写上自己下载到linux上面的文件夹路径 10 | -------------------------------------------------------------------------------- /fabfile.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | from config import * 3 | from fabric.api import local,cd,run,env 4 | 5 | env.hosts=['user@ip:22',] #写上自己的vps的ip和用户 6 | env.password = 'pwd' #写上vps用户的密码 7 | 8 | # youtube的网址里面出现的=要变成\= 否则fabric无法解析 不知道为什么 9 | def dl(url): 10 | with cd(download_dir): 11 | cmd = 'youtube-dl --max-quality FORMAT %s' % url 12 | run(cmd) #远程操作用run 13 | 14 | def wget(url): 15 | with cd(download_dir): 16 | cmd = 'wget -c %s' % url 17 | run(cmd) -------------------------------------------------------------------------------- /kuaipan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/no13bus/download4kuaipan/ee1bc34e56b5c23e0b42c20102aa1f555d0562cd/kuaipan.png -------------------------------------------------------------------------------- /requirement.txt: -------------------------------------------------------------------------------- 1 | youtube-dl 2 | fabric 3 | pyinotify 4 | supervisord -------------------------------------------------------------------------------- /快盘.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/no13bus/download4kuaipan/ee1bc34e56b5c23e0b42c20102aa1f555d0562cd/快盘.png --------------------------------------------------------------------------------