11package com.lanzou.cloud.network
22
3+ import android.util.Log
34import com.drake.net.Get
45import com.drake.net.Post
56import com.lanzou.cloud.LanzouApplication
@@ -8,6 +9,8 @@ import com.lanzou.cloud.config.Api
89import com.lanzou.cloud.config.SPConfig
910import com.lanzou.cloud.model.BaseLanzouResponse
1011import com.lanzou.cloud.model.FileInfoModel
12+ import com.lanzou.cloud.model.LanzouDownloadModel
13+ import com.lanzou.cloud.model.LanzouResolveFileModel
1114import com.lanzou.cloud.model.LanzouShareFolderModel
1215import com.lanzou.cloud.model.LanzouUrlModel
1316import com.lanzou.cloud.model.ProfileModel
@@ -257,9 +260,63 @@ object LanzouRepository {
257260 *
258261 * @param url 文件地址
259262 */
260- fun parseFile (url : String ) {
261- val document = Jsoup .connect(url).userAgent(Repository .USER_AGENT ).get()
262-
263+ suspend fun parseFile (url : String , pwd : String? = null): Result <LanzouResolveFileModel > {
264+ return coroutineScope {
265+ runCatching {
266+ // TODO: 解析文件夹待完成
267+ val document = Jsoup .connect(url).userAgent(Repository .USER_AGENT ).get()
268+ val passwordDiv = document.selectFirst(" #file" )
269+ if (passwordDiv != null ) {
270+ // 这是有密码的文件
271+ val file = document.selectFirst(" #file" ) ? : throw NullPointerException (" 解析文件失败" )
272+ val href = file.selectFirst(" a.n_login" )?.attr(" href" )
273+ ? : throw NullPointerException (" 获取文件id失败" )
274+ val idRegex = " f=(\\ d+)" .toRegex()
275+ val fileId = idRegex.find(href)?.destructured?.component1()
276+ ? : throw NullPointerException (" 获取文件id失败" )
277+ val signRegex = " 'sign':'(.*?)'," .toRegex()
278+ val sign =
279+ signRegex.findAll(document.html()).toList().getOrNull(1 )?.destructured?.component1()
280+ ? : throw NullPointerException (" 获取 sign 失败" )
281+ val result =
282+ Post <LanzouDownloadModel >(LanzouApplication .LANZOU_SHARE_BASE_URL + Api .AJAX_PHP ) {
283+ converter = SerializationConverter (rawData = true )
284+ addHeader(" Referer" , url)
285+ addQuery(" file" , fileId)
286+ param(" action" , " downprocess" )
287+ param(" sign" , sign)
288+ param(" kd" , 1 )
289+ param(" p" , pwd)
290+ }.await()
291+ if (result.status != 1 ) {
292+ throw kotlin.IllegalStateException (" 解析文件失败" )
293+ }
294+ val shareTime = file.selectFirst(" .n_file_info .n_file_infos" )?.text() ? : " "
295+ val fileSize = file.selectFirst(" .n_filesize" )?.text() ? : " "
296+ LanzouResolveFileModel (
297+ url = url,
298+ pwd = pwd,
299+ downloadUrl = result.dom + " /file/" + result.url,
300+ fileName = result.inf,
301+ fileSize = fileSize.replace(" 大小:" , " " ),
302+ shareTime = shareTime
303+ )
304+ } else {
305+ // 没有密码的文件
306+ val fileName = document.selectFirst(" .d>div" )?.text()
307+ val nodes = document.selectFirst(" .d2 table td" )?.textNodes()
308+ Log .i(" jdy" , " nodes: $nodes " )
309+ LanzouResolveFileModel (
310+ url,
311+ pwd,
312+ fileName = fileName ? : " " ,
313+ fileSize = nodes?.getOrNull(1 )?.toString() ? : " " ,
314+ shareTime = nodes?.getOrNull(3 )?.toString() ? : " " ,
315+ remark = nodes?.getOrNull(7 )?.toString()?.replace(" \n " , " " ) ? : " " ,
316+ )
317+ }
318+ }
319+ }
263320 }
264321
265322 private fun getFileRealName (fileInfoModel : FileInfoModel ) {
0 commit comments