kissguan 发表于 2021-9-12 16:27:14

求位懂java的大佬看看这个代码


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
* @author ohy
* @date 2021-08-26 09:59:38
**/
public class Test {

    static String cookie ="";

    public static void main(String[] args) {
      String url = "https://drive.kdocs.cn/api/v5/links?offset=0&count=100&orderby=file_mtime&order=DESC&append=false&ignore=link";
      String res = HttpRequest.get(url).header("cookie", cookie).execute().body();
      JSONObject jsonObject = JSONObject.parseObject(res);
      String path = "C:\\Users\\montnets\\Desktop\\test\\"
                + jsonObject.getJSONArray("share").getJSONObject(0).getString("share_name");
      parseGroup(jsonObject.getJSONArray("share").getJSONObject(0).getJSONObject("group").getString("groupid"), path, "0");
    }

    private static void parseGroup(String id, String path, String parentId) {
      String url = "https://drive.kdocs.cn/api/v5/groups/" + id + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30";
      if (!parentId.equals("0")) url += "&parentid=" + parentId;
      String res = HttpRequest.get(url).header("cookie", cookie).execute().body();
      JSONObject jsonObject = JSONObject.parseObject(res);
      JSONArray array = jsonObject.getJSONArray("files");
      for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            if (object.getString("ftype").equals("folder")) {
                String groupId = object.getString("id");
                String name = object.getString("fname");
                parseGroup(id, path + "\\" + name, groupId);
            } else if (object.getString("ftype").equals("file")) {
                String fId = object.getString("id");
                String name = object.getString("fname");
                String getdownUrl = "https://drive.kdocs.cn/api/v3/groups/" + id + "/files/" + fId + "/download?isblocks=false";
                res = HttpRequest.get(getdownUrl).header("cookie", cookie).execute().body();
                String downUrl = JSONObject.parseObject(res).getJSONObject("fileinfo").getString("url");
                HttpUtil.downloadFile(downUrl, path + "/" + name);
            }
      }
    }
}


主要是看不懂java的代码,求大佬帮忙看看能不能给整成python的样式

redforce 发表于 2021-9-12 16:48:12

url1 = "https://drive.kdocs.cn/api/v5/links?offset=0&count=100&orderby=file_mtime&order=DESC&append=false&ignore=link"
url2 = f"https://drive.kdocs.cn/api/v5/groups/" + {id} + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30"
#实例一下
res1= requests.get(url1)
id_list = res1.json().get('id')
for id in id_list:
    url2 = f"https://drive.kdocs.cn/api/v5/groups/" + {id} + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30"
    resp2 = requests.get(url)
    with open(f'{id}',mode='wb',encoding='UTF-8') as file:
       file.write(resp2.content)
大概意思就是从url1的响应信息(json格式)中拿到id,id有很多个,然后循环的传到url2中,访问url2,下载个什么东东保存到本地
注意需要登录

kissguan 发表于 2021-9-12 17:39:10

redforce 发表于 2021-9-12 16:48
注意需要登录

哇果然换成python就能看懂了感谢感谢

redforce 发表于 2021-9-13 11:37:10

代码要改一下。第5行会报错。返回的结果是列表嵌套字典
url1 = "https://drive.kdocs.cn/api/v5/links?offset=0&count=100&orderby=file_mtime&order=DESC&append=false&ignore=link"
url2 = f"https://drive.kdocs.cn/api/v5/groups/" + {id} + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30"
#实例一下
res1= requests.get(url1)
id_list = res1.json()
for id1 in id_list:
    id1 = id1.get('id')
    url2 = f"https://drive.kdocs.cn/api/v5/groups/" + {id1} + "/files?linkgroup=true&include=acl,pic_thumbnail&offset=0&count=30"
    resp2 = requests.get(url)
    with open(f'{id}',mode='wb',encoding='UTF-8') as file:
       file.write(resp2.content)
页: [1]
查看完整版本: 求位懂java的大佬看看这个代码