nump commited on
Commit
e676a42
1 Parent(s): b95f353

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +143 -0
main.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import json
3
+ import time
4
+
5
+ import requests
6
+ from crypto_plus import CryptoPlus
7
+ from flask import Flask, request
8
+
9
+ rsa = CryptoPlus.loads('''-----BEGIN PUBLIC KEY-----
10
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC72/8TD242+vn0FDQm8YyeY2WH
11
+ rNIxpiCgGT6H5EbDgo7mAXy5+LtJ/imCrqfYli4mwW3SPagtGlTo1OrlqafX+pIs
12
+ ehYnKuMQEW9nPbJ0z3ItrFx1cTC70Dx3mk6mrK+KOx6XgfiLqrgGy/wysbPX5PdN
13
+ Apg4Wc3GsMk8UpdtGQIDAQAB
14
+ -----END PUBLIC KEY-----''')
15
+
16
+
17
+ def is_expired(access_token):
18
+ if not access_token:
19
+ return True
20
+ try:
21
+ message, signature = access_token.rsplit('.', 1)
22
+ header, payload = message.split('.')
23
+ payload = payload + '=' * - (len(payload) % - 4)
24
+ signature = signature + '=' * - (len(signature) % - 4)
25
+ exp = json.loads(base64.b64decode(payload).decode()).get('exp')
26
+ return exp - time.time() < 10 or not rsa.verify(message.encode(), base64.urlsafe_b64decode(signature))
27
+ except:
28
+ return True
29
+
30
+
31
+ app = Flask(__name__)
32
+
33
+
34
+ @app.route('/', methods=['GET', 'POST'])
35
+ def download_url_form():
36
+ if request.method == 'POST':
37
+ file_id = request.form['file_id']
38
+ access_token = request.form['access_token']
39
+ if is_expired(access_token):
40
+ return 'token is expired', 401
41
+
42
+ drive_info = requests.post('https://bj29.api.aliyunpds.com/v2/drive/list_my_drives', json={
43
+ }, headers={
44
+ "Authorization": f"Bearer {access_token}",
45
+ }).json()
46
+ driver_id_map = {i['category']: i['drive_id'] for i in drive_info['items'] if
47
+ i['category'] in ['backup', 'resource']}
48
+ resp = requests.post('https://bj29.api.aliyunpds.com/v2/file/get_download_url', json={
49
+ 'drive_id': driver_id_map['resource'],
50
+ 'file_id': file_id,
51
+ 'expire_sec': 115200,
52
+ }, headers={
53
+ 'Authorization': f'Bearer {access_token}',
54
+ })
55
+ if resp.status_code == 404:
56
+ resp = requests.post('https://bj29.api.aliyunpds.com/v2/file/get_download_url', json={
57
+ 'drive_id': driver_id_map['backup'],
58
+ 'file_id': file_id,
59
+ 'expire_sec': 115200,
60
+ }, headers={
61
+ 'Authorization': f'Bearer {access_token}',
62
+ })
63
+ if resp.status_code == 200:
64
+ data = resp.json()
65
+ url = data.get('cdn_url')
66
+ if not url:
67
+ url = data.get('url')
68
+ return f'<h1>Download URL:</h1><p>{url}</p>'
69
+ return f'<h1>Error:</h1><p>{resp.reason}</p>'
70
+
71
+ # 如果是 GET 请求,渲染表单
72
+ return '''
73
+ <!DOCTYPE html>
74
+ <html lang="en">
75
+ <head>
76
+ <meta charset="UTF-8">
77
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
78
+ <title>Download URL Form</title>
79
+ <style>
80
+ body {
81
+ font-family: Arial, sans-serif;
82
+ background-color: #f0f0f0;
83
+ }
84
+ .container {
85
+ max-width: 400px;
86
+ margin: 50px auto;
87
+ padding: 20px;
88
+ background-color: #fff;
89
+ border-radius: 5px;
90
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
91
+ }
92
+ h1 {
93
+ font-size: 24px;
94
+ margin-bottom: 20px;
95
+ text-align: center;
96
+ }
97
+ form {
98
+ display: flex;
99
+ flex-direction: column;
100
+ }
101
+ label {
102
+ font-size: 16px;
103
+ margin-bottom: 8px;
104
+ }
105
+ input[type="text"] {
106
+ padding: 10px;
107
+ margin-bottom: 20px;
108
+ border: 1px solid #ccc;
109
+ border-radius: 5px;
110
+ font-size: 16px;
111
+ }
112
+ button {
113
+ padding: 10px;
114
+ background-color: #007bff;
115
+ color: #fff;
116
+ border: none;
117
+ border-radius: 5px;
118
+ font-size: 16px;
119
+ cursor: pointer;
120
+ }
121
+ button:hover {
122
+ background-color: #0056b3;
123
+ }
124
+ </style>
125
+ </head>
126
+ <body>
127
+ <div class="container">
128
+ <h1>Download URL Form</h1>
129
+ <form action="/" method="post">
130
+ <label for="file_id">File ID:</label>
131
+ <input type="text" id="file_id" name="file_id" required>
132
+ <label for="access_token">Access Token:</label>
133
+ <input type="text" id="access_token" name="access_token" required>
134
+ <button type="submit">Get Download URL</button>
135
+ </form>
136
+ </div>
137
+ </body>
138
+ </html>
139
+ '''
140
+
141
+
142
+ if __name__ == '__main__':
143
+ app.run('0.0.0.0',7860)