随机故事,实现MP3下载功能
authorcheng <chengyf@zhihuishixun.com>
Fri, 12 Sep 2025 03:46:04 +0000 (11:46 +0800)
committercheng <chengyf@zhihuishixun.com>
Fri, 12 Sep 2025 03:46:04 +0000 (11:46 +0800)
code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c

index 639f1b9..9bd775a 100644 (file)
@@ -1,6 +1,25 @@
 #include "ln_api.h"
 #include <stdio.h>
 
+#define story_random_file_path "/user/leiniao/story/random.txt"
+
+static void ln_parse_story_random_file(void)
+{
+    printf("Starting to parse story choose file...\n");
+
+    uint32_t result = ln_parse_file_and_process_data(story_random_file_path, LN_HTTP_RECV_STORY_RANDOM);
+
+    if (result == ERRCODE_SUCC)
+    {
+        printf("Story file parsing completed successfully!\n");
+    }
+    else
+    {
+        printf("Story file parsing failed!\n");
+    }
+}
+
+
 uint32_t ln_story_random_http(const char *device_id,
                               const char *device_token,
                               const char *story_theme)
@@ -111,6 +130,23 @@ uint32_t ln_story_random_http(const char *device_id,
         return ret;
     }
 
+    // 打开文件准备写入
+    const char *save_path_folder = "/user/leiniao/story/";
+    // 判断目录是否存在,不存在则创建
+    if (access(save_path_folder, F_OK) == -1)
+    {
+        uapi_create_folder(save_path_folder);
+    }
+    FILE *fd = fopen(story_random_file_path, "w+");
+    if (!fd)
+    {
+        printf("story choose: fopen failed for %s:\n", story_random_file_path);
+        HttpClientClse(&client);
+        HttpClientUnprepare(&clientData);
+        return ERRCODE_FAIL;
+    }
+
+    size_t total_written = 0;
     do
     {
         HTTPC_RESULT recv_res = HttpClientRecvResponse(&client, &clientData);
@@ -120,8 +156,17 @@ uint32_t ln_story_random_http(const char *device_id,
             // 处理接收到的数据块
             if (clientData.contentBlockLen > 0)
             {
-                do_ln_http_server_recv_data(LN_HTTP_RECV_STORY_RANDOM,
-                                            clientData.responseBuf, clientData.contentBlockLen);
+                size_t written = fwrite(clientData.responseBuf, 1, clientData.contentBlockLen, fd);
+                if (written != clientData.contentBlockLen)
+                {
+                    printf("story random: write failed, written=%zu, expected=%d\n",
+                           written, clientData.contentBlockLen);
+                    HttpClientClse(&client);
+                    HttpClientUnprepare(&clientData);
+                    fclose(fd);
+                    return ERRCODE_FAIL;
+                }
+                total_written += written;
             }
         }
         else
@@ -133,5 +178,11 @@ uint32_t ln_story_random_http(const char *device_id,
 
     HttpClientUnprepare(&clientData);
     HttpClientClse(&client);
+
+    if (total_written > 0)
+    {
+        ln_parse_story_random_file();
+    }
+
     return ERRCODE_SUCC;
 }