|
@@ -0,0 +1,460 @@
|
|
|
|
|
+/*
|
|
|
|
|
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
|
|
|
+ * 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.
|
|
|
|
|
+ */
|
|
|
|
|
+import { FFmpeg, FFProgressMessageParser } from "@sj/ffmpeg";
|
|
|
|
|
+import fs from '@ohos.file.fs';
|
|
|
|
|
+import { FFMpegTags } from "./Utility";
|
|
|
|
|
+import { http } from "@kit.NetworkKit";
|
|
|
|
|
+import { BusinessError } from "@kit.BasicServicesKit";
|
|
|
|
|
+import ResponseCode from '@ohos.net.http';
|
|
|
|
|
+import { LogUtil, PreferencesUtil, StrUtil } from "@pura/harmony-utils";
|
|
|
|
|
+import { CommonConstants } from "../constants/CommonConstants";
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 修复音频文件的元数据标签
|
|
|
|
|
+ * @param inputPath 输入文件路径
|
|
|
|
|
+ * @param metadata 要更新的元数据对象
|
|
|
|
|
+ * @param overwrite 是否覆盖源文件(可选,默认false)
|
|
|
|
|
+ * @param outputPath 指定输出路径(可选,优先级高于overwrite)
|
|
|
|
|
+ * @returns Promise<boolean> 成功返回true,失败返回false
|
|
|
|
|
+ */
|
|
|
|
|
+export async function repairAudioMetadata(
|
|
|
|
|
+ inputPath: string,
|
|
|
|
|
+ lyrics:string,
|
|
|
|
|
+ metadata: FFMpegTags,
|
|
|
|
|
+ overwrite: boolean = false,
|
|
|
|
|
+ outputPath: string = ''
|
|
|
|
|
+): Promise<boolean> {
|
|
|
|
|
+ // 确保metadata是对象类型
|
|
|
|
|
+ if (typeof metadata !== 'object' || metadata === null) {
|
|
|
|
|
+ console.error('元数据参数必须是一个对象');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 确定输出路径
|
|
|
|
|
+ let finalOutputPath: string = '';
|
|
|
|
|
+ let useTempFile: boolean = false;
|
|
|
|
|
+
|
|
|
|
|
+ if (outputPath && outputPath.length > 0) {
|
|
|
|
|
+ finalOutputPath = outputPath;
|
|
|
|
|
+ } else if (overwrite) {
|
|
|
|
|
+ // 使用临时文件方式处理覆盖,保持原文件扩展名
|
|
|
|
|
+ const timestamp = new Date().getTime();
|
|
|
|
|
+ const lastDotIndex = inputPath.lastIndexOf('.');
|
|
|
|
|
+ if (lastDotIndex >= 0) {
|
|
|
|
|
+ finalOutputPath = inputPath.substring(0, lastDotIndex) + '_' + timestamp + inputPath.substring(lastDotIndex);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ finalOutputPath = inputPath + '_' + timestamp;
|
|
|
|
|
+ }
|
|
|
|
|
+ useTempFile = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const lastDotIndex = inputPath.lastIndexOf('.');
|
|
|
|
|
+ if (lastDotIndex >= 0) {
|
|
|
|
|
+ finalOutputPath = inputPath.substring(0, lastDotIndex) + '_tagged' + inputPath.substring(lastDotIndex);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ finalOutputPath = inputPath + '_tagged';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 构建FFmpeg命令
|
|
|
|
|
+ const commands: string[] = [
|
|
|
|
|
+ "ffmpeg",
|
|
|
|
|
+ "-i", inputPath
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ // 添加元数据参数
|
|
|
|
|
+ const dynamicMetadata = metadata as Record<string, string>;
|
|
|
|
|
+ Object.keys(dynamicMetadata).forEach((key) => {
|
|
|
|
|
+ const value = dynamicMetadata[key];
|
|
|
|
|
+ if (value) {
|
|
|
|
|
+ commands.push("-metadata");
|
|
|
|
|
+ commands.push(`${key}=${value}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ //额外再次添加下lyrics-XXX的歌词参数,以便其他音乐播放器可以识别歌词
|
|
|
|
|
+ // 额外添加歌词自定义标签(修复后的核心代码)
|
|
|
|
|
+ if (StrUtil.isNotEmpty(lyrics)) { // 确保歌词内容存在时才添加
|
|
|
|
|
+ // 多种歌词标签格式
|
|
|
|
|
+ const lyricTags = [
|
|
|
|
|
+ `LYRICS=${lyrics}`,
|
|
|
|
|
+ `lyrics-XXX=${lyrics}`,
|
|
|
|
|
+ `USLT::XXX=${lyrics}`,
|
|
|
|
|
+ // `UNSYNCEDLYRICS=${lyrics}`
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ lyricTags.forEach(tag => {
|
|
|
|
|
+ commands.push("-metadata");
|
|
|
|
|
+ commands.push(tag);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ commands.push(
|
|
|
|
|
+ "-map", "0",
|
|
|
|
|
+ "-map_metadata", "0",
|
|
|
|
|
+ "-id3v2_version", "3",
|
|
|
|
|
+ "-codec", "copy",
|
|
|
|
|
+ "-y",
|
|
|
|
|
+ finalOutputPath
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ await FFmpeg.execute(commands, {
|
|
|
|
|
+ logCallback: (logLevel: number, logMessage: string) => {
|
|
|
|
|
+ //console.log(`onecold logCallback [${logLevel}]${logMessage}`);
|
|
|
|
|
+ },
|
|
|
|
|
+ progressCallback: (message: string) => {
|
|
|
|
|
+ //console.log(`onecold progressCallback [progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`);
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 如果是使用临时文件,需要替换原文件
|
|
|
|
|
+ if (useTempFile) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ console.info(`onecold unlinkSync`);
|
|
|
|
|
+ // 先删除原文件,再重命名临时文件
|
|
|
|
|
+ await fs.unlinkSync(inputPath);
|
|
|
|
|
+ console.info(`onecold unlinkSync2`);
|
|
|
|
|
+ fs.renameSync(finalOutputPath, inputPath);
|
|
|
|
|
+ console.info(`onecold renameSync`);
|
|
|
|
|
+ finalOutputPath = inputPath; // 更新为最终路径
|
|
|
|
|
+ } catch (renameError) {
|
|
|
|
|
+ console.error(`onecold 文件替换失败: ${JSON.stringify(renameError)}`);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ console.info(`onecold 元数据修复成功,保存路径: ${finalOutputPath}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证文件是否存在
|
|
|
|
|
+ try {
|
|
|
|
|
+ const file = await fs.open(finalOutputPath, fs.OpenMode.READ_ONLY);
|
|
|
|
|
+ await fs.close(file.fd);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error(`onecold 文件验证失败: ${JSON.stringify(e)}`);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ let errorMsg: string = '';
|
|
|
|
|
+ if (error instanceof Error) {
|
|
|
|
|
+ errorMsg = error.message;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ errorMsg = String(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ console.error(`onecold元数据修复失败 ${errorMsg}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 清理临时文件(如果存在)
|
|
|
|
|
+ if (useTempFile) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (fs.accessSync(finalOutputPath)) {
|
|
|
|
|
+ fs.unlinkSync(finalOutputPath);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (cleanupError) {
|
|
|
|
|
+ console.warn('清理临时文件失败:', cleanupError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 修改音乐文件的封面
|
|
|
|
|
+ * @param inputPath 音乐文件路径
|
|
|
|
|
+ * @param coverImagePath 封面图片路径(支持HTTP地址或本地地址)
|
|
|
|
|
+ * @param overwrite 是否覆盖源文件(可选,默认false)
|
|
|
|
|
+ * @param outputPath 指定输出路径(可选,优先级高于overwrite)
|
|
|
|
|
+ * @returns Promise<boolean> 成功返回true,失败返回false
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
|
|
+ * 修改音乐文件的封面
|
|
|
|
|
+ * @param context 上下文对象
|
|
|
|
|
+ * @param inputPath 音乐文件路径
|
|
|
|
|
+ * @param coverImagePath 封面图片路径(支持HTTP地址或本地地址)
|
|
|
|
|
+ * @param overwrite 是否覆盖源文件(可选,默认false)
|
|
|
|
|
+ * @param outputPath 指定输出路径(可选,优先级高于overwrite)
|
|
|
|
|
+ * @returns Promise<boolean> 成功返回true,失败返回false
|
|
|
|
|
+ */
|
|
|
|
|
+export async function changeMusicCover(
|
|
|
|
|
+ context: Context,
|
|
|
|
|
+ inputPath: string,
|
|
|
|
|
+ coverImagePath: string,
|
|
|
|
|
+ overwrite: boolean = false,
|
|
|
|
|
+ outputPath: string = ''
|
|
|
|
|
+): Promise<boolean> {
|
|
|
|
|
+ // 确定输出路径
|
|
|
|
|
+ let finalOutputPath: string = '';
|
|
|
|
|
+ let useTempFile: boolean = false;
|
|
|
|
|
+
|
|
|
|
|
+ if (outputPath && outputPath.length > 0) {
|
|
|
|
|
+ finalOutputPath = outputPath;
|
|
|
|
|
+ } else if (overwrite) {
|
|
|
|
|
+ // 使用临时文件方式处理覆盖,保持原文件扩展名
|
|
|
|
|
+ const timestamp = new Date().getTime();
|
|
|
|
|
+ const lastDotIndex = inputPath.lastIndexOf('.');
|
|
|
|
|
+ if (lastDotIndex >= 0) {
|
|
|
|
|
+ finalOutputPath = inputPath.substring(0, lastDotIndex) + '_' + timestamp + inputPath.substring(lastDotIndex);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ finalOutputPath = inputPath + '_' + timestamp;
|
|
|
|
|
+ }
|
|
|
|
|
+ useTempFile = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const lastDotIndex = inputPath.lastIndexOf('.');
|
|
|
|
|
+ if (lastDotIndex >= 0) {
|
|
|
|
|
+ finalOutputPath = inputPath.substring(0, lastDotIndex) + '_covered' + inputPath.substring(lastDotIndex);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ finalOutputPath = inputPath + '_covered';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果是网络图片,先下载到临时文件
|
|
|
|
|
+ let tempCoverPath = coverImagePath;
|
|
|
|
|
+ console.log(`onecold 开始下载 coverImagePath = [${coverImagePath}]`);
|
|
|
|
|
+ if (coverImagePath.startsWith('http')) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 创建临时文件路径
|
|
|
|
|
+ const tempDir = context.filesDir + '/'; // 默认缓存目录
|
|
|
|
|
+ const tempFileName = 'temp_cover.jpg';
|
|
|
|
|
+ tempCoverPath = `${tempDir}${tempFileName}`;
|
|
|
|
|
+
|
|
|
|
|
+ // 下载图片
|
|
|
|
|
+ const result = await loadImageWithUrl(coverImagePath, tempCoverPath);
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ console.error('onecold 下载封面图片失败');
|
|
|
|
|
+ // 清理可能已创建的临时文件
|
|
|
|
|
+ if (tempCoverPath !== coverImagePath) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fs.unlinkSync(tempCoverPath);
|
|
|
|
|
+ } catch (unlinkError) {
|
|
|
|
|
+ console.warn('onecold 清理临时文件失败:', unlinkError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error('onecold 下载封面图片时出错:', error);
|
|
|
|
|
+ // 清理可能已创建的临时文件
|
|
|
|
|
+ if (tempCoverPath !== coverImagePath) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fs.unlinkSync(tempCoverPath);
|
|
|
|
|
+ } catch (unlinkError) {
|
|
|
|
|
+ console.warn('onecold 清理临时文件失败:', unlinkError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ console.log(`onecold 开始下载 tempCoverPath = [${tempCoverPath}]`);
|
|
|
|
|
+ // 构建FFmpeg命令
|
|
|
|
|
+ const commands: string[] = [
|
|
|
|
|
+ "ffmpeg",
|
|
|
|
|
+ "-i", inputPath,
|
|
|
|
|
+ "-i", tempCoverPath,
|
|
|
|
|
+ "-map", "0:0", // 映射音频流
|
|
|
|
|
+ "-map", "1:0", // 映射封面图片流
|
|
|
|
|
+ "-c", "copy", // 复制音频流
|
|
|
|
|
+ "-id3v2_version", "3", // ID3v2版本
|
|
|
|
|
+ "-y", // 覆盖输出文件
|
|
|
|
|
+ finalOutputPath
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ await FFmpeg.execute(commands, {
|
|
|
|
|
+ logCallback: (logLevel: number, logMessage: string) => {
|
|
|
|
|
+ //console.log(`onecold [FFmpeg LOG] [${logLevel}]${logMessage}`);
|
|
|
|
|
+ },
|
|
|
|
|
+ progressCallback: (message: string) => {
|
|
|
|
|
+ //console.log(`onecold [FFmpeg progress]${JSON.stringify(FFProgressMessageParser.parse(message))}`);
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 如果是使用临时文件,需要替换原文件
|
|
|
|
|
+ if (useTempFile) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 先删除原文件,再重命名临时文件
|
|
|
|
|
+ await fs.unlinkSync(inputPath);
|
|
|
|
|
+ fs.renameSync(finalOutputPath, inputPath);
|
|
|
|
|
+ finalOutputPath = inputPath; // 更新为最终路径
|
|
|
|
|
+ } catch (renameError) {
|
|
|
|
|
+ console.error(`onecold 文件替换失败: ${JSON.stringify(renameError)}`);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ console.info(`onecold 封面修改成功,保存路径: ${finalOutputPath}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 验证文件是否存在
|
|
|
|
|
+ try {
|
|
|
|
|
+ const file = await fs.open(finalOutputPath, fs.OpenMode.READ_ONLY);
|
|
|
|
|
+ await fs.close(file.fd);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果是下载的临时图片,清理临时文件
|
|
|
|
|
+ if (tempCoverPath !== coverImagePath) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fs.unlinkSync(tempCoverPath);
|
|
|
|
|
+ } catch (unlinkError) {
|
|
|
|
|
+ console.warn('onecold 清理临时文件失败:', unlinkError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error(`onecold 文件验证失败: ${JSON.stringify(e)}`);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ let errorMsg: string = '';
|
|
|
|
|
+ if (error instanceof Error) {
|
|
|
|
|
+ errorMsg = error.message;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ errorMsg = String(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ console.error(`onecold封面修改失败: ${errorMsg}`);
|
|
|
|
|
+
|
|
|
|
|
+ // 清理临时文件
|
|
|
|
|
+ if (tempCoverPath !== coverImagePath) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fs.unlinkSync(tempCoverPath);
|
|
|
|
|
+ } catch (unlinkError) {
|
|
|
|
|
+ console.warn('onecold 清理临时文件失败:', unlinkError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果使用了临时文件但处理失败,也需要清理
|
|
|
|
|
+ if (useTempFile && fs.accessSync(finalOutputPath)) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fs.unlinkSync(finalOutputPath);
|
|
|
|
|
+ } catch (cleanupError) {
|
|
|
|
|
+ console.warn('onecold 清理临时输出文件失败:', cleanupError);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 下载图片到指定路径
|
|
|
|
|
+ * @param url 图片URL
|
|
|
|
|
+ * @param outputPath 输出路径
|
|
|
|
|
+ * @returns Promise<boolean> 成功返回true,失败返回false
|
|
|
|
|
+ */
|
|
|
|
|
+export async function loadImageWithUrl( url: string, outputPath: string,): Promise<boolean > {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ http.createHttp().request(url, { method: http.RequestMethod.GET, connectTimeout: 60000, readTimeout: 60000 },
|
|
|
|
|
+ async (error: BusinessError, data: http.HttpResponse) => {
|
|
|
|
|
+ if (error) {
|
|
|
|
|
+ console.error(`http request failed with. Code: ${error.code}, message: ${error.message}`);
|
|
|
|
|
+ reject(false);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (ResponseCode.ResponseCode.OK === data.responseCode) {
|
|
|
|
|
+ let imageBuffer: ArrayBuffer = data.result as ArrayBuffer;
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 获取相册路径
|
|
|
|
|
+ let file = await fs.open(outputPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
|
|
|
|
|
+ // 写入文件
|
|
|
|
|
+ await fs.write(file.fd, imageBuffer);
|
|
|
|
|
+ // 关闭文件
|
|
|
|
|
+ await fs.close(file.fd);
|
|
|
|
|
+
|
|
|
|
|
+ // 返回文件标识符
|
|
|
|
|
+ resolve(true);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error("error is " + JSON.stringify(error));
|
|
|
|
|
+ reject(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error("error occurred when image downloaded!");
|
|
|
|
|
+ reject(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+export function getApiLyric(apiUrl:string,title: string, artist: string, isApi2: boolean): Promise<string> {
|
|
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (StrUtil.isNotEmpty(title) && title === '全世界最好的你') {
|
|
|
|
|
+ artist = '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // let baseUrl = PreferencesUtil.getStringSync('LRC_API', '');
|
|
|
|
|
+ if (apiUrl == '') {
|
|
|
|
|
+ LogUtil.debug("Heanup 未设置API");
|
|
|
|
|
+ resolve('');
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (apiUrl.includes(CommonConstants.LRC_API_2)) {
|
|
|
|
|
+ isApi2 = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let requestUrl = apiUrl +
|
|
|
|
|
+ '?title=' + encodeURIComponent(title.trim()) +
|
|
|
|
|
+ '&artist=' + encodeURIComponent(artist.trim());
|
|
|
|
|
+ console.info(`onecold requestUrl = `+requestUrl);
|
|
|
|
|
+ const httpRequest = http.createHttp();
|
|
|
|
|
+ const options: http.HttpRequestOptions = {
|
|
|
|
|
+ method: http.RequestMethod.GET,
|
|
|
|
|
+ readTimeout: 3000,
|
|
|
|
|
+ connectTimeout: 3000,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const response: http.HttpResponse = await httpRequest.request(requestUrl, options);
|
|
|
|
|
+ console.info(`onecold requestUrl 00= `+response.responseCode);
|
|
|
|
|
+ if (response.responseCode === 200) {
|
|
|
|
|
+ let res = response.result as string;
|
|
|
|
|
+ let fileContent = '';
|
|
|
|
|
+
|
|
|
|
|
+ if (isApi2) {
|
|
|
|
|
+ fileContent = res;
|
|
|
|
|
+ console.info(`onecold fileContent 1111= `+fileContent);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.info(`onecold fileContent 22= `+fileContent);
|
|
|
|
|
+ let parsedData: lyricInfo[] = JSON.parse(res) as lyricInfo[];
|
|
|
|
|
+ fileContent = parsedData[0].lyrics || '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ LogUtil.debug("onecold fileContent 11 =" + fileContent);
|
|
|
|
|
+ resolve(fileContent);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ console.log('onecold getLyric--失败', JSON.stringify(response));
|
|
|
|
|
+ resolve('');
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error('onecold getLyric catch--失败' + JSON.stringify(error));
|
|
|
|
|
+ resolve('');
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+interface lyricInfo{
|
|
|
|
|
+ code:number
|
|
|
|
|
+ album:number
|
|
|
|
|
+ artist:string
|
|
|
|
|
+ lyrics:string
|
|
|
|
|
+ cover_url:string
|
|
|
|
|
+ status:string
|
|
|
|
|
+}
|