Skip to content

Commit 63e95da

Browse files
author
abbeyokgo
committed
修复磁力下载bug&有特殊字符的文件上传、展示bug
1 parent 74ffb7a commit 63e95da

8 files changed

Lines changed: 93 additions & 65 deletions

File tree

function.py

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def GetToken(Token_file='token.json',user='A'):
115115
if token.get('access_token'):
116116
with open(token_path,'w') as f:
117117
json.dump(token,f,ensure_ascii=False)
118-
else:
119-
print token
118+
else:
119+
print token
120120
except:
121121
with open(os.path.join(data_dir,'{}_Atoken.json'.format(user)),'r') as f:
122122
Atoken=json.load(f)
@@ -176,7 +176,7 @@ def Dir(path=u'A:/'):
176176
n_path=n_path[:-1]
177177
if not n_path.startswith('/'):
178178
n_path='/'+n_path
179-
n_path=urllib.quote(n_path)
179+
n_path=urllib.quote(n_path.encode('utf-8'))
180180
BaseUrl=app_url+u'v1.0/me/drive/root:{}:/children?expand=thumbnails'.format(n_path)
181181
queue=Queue()
182182
# queue.put(dict(url=BaseUrl,grandid=grandid,parent=parent,trytime=1))
@@ -232,7 +232,7 @@ def Dir_all(path=u'A:/'):
232232
items.delete_many({'parent':parent_id})
233233
grandid=idx+1
234234
parent=parent_id
235-
n_path=urllib.quote(n_path)
235+
n_path=urllib.quote(n_path.encode('utf-8'))
236236
BaseUrl=app_url+u'v1.0/me/drive/root:{}:/children?expand=thumbnails'.format(n_path)
237237
queue=Queue()
238238
g=GetItemThread(queue,user)
@@ -329,7 +329,9 @@ def GetItem(self,url,grandid=0,parent='',trytime=1):
329329
if value.get('folder').get('childCount')==0:
330330
continue
331331
else:
332-
url=app_url+'v1.0/me'+value.get('parentReference').get('path')+'/'+value.get('name')+':/children?expand=thumbnails'
332+
parent_path=value.get('parentReference').get('path').replace('/drive/root:','')
333+
path=urllib.quote(convert2unicode(parent_path+'/'+value['name']))
334+
url=app_url+'v1.0/me/drive/root:{}:/children?expand=thumbnails'.format(path)
333335
self.queue.put(dict(url=url,grandid=grandid+1,parent=item['id'],trytime=1))
334336
else:
335337
items.delete_one({'id':value['id']})
@@ -358,7 +360,9 @@ def GetItem(self,url,grandid=0,parent='',trytime=1):
358360
if value.get('folder').get('childCount')==0:
359361
continue
360362
else:
361-
url=app_url+'v1.0/me'+value.get('parentReference').get('path')+'/'+value.get('name')+':/children?expand=thumbnails'
363+
parent_path=value.get('parentReference').get('path').replace('/drive/root:','')
364+
path=urllib.quote(convert2unicode(parent_path+'/'+value['name']))
365+
url=app_url+'v1.0/me/drive/root:{}:/children?expand=thumbnails'.format(path)
362366
self.queue.put(dict(url=url,grandid=grandid+1,parent=item['id'],trytime=1))
363367
else:
364368
if items.find_one({'id':value['id']}) is not None: #文件存在
@@ -407,12 +411,12 @@ def GetItem(self,url,grandid=0,parent='',trytime=1):
407411
def GetItemByPath(self,path):
408412
app_url=GetAppUrl()
409413
token=GetToken(user=self.user)
414+
path=urllib.quote(convert2unicode(path))
410415
if path=='' or path=='/':
411416
url=app_url+u'v1.0/me/drive/root/'
412-
if path=='/':
417+
else:
413418
url=app_url+u'v1.0/me/drive/root:{}:/'.format(path)
414419
header={'Authorization': 'Bearer {}'.format(token)}
415-
url=app_url+u'v1.0/me/drive/root:{}:/'.format(path)
416420
r=requests.get(url,headers=header)
417421
data=json.loads(r.content)
418422
return data
@@ -458,7 +462,7 @@ def UpdateFile(renew='all'):
458462
def FileExists(filename,user='A'):
459463
token=GetToken(user=user)
460464
headers={'Authorization':'bearer {}'.format(token),'Content-Type':'application/json'}
461-
search_url=app_url+"v1.0/me/drive/root/search(q='{}')".format(filename)
465+
search_url=app_url+"v1.0/me/drive/root/search(q='{}')".format(convert2unicode(filename))
462466
r=requests.get(search_url,headers=headers)
463467
jsondata=json.loads(r.text)
464468
if len(jsondata['value'])==0:
@@ -523,7 +527,7 @@ def _file_content(path,offset,length):
523527
def _upload(filepath,remote_path,user='A'): #remote_path like 'share/share.mp4'
524528
token=GetToken(user=user)
525529
headers={'Authorization':'bearer {}'.format(token)}
526-
url=app_url+'v1.0/me/drive/root:'+urllib.quote(remote_path)+':/content'
530+
url=app_url+'v1.0/me/drive/root:{}:/content'.format(urllib.quote(convert2unicode(remote_path)))
527531
r=requests.put(url,headers=headers,data=open(filepath,'rb'))
528532
data=json.loads(r.content)
529533
trytime=1
@@ -683,7 +687,7 @@ def AddResource(data,user='A'):
683687
def CreateUploadSession(path,user='A'):
684688
token=GetToken(user=user)
685689
headers={'Authorization':'bearer {}'.format(token),'Content-Type':'application/json'}
686-
url=app_url+u'v1.0/me/drive/root:{}:/createUploadSession'.format(urllib.quote(path.encode('utf8')))
690+
url=app_url+u'v1.0/me/drive/root:{}:/createUploadSession'.format(urllib.quote(convert2unicode(path)))
687691
data={
688692
"item": {
689693
"@microsoft.graph.conflictBehavior": "rename",
@@ -743,6 +747,8 @@ def Upload_for_server(filepath,remote_path=None,user='A'):
743747
remote_path=os.path.join(remote_path,os.path.basename(filepath))
744748
if not remote_path.startswith('/'):
745749
remote_path='/'+remote_path
750+
filepath=convert2unicode(filepath)
751+
remote_path=convert2unicode(remote_path)
746752
print('local file path:{}, remote file path:{}'.format(filepath,remote_path))
747753
if _filesize(filepath)<1024*1024*3.25:
748754
for msg in _upload(filepath,remote_path,user):
@@ -1075,14 +1081,6 @@ def get_aria2():
10751081
return e,False
10761082

10771083

1078-
def parse_result(data):
1079-
j=json.loads(data)
1080-
print js
1081-
try:
1082-
return j[0]['result']['status']
1083-
except Exception as e:
1084-
print e
1085-
return False
10861084

10871085
def download_and_upload(url,remote_dir,user,gid=None):
10881086
p,status=get_aria2()
@@ -1104,12 +1102,13 @@ def download_and_upload(url,remote_dir,user,gid=None):
11041102
item['gid']=gid
11051103
a=json.loads(p.tellStatus(gid))[0]["result"]
11061104
if 'magnet:?xt=' in url:
1107-
name=re.findall('magnet:\?xt=urn:btih:(.{,40})',url)[0]+'.torrent'
1105+
name=re.findall('magnet:\?xt=urn:btih:(.{,40})',url)[0].lower()+'.torrent'
11081106
localpath=os.path.join(down_path,name)
11091107
else:
11101108
name=a['files'][0]['path'].replace(down_path+'/','').replace(down_path,'').replace(down_path[:-1],'')
11111109
localpath=a['files'][0]['path']
11121110
item['name']=name
1111+
item['idx']=0
11131112
item['localpath']=localpath
11141113
item['downloadUrl']=url
11151114
item['user']=user
@@ -1127,17 +1126,18 @@ def download_and_upload(url,remote_dir,user,gid=None):
11271126
old_status['status']=0
11281127
old_status['up_status']=u'磁力文件,无需上传'
11291128
down_db.find_one_and_update({'gid':gid},{'$set':old_status})
1130-
magnet=re.findall('magnet:\?xt=urn:btih:(.{,40})',down_db.find_one({'gid':gid})['downloadUrl'])[0]+'.torrent'
1129+
magnet=re.findall('magnet:\?xt=urn:btih:(.{,40})',down_db.find_one({'gid':gid})['downloadUrl'])[0].lower()+'.torrent'
11311130
old_path=os.path.join(down_path,magnet)
11321131
try:
11331132
os.remove(old_path)
11341133
except:
11351134
print("删除种子文件失败")
11361135
gid=a.get('followedBy')[0]
11371136
aa=json.loads(p.tellStatus(gid))[0]["result"]
1138-
for file in aa['files']:
1137+
for idx,file in enumerate(aa['files']):
11391138
new_item={}
11401139
new_item['gid']=gid
1140+
new_item['idx']=idx
11411141
new_item['name']=file['path'].replace(down_path+'/','').replace(down_path,'').replace(down_path[:-1],'')
11421142
new_item['localpath']=file['path']
11431143
new_item['downloadUrl']=aa['infoHash']
@@ -1150,40 +1150,49 @@ def download_and_upload(url,remote_dir,user,gid=None):
11501150
new_item['status']=1
11511151
down_db.insert_one(new_item)
11521152
a=json.loads(p.tellStatus(gid))[0]["result"]
1153-
name=a['files'][0]['path'].replace(down_path+'/','').replace(down_path,'').replace(down_path[:-1],'')
1154-
new_value={'down_status':u'{}%'.format(round(float(a['completedLength'])/(float(a['totalLength'])+0.1)*100,0))}
1155-
new_value['name']=name
1156-
new_value['size']=humanize.naturalsize(a['totalLength'], gnu=True)
1157-
new_value['localpath']=a['files'][0]['path']
1158-
if a['status']=='complete' or (a['completedLength']==a['totalLength'] and int(a['totalLength'])!=0):
1159-
new_value['up_status']=u'准备上传'
1160-
down_db.update_many({'gid':gid},{'$set':new_value})
1161-
upload_status(gid,remote_dir,user)
1162-
break
1163-
elif a['status']=='active' or a['status']=='waiting':
1164-
time.sleep(1)
1165-
down_db.update_many({'gid':gid},{'$set':new_value})
1166-
else:
1167-
print('下载出错')
1168-
new_value['down_status']=u'下载出错'
1169-
new_value['status']=-1
1170-
print new_value
1171-
down_db.update_many({'gid':gid},{'$set':new_value})
1172-
break
1153+
for idx,file in enumerate(a['files']):
1154+
t=down_db.find_one({'gid':gid,'idx':idx})
1155+
if t['down_status']=='100.0%':
1156+
if t['up_status']=='待机':
1157+
new_value['up_status']=u'准备上传'
1158+
down_db.find_one_and_update({'gid':gid,'idx':idx},{'$set':new_value})
1159+
upload_status(gid,idx,remote_dir,user)
1160+
else:
1161+
continue
1162+
name=file['path'].replace(down_path+'/','').replace(down_path,'').replace(down_path[:-1],'')
1163+
new_value={'down_status':u'{}%'.format(round(float(file['completedLength'])/(float(file['length'])+0.1)*100,0))}
1164+
new_value['name']=name
1165+
new_value['size']=humanize.naturalsize(file['length'], gnu=True)
1166+
new_value['localpath']=file['path']
1167+
if a['status']=='complete' or (file['completedLength']==file['length'] and int(file['length'])!=0):
1168+
new_value['up_status']=u'准备上传'
1169+
down_db.find_one_and_update({'gid':gid,'idx':idx},{'$set':new_value})
1170+
upload_status(gid,idx,remote_dir,user)
1171+
break
1172+
elif a['status']=='active' or a['status']=='waiting':
1173+
time.sleep(1)
1174+
down_db.find_one_and_update({'gid':gid,'idx':idx},{'$set':new_value})
1175+
else:
1176+
print('下载出错')
1177+
new_value['down_status']=u'下载出错'
1178+
new_value['status']=-1
1179+
down_db.find_one_and_update({'gid':gid,'idx':idx},{'$set':new_value})
1180+
break
11731181

1174-
def upload_status(gid,remote_dir,user):
1175-
items=down_db.find({'gid':gid})
1182+
def upload_status(gid,idx,remote_dir,user):
1183+
items=down_db.find({'gid':gid,'idx':idx})
11761184
for item in items:
11771185
localpath=item['localpath']
11781186
if not remote_dir.endswith('/'):
11791187
remote_dir=remote_dir+'/'
1188+
remote_path=os.path.join(remote_dir,item['name'])
11801189
if not os.path.exists(localpath):
11811190
new_value={}
11821191
new_value['up_status']=u'本地文件不存在。检查:{}'.format(localpath)
11831192
new_value['status']=-1
11841193
down_db.find_one_and_update({'_id':item['_id']},{'$set':new_value})
11851194
return
1186-
_upload_session=Upload_for_server(localpath,remote_dir,user)
1195+
_upload_session=Upload_for_server(localpath,remote_path,user)
11871196
while 1:
11881197
try:
11891198
new_value={}
@@ -1240,6 +1249,7 @@ def get_tasks(status):
12401249
for t in tasks:
12411250
info={}
12421251
info['gid']=t['gid']
1252+
info['idx']=t['idx']
12431253
info['name']=t['name']
12441254
info['downloadUrl']=t['downloadUrl']
12451255
info['size']=t['size']
@@ -1260,6 +1270,7 @@ def Aria2Method(action,**kwargs):
12601270
eval('p.{}()'.format(action))
12611271
elif action in ['remove','removeAll']:
12621272
for gid in kwargs['gids']:
1273+
gid,idx=gid.split('#')
12631274
p.forceRemove(gid)
12641275
elif action=='restart':
12651276
for gid in kwargs['gids']:
@@ -1288,8 +1299,9 @@ def DBMethod(action,**kwargs):
12881299
elif action in ['remove','removeAll']:
12891300
result=[]
12901301
for gid in kwargs['gids']:
1291-
info={'gid':gid}
1292-
task=down_db.find_one({'gid':gid})
1302+
gid,idx=gid.split('#')
1303+
info={'gid':gid,'idx':int(idx)}
1304+
task=down_db.find_one({'gid':gid,'idx':int(idx)})
12931305
if task['down_status']=='100.0%' and 'partition upload success' in task['up_status']:
12941306
info['msg']='正在上传的任务,无法更改状态'
12951307
else:

run.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ def page_not_found(e):
419419
@app.route('/',methods=['POST','GET'])
420420
@limiter.limit("200/minute;50/second")
421421
def index(path='A:/'):
422+
path=urllib.unquote(path)
422423
if path=='favicon.ico':
423424
return redirect('https://onedrive.live.com/favicon.ico')
424425
if items.count()==0:
@@ -427,6 +428,10 @@ def index(path='A:/'):
427428
else:
428429
#subprocess.Popen('python {} UpdateFile'.format(os.path.join(config_dir,'function.py')),shell=True)
429430
return make_response('<h1>正在更新数据!如果您是网站管理员,请在后台运行命令:python function.py UpdateFile</h1>')
431+
try:
432+
path.split(':')
433+
except:
434+
path='A:/'+path
430435
#参数
431436
user,n_path=path.split(':')
432437
if n_path=='':
@@ -499,24 +504,25 @@ def show(fileid,user):
499504
ext=name.split('.')[-1].lower()
500505
path=GetPath(fileid)
501506
if request.method=='POST':
502-
url=request.url.replace(':80','').replace(':443','')
507+
url=request.url.replace(':80','').replace(':443','').encode('utf-8')
508+
inner_url='/'.join(url.split('/')[:3])+'/'+urllib.quote('/'.join(url.split('/')[3:]))
503509
if ext in ['csv','doc','docx','odp','ods','odt','pot','potm','potx','pps','ppsx','ppsxm','ppt','pptm','pptx','rtf','xls','xlsx']:
504510
downloadUrl=GetDownloadUrl(fileid,user)
505511
url = 'https://view.officeapps.live.com/op/view.aspx?src='+urllib.quote(downloadUrl)
506512
return redirect(url)
507513
elif ext in ['bmp','jpg','jpeg','png','gif']:
508-
return render_template('show/image.html',url=url,path=path,cur_user=user)
514+
return render_template('show/image.html',url=url,inner_url=inner_url,path=path,cur_user=user)
509515
elif ext in ['mp4','webm']:
510-
return render_template('show/video.html',url=url,path=path,cur_user=user)
516+
return render_template('show/video.html',url=url,inner_url=inner_url,path=path,cur_user=user)
511517
elif ext in ['mp4','webm','avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'mkv', 'asf']:
512-
return render_template('show/video2.html',url=url,path=path,cur_user=user)
518+
return render_template('show/video2.html',url=url,inner_url=inner_url,path=path,cur_user=user)
513519
elif ext in ['avi','mpg', 'mpeg', 'rm', 'rmvb', 'mov', 'wmv', 'mkv', 'asf']:
514-
return render_template('show/video2.html',url=url,path=path,cur_user=user)
520+
return render_template('show/video2.html',url=url,inner_url=inner_url,path=path,cur_user=user)
515521
elif ext in ['ogg','mp3','wav']:
516-
return render_template('show/audio.html',url=url,path=path,cur_user=user)
522+
return render_template('show/audio.html',url=url,inner_url=inner_url,path=path,cur_user=user)
517523
elif CodeType(ext) is not None:
518524
content=_remote_content(fileid,user)
519-
return render_template('show/code.html',content=content,url=url,language=CodeType(ext),path=path,cur_user=user)
525+
return render_template('show/code.html',content=content,url=url,inner_url=inner_url,language=CodeType(ext),path=path,cur_user=user)
520526
else:
521527
downloadUrl=GetDownloadUrl(fileid,user)
522528
return redirect(downloadUrl)
@@ -551,6 +557,7 @@ def robot():
551557
app.jinja_env.globals['FetchData']=FetchData
552558
app.jinja_env.globals['path_list']=path_list
553559
app.jinja_env.globals['CanEdit']=CanEdit
560+
app.jinja_env.globals['quote']=urllib.quote
554561
app.jinja_env.globals['len']=len
555562
app.jinja_env.globals['enumerate']=enumerate
556563
app.jinja_env.globals['os']=os

0 commit comments

Comments
 (0)