Skip to content

Commit 34653ef

Browse files
authored
Merge pull request #54 from callmefisher/miku_0625
update miku mcp tools
2 parents 7b665c3 + 782fd30 commit 34653ef

File tree

1 file changed

+78
-23
lines changed

1 file changed

+78
-23
lines changed

src/mcp_server/core/live_streaming/live_streaming.py

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,33 @@
1616
class LiveStreamingService:
1717
def __init__(self, cfg: config.Config = None):
1818
self.config = cfg
19-
self.api_key = cfg.live_api_key if cfg else None
19+
self.live_api_key = cfg.live_api_key if cfg else None
2020
self.live_endpoint = cfg.live_endpoint if cfg else "mls.cn-east-1.qiniumiku.com"
2121
self.access_key = cfg.access_key if cfg else None
2222
self.secret_key = cfg.secret_key if cfg else None
2323

2424

2525
def _get_auth_header(self, method: str, url: str, content_type: Optional[str] = None, body: Optional[str] = None) -> Dict[str, str]:
26-
"""
27-
Generate authorization header
28-
Priority: QINIU_ACCESS_KEY/QINIU_SECRET_KEY > API KEY
29-
"""
30-
# Priority 1: Use QINIU_ACCESS_KEY/QINIU_SECRET_KEY if configured
26+
27+
# Priority 1: Fall back to API KEY if ACCESS_KEY/SECRET_KEY not configured
28+
if self.live_api_key and self.live_api_key != "YOUR_QINIU_LIVE_API_KEY":
29+
return {
30+
"Authorization": f"Bearer {self.live_api_key}"
31+
}
32+
33+
# Priority 2: Use QINIU_ACCESS_KEY/QINIU_SECRET_KEY if configured
3134
if self.access_key and self.secret_key and \
3235
self.access_key != "YOUR_QINIU_ACCESS_KEY" and \
3336
self.secret_key != "YOUR_QINIU_SECRET_KEY":
3437
# Generate Qiniu token for the request
3538
# For live streaming API, we use a simple token format
3639
token = self._generate_qiniu_token(method, url, content_type, body)
40+
# token = generate_signature(method, url, body,self.access_key, self.secret_key)
3741
return {
3842
"Authorization": f"Qiniu {token}"
3943
}
40-
41-
# Priority 2: Fall back to API KEY if ACCESS_KEY/SECRET_KEY not configured
42-
if not self.api_key or self.api_key == "YOUR_QINIU_API_KEY":
43-
raise ValueError("Neither QINIU_ACCESS_KEY/QINIU_SECRET_KEY nor QINIU_API_KEY is configured")
44-
45-
return {
46-
"Authorization": f"Bearer {self.api_key}"
44+
return {
45+
"Authorization": "Qiniu ak:sk"
4746
}
4847

4948
def _build_bucket_url(self, bucket: str) -> str:
@@ -87,15 +86,35 @@ async def create_bucket(self, bucket: str) -> Dict[str, Any]:
8786
Dict containing the response status and message
8887
"""
8988
url = self._build_bucket_url(bucket)
90-
headers = self._get_auth_header(method="PUT",url=url)
91-
92-
logger.info(f"Creating bucket: {bucket} at {url}")
89+
data = {}
90+
bodyJson = json.dumps(data)
91+
auth_headers = self._get_auth_header(method="PUT", url=url, content_type="application/json", body=bodyJson)
92+
headers = {"Content-Type": "application/json"}
93+
# 如果有认证头,添加到headers中
94+
if auth_headers:
95+
headers.update(auth_headers)
96+
97+
# 打印 HTTP 请求信息
98+
print("=== HTTP 请求信息 ===")
99+
print("方法: PUT")
100+
print(f"URL: {url}")
101+
print("请求头:")
102+
for key, value in headers.items():
103+
print(f" {key}: {value}")
104+
print("请求体: {}")
105+
print("===================")
106+
107+
print(f"Creating bucket: {bucket} at {url}")
93108

94109
async with aiohttp.ClientSession() as session:
95-
async with session.put(url, headers=headers) as response:
110+
async with session.put(url, headers=headers, data=bodyJson) as response:
96111
status = response.status
97112
text = await response.text()
98113

114+
print(f"状态码: {status}")
115+
print(f"响应内容: {text}")
116+
print("==================")
117+
99118
if status == 200 or status == 201:
100119
logger.info(f"Successfully created bucket: {bucket}")
101120
return {
@@ -127,12 +146,17 @@ async def create_stream(self, bucket: str, stream: str) -> Dict[str, Any]:
127146
Dict containing the response status and message
128147
"""
129148
url = self._build_stream_url(bucket, stream)
130-
headers = self._get_auth_header(method="PUT", url=url)
149+
data = {}
150+
bodyJson = json.dumps(data)
151+
headers = {
152+
**self._get_auth_header(method="PUT", url=url, content_type="application/json", body=bodyJson),
153+
"Content-Type": "application/json"
154+
}
131155

132156
logger.info(f"Creating stream: {stream} in bucket: {bucket} at {url}")
133157

134158
async with aiohttp.ClientSession() as session:
135-
async with session.put(url, headers=headers) as response:
159+
async with session.put(url, headers=headers, data=bodyJson) as response:
136160
status = response.status
137161
text = await response.text()
138162

@@ -463,7 +487,6 @@ async def list_streams(self, bucket_id: str) -> Dict[str, Any]:
463487
def _generate_qiniu_token(self, method: str, url: str, content_type: Optional[str] = None, body: Optional[str] = None) -> str:
464488
if not self.access_key or not self.secret_key:
465489
raise ValueError("QINIU_ACCESS_KEY and QINIU_SECRET_KEY are required")
466-
467490
# Parse the URL
468491
parsed = urlparse(url)
469492

@@ -475,7 +498,7 @@ def _generate_qiniu_token(self, method: str, url: str, content_type: Optional[st
475498
data += f"?{parsed.query}"
476499

477500
# 3. Add Host
478-
data += f"\nHost: {parsed.netloc}"
501+
data += f"\nHost: {parsed.hostname}"
479502

480503
# 4. Add Content-Type if exists and not empty
481504
if content_type:
@@ -500,9 +523,41 @@ def _generate_qiniu_token(self, method: str, url: str, content_type: Optional[st
500523
sign = hmac.new(secret_bytes, data_bytes, hashlib.sha1).digest()
501524

502525
# 8. URL-safe Base64 encode
503-
encoded_sign = base64.urlsafe_b64encode(sign).decode('utf-8')
526+
encoded_pre = base64.b64encode(sign).decode('utf-8')
527+
encoded_sign = encoded_pre.replace('+', '-').replace('/', '_')
504528

505529
# 9. Construct and return the Qiniu token
506530
qiniu_token = f"{self.access_key}:{encoded_sign}"
531+
return qiniu_token
532+
533+
534+
535+
536+
537+
538+
def generate_signature(method, url, body, ak, sk):
539+
parsed_url = urlparse(url)
540+
541+
# 构建签名数据
542+
data = method + " " + parsed_url.path
543+
544+
if parsed_url.query:
545+
data += "?" + parsed_url.query
546+
547+
data += "\nHost: " + parsed_url.hostname
548+
data += "\nContent-Type: application/json"
549+
550+
if body:
551+
data += "\n\n" + body
552+
print(data)
553+
# 使用HMAC-SHA1进行签名
554+
hmac_sha1 = hmac.new(sk.encode('utf-8'), data.encode('utf-8'), hashlib.sha1)
555+
hmac_result = hmac_sha1.digest()
556+
557+
sign = ak + ":" + base64_url_safe_encode(hmac_result)
558+
return sign
507559

508-
return qiniu_token
560+
def base64_url_safe_encode(data):
561+
encoded = base64.b64encode(data).decode('utf-8')
562+
encoded = encoded.replace('+', '-').replace('/', '_')
563+
return encoded

0 commit comments

Comments
 (0)